/*登录页面验证*/
function checkLogin() {
    if (checkNull("u_name", "请填写用户名！", 1) == false) {
        return false;
    }
    if (checkNull("u_pass", "请填写密码！", 0) == false) {
        return false;
    }
}
/*注册页面验证*/
function checkReg() {
	var s_name = document.getElementById("s_name");
	
    if (checkNull("uname", "请填写姓名！", 1) == false) {
        return false;
    }
	
	//检测用户名是否已经注册
	//if ( checkUname(document.getElementById("s_name").value) == false) {
    //    return false;
    //}
	
    if (checkCHIN("uname", "请填写真实姓名") == false) {
        return false;
    }
	
	if ( s_name.value.length<6 ) {
        alert("用户名少于6位，请更换用户名");
		s_name.value="";
		s_name.focus();
		return false;
    }
	
    if (document.getElementsByName("sex")[0].checked == false && document.getElementsByName("sex")[1].checked == false) {
        alert("请选择性别");
        return false;
    }
    if (checkNull("phone", "请填写电话！", 1) == false) {
        return false
    }
    if (checkTel("phone", "请正确填写11位的手机号码或固定电话号码（例如010-12345678）！") == false) {
        return false;
    }
    if (checkNull("pwd", "请填写密码", 0) == false) {
        return false;
    }
    if (checkEqual("pwd", "repassword", "两次输入的密码不一致") == false) {
        return false;
    }
	
}
/*基本资料完善页面验证*/
function checkInfor() {
    if (checkNull("tel", "请填写电话！", 1) == false) {
        return false
    }
    if (checkTel("tel", "请正确填写11位的手机号码或固定电话号码（例如010-12345678）！") == false) {
        return false;
    }
    if (checkNull("email", "请填写电子邮件", 1) == false) {
        return false;
    }
    if (checkEmail("email", "电子邮件格式不正确，请重新输入") == false) {
        return false;
    }
}

/*学员验证*/
function checkStudent() {
    if (checkNull("stuId", "请填写学号！", 1) == false) {
        return false
    }
    if (checkNull("identityCard", "请填写身份证号！", 1) == false) {
        return false
    }

}
/*证书查询页面验证*/
function checkQuery() {

	if (checkNull("uname", "请填写姓名！", 1) == false) {
		  return false
//	  } 
//	  else if (checkCHIN('uname', '请填写中文') == false) {
//		  return false;
	}
	//add by li :if validIDtype is personal ID,then check ,else step
	var typeSelect=document.getElementById("validIDtype");
	if(typeSelect.options[typeSelect.selectedIndex].value=="1")
	{
	  if (checkNull("IdCardNo", "请填写身份证号！", 1) == false) {
		  return false
	  } else if (checkCert('IdCardNo', '请检查输入的身份证号是否正确！') == false) {
		  return false;
	  }
	}
    return true;
}

function trim(str) {
    return rtrim(ltrim(str));
}

//去掉字符串左边的空格 
function ltrim(s) {
    if (s == null) return "";
    var whitespace = new String(" \t\n\r");
    var str = new String(s);
    if (whitespace.indexOf(str.charAt(0)) != -1) {
        var j = 0,
        i = str.length;
        while (j < i && whitespace.indexOf(str.charAt(j)) != -1) {
            j++;
        }
        str = str.substring(j, i);
    }
    return str;
}

//去掉字符串右边的空格 
function rtrim(s) {
    if (s == null) return "";
    var whitespace = new String(" \t\n\r");
    var str = new String(s);
    if (whitespace.indexOf(str.charAt(str.length - 1)) != -1) {
        var i = str.length - 1;
        while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1) {
            i--;
        }
        str = str.substring(0, i + 1);
    }
    return str;
}
/*验证email*/
function checkEmail(mail, alertStr) {
    var email = trim(document.getElementById(mail).value);
    var reg = /^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;
    if (reg.test(email) == false) {
        alert(alertStr + "，例如zhangsan@qingniao.com.cn或zhangsan@qingniao.com");
        return false;
    }
}
/*验证两次输入的内容相同*/
function checkEqual(pwd, repwd, alertStr) {
    var repwd = document.getElementById(repwd).value;
    var pwd = document.getElementById(pwd).value;
    if (pwd != repwd) {
        alert(alertStr);
        return false;
    }
}
/*验证手机号码或固定电话*/
function checkTel(tel, alertStr) {
    var phone = document.getElementById(tel).value;
    var regMobile = /^1\d{10}$/;
    var regTel = /^\d{3,4}-\d{7,8}$/;
    if (regMobile.test(phone) == false && regTel.test(phone) == false) {
        alert(alertStr);
        return false;
    }
}

/*验证文本框或下拉列表框内容不能为空,num=0时不去掉左右空格，num=1时去掉左右空格*/
function checkNull(elementId, alertStr, num) {
    var eValue;
    if (num == 0) {
        eValue = document.getElementById(elementId).value;
    } else {
        eValue = trim(document.getElementById(elementId).value);
    }
    if (eValue == "") {
        alert(alertStr);
        document.getElementById(elementId).focus();
        return false;
    }
}

/*验证输入内容必须为汉字*/
function checkCHIN(elementId, alertStr) {
    var eValue;
    eValue = document.getElementById(elementId).value;

    obj = eValue.match(/^[\u4e00-\u9fa5]*$/g);
    if (obj == null) {
        alert(alertStr);
        document.getElementById(elementId).value = "";
        document.getElementById(elementId).focus();
        return false;
    }
}
//检查身份证号码合法性*/
function checkCert(elementId, alertStr) {
    var eVlaue = document.getElementById(elementId).value;
	eVlaue =trim(eVlaue);  //add by li
	var result=false;
	if (eVlaue.length == 15) 	
		result=validId15(eVlaue) ;
	else if(eVlaue.length == 18)
		result=validId18(eVlaue) ;
    if (!result){
			alert(alertStr);
        	document.getElementById(elementId).focus();
			return false;  //add by li
	};
	return true; //add by li
}


 //校验18位的身份证号码      
function validId18(_id) {
	for (var i = 0; i < _id.length; i++) {
        //校验每一位的合法性      
        if (_id.charAt(i) < '0' || _id.charAt(i) > '9') {
		     //add by li
			if(i==17){
				if((_id.charAt(i)=='X') || (_id.charAt(i)=='x'))	continue;
			}
            return false;
            break;
        }
    }
    var year = _id.substr(6, 4);
    var month = _id.substr(10, 2);
    var day = _id.substr(12, 2);
    //var sexBit = _id.substr(14);
    //校验年份位      
    if (year < '1940' || year > '1999') return false;
    //校验月份      
    if (month < '01' || month > '12') return false;
    //校验日      
    if (day < '01' || day > '31') return false;
    //设置性别      
    /*if (sexBit % 2 == 0) {
        sex = "female";
    } else {
        sex = "male";
    }
	*/
    return true;
}

/*
//校验18位的身份证号码 
function validId18(_id) {
    var powers = new Array("7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2");
    var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
    var _num = _id.substr(0, 17);
    var _parityBit = _id.substr(17);
    var _power = 0;
    for (var i = 0; i < 17; i++) {
        //校验每一位的合法性      
        if (_num.charAt(i) < '0' || _num.charAt(i) > '9') {
            return false;
            break;
        } else {
            //加权      
            _power += parseInt(_num.charAt(i)) * parseInt(powers[i]);
            //设置性别      
            if (i == 16 && parseInt(_num.charAt(i)) % 2 == 0) {
                sex = "female";
            } else {
                sex = "male";
            }
        }
    }
    //取模      
    var mod = parseInt(_power) % 11;
    if (parityBit[mod].toLowerCase() == _parityBit.toLowerCase()) {
        return true;
    }
    return false;
}
*/

//校验15位的身份证号码      
function validId15(_id) {

    for (var i = 0; i < _id.length; i++) {
        //校验每一位的合法性      
        if (_id.charAt(i) < '0' || _id.charAt(i) > '9') {
            return false;
            break;
        }
    }
    var year = _id.substr(6, 2);
    var month = _id.substr(8, 2);
    var day = _id.substr(10, 2);
    var sexBit = _id.substr(14);
    //校验年份位      
    if (year < '01' || year > '99') return false;
    //校验月份      
    if (month < '01' || month > '12') return false;
    //校验日      
    if (day < '01' || day > '31') return false;
    //设置性别      
    if (sexBit % 2 == 0) {
        sex = "female";
    } else {
        sex = "male";
    }
    return true;
}

//add by li:for form.php,student check service
function checkStu(){
	var typeSelect=document.getElementById("validIDtype");
	if(typeSelect.options[typeSelect.selectedIndex].value=="1")
	{
	  if (checkNull("IdCardNo", "请填写身份证号！", 1) == false) {
		  return false
	  } else if (checkCert('IdCardNo', '请检查输入的身份证号是否正确！') == false) {
		  return false;
	  }
	}
    return true;
}


function checkUname(uname){
	var xmlhttp;
	try{ xmhhttp=new ActiveXObjec("Msxml2.XMLHTTP");}
	catch(e){
		try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
		catch(e){
			try{ xmlhttp=new XMLHttpRequest();}
			catch(e){}
		}
	}
	
	xmlhttp.onreadystatechange=function(){
		if(4==xmlhttp.readyState){
			if(200==xmlhttp.status){	   
				var num = parseInt(xmlhttp.responseText);
				if(num>0){
					alert("用户名重复，请重新选择用户名！");
					document.regform.s_name.value="";
					document.regform.s_name.focus();
					return false;
				}
			}else{
				alert(xmlhttp.status);
			
			}
		}
	}
	
	xmlhttp.open("get","checkReg.php?n="+uname);
	xmlhttp.send(null);	

}

