function checkspace(checkstr) {
	var str = '';
	for(i = 0; i < checkstr.length; i++) {
		str = str + ' ';
	}
	return (str == checkstr);
}
function getCityList(){
	var provinceID=document.getElementById("province").value;
	
	if(provinceID!=""){
		var citySel=document.getElementById("city");
		var hospitalSel=document.getElementById("hospital");
		var deptSel=document.getElementById("dept");
		citySel.length=1;
		hospitalSel.length=1;
		deptSel.length=1;
		$.ajax({
		    type: "post",
		    url : "ajax/get_cityList.jspx",
		    dataType:"json",
		    data: 'provinceID='+provinceID,
		    success: function(json){
			    var cityList=json.jsonRoot.cityList;
		    	//alert(cityList.length);
		    	//data = eval('('+json.jsonRoot+')');
			    //alert(data);
				
				for(var i=0;i<cityList.length;i++){
					citySel.options[i+1]=new Option(cityList[i].value,cityList[i].key);
					//alert(cityList[i].key);
				}
		    },
		    error: function(){
			     alert('加载城市出错');
			}
		});
	}
}
function getHospitalList(){
	var provinceID=document.getElementById("province").value;
	var cityID=document.getElementById("city").value;
	if(provinceID!=""&&cityID!=""){
		var hospitalSel=document.getElementById("hospital");
		var deptSel=document.getElementById("dept");
		hospitalSel.length=1;
		deptSel.length=1;
		$.ajax({
		    type: "post",
		    url : "ajax/get_hospitalList.jspx",
		    dataType:"json",
		    data: {"provinceID":provinceID,"cityID":cityID},
		    success: function(json){
			    var hospList=json.jsonRoot.hospList;
		    	//alert(cityList.length);
		    	//data = eval('('+json.jsonRoot+')');
			    //alert(data);
				
				for(var i=0;i<hospList.length;i++){
					hospitalSel.options[i+1]=new Option(hospList[i].value,hospList[i].key);
					//alert(cityList[i].key);
				}
		    },
		    error: function(){
			     alert('加载医院出错');
			}
		});
	}
}
function getDeptList(){
	var hospitalID=document.getElementById("hospital").value;
	if(hospitalID!=""){
		var deptSel=document.getElementById("dept");
		deptSel.length=1;
		$.ajax({
		    type: "post",
		    url : "ajax/get_deptList.jspx",
		    dataType:"json",
		    data: 'hospitalID='+hospitalID,
		    success: function(json){
			    var deptList=json.jsonRoot.deptList;
		    	//alert(cityList.length);
		    	//data = eval('('+json.jsonRoot+')');
			    //alert(data);
				
				for(var i=0;i<deptList.length;i++){
					deptSel.options[i+1]=new Option(deptList[i].value,deptList[i].key);
					//alert(cityList[i].key);
				}
		    },
		    error: function(){
			     alert('加载科室出错');
			}
		});
	}
}

//验证特殊字符
function strangecode(code){   
	var strangCode = "~`!@#$%^&*()_+=-|\'/？,，。<>";
    var temp;
    for (var i = 0; i<code.length; i++ )
    {
        temp = code.substring(i,i+1);
        if (strangCode.indexOf(temp) !=-1)
        {
            return false;
        }
    }
}
function checkEmail(str){
    if(str.Trim() == "")
        return true;
    var pattern = /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; 
    flag = pattern.test(str); 
    if(!flag){ 
    	//email格式不正确!
    	return false; 
    }else{
    	return true;
    }

}
function check_fm(){
	var cardType=document.getElementById('userCardType');
	var userCard=document.getElementById('userCard');
	var userName=document.getElementById('userName');
	var userSex=document.getElementById('userSex');
	//var userAge=document.getElementById('age');
	//var userBirthday=document.getElementById('birthday');
	var useryear=document.getElementById('year');
	var userPhone=document.getElementById('userPhone');
	var userMail=document.getElementById('userMail');
	var userHospCard=document.getElementById('userHospCard');
	
	var checkCode=document.getElementById('checkCode');
	var sel=document.getElementById("feeType");
	var hospArea=document.getElementById('hospArea');
	var msg=document.getElementById('msg');
	if(msg.innerText=="正在验证..."){
		return false;
	}
	if(sel)
	document.getElementById("regBean_feeTypeName").value=sel.options[sel.selectedIndex].text;;
	
	
	//验证身份证
    if (userCard.value.Trim() == ""){
        alert("请填写患者证件号！\n正式挂号就诊时，医院需核对。");
        userCard.focus();        
        return false;
    }else if(cardType.value==1){
    	if(!isIdCardNo(userCard.value)){
    		alert("请填写正确的身份证号码！");
    		userCard.focus();        
            return false;
        }
    }

   //验证姓名
    if (userName.value.Trim()==""){
        alert("\n患者姓名不能为空！\n\n（必须是2－6个汉字录入或者英文字母4－12个）！");
        userName.focus();        
        return false;
    }else if (userName.value.Trim().length <2 ){
        alert("\n患者姓名不能小于4位");
        userName.focus();
        return false;
    }else if (strangecode(userName.value)==false || userName.value.indexOf(" ") != -1){
        alert("\n患者姓名不能含有特殊字符或者空格！");
        userName.focus();        
        return false;
    }else if(fnCheckUserName()==false){
       return false;
    
    }
    
    if(userSex.value==""){
    	alert("请选择您的性别");
    	return false;
    }
    if(useryear.value==""){
    	alert("请填写您的出生年份");
    	//userBirthday.focus();
    	return false;
    }else{
    	if(!fnCmBirthday()){
    		//alert("出生日期不能大于或等于当前日期");
    		//userBirthday.focus();
        	return false;
    	}
    }
    
    
  //验证电话号码
	if(userPhone.value.length!=11){
	alert('手机号输入有误！\n请填写准确手机号。');
	userPhone.focus();
	return false;
	}
	var myreg = /^(((13[4-9]{1})|109|125|147|182|187|188|(15[0-2]{1})|(15[7-9]{1}))+\d{8})$/;
	var allreg = /^(((13[0-9]{1})|(15[0-9]{1})|180|182|(18[5-9]{1}))+\d{8})$/;
	if(hospArea.value=="32"){
		if(!allreg.test(userPhone.value)){
			alert('手机号输入有误！');
			userPhone.focus();
			return false;
		}
	}else{
		if(!myreg.test(userPhone.value)){
		alert('手机号输入有误！\n请填写中移动手机号，并准确填写。');
		userPhone.focus();
		return false;
		}
	}
	
    if(!checkEmail(userMail.value)){
    	alert("邮箱格式不正确");
    	userMail.focus();
    	return false;
    }
    if(!document.getElementById('selRad1').checked&&!document.getElementById('selRad2').checked){
    	alert("患者是否有就诊卡");
    	return false;
    }
    if(document.getElementById('selRad1').checked){
    	if(userHospCard.value==""){
        	alert("请填写患者就诊卡");
        	userHospCard.focus();
        	return false;
        }
    	if(!checkHospCard()){
    		userHospCard.focus();
        	return false;
    	}
    }
    if(!document.getElementById('userSel1').checked&&!document.getElementById('userSel2').checked){
    	alert("医生临时停诊时您的选择是");
    	return false;
    }
   
    return true;
}

function addBookmark(title,url) {
	if (window.sidebar) { 
	window.sidebar.addPanel(title, url,""); 
	} else if( document.all ) {
	window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
	return true;
	}
}

/**
 * 严格验证身份证
 * @param num
 * @return
 */
function isIdCardNo(num) {             
	num = num.toUpperCase();           
	//身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X。             
	if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))){                
		//alert('输入的身份证号长度不对，或者号码不符合规定！\n15位号码应全为数字，18位号码末位可以为数字或X。'); 
		return false;          
	} 
	//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。
	//下面分别分析出生日期和校验位 
	var len, re; 
	len = num.length; 
	if (len == 15) { 
		re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/); 
		var arrSplit = num.match(re);  //检查生日日期是否正确 
		var dtmBirth = new Date('19' + arrSplit[2] + '/' + arrSplit[3] + '/' + arrSplit[4]); 
		var bGoodDay; bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4])); 
		if (!bGoodDay) {           
			//alert('输入的身份证号里出生日期不对！');              
			return false; 
		} else { 
			//将15位身份证转成18位 //校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。          
			var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);            
			var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');            
			var nTemp = 0, i;               
			num = num.substr(0, 6) + '19' + num.substr(6, num.length - 6);            
			for(i = 0; i < 17; i ++){                 
				nTemp += num.substr(i, 1) * arrInt[i];            
			}            
			num += arrCh[nTemp % 11];               
			return num;   
			}   
		} 
	if (len == 18) { 
		re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/); 
		var arrSplit = num.match(re);  
		//检查生日日期是否正确 
		var dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]); 
		var bGoodDay; bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4])); 
		if (!bGoodDay) { 
			//alert(dtmBirth.getYear()); 
			//alert(arrSplit[2]); 
			alert('输入的身份证号里出生日期不对！'); 
			return false; 
		} else { 
			//检验18位身份证的校验码是否正确。 
			//校验位按照ISO 7064:1983.MOD 11-2的规定生成，X可以认为是数字10。 
			var valnum; var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
			var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); 
			var nTemp = 0, i; for(i = 0; i < 17; i ++) { nTemp += num.substr(i, 1) * arrInt[i]; } 
			valnum = arrCh[nTemp % 11]; if (valnum != num.substr(17, 1)) { 
				//alert('18位身份证的校验码不正确！应该为：' + valnum); 
				return false; 
			} 
			return num; 
			} 
		} 
	return false; 
} 

function checkIsInt(e){
	if (!e) e = window.event;
    var code = e.keyCode | e.which | e.charCode;
    if (code >= 48 && code <= 57) return true; // 数字
    switch (code) {
        case 8: // 退格
        case 37: case 38: case 39: case 40: // 方向键
        case 13: // 回车
        case 46: // 删除
        case 45:
            return true;
    }
    return false;
}
function checkInValue(e){
	if (!e) e = window.event;
    var code = e.keyCode | e.which | e.charCode;
    
    if (code == 32) return false; // 数字
    var character = String.fromCharCode(code);
    var txt=new RegExp("[ ,\\`,\\~,\\!,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\<,\\>,\\{,\\},\\(,\\),\\'',\\;,\\=,\"]");
    //特殊字符正则表达式
    if (txt.test(character))
    {
    	return false;
    }

    return true;
}

function checkInValueNoNum(e){
	if (!e) e = window.event;
    var code = e.keyCode | e.which | e.charCode;
    
    if (code == 32) return false; // 数字
    var character = String.fromCharCode(code);
    var txt=new RegExp("[ \\d,\\`,\\~,\\!,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\<,\\>,\\{,\\},\\(,\\),\\'',\\;,\\=,\"]");
    //特殊字符正则表达式

    if (txt.test(character))
    {
    	return false;
    }

    return true;
}

	function fnCheckUserName(){
	 var userName=document.getElementById('userName');
      var txt=new RegExp("[ \\d,\\`,\\~,\\!,\#,\\$,\\%,\\^,\\+,\\*,\\&,\\\\,\\/,\\?,\\|,\\:,\\<,\\>,\\{,\\},\\(,\\),\\'',\\;,\\=,\"]");
       //特殊字符正则表达式
       if (txt.test(userName.value))
      {
        alert("姓名格式不正确");
    	userName.focus(); 
    	return false;
      }
      return true;
	}	

String.prototype.Trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}  
String.prototype.LTrim = function() {
	return this.replace(/(^\s*)/g, ""); 
}  
String.prototype.RTrim = function() { 
	return this.replace(/(\s*$)/g, ""); 
}
function validSpace(value)
{
   var pattern=/\s/;
   if(pattern.test(value)){
     alert('Input error');
   }
}
function checkLogin(){
	var loginName=document.getElementById('loginName');
	var loginPwd=document.getElementById('loginPwd');
	var checkCode=document.getElementById('checkCode');
	if(loginName.value==""){
		alert("手机号不能为空");
		loginName.focus();
		return false;
	}
	if(loginPwd.value==""){
		alert("密码不能为空");
		loginPwd.focus();
		return false;
	}
	if(checkCode.value==""){
		alert("验证码不能为空");
		checkCode.focus();
		return false;
	}
	return true;	
	
}
function addBookmark(title,url) {
	if (window.sidebar) { 
	window.sidebar.addPanel(title, url,""); 
	} else if( document.all ) {
	window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
	return true;
	}
}
//补0
function pad(str, n) {
	var len = str.toString().length;
	if(len>0){
	while(len < n) {
		str = '0' + str;
		len++;
	}
	}
	return str;  
}  


 

