﻿// JScript 文件
function resetForm()
{
	document.getElementById("aspnetForm").reset();
}

function checkPwdFormat(src)
{
	check = new String (src);
	check = check.toLowerCase();
	source = new String("0123456789abcdefghijklmnopqrstuvwxyz");
	for( i=0; i<check.length; i++)
	{
		pos =  source.indexOf(check.charAt(i));
		if ( pos < 0 )
			return false;
	}
	return true;
}

function checkEmailAddr(sEmail)
{
	// check email
	error = 0
	pos2 = sEmail.indexOf("@");
	pos3 = sEmail.indexOf(".");
	if ( pos2 == -1 || pos3 == -1)
	{
		error = -1;
	}
	if ( error != 0 )
	{
		return false;
	}
	else
	{
		return true;
	}
}

function trim(v_str)
{
	return v_str.replace(/^\s+|\s+$/g,"");
}

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 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 CheckUsername2(sUsername)
{
	var len1 = GetStrLen(sUsername);
	var len2 = sUsername.length;
	if (len1!=len2*2 || len2<2 || len2>6)
	{		
		//alert("您输入的真实姓名有误，请确认！");
		return false;
	}
	return true;
}

function CheckBirthday(v_card)
{
    var n = new Date(); 
    var y = n.getFullYear() - 18; 
    var m = n.getMonth() + 1; //从0开始计数 
    var d = n.getDate(); 
    
    var uy,um,ud;

	if(v_card.length==15)
	{
	    uy = parseInt("19" + v_card.substr(6,2));
	    um = parseInt(v_card.substr(8,2));
	    ud = parseInt(v_card.substr(10,2));
    }	
	if(v_card.length==18)
	{
	    uy = parseInt(v_card.substr(6,4));
	    um = parseInt(v_card.substr(10,2));
	    ud = parseInt(v_card.substr(12,2));
	}
	if(uy > y)
	{
		//alert("未成年！1");
	    return false;
	}
	if(uy == y && um > m)
	{
		//alert("未成年！2");
	    return false;
	}
	if(uy == y && um == m && ud > d)
	{
		//alert("未成年！3");
	    return false;
	}
	return true;
}

//检查身份证
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)
	{
	    if(v_card=="111111111111111")
	    {
		    return false;
	    }
	
	    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;
	    }

		//18位校验码验证 20100.05.28
        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 ismobile(mobile)
{
    if(mobile.length!=11)
    {
        return false;
    }
    //var myreg = /^(((13[0-9]{1})|159|(15[0-9]{1}))+\d{8})$/;
    var myreg = /^(\d{11})$/;
    if(!myreg.test(mobile))
    {
        return false;
    }
    return true;
}
