
//此文件提供WEB应用程序中常用的javascript方法。

//add by jack;
function lTrim(str) 
{ 
	if (str.charAt(0) == " ") 
	{ 
	  //如果字串左边第一个字符为空格 	
	  str = str.slice(1);//将空格从字串中去掉 
	  //这一句也可改成 str = str.substring(1, str.length); 
	  str = lTrim(str); //递归调用 
	} 
	return str; 
}

//去掉字串右边的空格 
function rTrim(str) 
{ 
  	var iLength; 

		iLength = str.length; 
		if (str.charAt(iLength - 1) == " ") 
		{ 
		//如果字串右边第一个字符为空格 
		str = str.slice(0, iLength - 1);//将空格从字串中去掉 
		//这一句也可改成 str = str.substring(0, iLength - 1); 
		str = rTrim(str); //递归调用 
		} 
		return str; 
} 

//去除字符串两边空格的函数
//参数：mystr传入的字符串
//返回：字符串mystr
function trim(mystr){
		while ((mystr.indexOf(" ")==0) && (mystr.length>1)){
		   mystr=mystr.substring(1,mystr.length);
		}//去除前面空格
		while ((mystr.lastIndexOf(" ")==mystr.length-1)&&(mystr.length>1)){
		   mystr=mystr.substring(0,mystr.length-1);
		}//去除后面空格
		if (mystr==" "){
		   mystr="";
		}
		return mystr;
}

//end


//检测所有控件输入值的合法性

//需要检验的控件必须含有tag，如：

//<input type="text" name="int" tag="整数|0|i|4|10|30">

function checkAll(p_frm) {

	var eCount = p_frm.elements.length;

	var v="";

	

	var discript = "";//输入值名称

	var nullflag = "1";//是否可为空值，0－不可，1－可

	var v_type = "s";//输入值类型，s－字符串， i－整数，f－浮点数，d－日期，e－邮件，c－身份证，p－邮编

	var length = "";//输入值长度

	var mins = "";//最小输入值

	var maxs = "";//最大输入值	

	

	for(var i = 0; i < eCount; i++) {



		discript = "";

		v_type = "s";

		length = "";

		nullflag = "1";

		maxs = "";

		mins = "";





		var e=p_frm.elements[i];

		if (e.tag == null || e.tag == "")

			continue;

		parseTag(e.tag);

		if (( v = getNextValue()) != null) {

			discript = v;

		}

		if ((v = getNextValue()) != null) {

			nullflag = v;

		}

		if ((v = getNextValue()) != null) {

			v_type = v;

		}

		if ((v = getNextValue()) != null) {

			length = v;

		}

		if ((v = getNextValue()) != null) {

			mins = eval(v);

		}

		if ((v = getNextValue()) != null) {

			maxs = eval(v);

		}

			

		if (checkValidate(e, discript, v_type, length, nullflag, maxs, mins) == "0")

			return false;

	}

	return true;

}



function checkType(p_frm) {

	var eCount = p_frm.elements.length;

	var v="";



	var discript = "";//输入值名称

	var nullflag = "1";//是否可为空值，0－不可，1－可

	var v_type = "s";//输入值类型，s－字符串， i－整数，f－浮点数，d－日期，e－邮件，c－身份证，p－邮编

	var length = "";//输入值长度

	var mins = "";//最小输入值

	var maxs = "";//最大输入值



	for(var i = 0; i < eCount; i++) {



		discript = "";

		v_type = "s";

		length = "";

		nullflag = "1";

		maxs = "";

		mins = "";





		var e=p_frm.elements[i];

		if (e.tag == null || e.tag == "")

			continue;

		parseTag(e.tag);

		if (( v = getNextValue()) != null) {

			discript = v;

		}

		if ((v = getNextValue()) != null) {

			nullflag = v;

		}
		//nullflag = 1 ;

		if ((v = getNextValue()) != null) {

			v_type = v;

		}

		if ((v = getNextValue()) != null) {

			length = v;

		}

		if ((v = getNextValue()) != null) {

			mins = eval(v);

		}

		if ((v = getNextValue()) != null) {

			maxs = eval(v);

		}



		if (checkValidateType(e, discript, v_type, length, nullflag, maxs, mins) == "0")

			return false;

	}

	return true;

}


/**

* 检验用户指定的对象是否合法

* @param element        用户需要校验的对象

* @param discript       对象的描述

* @param type           对象的类型:

*                       i  整数

*                       f  浮点           

*                       s  字符

*                       d  日期

*                       e  邮件

*                       c  身份证

*                       p  邮编

* @param length         对象的最大长度

* @param type           能否为空：

*                       1  可以为空

*                       0  不能为空

* @param maxs           最大值

* @param mins           最小值

* @return               全部条件合法返回true；否则返回false

*/

function checkValidate(element, discript, type, length, nullflag, maxs, mins) {

    if (element == "") {

        window.alert("函数调用出错,请输入控件!")

        return(0);

    }

    

    if (discript == "") {

        window.alert("函数调用出错,请输入控件描述!")

        return(0);

    }

    

    if (nullflag == 0) {
    	
        if (trim(element.value) == "") {

    	    window.alert("请输入" + discript + "！");

    	    element.focus();

    	    return(0); 

    	}

    }

    

    if (type != "") {

        switch (type) {	

    		case "i"://整数

    			{

    				if (element.value.length != 0 && isInteger(element.value) != true) {

    					window.alert(discript + "必须输入整数！")

    					element.focus();

    					return(0);		   

    				}

    				break;  

    			}

    

    		case "f"://浮点数

    			{

    				if (element.value.length != 0 && isNaN(element.value) == true) {

    			        window.alert(discript + "必须输入数字！")

    					element.focus();

    					return(0);

    				}

    				break;

    			}

    		case "s"://字符串

    			{

    				break;

    			}

    		case "d"://日期

    			{

    				if (element.value.length != 0 && isDate(element.value) == false) {

    			        window.alert(discript + "必须输入有效日期！")

    					element.focus();

    					return(0);		   

    				}

    			    break;

    			}

    	    case "e"://邮件

    			{

    				if (element.value.length != 0 && isEmail(element.value) == false) {

    			        window.alert("请输入有效的" + discript + "！")

    					element.focus();

    					return(0);		   

    				}

    			    break;

    			}

    		case "c"://身份证

    			{

    				if (element.value.length != 0 && isIdCard(element.value) == false) {

    			        window.alert("请输入有效的" + discript + "！")

    					element.focus();

    					return(0);		   

    				}

    			    break;

    			}

    		case "p"://邮编

    			{

    				if (element.value.length != 0 && isPostCode(element.value) == false) {

    			        window.alert("请输入有效的" + discript + "！")

    					element.focus();

    					return(0);		   

    				}

    			    break;

    			}

    	}

    }

    

    if (length != "" && type != "d") {

        if (lengthCheck(element.value, length)) {

    	    window.alert(discript + "的长度必须小于" + length/2 + "个汉字！" + length + "个字符！");

    	    element.focus();

    	    return(0);

    	}

    }

    

    if (maxs != "" && mins != "") {

        if (element.value < mins || element.value > maxs) {

            window.alert(discript + "的值必须小于" + maxs + ", 大于" + mins + "！");

    	    element.focus();

    	    return(0);

        }

    } else {

        if (maxs != "") {

    	    if (element.value > maxs) {

    	        window.alert(discript + "的值必须小于" + maxs + "！");

    		    element.focus();

    		    return(0);

    	    }

        }

        if (mins != "") {

    	    if (element.value < mins) {

    	        window.alert(discript + "的值必须大于" + mins + "！");

    		    element.focus();

    		    return(0);

    	    }

        }

    }

    

    return(1);

}

//只检验类型
function checkValidateType(element, discript, type, length, nullflag, maxs, mins) {

    if (element == "") {

        window.alert("函数调用出错,请输入控件!")

        return(0);

    }



    if (discript == "") {

        window.alert("函数调用出错,请输入控件描述!")

        return(0);

    }



   // if (nullflag == 0) {

     //   if (element.value == "") {

    //	    window.alert("请输入" + discript + "！");

    //	    element.focus();

    //	    return(0);

    //	}

    //}



    if (type != "") {

        switch (type) {

    		case "i"://整数

    			{

    				if (element.value.length != 0 && isInteger(element.value) != true) {

    					window.alert(discript + "必须输入整数！")

    					element.focus();

    					return(0);

    				}

    				break;

    			}



    		case "f"://浮点数

    			{

    				if (element.value.length != 0 && isNaN(element.value) == true) {

    			        window.alert(discript + "必须输入数字！")

    					element.focus();

    					return(0);

    				}

    				break;

    			}

    		case "s"://字符串

    			{

    				break;

    			}

    		case "d"://日期

    			{

    				if (element.value.length != 0 && isDate(element.value) == false) {

    			        window.alert(discript + "必须输入有效日期！")

    					element.focus();

    					return(0);

    				}

    			    break;

    			}

    	    case "e"://邮件

    			{

    				if (element.value.length != 0 && isEmail(element.value) == false) {

    			        window.alert("请输入有效的" + discript + "！")

    					element.focus();

    					return(0);

    				}

    			    break;

    			}

    		case "c"://身份证

    			{

    				if (element.value.length != 0 && isIdCard(element.value) == false) {

    			        window.alert("请输入有效的" + discript + "！")

    					element.focus();

    					return(0);

    				}

    			    break;

    			}

    		case "p"://邮编

    			{

    				if (element.value.length != 0 && isPostCode(element.value) == false) {

    			        window.alert("请输入有效的" + discript + "！")

    					element.focus();

    					return(0);

    				}

    			    break;

    			}

    	}

    }



    if (length != "" && type != "d") {

        if (lengthCheck(element.value, length)) {

    	    window.alert(discript + "的长度必须小于" + length/2 + "个汉字！" + length + "个字符！");

    	    element.focus();

    	    return(0);

    	}

    }



    if (maxs != "" && mins != "") {

        if (element.value < mins || element.value > maxs) {

            window.alert(discript + "的值必须小于" + maxs + ", 大于" + mins + "！");

    	    element.focus();

    	    return(0);

        }

    } else {

        if (maxs != "") {

    	    if (element.value > maxs) {

    	        window.alert(discript + "的值必须小于" + maxs + "！");

    		    element.focus();

    		    return(0);

    	    }

        }

        if (mins != "") {

    	    if (element.value < mins) {

    	        window.alert(discript + "的值必须大于" + mins + "！");

    		    element.focus();

    		    return(0);

    	    }

        }

    }



    return(1);

}

//判断是否整数

function isInteger(integer) {

    var count;

    var numchar;

    var numvalue;	

    for (count = 0; count < integer.length; count++) {

    	numchar = integer.charAt(count);

    	numvalue = numchar - '0';

    	if (!(numvalue >= 0 && numvalue <= 9))

    		return false;

    }

    return true;

}

//判断是否字母

function isCharacter(c) {

    var count;

    var value;	

    for (count = 0; count < c.length; count++) {

    	value = c.charAt(count);

    	if (!((value >= 'a' && value <= 'z') || (value >= 'A' && value <= 'Z')))
    		return false;

    }

    return true;

}

function isValidPassword(c) {
	var count;

    var str;
    var charFlag = 0;
    var numFlag = 0;

    for (count = 0; count < c.length; count++) {

    	str = c.charAt(count);
    	if(isCharacter(str)) {
    		charFlag = 1;		
    	}
    	else if(isInteger(str)) {
    		numFlag = 1;		
    	}
    	else
    		return -1;
    }
    if(charFlag == 0|| numFlag ==0)
    	return 0

    return 1;		
}

//判断是否有效邮件

function isEmail(mail) {

    if (killSpace(mail) == "") return false;

    

	var v_email = mail.substring(mail.indexOf("@") + 1)

	if ( (mail.indexOf("@") == -1) ||

              (mail.indexOf("@") == 0)  ||

               (mail.indexOf("@") != mail.lastIndexOf("@")) ||

                 (v_email.indexOf(".") == -1)  ||

	                   (v_email.indexOf(".") == 0) ||

                     ((mail.indexOf(".") + 1) == mail.length) ) {

		return false;

	}	

	return true;    

}



//判断是否为身份证

function isIdCard(idCard) {

    var v_idCard = killSpace(idCard);

    if (v_idCard == "") return false;

    

    if (v_idCard.length != 15 && v_idCard.length != 18) {

        alert("身份证必须为15位或18位！");

        return false;

    }

    

    if (!isInteger(v_idCard)) {

        alert("身份证必须全部为数字！");

        return false;

    }else {

        return true;

    }

}



//判断是否为邮编

function isPostCode(postCode) {

    var v_postCode = killSpace(postCode);

    if (v_postCode == "") return false;

        

    if (v_postCode.length != 6) {

        alert("邮政编码必须为6位数字！");

        return false;

    }

    

    if (!isInteger(v_postCode)) {

        alert("邮政编码必须全部为数字！");

        return false;

    }else {

        return true;

    }

}



/**

* 调用此函数校验日期。

* @param s         输入日期string s,如2001-01-01

* @return          若输入的日期没有包含非法字符，以及该日期合法，则返回true；否则返回false

*/

function isDate(s) {

    if (s.length != 10 || s.charAt(4) != '-' || s.charAt(7) != '-')

    	return false;

    if (!isInteger(s.substring(0, 4)) || !isInteger(s.substring(5, 7)) || !isInteger(s.substring(8, 10)) || eval(s.substring(0, 4)) < 1 || eval(s.substring(5, 7)) < 1 || eval(s.substring(8, 10)) < 1)

    	return false;

    if (checkDate(s.substring(0, 4), s.substring(5, 7), s.substring(8, 10)) == false)

    	return false;		

    return true;

}



//检验日期是否合法

function checkDate(year, month, day) {

    var iyear;

    var imonth;

    var iday;

    if(year.length != 4 || month.length != 2 || day.length != 2)  

    	return false;

    if (!isInteger(year) || !isInteger(month) || !isInteger(day))

    	return false;

    iyear = getValueOfInt(year);

    imonth = getValueOfInt(month);

    iday = getValueOfInt(day);

    if (imonth < 1 || imonth > 12) return false;

    switch(imonth) {

    	case 1:

    	case 3:

    	case 5:

    	case 7:

    	case 8:

    	case 10:

    	case 12:

    		if (iday > 31) return false;

    			break;

    	case 4:

    	case 6:

    	case 9:

    	case 11:

    		if (iday > 30) return false;

    		break;

    	default:

    		if(mod(iyear, 4) == 0 && (mod(iyear, 100) != 0 || mod(iyear, 400) == 0)) {//判断是否润年

    			if (iday > 29) return false;

    		}else {

    			if (iday > 28) return false;

    		}

    }

	return true;

}



//取得输入字符串所代表的整数值

function getValueOfInt(string) {

    var count;

    var numchar;

    var numvalue;

    var value;

    value = 0;

    for ( count = 0; count < string.length; count++) {

    	numchar = string.charAt(count);

    	numvalue = numchar - '0';

    	value = value * 10 + numvalue;

    }

    return value;

}



var e_tag; //当前正在分拆得Tag值



//开始解析分拆Tag值

function parseTag(p_tag) {

	e_tag = p_tag;

}



//得到下一个分拆处理的值

function getNextValue() {

	if(e_tag == null || e_tag == "")

		return null;

	var p = e_tag.indexOf("|");

	if(p == -1)

		p = e_tag.length;

	var r = e_tag.substring(0,p);

	e_tag = e_tag.substring(p + 1, e_tag.length);

	return r;

}



//删除空格处理，供checkValidate调用

function killSpace(x) {

    while ((x.length > 0) && (x.charAt(0) == ' '))

    	x = x.substring(1, x.length)

    while ((x.length > 0) && (x.charAt(x.length - 1) == ' '))

    	x = x.substring(0, x.length - 1)

    return x;

}



//计算中文长度

function count_char(str) {

	var len = 0;

	for(i = 0; i < str.length; i++) {

		var ech = escape(str.charAt(i));

		if ( ech.length > 4 ){

//			len++;



//// 修改下面的数字，len + 1 表示2个字符代表一个中文字，len + 5 表示6个字符代表一个中文字

			len = len + 1;



/*

			if (ech>"%u07ff")

				len++;

*/			

		}

		len ++;

	}

	return len;

}



//检查输入的内容是否超过指定的长度

function lengthCheck(text, size) {

	var len = count_char(text);

	if ( len > size ) {

        return true;

	}

	return false;

}





function mod(var1,var2) {

    return (var1%var2);

}
	







