function GetStrLen(v_str)
{
  var len = 0;
  for(i=0; i<v_str.length; i++)
	{
  	if(v_str.charCodeAt(i) > 128)
		{
  		len = len + 2;
  	}
		else
		{
  		len = len + 1;
  	}
  }

  return len;
}

function len(v_str)
{
	return GetStrLen(v_str);
}

function trim(v_str)
{
	return v_str.replace(/^\s+|\s+$/g,"");
}

function ltrim(v_str)
{
	return v_str.replace(/^\s+/,"");
}

function rtrim(v_str)
{
	return v_str.replace(/\s+$/,"");
}

function isRuleValue(v_value)
{
	var reg = /--{1}|\'{1}|;{1}|"{1}|\|{1}/g;
	if (reg.test(v_value))
		return false;
	else
		return true;
}

//检查用户姓名
function CheckUsername(v_name)
{
	if (v_name.length == 0)
	{
		alert("请输入姓名！");
		return false;
	}

	var len = GetStrLen(v_name)
	if (len>20 || len<4)
	{
		alert("您输入的姓名有误！");
		return false;
	}

	if (!isRuleValue(v_name))
	{
		alert("您输入的姓名有误！");
		return false;
	}

	return true;
}

//检查护照
function CheckPassport(v_card)
{
	if (v_card.length == 0)
	{
		alert("请输入护照号码！");
		return false;
	}

	var reg = /^\w{5,20}$/;
	if (!reg.test(v_card))
	{
		alert("护照号码输入错误！");
		return false;
	}

	return true;
}

//检查地址
function CheckAddress(v_address)
{
	if (v_address.length == 0)
	{
		alert("请输入地址！");
		return false;
	}

	var len = GetStrLen(v_address)
	if (len>40 || len<10)
	{
		alert("您输入的地址有误！");
		return false;
	}

	if (!isRuleValue(v_address))
	{
		alert("您输入的地址有误！");
		return false;
	}

	return true;
}

//检查邮编
function CheckZip(v_zip)
{
	var reg = /^\d{6}$/;
	if (!reg.test(v_zip))
	{
		alert("您输入的邮编有误！");
		return false;
	}

	return true;
}

//检查Email
function CheckEmail(v_email)
{
	var reg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;
	if (!reg.test(v_email))
	{
		alert("E-mail地址有误！");
		return false;
	}

	return true;
}

//检查手机号码
function CheckMobile(v_mobile)
{
    var reg1=/^1[3][4-9]\d{8}$/;
    var reg2=/^1[5][089]\d{8}$/;
	
	if (!reg1.test(v_mobile) && !reg2.test(v_mobile))
	{
		alert("手机号码输入错误！");
		return false;
	}

	return true;
}

//检查电话区号
function CheckPhoneArea(v_area)
{
	var reg = /^0\d{2,3}$/;
	if (!reg.test(v_area))
	{
		alert("电话区号输入错误！");
		return false;
	}

	return true;
}

//检查电话
function CheckPhone(v_phone)
{
	var reg = /^\d{6,11}$/;
	if (!reg.test(v_phone))
	{
		alert("电话号码输入错误！");
		return false;
	}

	return true;
}

//检查日期
function CheckDate(v_date)
{
	var reg = /^\d{4}-\d{1,2}-\d{1,2}$/;
	if (!reg.test(v_date))
	{
		alert("请输入正确的购买日期！");
		return false;
	}

	return true;
}

//检查密宝序列号
function CheckEkeyid(v_ekeyid)
{
	var reg = /^\d{9}$/;
	if (!reg.test(v_ekeyid))
	{
		alert("盛大密宝序列号输入错误！");
		return false;
	}

	return true;
}

//检查数字帐号
function CheckSdid(v_sdid)
{
	var reg = /^\d{5,12}$/;
	if (!reg.test(v_sdid))
	{
		alert("数字帐号输入错误！");
		return false;
	}

	return true;
}


function CheckEkeyType(v_ekeyid)
{
	//Ｄ６：10开头；Ｅ８：8开头，Ｓ６：12开头；Ａ８：13开头;TV:103开头的
	var reg = /^\d{9}$/;
	if (!reg.test(v_ekeyid))	return "";

	reg = /^103\d+/;
	if (reg.test(v_ekeyid)) 	return "TV";

	reg = /^10\d+/;
	if (reg.test(v_ekeyid)) 	return "D6";

	reg = /^8\d+/;
	if (reg.test(v_ekeyid)) 	return "E8";

	reg = /^12\d+/;
	if (reg.test(v_ekeyid)) 	return "S6";

	reg = /^13\d+/;
	if (reg.test(v_ekeyid)) 	return "A8";

	return "";
}

//检查密宝密码
function CheckEkey(v_ekey)
{
	var reg = /^\d{4,8}$/;
	if (!reg.test(v_ekey))
	{
		alert("密宝密码输入错误！");
		return false;
	}

	return true;
}

//检查密宝服务号
function CheckEkeysn(v_ekeysn)
{
	var reg = /^\d{14}(\d{2})?$/;
	if (!reg.test(v_ekeysn))
	{
		alert("密宝服务号输入错误！");
		return false;
	}

	return true;
}

//检查附加码
function CheckExcode(v_code)
{
	var reg = /^\d{4}$/;
	if (!reg.test(v_code))
	{
		alert("附加码输入错误！");
		return false;
	}

	return true;
}

//检查静态密码
function CheckPassword(v_pwd)
{
	var len = GetStrLen(v_pwd);
	if (len<4 || len>10)
	{
		alert("静态密码输入错误，密码长度应为4-10个字符！");
		return false;
	}

	return true;
}

function getElementsByName_ff(v_name)
{
	var elem = document.getElementsByTagName("input");
	var arr = new Array();
	for(i=0, iarr=0; i < elem.length; i++)
	{
		att = elem[i].getAttribute("name");
		if(att == v_name)
		{
			arr[iarr] = elem[i];
			iarr++;
		}
	}

  return arr;
}

//检查Radio值
function GetCheckedRadio(v_name)
{
	var arrRadio = getElementsByName_ff(v_name)
	var arr = new Array();
	for (i=0; i<arrRadio.length; i++)
	{
		if (arrRadio[i].checked)
			return arrRadio[i];
	}

	return null;
}

function GetCheckedBox(v_name)
{
	var arrBox = getElementsByName_ff(v_name)
	var arr = new Array();
	for (i=0, iarr=0; i<arrBox.length; i++)
	{
		if (arrBox[i].checked)
		{
			arr[iarr] =  arrBox[i];
			iarr++;
		}
	}

	return arr;
}

//检查身份证
function CheckIdcard(v_card)
{
	var reg = /^\d{15}(\d{2}[0-9X])?$/i;
	if (!reg.test(v_card))
	{
		alert("证件号码输入错误！");
		return false;
	}

	if(v_card.length==15)
	{
	    var n = new Date();
        var y = n.getFullYear();
	    if(parseInt("19" + v_card.substr(6,2)) < 1900 || parseInt("19" + v_card.substr(6,2)) > y)
	    {
            //alert("生日年份范围错误");
		    alert("证件号码输入错误！");
		    return false;
	    }

	    var birth = "19" + v_card.substr(6,2) + "-" + v_card.substr(8,2) + "-" + v_card.substr(10,2);
	    if(!isDate(birth))
	    {
            //alert("生日错误");
		    alert("证件号码输入错误！");
		    return false;
	    }
    }
	if(v_card.length==18)
	{
	    var n = new Date();
        var y = n.getFullYear();
	    if(parseInt(v_card.substr(6,4)) < 1900 || parseInt(v_card.substr(6,4)) > y)
	    {
            //alert("生日年份范围错误");
		    alert("证件号码输入错误！");
		    return false;
	    }

	    var birth = v_card.substr(6,4) + "-" + v_card.substr(10,2) + "-" + v_card.substr(12,2);
	    if(!isDate(birth))
	    {
            //alert("生日错误");
		    alert("证件号码输入错误！");
		    return false;
	    }

        iW = new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1);

        iSum = 0;
        for( i=0;i<17;i++)
        {
            iC = v_card.charAt(i);
            iVal = parseInt(iC);
            iSum += iVal * iW[i];
        }

        iJYM = iSum % 11;
        if(iJYM == 0) sJYM = "1";
        else if(iJYM == 1) sJYM = "0";
        else if(iJYM == 2) sJYM = "x";
        else if(iJYM == 3) sJYM = "9";
        else if(iJYM == 4) sJYM = "8";
        else if(iJYM == 5) sJYM = "7";
        else if(iJYM == 6) sJYM = "6";
        else if(iJYM == 7) sJYM = "5";
        else if(iJYM == 8) sJYM = "4";
        else if(iJYM == 9) sJYM = "3";
        else if(iJYM == 10) sJYM = "2";

        var cCheck = v_card.charAt(17).toLowerCase();
        if( cCheck != sJYM )
        {
            //alert("校验位不符");
            alert("证件号码输入错误");
            return false;
        }
	}

	return true;
}

//日期格式：YYYY-MM-DD
function isDate(strDate)
{
   var strSeparator = "-"; //日期分隔符
   var strDateArray;
   var intYear;
   var intMonth;
   var intDay;
   var boolLeapYear;

   strDateArray = strDate.split(strSeparator);

   if(strDateArray.length!=3) return false;

   intYear = parseInt(strDateArray[0],10);
   intMonth = parseInt(strDateArray[1],10);
   intDay = parseInt(strDateArray[2],10);

   if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay)) return false;

   if(intMonth>12||intMonth<1) return false;

   if((intMonth==1||intMonth==3||intMonth==5||intMonth==7||intMonth==8||intMonth==10||intMonth==12)&&(intDay>31||intDay<1)) return false;

   if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30||intDay<1)) return false;

   if(intMonth==2){
      if(intDay<1) return false;

      boolLeapYear = false;
      if((intYear%100)==0){
         if((intYear%400)==0) boolLeapYear = true;
      }
      else{
         if((intYear%4)==0) boolLeapYear = true;
      }

      if(boolLeapYear){
         if(intDay>29) return false;
      }
      else{
         if(intDay>28) return false;
      }
   }

   return true;
}

function myGetValueById(v_id)
{
	var obj = document.getElementById(v_id);
	if (obj == null)
		return "";
	else
		return obj.value;
}

function myIsNullById(v_id)
{
	var obj = document.getElementById(v_id);
	if (obj==null || trim(obj.value)=="")
		return true;
	else
		return false;
}