﻿function CheckForm(form,mode) {
    if (Validator.Validate(form, mode)===true) {
        if($("BtnSubmit").id) {
            $('BtnSubmit').disabled = true;
            setTimeout("$('BtnSubmit').disabled=false",900000);
        }
        return true;
    } return false;
}
Validator = {
    Require : /.+/,
    Email : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
    Phone : /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,5})?$/,
    Mobile : /^((\(\d{2,3}\))|(\d{3}\-))?1\d{10}$/,
    IdCard : /^\d{17}[\d|X]|\d{15}$/,
    Chinese : /^[\u0391-\uFFE5]+$/,
    Url : /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?$/,
    Number : /^[+|-]?\d+$/,
    Zip : /^\d{6}$/,
    QQ : /^[1-9]\d{4,15}$/,
    Double : /^\d+(\.\d+)?$/,    
    Username : /^[\u4e00-\u9fa5_a-zA-Z0-9]{2,20}$/,
    Pwd : /^\S{6,16}$/,
    Date : /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[1-9]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/,
    Repwd : "value==document.getElementsByName(getAttribute('to'))[0].value",
    Vcode : /^\d{4}$/,
    Myname : /^[\u0391-\uFFE5]{2,10}$/,
    Filter : "this.DoFilter(value,getAttribute('accept'))",
    Limit : "this.limit(value.length,getAttribute('min'),  getAttribute('max'))",
    LimitB : "this.limit(this.LenB(value), getAttribute('min'), getAttribute('max'))",
    Range : "getAttribute('min') < (value|0) && (value|0) < getAttribute('max')",
    Custom : "this.Exec(value, getAttribute('regexp'))",
    Group : "this.MustChecked(getAttribute('name'), getAttribute('min'), getAttribute('max'))",
    ErrorItem : [document.forms[0]],
    ErrorMsg : ["以下原因导致提交失败：\t\t\t\t"],
    Validate : function(theForm,mode) {
        var obj = theForm || event.srcElement;
        this.ErrorMsg.length = 1;
        this.ErrorItem.length = 1;
        this.ErrorItem[0] = obj;
        for(var i=0,j=obj.elements.length; i<j; i++){
			with(obj.elements[i]){
				var _dataType = getAttribute("dataType");
				if(typeof(_dataType) == "object" || typeof(this[_dataType]) == "undefined")  continue;
				this.ClearState(obj.elements[i]);
				if(getAttribute("require") == "false" && value == "") continue;
				var _or = getAttribute("or");
				if(_or) {
				    if(value==""&&document.getElementsByName(_or)[0].value!="") continue;
				}
				switch(_dataType){
					case "Repwd" :
					case "Range" :
					case "Compare" :
					case "Custom" :
					case "Group" : 
					case "Limit" :
					case "LimitB" :
					case "Filter" :
						if(!eval(this[_dataType]))	{
							this.AddError(i, getAttribute("msg"));
						}
						break;
					default :
						if(!this[_dataType].test(value)){
							this.AddError(i, getAttribute("msg"));
						}
						break;
				}
			}
		}
		if (this.ErrorMsg.length > 1) {
		    mode = mode || 1;
		    var errCount = this.ErrorItem.length;
		    switch(mode) {
		        case 1:
		            alert(this.ErrorMsg.join("\n"));
		            if (this.ErrorItem[1].style.display != 'none')
				        this.ErrorItem[1].focus();
				    break;
                case 2:
                default :
                    for(var i=1;i<errCount;i++){
                        try {
                            var span = document.createElement("SPAN");
					        span.id = "__ErrorMsg";
					        span.style.color = "red";
					        this.ErrorItem[i].parentNode.appendChild(span);
					        span.innerHTML = this.ErrorMsg[i].replace(/\d+:/,"&nbsp;");
					    }
					    catch(e){alert(e.description);}
				    }
				    if (this.ErrorItem[1].style.display != 'none')
				        this.ErrorItem[1].focus();
				    break;
		    }
		    return false;
		}
		return true;
    },
    ClearState : function(elem) {
        with(elem) {
            var lastNode = parentNode.childNodes[parentNode.childNodes.length-1];
            if(lastNode.id == "__ErrorMsg") parentNode.removeChild(lastNode);
        }
    },
    AddError : function(index, str) {
        this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index];
		this.ErrorMsg[this.ErrorMsg.length] = this.ErrorMsg.length + ":" + str;
    },
    DoFilter : function(input, filter) {
        return new RegExp("^.+\.(?=EXT)(EXT)$".replace(/EXT/g, filter.split(/\s*,\s*/).join("|")), "gi").test(input);
    },
    limit : function(len,min, max){
		min = min || 0;
		max = max || Number.MAX_VALUE;
		return min <= len && len <= max;
	},
	LenB : function(str){
		return str.replace(/[^\x00-\xff]/g,"**").length;
	},
	Exec : function(op, reg){
		return new RegExp(reg,"g").test(op);
	},
	MustChecked : function(name, min, max){
		var groups = document.getElementsByName(name);
		var hasChecked = 0;
		min = min || 1;
		max = max || groups.length;
		for(var i=groups.length-1;i>=0;i--)
			if(groups[i].checked) hasChecked++;
		return min <= hasChecked && hasChecked <= max;
	}
}
