//????????????????????
String.prototype.trim = function () {
    return this.replace(/(^\s+)|(\s+$)/g, "");
}
//??????????????????
String.prototype.isEmpty = function () {
    return !(/.?[^\s??]+/.test(this));
}
//????????????????????????????????
String.prototype.isBetween = function (val,min,max) {
    if(isNaN(val) == false){
		val = val*1;
		return isNaN(val) == false  &&  val >= min  &&  val <= max;
	}else{
		return false
	}
	
}
//??????????????????
String.prototype.getBetweenVal = function (what) {
    var val = this.split(',');
    var min = val[0];
    var max = val[1] == null ? val[0] : val[1];
    if (parseInt(min) > parseInt(max)) {
        min = max;
        max = val[0];
    }
    return what == 'min' ? (isNaN(min) ? null : min) : (isNaN(max) ? null : max);
}

var validator = function (formObj,tt) {
	if(tt==null){ tt=0;}
    this.allTags = formObj.getElementsByTagName('*');
    //????????????????????
	this.reg = new Object();
	this.reg.tload = 1;
    this.reg.english = /^[a-zA-Z0-9_\-]+$/;
    this.reg.chinese = /^[\u0391-\uFFE5]+$/;
    this.reg.number = /^[-\+]?\d+(\.\d+)?$/;
    this.reg.integer = /^[-\+]?\d+$/;
    this.reg.float = /^[-\+]?\d+(\.\d+)?$/;
    this.reg.date = /^(\d{4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
    this.reg.email = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    this.reg.url = /^(((ht|f)tp(s?))\:\/\/)[a-zA-Z0-9]+\.[a-zA-Z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;
    this.reg.phone = /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
    this.reg.mobile = /^((\(\d{2,3}\))|(\d{3}\-))?((13\d{9})|(15\d{9})|(18\d{9}))$/;
    this.reg.ip = /^(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5]).(0|[1-9]\d?|[0-1]\d{2}|2[0-4]\d|25[0-5])$/;
    this.reg.zipcode = /^[1-9]\d{5}$/;
    this.reg.qq = /^[1-9]\d{4,10}$/;
    this.reg.msn = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
    this.reg.idcard = /(^\d{15}$)|(^\d{17}[0-9Xx]$)/;
    //????????????
    this.tip = new Object();
    this.tip.unknow = '????????????????????????????????';
    this.tip.paramError = '????????????????????????????';
    this.tip.required = '????????????';
    this.tip.english = '?????????????????????? (a-zA-Z0-9_)??';
    this.tip.chinese = '????????????????';
    this.tip.number = '????????????????????';
    this.tip.integer = '????????????????????';
    this.tip.float = '??????????????????????';
    this.tip.date = '???????????????????????? (??????2014-10-10)';
    this.tip.email = '????????????????????????????';
    this.tip.url = '??????????????????????????';
    this.tip.phone = '????????????????????????';
    this.tip.mobile = '????????????????????????';
    this.tip.ip = '??????????????IP??????';
    this.tip.zipcode = '????????????????????????';
    this.tip.qq = '??????????????QQ??????';
    this.tip.msn = '??????????????MSN??????';
    this.tip.idcard = '??????????????????????????';
    
	//????????????
    this.getControlName = function () {
        return this.element.getAttribute('controlName') == null ? '????????????': this.element.getAttribute('controlName');
    }
	
    //????????
    this.setFocus = function (ele) {
        try {
            ele.focus();
        } catch (e){}
    }

    //????????????
    this.setBorderColor = function (ele) {
        var borderColor = ele.currentStyle ?
                          ele.currentStyle.borderColor :
                          document.defaultView.getComputedStyle(ele, null)['borderColor'];
        ele.style.borderColor = '#ff9900';
        ele.onkeyup = function () {
            this.style.borderColor = borderColor;
        }
    }
    
	//????????????????
    this.feedback = function (type) {
		this.reg.tload = 0;
		try {
            var msg = eval('this.tip.' + type) == undefined ?
                      type :
                      this.getControlName() + eval('this.tip.' + type);
        } catch (e) {
            msg = type;
        }
        this.setBorderColor(this.element);
        alert(msg);
        this.setFocus(this.element);

    };

    //??????????????
    this.validate = function () {
        var v = this.element.value;
        //????????????????
        var required = this.element.getAttribute('required');
        if (required != null  &&  v.isEmpty()) {
            this.feedback('required');
            return false;
        }
        //????????????????
        var dataType = this.element.getAttribute('dataType');
        if (!v.isEmpty()  &&  dataType != null  &&   dataType.toLowerCase() != 'password') {
            dataType = dataType.toLowerCase();
            try {
                if (!(eval('this.reg.' + dataType)).test(v)) {
                    this.feedback(dataType);
                    return false;
                }
            } catch(e) {
                this.feedback('unknow');
                return false;
            }
        }
        //????????????
        var confirm = this.element.getAttribute('confirm');
        if (confirm != null) {
            try {
                var data = eval('formObj.' + confirm + '.value');
                if (v != data) {
                    alert('??????????????????????????????????');
                    this.setBorderColor(this.element);
                    this.setFocus(this.element);
                    return false;
                }
            } catch (e) {
                this.feedback('paramError');
                return false;
            }
        }
        //????????????
        var dataBetween = this.element.getAttribute('dataBetween');
        if (!v.isEmpty()  &&  dataBetween != null) {
            var min = dataBetween.getBetweenVal('min');
            var max = dataBetween.getBetweenVal('max');
            if (min == null || max == null) {
                this.feedback('paramError');
                return false;
            }
            if (!v.isBetween(v.trim(),min,max)) {
                this.feedback(this.getControlName() + '?????????? ' + min + '-' + max + ' ????????????'+'['+ v.trim() +']');
                return false;
            }
        }
        //????????????
        var dataLength = this.element.getAttribute('dataLength');
        if (!v.isEmpty()  &&  dataLength != null) {
            var min = dataLength.getBetweenVal('min');
            var max = dataLength.getBetweenVal('max');
            if (min == null || max == null) {
                this.feedback('paramError');
                return false;
            }
            if (!v.isBetween(v.trim().length, min, max)) {
                this.feedback(this.getControlName() + '?????? ' + min + '-' + max + ' ????????');
                return false;
            }
        }
		

		//this.reg.tload ++;
		
		//alert('????='+this.reg.tload);
		//
		return true;
		
    };

    //??????????????
    this.init = function () {
        for (var i=0; i<this.allTags.length; i++) {
            if (this.allTags[i].tagName.toUpperCase() == 'INPUT' ||
                this.allTags[i].tagName.toUpperCase() == 'SELECT' ||
                this.allTags[i].tagName.toUpperCase() == 'TEXTAREA')
            {
                this.element = allTags[i];
                if (!this.validate())
                    return false;
            }
        }

		if(tt==1 && this.reg.tload==1){ layer.load('??????????????...',0);}	//????????
    };
	
    return this.init();
	
}



//????
function MM_jumpMenu(targ,selObj,restore){ //v3.0
	  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	  if (restore) selObj.selectedIndex=0;
}
//----------------------------------------------------------------------------

//m????
function mqh(ta,jg)	//??ta????,??????jg????(??????????????)
{	
v = 0;
o = document.getElementsByName(ta);

for(c=0;c<o.length;c++){
	v+=parseFloat(o[c].value)
	};

document.getElementsByName(jg)[0].value=v;

/*
for(c=0;c<o.length;c++){
	v+=parseFloat(o[c].value)
	};
document.getElementsByName(jg)[0].value=v;
*/
}

//m????
function mqc(tc,td,jg)		//??tc-td????,??????jg????(??????????id)
{
mc=document.getElementById(tc).value;
md=document.getElementById(td).value;
ma = 0 ;
if (mc>0){
	ma = mc-md;
	if (ma<0) ma="????";
}
document.getElementsByName(jg)[0].value=ma;
}


//----------------------------------------------------------------------------
//????
function selectAll(checkname){
	var a = document.getElementsByName(checkname);
	if(a[0].checked){
		for(var i = 0;i<a.length;i++){
			if(a[i].type == "checkbox") a[i].checked = false;
		}
	}else{
		for(var i = 0;i<a.length;i++){
			if(a[i].type == "checkbox") a[i].checked = true;
		}
	}

}