﻿function left(mainStr,lngLen)
{
     if (lngLen>0) {return mainStr.substring(0,lngLen)}
     else{return null}
} 

function right(mainStr,lngLen)
{
     if (mainStr.length-lngLen>=0 && mainStr.length>=0 && mainStr.length-lngLen<=mainStr.length) {
     return mainStr.substring(mainStr.length-lngLen,mainStr.length)}
     else{return null}
}
function mid(mainStr,starnum,endnum)
{
     if (mainStr.length>=0){
     return mainStr.substr(starnum,endnum)
     }else{return null}
 }
 
 //是否有效整数
function IsInteger (strVal)
{
    var reVal;
    strVal = strVal;
    if (strVal.length == "")
        return (false);
    reVal = /^[\-\+]?([1-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)$/;
    return (reVal.test (strVal));
}
//只能输入整数
function onlyNum(o)   
{
    o.value=o.value.replace(/[^\d]/g,'');   
}
//只能输入数字和小数点的文本框
function clearNoNum(obj)
{
   //先把非数字的都替换掉，除了数字和.
   obj.value = obj.value.replace(/[^\d.]/g,"");
   //必须保证第一个为数字而不是.
   obj.value = obj.value.replace(/^\./g,"");
   //保证只有出现一个.而没有多个.
   obj.value = obj.value.replace(/\.{2,}/g,".");
   //保证.只出现一次，而不能出现两次以上
   obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
}
function JHshNumberText(a)
{ 
  var fa="";
  if(a.value.substring(0,1)=="-")
      fa="-";

  var str=(a.value.replace(/[^0-9.]/g,'')).replace(/[.][0-9]*[.]/, '.');
  

  if (str.substring(0,1)==".")
   str="0"+str;
   a.value=fa+str;

} 

function isEmpty(s) 
{
  if (s == null || s=="undefined" || s.length == 0)
    return true;
  return !/\S/.test(s);
}

String.prototype.trim = function()
{
    // 用正则表达式将前后空格
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function()
{
    return this.replace(/(^\s*)/g,"");
}
String.prototype.RTrim = function()
{
    return this.replace(/(\s*$)/g,"");
} 
//检查日期是否合法，只能对"2005-02-03"这样的字符串
//下面两个函数
function Date_getDay(aiYear,aiMonth)
{
 var loDay = [0,31,28,31,30,31,30,31,31,30,31,30,31];
 if (aiYear%4==0) loDay[2] = 29;
 return loDay[aiMonth];
}
function Date_istrue(asDate)
{
 var lsDate  = asDate + "";
 var loDate  = lsDate.split("-");
 if (loDate.length!=3) return false; 
 var liYear  = parseInt(loDate[0]);
 var liMonth = parseInt(loDate[1]);
 var liDay   = parseInt(loDate[2]);
 if ((loDate[0].length>4)||(loDate[1].length>2)||(loDate[2].length>2)) return false;
 if (isNaN(liYear)||isNaN(liMonth)||isNaN(liDay)) return false;
 if ((liYear<1900)||(liYear>3000)) return false;
 if ((liMonth>12)||(liMonth<=0))   return false;
 if (Date_getDay(liYear,liMonth)<liDay) return false;
 return !isNaN(Date.UTC(liYear,liMonth,liDay));
}

function clearDefault(el)
{
  if (el.defaultValue==el.value) el.value = "";
}
function resetDefault(el)
{
  if (isEmpty(el.value)) el.value=el.defaultValue;
}

function Search(language)
{
    var keyword=document.getElementById("keyword");
    if(isEmpty(keyword.value) || keyword.defaultValue==keyword.value)
    {
        if(language=="cn")
        {
            alert("请输入查询关键字！");
        }
        else
        {
            alert("Please input keywords!");
        }
        keyword.focus();
        return false;
    }
    location.href="Products_Search.aspx?keyword="+encodeURI(keyword.value);
}

function SetLeftHeight()
{
    var href=location.href.toLowerCase();
    var last=href.substr(href.length-1,1);
    if((last!="/") && (href.indexOf("default.aspx")<=0))
    {
        var index_right=document.getElementById("index_right");
        var index_left=document.getElementById("index_left");
        if(!isEmpty(index_left) && !isEmpty(index_right))
        {
            var span=40;
            if (href.indexOf("productsinfo.aspx")>=0)
            {
                span=118;
            }
            if (href.indexOf("products.aspx")>=0)
            {
                span=118;
            }
            var h=index_right.offsetHeight;
            var hcur=h-span;
            if(h>324)
            {
                h=hcur;
                var mid=$("#index_left .mid .class");
                if(!isEmpty(mid))
                {
                    mid.height(h);
                }
                else
                {
                    $("#index_left .mid").height(h);
                }                
            }
            else
            {
                var mid=$("#index_left .mid .class");
                var he=259;
                if(!isEmpty(mid))
                {
                    mid.height(he);
                }
                else
                {
                    $("#index_left .mid").height(he);
                }                
                $("#index_right .infoCon").height(318);
            }
        }
    }
}

function SelectAll(obj,Containers,Selected)
{
    var CheckBox_All=Containers.getElementsByTagName("INPUT");
    for(var j=0;j<CheckBox_All.length;j++)
    {
        if (CheckBox_All[j].type == 'checkbox')
        {
            if(!CheckBox_All[j].disabled)
            {
                CheckBox_All[j].checked=obj.checked;
            }
        }
    }
    setSelected(Containers,Selected);
}
function setSelected(Containers,Selected)
{
    var checked=0;
    var ch=false;
    var CheckBox_All=Containers.getElementsByTagName("INPUT");
    Selected.value="";
    for(var j=0;j<CheckBox_All.length;j++)
    {
        if(!CheckBox_All[j].disabled)
        {
            if (CheckBox_All[j].type == 'checkbox' && CheckBox_All[j].checked)
            {
                Selected.value=Selected.value+CheckBox_All[j+1].value+",";
                checked++;
            }
         }
         else
         {
            checked++;
         }
    }
//    var checkAll=document.getElementById("checkAll");
//    if(checked == CheckBox_All.length/2)
//    {
//        checkAll.checked=true;
//    }
//    else
//    {
//        checkAll.checked=false;
//    }
}


