/***************************************************************************************************/
/* FileName：CheckForm_EN.js                                                                       */
/* Description:this file contents the libiry  of javascript functions that used for check the form.*/
/* Author:Bing                                                                                     */
/* Datetime:2006-09-26.                                                                            */
/***************************************************************************************************/


//function：ShowDialog 
//description：show model or not.
//parameters：url，is Modelless ，width，height，scroll or not(yes or no))
//returns：the page to open.
function ShowDialog(url, blnModeless, width, height, strScroll) {
	var basePage="ShowDialog.htm";
	
	if (blnModeless=='no'){
		var arr = showModalDialog(basePage, url, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no;scroll:" + strScroll + ";status:no");
	}
	else{
		var arr = showModelessDialog(basePage, url, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no;scroll:" + strScroll + ";status:no");
	}
}


//function :CheckAll
//description:check the form,main function
//parameters:the form name
//reutrn:
function CheckAll(myform){
	var i;
	var myform = document.forms[myform];
	for (i=0;i<myform.elements.length;i++){
		
		if (myform.elements[i].getAttribute("showName") == "null" ) continue;
		if (myform.elements[i].getAttribute("dataType")=="Comfirmpassword")
		{
			if(myform.elements[i].value !=myform.elements[i-1].value)
			{
				alert("两次输入的密码不一致，请重新输入.")
				myform.elements[i].focus();
				return false;
			}
		}
		else if (CheckInput(myform.elements[i])==false){
			myform.elements[i].focus();
			return false;
		}
	 }
	 return true;
}
//function:CheckInput
//description:check the form item.
//parameters:input data.
//return:
function CheckInput(input)
{
	var image;
	var i;
	var error = false;
	//check the input string's lenght,if the lenght too long then throw error.
	if( input.getAttribute("maxSize") != null ){
		if (strlen(input.value)>parseInt(input.getAttribute("maxSize"))){
			alert("The lenght of "+input.getAttribute("showName")+" is "+strlen(input.value)+"\n\n Longer then"+input.getAttribute("maxSize")+"\n\nPlease input again.");
			error = true;
		}
	}
	
	//check the input is null or not
	else if (input.getAttribute("nullable") =="no" && isnull(input.value)){
		alert(""+input.getAttribute("showName")+"不能为空.");
		error = true;
	}
	else{
 		//check the data is right type or not.
		switch(input.getAttribute("dataType"))
		{
			//safe or not
			case "safe": 
				if(IsSafe(input.value)==false){
					alert("The value of "+input.getAttribute("showName")+" has wrong char.\n\nPlease input again.");
					error = true;
				}
				break;
				
			//int or not
		 	case "int":
				if (IsInt(input.value)==false){
					alert("Invalid "+input.getAttribute("showName")+" number.");
					error = true;
				}
				break;
				
			//float or not
		 	case "float": 
				if (IsFloat(input.value)==false){
					alert("Invalid "+input.getAttribute("showName")+".");
					error = true;
				}
				break;
				
			//check email
			case "email": 
				 if(isnull(input.value)){
					
				 break;	
				
			     }
			
				if(EmailCheck(input.value)==false){
					alert(""+input.getAttribute("showName")+"格式错误.");
					error =true;
				}
				break;
				
			//check if URL
			case "url":
			     if(isnull(input.value)){
					
				 break;	
				
			     }
				if(IsURL(input.value)==false){
					alert(""+input.getAttribute("showName")+"格式错误.")
					error=true;
				}
				break;
			
			//telephone.
			case "tel": 
				if(CheckTEL(input.value)==false){
					alert(""+input.getAttribute("showName")+" 应该为数字.")
					error=true;
				}
				break;
			
			//password
			case "password": 
				if(CheckPWD(input.value)==false){
					alert("Password format required.")
					error=true;
				}
				break;
			
			//is date or not
			case "date": 
				if(chkdate(input.value)==false){
					alert("Datetime format is required.")
					error=true;
				}
				break;
				
			default		: break;
		}
	}
	
	//if gets error then throw it.
	if (error){
		return false;
	}
	else{
		return true;
	}
}




//function：EmailCheck  
//Description: check the email is valied.
//parameters:emailStr(the str to be checked.)
//returns：false(if not valited);  true(if valited)
function EmailCheck(emailStr){
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var emailPat=/^(.+)@(.+)$/;
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null){
		//alert("Email format required!");
		return false;
	}
	
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++){
		if (user.charCodeAt(i)>127){
			//alert("Email format required!");
			return false;
		}
	}
	
	for (i=0; i<domain.length; i++){
		if (domain.charCodeAt(i)>127){
			//alert("Email format required!");
			return false;
		}
	}
	
	if (user.match(userPat)==null){
		//alert("Email format required!");
		return false;
	}
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null){
		for (var i=1;i<=4;i++){
			if (IPArray[i]>255){
				//alert("Email format required!");
				return false;
			}
		}
		return true;
	} 
	
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++){
		if (domArr[i].search(atomPat)==-1){
			//alert("Email format required!");
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1){
		//alert("Email format required!");
		return false;
	}
	if (len<2){
		//alert("Email format required!");
		return false;
	}
	
	return true;
}




//funtion:IsURL
//description:Check the url format is right or not.
//parameters:str(the url string)
//return:if right then return true or return false.
function IsURL(str){
  var reg=/^http:\/\/[\w\.]+\w+/;
  return reg.test( str );
}




//function:IsSafe
//decription:check the str is safe or not.
//parameters:str
//return:if safe then true or false.
function IsSafe(str){
	if ( (str.indexOf('\'')==-1) && (str.indexOf(';')==-1) ) {
		return true;
	}
	return false;
}
	



//function:strlen
//decription:check the lenght of the string
//parameters:str
//return :the lenght of the string
function strlen(str){
	var i;
	var len;
	
	len = 0;
	for (i=0;i<str.length;i++){
		if (str.charCodeAt(i)>255){
			len+=2;
		} else {
			len++;
		}
	}
	return len;
}




//function:isnull
//decription:check the str is null or not.
//parameters:the string to chekc
//reutrn:if not isnull then return true or return false.
function isnull(str)
{
    var i;
    
    for (i=0;i<str.length;i++)
    {
        if (str.charAt(i)!='') {
			return false;
		}
    }
    return true;
}




//function:IsFloat
//description:check the string is float or not.
//parameters:the string to be checked.
//return:if is float then reutrn true or return false.
function  IsFloat(str)  {  
	var  i,j,strTemp;  
	strTemp="0123456789.";  
	
	for  (i=0;i<str.length;i++)  {  
		
		j=strTemp.indexOf(str.charAt(i));              
		if (j==-1)  {  
			return  false;  
		}  
	}  	  
	return  true;  
}  
 


//function:IsInt
//description:the string to be checked
//parameters:str
//return:if is IsInt then return true or return false.
function IsInt(str){
	var number_chars = "1234567890";
	var i;

	for (i=0;i<str.length;i++){
		if (number_chars.indexOf(str.charAt(i))==-1) {
			return false;
		}
	}
	return true;
}






 
//function：chkdate  
//descrition:check the string is date or not
//parameters:the string to be checked.  
//reutrn :true for is date and false for not.
function  chkdate(datestr)  {  
     var  lthdatestr  
     if  (datestr  !=  ""){ 
         lthdatestr=  datestr.length  ;  
	 } else {
		lthdatestr=0;  
	 }
				 
	var  tmpy="";  
	var  tmpm="";  
	var  tmpd="";  
	var  status;  
	status=0;  
	
	if(lthdatestr == 0) { 
		return  false ;
	}
	
	 
	for  (i=0;i<lthdatestr;i++)  { 
		if(datestr.charAt(i)==  '-'){  
			status++;  
	    } 
		
		if (status>2){ 
			return  false;  
		}  
		
		if ((status==2) && (datestr.charAt(i)!='-')){  
			tmpy=tmpy+datestr.charAt(i)  
		} 
		
		if ((status==1) && (datestr.charAt(i)!='-')){  
			tmpm=tmpm+datestr.charAt(i)  
		}  
		
		if ((status==0)  &&  (datestr.charAt(i)!='-')){  
			tmpd=tmpd+datestr.charAt(i)  
		}  
		
	}  
	year=new  String  (tmpy);  
	month=new  String  (tmpm);  
	day=new  String  (tmpd)  
	  
	if  ((tmpy.length!=4)    ||  (tmpm.length>2)    ||  (tmpd.length>2)) {  
		 return  false;  
	} 
	
	if  (!((1<=month)  &&  (12>=month)  &&  (31>=day)  &&  (1<=day))  ) {  
		 return  false;  
	} 
	
	if  (!((year  %  4)==0)  &&  (month==2)  &&  (day==29)){  
		 return  false;   
	}
	
	if  ((month<=7)  &&  ((month  %  2)==0)  &&  (day>=31)){  			
		 return  false;  
	} 
	
	if  ((month>=8)  &&  ((month  %  2)==1)  &&  (day>=31)){  
		 return  false;   
	}  
	
	if  ((month==2)  &&  (day==30)){  
		 return  false;  
	}  
	 
	return  true;  
}  
	
	
	
//Function：CheckTEL  
//Description：Check the string is telephone or not  
//parameters:the string to be checked.  
//return:if is telephon code return true or reutrn false.
function  CheckTEL(TEL)  
{  
	var  i,j,strTemp;  
	strTemp="0123456789-()#  ";  
	for  (i=0;i<TEL.length;i++)  
	{  
		   j=strTemp.indexOf(TEL.charAt(i));              
		   if  (j==-1)  
		   {  
		   //说明有字符不合法  
					   return  0;  
		   }  
	}  
	
	return  1;  
}  


//function:checkPWD
//description:check the password has wrong 
//参数说明：要检查的字符串  
//返回值：0：含有  1：全部为数字或字母  
function  CheckPWD(str)  
{  
   var  strSource  ="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";  
   var  ch;  
   var  i;  
   var  temp;  
     
   for  (i=0;i<=(str.length-1);i++)  
   {  
       ch  =  str.charAt(i);  
       temp  =  strSource.indexOf(ch);  
       if  (temp==-1)    
       {  
         return  0;  
       }  
   }  
   if  (strSource.indexOf(ch)==-1)  
   {  
       return  0;  
   }  
   else  
   {  
       return  1;  
   }    
}  





