/*
* 错误处理 v3.0
*/
function ExpCheck(){
    this.focused=false;
    this.ERROR=new Object();
    //去掉首位空格的方法
    this.trim=function(str){
        if(!str){str="";}
        return str.replace(/(^\s*)|(\s*$)/g, "");
    };
    /*
    * 验证表单项数据合法性
    * 成功返回true否则返回错误代码和错误描述
    */
    this.validForm=function(fm,r){
        var len=fm.length;
        var rel=0,i;
        for(i=(len-1);i>=0;i--){
            if(fm[i].getAttribute("rule")!="" && fm[i].getAttribute("rule")!=null){
                if(!this.checkValid(fm[i],fm[i].value)){rel=1;}
            }
        }

        for(i=(len-1);i>=0;i--){
            /****密码确认start***/
            if(fm[i].getAttribute("rule")!=null && fm[i].type.toLowerCase()=="password"){
                for(var j=i+1;j<len;j++){
                    if(fm[j].type.toLowerCase()=="password"){
                        if(fm[j].value!=fm[i].value){
                            this.showMsg(fm[j],'两次输入的密码不一致');
                            return false;
                        }else{
                            this.clearMsg(fm[j]);
                        }
                    }
                }
            }
            /****密码确认end***/
        }
        if(rel==1||r==1){return false;}
        return true;
    };

    this.showMsg=function(label,text){
        var show_error=$(label.getAttribute("name")+"_msg");
        if(show_error){
            show_error.innerHTML=text;
        }else{
            var _top=getPos(label,"Top")+label.offsetHeight;
            var _left=getPos(label,"Left");
            this.clearMsg(label);
            var textnode=document.createTextNode('↑'+text);
            var spannode=document.createElement("SPAN");
            spannode.appendChild(textnode);
            spannode.setAttribute("id",label.getAttribute("name")+"_msg");
            spannode.setAttribute("class","validField");
            spannode.setAttribute("className","validField");
            spannode.style.whiteSpace="nowrap";
            spannode.style.position="absolute";
            spannode.style.top=_top+"px";
            spannode.style.left=_left+"px";
            //label.style.border="1px solid red";
            var theBody = document.getElementsByTagName("BODY")[0];
            theBody.insertBefore(spannode,theBody.firstChild);
            if(spannode.offsetWidth<label.offsetWidth){spannode.style.width=parseInt(label.offsetWidth,10)-10;}
        }
        addEvent(label,"click",this.clearMsg);
    };
    this.clearMsg=function(a){
        var theBody = document.getElementsByTagName("BODY")[0],_id;
        try{
            if(a && a.getAttribute("name")+"_msg"){
                _id=a.getAttribute("name")+"_msg";
                o=document.getElementById(_id);
                if(o){theBody.removeChild(o);}
            }
        }catch(e){}
        // alert(a+"--"+a.nodeName+"---"+a.target+"----"+a.tagName);return ;
        try{
            if(isIE){
                if(window.event.srcElement.tagName=="A"){
                    a=window.event.srcElement.parentNode;
                }else{
                    a=window.event.srcElement;
                }
                if(a.tagName=="INPUT"){	e=a;}
            }else{
                if(a.target){
                    e=a.target;
                }else{
                    if(a.tagName=="INPUT"){e=a;}
                }
            }

            _id=e.getAttribute("name")+"_msg";
            o=document.getElementById(_id);
            if(o){theBody.removeChild(o);}

        }catch(e){}
        try{
            var show_error=$(a.getAttribute("name")+"_msg");
            if(show_error){show_error.innerHTML='';}
        }catch(e){}
        return;
    };
    /*
    * 检测字符串长度
    */
    this.utf8=3;
    this.gbk=2;

    this.len=function(str,entype){
        var s=escape(str);
        var array=s.split("%u");
        var total=str.length;
        var noascii=array.length-1;
        var ascii=total-noascii;
        return noascii*entype+ascii;
        //alert("none ascii:"+noasc+"\nascii:"+asc+"\ntotal:"+t);
    };
    this.rep=function(str){
        if(!str){return str;}
        var sp='１~２~３~４~５~６~７~８~９~０~－~（~）~［~］~，~。~｛~｝~　';
        var sr='1~2~3~4~5~6~7~8~9~0~-~(~)~[,~]~,~.~{~}~ ';
        p=sp.split('~');
        r=sr.split('~');
        for(var i=0;i<p.length;i++){
            eval("str=str.replace(/"+p[i]+"/g,'"+r[i]+"');");
        }
        return str;
    };
    /*
    * 判断数据项是否符合正则规则
    * 输入区域的值做trim处理,判断密码区域的值不做trim处理
    * obj 要校验的数据项的校验器
    * val 要校验的内容
    */
    this.checkValid=function(field,val){
        var faild=false,limit;
        if(field.type.toLowerCase()!="password"){
            field.value=this.rep(this.trim(val));
        }
        var rules=field.getAttribute("rule").split("|");
        for(var i=0;i<rules.length;i++){
            var comp=rules[i];
            if(comp.indexOf('NOTNULL')==0){
                if(this.isNull(this.trim(val))==true){
                    this.showMsg(field,"不能为空");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;

                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('MAX')==0){
                limit=comp.substr(comp.indexOf('_')+1);
                if(val.length>parseInt(limit,10)){
                    this.showMsg(field,"不能多于"+limit+"个字");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('MIN')==0){
                limit=comp.substr(comp.indexOf('_')+1);
                if(val.length<parseInt(limit,10)){
                    this.showMsg(field,"不能少于"+limit+"个字");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if (comp.indexOf('NUMBER')==0)
            {
               if(this.isNum(val)==false){
                    this.showMsg(field,"必须全部是数字");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('DOMAIN')==0){
                faild=false;
                if(this.hasBlank(val)==true){
                    faild=true;
                    this.showMsg(field,"不能含有空格");
                }else if(this.hasSpecialChar(val,'@')==true){
                    faild=true;
                    this.showMsg(field,"不能含有非法特殊字符");
                }else if(this.isNum(val)==true){
                    faild=true;
                    this.showMsg(field,"不能全部是数字");
                }else if(this.hasUpperChar(val)==true){
                    faild=true;
                    this.showMsg(field,"不能含有大写字母");
                }else if(this.hasZHCNchar(val)==true){
                    faild=true;
                    this.showMsg(field,"不支持中文文字");
                }
                if(faild){
                    field.value=this.trim(val);
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('USERNAME')==0){
                faild=false;
                if(this.hasBlank(val)==true){
                    faild=true;
                    this.showMsg(field,"不能含有空格");
                }else if(this.hasZHCNchar(val)==true){
                    faild=true;
                    this.showMsg(field,"不能含有中文文字");
                }else if(this.hasSpecialChar(val)==true){
                    faild=true;
                    this.showMsg(field,"不能含有特殊字符");
                }else if(this.isNum(val)==true){
                    faild=true;
                    this.showMsg(field,"不能全部是数字");
                }else if(this.hasUpperChar(val)==true){
                    faild=true;
                    this.showMsg(field,"不能含有大写字母");
                }
                if(faild){
                    field.value=this.trim(val);
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('EMAIL')==0){
                if(this.isEmail(val)==false){
                    this.showMsg(field,"Email格式不正确");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('PASSWORD')==0){
                faild=false;
                if(this.hasBlank(val)==true){
                    faild=true;
                    this.showMsg(field,"不能含有空格");
                }else if(this.hasZHCNchar(val)==true){
                    faild=true;
                    this.showMsg(field,"不能含有中文文字");
                }
                if(faild){
                    field.value=this.trim(val);
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('PHONE')==0){
                if(this.isPhoneNO(val)==false){
                    this.showMsg(field,"电话格式不正确");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('POSTALCODE')==0){
                if(this.isPostalcode(val)==false){
                    this.showMsg(field,"邮政编码格式不正确");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('VALIDCODE')==0){
                if(this.isEmpty(val)){
                    this.showMsg(field,"请输入验证码");
                }
                limit=comp.substr(comp.indexOf('_')+1);
                if(val.length!=parseInt(limit,10)){
                    this.showMsg(field,"验证码输入不正确");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('URL')==0){
                if(this.isUrl(val)==false){
                    this.showMsg(field,"URL格式不正确");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else if(comp.indexOf('FLOAT')==0){
                if(this.isFloat(val)==false){
                    this.showMsg(field,"只能包含数字和小数点'.'");
                    if(typeof(span2control)=="function"){
                        span2control(field);
                    }else{
                        this.select(field);
                        this.focus(field);
                    }
                    return false;
                }else{
                    this.clearMsg(field);
                }
            }else{
                this.clearMsg(field);
            }
        }
        return true;
    };
    this.select=function(field){
        if(field.value!=''){field.select();}
    };
    this.focus=function(field){
        //if(!this.focused){field.focus();this.focused=true;}
        field.focus();
    };
    this.setMessages=function(msgs){
        this.ERROR=msgs;
    };
    this.getMessage=function(code){
        return _msg[code];
    };

    this.isNum=function(str){
        var re=/^[0-9]*$/;
        return re.test(str);
    };

    this.isArray=function(str){
        // return is_array(str);
    };
    this.isBool=function(str){
        //return is_bool(str);
    };
    this.isCallable=function(str,syntax_only,callable_name){
        //      return is_callable(str,$syntax_only="",$callable_name="");
    };
    this.isFloat=function(str){
         //       return is_float(str);
        if(this.isEmpty(str)){
            return null;
        }else{
            var re=/^[0-9\.]+$/;
            return re.test(str);
        }
    };
    this.isInt=function(str){
        //    return is_int(str);
    };
    this.isNull=function(str){
        return (str=="")?true:false;
    };
    this.isNumeric=function(str){
        //        return is_numeric(str);
    };
    this.isObject=function(str){
        return (typeof(str)=='object')?true:false;
    };
    this.isScalar=function(str){
        //return is_scalar(str);
    };
    this.isString=function(str){
        return (typeof(str)=='string')?true:false;
    };

    this.isStrlen=function(str,logic,number,charset){
        var len=str.length;
        switch (logic.toLowerCase()){
            case "gt":            return (len > number)?true:false;            break;
            case "lt":            return (len < number)?true:false;            break;
            case "ge|gteq":       return (len >= number)?true:false;           break;
            case "le|lteq":       return (len <= number)?true:false;           break;
            case "ne|neq":        return (len != number)?true:false;           break;
            default:              return (len = number)?true:false;
        }
    };
    this.isEmpty=function(str){
        return (this.trim(str)=="")?true:false;
    };
    this.isEmail=function(str){
        if(this.isEmpty(str)){
            return null;
        }else{
            var re=/^[_\.0-9a-z\-]+@([0-9a-z][0-9a-z\-]+\.)+[a-z]{2,5}$/;
            return re.test(str);
        }
    };

    this.isPhoneNO=function(str){
        if(this.isEmpty(str)){
            return null;
        }else{
            var re=/^[0-9\-]+/;
            return re.test(str);
        }
    };

    this.hasSpecialChar=function(str,other){
        if(this.isEmpty(str)){
            return null;
        }else{
            if(other=='@'){var re=/[@\.\*%<>\/\\\^\uFF00-\uFFFF]/;}else{var re=/[\.\*%<>\/\\\^\uFF00-\uFFFF]/;}
            return re.test(str);
            }
        };

        this.isPostalcode=function(str){
            if(this.isEmpty(str)){
                return null;
            }else{
                var re=/^[0-9]{3,8}$/;
                return re.test(str);
            }
        };
        this.isUrl=function (str){
            var re=/[^\w\%\:\&\?\/\=\.\_\-]/;
            if(re.test(str)==true){return false;}
            else{
                var re=/^[\w]{3,5}\:\/\//;
                return (re.test(str))?true:false;
            }
        };
        this.hasBlank=function(str){
            if(this.isEmpty(str)){
                return null;
            }else{
                var re=/[ ]/;
                return re.test(str);
            }
        };
        this.isFirstCharDownEng=function(str){

        };
        this.hasUpperChar=function(str){
            if(this.isEmpty(str)){
                return null;
            }else{
                var re=/[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/;
                return re.test(str);
            }
        };
        this.hasZHCNchar=function(str){
            if(this.isEmpty(str)){
                return null;
            }else{
                var re=/[\u4E00-\u9FA5]/g;
                return re.test(str);
            }
        };
    }