var yyyy;
var mm;
var dd;
var birthday;
var sex;

function getCardYear(){
    return yyyy;
}
function getCardMonth(){
    return mm;
}
function getCardDate(){
    return dd;
}
function getCardBirthday(){
    return birthday;
}
function getCardSex(){
    return sex;
}
function getCardAge(){
    var mm=getCardMonth();
    if (mm<10)
       mm=mm.substring(1,2);
			
		var d;
    d = new Date();	
    return Math.floor((parseInt(d.getYear())*12+parseInt(d.getMonth())-parseInt(getCardYear())*12-parseInt(mm))/12);
}

//判断是否大龄,男50,女40
function isBigAge(){
    if (parseInt(getCardAge())>=40 && parseInt(getCardSex())==2)
        return "1";
    if (parseInt(getCardAge())>=50 && parseInt(getCardSex())==1)
        return "1";
    return "0";
}
//校验身份证号码
function CheckCardValue(idCard){
    var id=idCard;
    var id_length=id.length;

    if (id_length==0){
        alert("请输入身份证号码!");
        return false;
    }

    if (id_length!=15 && id_length!=18){
        alert("身份证号长度应为15位或18位！");
        return false;
    }

    if (id_length==15){
        yyyy="19"+id.substring(6,8);
        mm=id.substring(8,10);
        dd=id.substring(10,12);

        if (mm>12 || mm<=0){
            //alert("输入身份证号,月份非法！"+mm);   
            alert("输入身份证号,月份非法！");
            return false;
        }

        if (dd>31 || dd<=0){
            alert("输入身份证号,日期非法！");
            return false;
        }

        birthday=yyyy+ "-" +mm+ "-" +dd;

        if ("13579".indexOf(id.substring(14,15))!=-1){
            sex="1";
        }else{
            sex="2";
        }
    }else if (id_length==18){
        if (id.indexOf("X") > 0 && id.indexOf("X")!=17 || id.indexOf("x")>0 && id.indexOf("x")!=17){
            alert("身份证中\"X\"输入位置不正确！");
            return false;
        }

        yyyy=id.substring(6,10);
        if (yyyy>2200 || yyyy<1900){
            alert("输入身份证号,年度非法！");
            return false;
        }

        mm=id.substring(10,12);
        if (mm>12 || mm<=0){
            alert("输入身份证号,月份非法！");
            return false;
        }

        dd=id.substring(12,14);
        if (dd>31 || dd<=0){
            alert("输入身份证号,日期非法！");
            return false;
        }

        if (id.charAt(17)=="x" || id.charAt(17)=="X")
        {
            if ("x"!=GetVerifyBit(id) && "X"!=GetVerifyBit(id)){
                alert("身份证校验错误，请检查最后一位！");
                return false;
            }

        }else{
            if (id.charAt(17)!=GetVerifyBit(id)){
                alert("身份证校验错误，请检查最后一位！");
                return false;
            }
        }

        birthday=id.substring(6,10) + "-" + id.substring(10,12) + "-" + id.substring(12,14);
        if ("13579".indexOf(id.substring(16,17)) > -1){
            sex="1";
        }else{
            sex="2";
        }
    }

    return true;
}
//15位转18位中,计算校验位即最后一位
function GetVerifyBit(id){
    var result;
    var nNum=eval(id.charAt(0)*7+id.charAt(1)*9+id.charAt(2)*10+id.charAt(3)*5+id.charAt(4)*8+id.charAt(5)*4+id.charAt(6)*2+id.charAt(7)*1+id.charAt(8)*6+id.charAt(9)*3+id.charAt(10)*7+id.charAt(11)*9+id.charAt(12)*10+id.charAt(13)*5+id.charAt(14)*8+id.charAt(15)*4+id.charAt(16)*2);
    nNum=nNum%11;
    switch (nNum) {
       case 0 :
          result="1";
          break;
       case 1 :
          result="0";
          break;
       case 2 :
          result="X";
          break;
       case 3 :
          result="9";
          break;
       case 4 :
          result="8";
          break;
       case 5 :
          result="7";
          break;
       case 6 :
          result="6";
          break;
       case 7 :
          result="5";
          break;
       case 8 :
          result="4";
          break;
       case 9 :
          result="3";
          break;
       case 10 :
          result="2";
          break;
    }
    //document.write(result);
    return result;
}
//15位转18位
function Get18(idCard){
 if (CheckCardValue(idCard)){
  var id = idCard;
  var id18=id;
  if (id.length==0){
   alert("请输入15位身份证号！");
   return false;
  }
  if (id.length==15){
	id18=id.substring(0,6)+"19"+id.substring(6,15);
   //if (id.substring(6,8)>20){
   // id18=id.substring(0,6)+"19"+id.substring(6,15);
   //}else{
   // id18=id.substring(0,6)+"20"+id.substring(6,15);
  // }

   id18=id18+GetVerifyBit(id18);
  }

  return id18;
 }else{
  return id18;
 }
}

//18位转15位
function Get15(idCard){
	idCard = idCard.substring(0,6) + idCard.substring(8,17); 
	return idCard;
}

function convertCard(cardid) {
	if (cardid + '' == 'undefined' || cardid == "") {
		return "";
	}
	
	if (CheckCardValue(cardid) == false) {
		return cardid;	
	}
			
	if (cardid.length== 15){	
		cardid = Get18(cardid);	
	} else if (cardid.length== 18) {
		cardid = Get15(cardid);
	} else {
		alert('身份证号码输入有误！');
	} 
	return cardid; 
}

   function isCardid(idCardObj, dateObj, SexObj) {
        var cardid = idCardObj.value;
        if (CheckCardValue(cardid) == true) {
						idCardObj.value = Get18(cardid);
						
						if (dateObj == '' || dateObj + '' == 'undefined') {
							return;
						}
						
						if (dateObj != null && dateObj != ""  && dateObj != "undefined") {
            	dateObj.value = getCardBirthday();
						}
						
						if (SexObj == '' || SexObj  + '' == 'undefined') {
							return;
						}
												
            var sex = getCardSex();
            if (sex == 1) {
                SexObj.value  = "男";
            } else {
                SexObj.value  = "女";           
            } 
        }
    }

    function isCardidRows(idCardObj, dateObj, SexObj, ageObj) {
        var cardid = idCardObj.value;
        if (CheckCardValue(cardid) == true) {
            idCardObj.value = Get18(cardid);
						if (dateObj != '' && dateObj != 'undefined') {
							document.forms[0].all[dateObj].value =  getCardBirthday(); 
						}

						if (ageObj != '' && ageObj != 'undefined') {
							document.forms[0].all[ageObj].value =  getCardAge(); 
						}
												
						if (SexObj == '' || SexObj == 'undefined') {
							return;
						}
												
            var sex = getCardSex();
						var obj = FindSelectObject(SexObj); 
						SetDestSelectObject(obj);
            if (sex == 1) {
                SetSelectedIndex("男");
            } else {
                SetSelectedIndex("女");
            } 
        }
    }

