//检查用户姓名（防沉迷用）
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 CheckIdcard2(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;
}

//检查身份证
function CheckIdcard3(v_card)
{
	var reg = /^\d{15}(\d{2}[0-9X])?$/i;
	if (!reg.test(v_card))
	{
		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)
		{
			return false;
		}

		var birth = "19" + v_card.substr(6,2) + "-" + v_card.substr(8,2) + "-" + v_card.substr(10,2);
		if(!isdate(birth))
		{
			return false;
		}

		if (v_card == "111111111111111")
			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)
		{
			return false;
		}

		var birth = v_card.substr(6,4) + "-" + v_card.substr(10,2) + "-" + v_card.substr(12,2);
		if(!isdate(birth))
		{
			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 CheckForm()
{
	var selectStatus, personName, personIdCard;
	/*
  if(document.getElementById("status2").checked)
  {
      selectStatus = document.getElementById("status2").value;
  }
  if(document.getElementById("status1").checked)
  {
      selectStatus = document.getElementById("status1").value;
  }
  */

  personName = document.getElementById("username").value;
  personIdCard = document.getElementById("idcard").value;

  //验证名字和身份证
  var checkUsername = CheckUsername2(personName);
  var checkIdcard = CheckIdcard3(personIdCard);
	if (!checkUsername)
	{
      alert("您的姓名未填或者不规范！");
      return false
  }

	if (!checkIdcard)
	{
      alert("您的身份证未填或者不规范！");
      return false
  }

	//var checkIdcard2 = CheckIdcard2(personIdCard);
  var checkBirthday = CheckBirthday(personIdCard);
	if (!checkIdcard || !checkBirthday)
	{
		if (!confirm("您输入的身份证错误或年龄未满18周岁，将被纳入盛大防沉迷系统，是否继续？"))
			return false;
	}


  /*
  //成年
  if(selectStatus == "2")
  {
      if(!checkUsername || !checkIdcard || !checkBirthday )
      {
          if(!confirm("您的" + msg + "未填或者不规范，可能会在6个月后被防沉迷，确定吗？"))
          {
              return false
          }
      }
  }

  //未成年
  if(selectStatus == "1")
  {
      var msg_1 = "由于您是未成年人，将接受防沉迷限制，确定吗？";
      if(msg != "")
      {
          msg_1 = "您的" + msg + "未填或者不规范，" + msg_1;
      }
      if(!confirm(msg_1))
      {
          return false
      }
  }
  */

	window.onbeforeunload = null;
  return true;
}
