//Copyright 2000-2003 (C) Timeless Software, Ltd. All Rights Reserved.

//This software is the proprietary information of Timeless, Software, Ltd.



//此文件提供WEB应用程序中常用的javascript方法。



//检测所有控件输入值的合法性

//需要检验的控件必须含有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;

}



/**

* 检验用户指定的对象是否合法

* @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 (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"://字符串

    			{
					if (element.value.length != 0 && element.value.trim()=="") {
					  	window.alert("请输入" + discript)
					  	element.focus();
    					return(0);	
					} 	
    				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;

    			}
			case "h":
    			{
    				if (element.value.length != 0 && validateURL(element.value) == false) {
    			        window.alert("请输入合法的网页地址!");
    					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);
		
		if(count==0&&integer.length>1&&numchar=='-' ){
			continue;
		}
    	
    	numvalue = numchar - '0';

    	if (!(numvalue >= 0 && numvalue <= 9))

    		return false;

    }

    return true;

}



//判断是否有效邮件

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;

}

//校验字符串不为空
String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}




function mod(var1,var2) {

    return (var1%var2);

}	
function validateURL(url){  
{   
	var flag;
	var emailRegS=/^http([s])?[:][/][/][A-Za-z0-9 -]+\.[A-Za-z0-9]+[\/=\?%\-&_~@[\]:+!]*([^<>\'\'\"\"])*$/;
	if (!emailRegS.exec(url)) return false ;
		return true  ;
	} 
}







