﻿

         var totalcount=0;               //统计用,总共用了多少个的函数
         var funclist=[];                //用过的函数的集合
         var successcount=0;  
          //检测所有必填字段的正确性
        function checkAll()
        {
            for(var i=0;i<totalcount;i++)
            {
                if(funclist[i].boolValidate)
                {
                    if(funclist[i].input == "")
                    {
                        //window.alert(funclist[i].inputs.length);
                        for(var j =0; j< funclist[i].inputs.length ; j++)
                        {
                            if(!window.attachEvent)//Not IE
                                funclist[i].runAjax()//已经验证过了
                            funclist[i].validate(funclist[i].inputs[j],funclist[i].divid);
                        }
                    }
                    else
                    {
                        if(!window.attachEvent)//Not IE
                            funclist[i].runAjax()//已经验证过了
                        funclist[i].validate(funclist[i].input,funclist[i].divid);        
                    }
                }
                else
                    successcount++;
            }
            //window.alert("successcount:"+successcount+";totalcount:" + totalcount);
            if(successcount==totalcount)
            {
                //alert(successcount);
                //alert("成功");
                successcount=0;
                //funcount=0;
                funclisttmp=[];             //统计用
                return true;
            }
            else
            {               
                //alert(successcount);
                //alert(totalcount);
                successcount=0;
                //funcount=0;
                funclisttmp=[];             //统计用

                return false;
            }
        }


        CheckUserName = function()
        {
            var ts1 ="";
            var ts2 = "";
            var ts3 = "";
            var ts4 = "";
            var ts5 = "";
            var allownull=false;
            var minl=0; //最短长度
            var maxl=1000;//最长
            var url = "";
            var parname = "AccountId";
            var imageload = "../images/indicator.gif";
            var imagecursor = "../images/plaint.gif";
            var imagewrong = "../images/wrong.gif";
            var imageright = "../images/right.gif";
            var showrightimg=true; //正确时显示正确图标
            var funname = "";  //验证函数
            var http_request = false;
            var associate="";  //协作的控件的ID（目前是dropdownlist）
            var isfocus=true; //设置验证是onfocus 还是 onkeydown
            var ispropertychange=false;//设置验证是onpropertychange 还是 onfocus
            var input="";  //输入框
            var inputs = new Array();//输入框数组,不能同input共同使用
            var divid="";  //显示错误信息的容器
            var success=false;  //成功状态
            var boolAjax=true;//省去ajax验证
            var isAjax=false;//Ajax是否验证过
            var isValidate = true;//是否在checkall()中重新验证
            //////////////////////////////////////////////内置函数
            this.setisfocus=function(trueorfalse)//是焦点离开触发还是键盘触发
            {
                isfocus=(trueorfalse == null ? isfocus : trueorfalse);
            }
            this.setispropertychange=function(trueorfalse)//是文本改变触发还是焦点离开触发
            {
                ispropertychange=(trueorfalse == null ? ispropertychange : trueorfalse);
            }
            this.setassociatecontrol=function(controlid)
            {
                //设置协作的控件的ID，目前只支持dropdownlist
                associate=(controlid == null ? associate : controlid);
            }
            this.setshowrightimg=function(trueorfalse)
            {
                showrightimg = (trueorfalse == null ? showrightimg : trueorfalse);
            }
            this.setrule = function(isallownull,minlength,maxlength)
            {
                /**********************************
                 * 设置配置
                 * 1－是否允许空
                 * 2－最小长度
                 * 3－最大长度
                 **********************************/
                 
                allownull = (isallownull == null ? allownull : isallownull);
                minl = (minlength == null ? minl : minlength);
                maxl = (maxlength == null ? maxl : maxlength);
            }
            this.setcont = function(str1, str2, str3, str4, str5)
            {
                /**********************************
                 * 设置提示信息，顺序依次是
                 * 1－提示输入规则
                 * 2－检测用户名成功
                 * 3－检测用户名被人使用
                 * 4－用户名不符合规则（在加入用户名检查后使用）
                 * 5－不能为空信息
                 **********************************/
                ts1 = (str1 == null ? ts1 : str1);
                ts2 = (str2 == null ? ts2 : str2);
                ts3 = (str3 == null ? ts3 : str3);
                ts4 = (str4 == null ? ts4 : str4);
                ts5 = (str5 == null ? ts5 : str5);
                funclist[totalcount]=this; 
                totalcount++;
            }
            this.setimage = function(load, wrong, right,cursor)
            {
                /**********************************
                 * 设置图片的路径
                 * 1－查询时候显示图片
                 * 2－错误时显示图片
                 * 3－正确时显示图片
                 * 4－点击时显示图片
                 **********************************/
                imageload = (load == null ? imageload : load);
                imagewrong = (wrong == null ? imagewrong : wrong);
                imageright = (right == null ? imageright : right);
                imagecursor = (cursor == null ? imagecursor : cursor);
            }
            this.seturl = function(u, p)
            {
                /**********************************
                 * 设置后台页面的路径以及参数
                 * 1－后台页面名称×（aspx。jsp。asp均可）
                 * 2－超链接后面带的参数，默认是username。
                 **********************************/
                url = u;
                parname = (p == null ? parname : p);
            }
            this.setcheckfunction = function(functionname)
            {
              
                /**********************************
                 * 设置检查用户名合法的方法
                 * 1－直接传入方法名称，此方法需要有bool型的返回值，用户自定义编写
                 **********************************/
                funname = (functionname == null ? funname : functionname);
                
            }
            //在FF下初始Ajax验证
            this.runAjax =function()
            {
                if(isAjax)
                    boolAjax=false;
            }
            //是否验证属性
            this.boolValidate =function(value)
            {
               isValidate = value;
            }
            //添加验证方法////////////////////////////////////////////////////////////////////////////
            this.apply = function(inpurid, tsid)
            { 
                this.input = inpurid;
                this.divid = tsid;
                var a = inpurid;
                var b = tsid;
                if (typeof(a) == "string") 
                    a = document.getElementById(a);
                if (typeof(b) == "string") 
                {
                    b = document.getElementById(b);
                    //this.container=b;
                }
                
                if(isfocus && ispropertychange==false)//如果是focus触发，而不是propertychange触发
                {
                    if(!window.attachEvent)//Not IE
                    {
                         a.addEventListener("focus",function(){ insertfun(getimg(imagecursor)+ts1);},true); 
                    }
                    else
                        a.attachEvent("onfocus",function(){ insertfun(getimg(imagecursor)+ts1);})
//                    a.onfocus = function()
//                    {
//                        insertfun(getimg(imagecursor)+ts1);
//                    }
                    if(!window.attachEvent)//Not IE
                    {
                         a.addEventListener("blur",function(){ validatebegin(a,b);},true); 
                    }
                    else
                        a.attachEvent("onblur",function(){ validatebegin(a,b);})
//                    a.onblur = function()
//                    {
//                         validatebegin(a,b);
//                    }
                }
                else if(isfocus==false && ispropertychange==false)//如果不是focus触发，也不是propertychange触发，那就是onmouseup触发
                {
//                    if(!window.attachEvent)//Not IE
//                    {
//                         a.addEventListener("mouseup",function(){ insertfun(getimg(imagecursor)+ts1);},true); 
//                    }
//                    else
//                        a.attachEvent("onmouseup",function(){ insertfun(getimg(imagecursor)+ts1);})
//                    a.onmouseup = function()
//                    {
//                        insertfun(getimg(imagecursor)+ts1);
//                    }
                    if(!window.attachEvent)//Not IE
                    {
                         a.addEventListener("keyup",function(){ validatebegin(a,b);},true); 
                    }
                    else
                        a.attachEvent("onkeyup",function(){ validatebegin(a,b);})
//                    a.onkeyup = function()
//                    {
//                         validatebegin(a,b);
//                    }
                }
                else//onpropertychange触发
                {
//                    if(!window.attachEvent)//Not IE
//                    {
//                         a.addEventListener("input",function(){ validatebegin(a,b);},true); 
//                    }
//                    else
//                        a.attachEvent("onpropertychange",function(){ validatebegin(a,b);})
                }
                //验证的事件
                function validatebegin(a,b)
                {
                    if(a.value!=null)//验证除输入框外其他控件的情况
                    {
                        if (a.value.length == 0) 
                        {
                            if(!allownull)
                            {
                            //不能为空
                                insertfun(getimg(imagewrong)+ts5);
                                success=false;
                            }
                            else
                            {
                                insertfun(ts2);
                                success=true;
                            }
                           
                           return;
                        }
                    }
                    if (funname == "") 
                    {
                        if(url!="")//没有定义验证方法，不执行用户名合法性验证
                        {
                            if(boolAjax)
                            {
                                isAjax=true;
                                makeRequest(url + "?" + parname + "=" + a.value);
                            }
                            else
                            {
                                success=true;
                                boolAjax=true;
                            }
                        }
                        else
                        {
                            insertfun(getimg(imagewrong));
                            success=false;
                        }
                    }
                    else 
                    {   //有验证合法性方法。先执行
                        var trueorfalse=false;
                        if(associate!="")
                        {
                            var associatecontrol=document.getElementById(associate);
                            trueorfalse=funname + "(\"" + a.value + "\",\""+associatecontrol.value+"\")";
                        }
                        else
                        {
                            if(a.value!=null)
                            {
                             //   trueorfalse = funname + "(\"" + a.value + "\","+minl+","+maxl+")";
                              trueorfalse = funname + "(\"" +a.value.replace(/\r/ig,"").replace(/\n/ig,"") + "\","+minl+","+maxl+")";
                             }
                            else
                            {
                                trueorfalse = funname + "(\"" + inpurid + "\","+minl+","+maxl+")"; //要是不是输入框,就把ID传进去
                            }
                        }
                        if (eval(trueorfalse)) 
                        {
                            //执行ajax操作
                            if(a.defaultValue != a.value)
                            {
                                if(url!="")
                                {
                                    if(boolAjax)
                                    {
                                        isAjax=true;
                                        makeRequest(url + "?" + parname + "=" + a.value);
                                    }
                                    else
                                    {
                                        success=true;
                                        boolAjax=true;
                                    }
                                }
                                else
                                {
                                    if(showrightimg)
                                    {
                                        insertfun(getimg(imageright)+ts2);
                                    }
                                    else
                                    {
                                        insertfun(ts2);
                                    }
                                    success=true;
                                }
                            }
                            else
                            {
                                if(showrightimg)
                                {
                                    insertfun(getimg(imageright)+ts2);
                                }
                                else
                                {
                                    insertfun(ts2);
                                }
                                success=true;
                            }
                        }
                        else 
                        {
                            insertfun(getimg(imagewrong)+ts4);
                            success=false;
                        }
                    }
                }
                function insertfun(val)
                {
                    b.innerHTML = val;
                }
                function getimg(val)
                {
                    return "<img src='" + val + "'  class=\'AjaxrightWrongImg\' />"
                }
                function makeRequest(url)
                {
                    insertfun(getimg(imageload));
                    http_request = false;
                    if (window.XMLHttpRequest) 
                    {
                        http_request = new XMLHttpRequest();
                        if (http_request.overrideMimeType) 
                        {
                            http_request.overrideMimeType('text/xml');
                        }
                    }
                    else 
                        if (window.ActiveXObject) 
                        {
                            try 
                            {
                                http_request = new ActiveXObject("Msxml2.XMLHTTP");
                            } 
                            catch (e) 
                            {
                                try 
                                {
                                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                                } 
                                catch (e) 
                                {
                                }
                            }
                        }
                    if (!http_request) 
                    {
                        alert('Giving up :( Cannot create an XMLHTTP instance');
                        return false;
                    }
                    http_request.onreadystatechange = alertContents;
                    http_request.open('GET', url, true);
                    http_request.send(null);
                }
                function alertContents()
                {
                    if (http_request.readyState == 4) 
                    {
                        if (http_request.status == 200) 
                        {
                            // alert("---"+http_request.responseText+"---");
                            if (http_request.responseText == 0) 
                            {
                                if(showrightimg)
                                {
                                    insertfun(getimg(imageright)+ts2);
                                }
                                else
                                {
                                    insertfun(ts2);
                                }
                                
                                success=true;
                            }
                            else 
                            {
                                if (http_request.responseText == 1) 
                                {
                                    insertfun(getimg(imagewrong) + ts3);
                                }
                                else 
                                {
                                    //alert("---"+http_request.responseText+"---");
                                    insertfun(ServerWrong);
                                }
                                success=false;
                             }
                        }
                        else 
                        {
                            alert('There was a problem with the request.');
                            success=false;
                        }
                    }
                    else
                    {
                        success=false;
                    }
                    
                }
                //整体验证///////////////////////////////////////////////////////////////////////////////
            this.validate = function(inpurid, tsid)
            {
                //alert(tsid);
                var a = inpurid;
                var b = tsid;
                if (typeof(a) == "string") 
                    a = document.getElementById(a);
                if (typeof(b) == "string") 
                    b = document.getElementById(b);
                
                validatebegin(a,b);
                if(success)
                    successcount++;
//                funclisttmp[funcount]=this;   //加入到数组
//                funcount++;                //更新数量
            }
            }
            
            //-------------------------------------------------------------------------------------------
            this.applys = function(inpurid, tsid)//支持多控件数组验证
            {
                boolAjax=true; 
                this.inputs = inpurid;
                this.divid = tsid;
                var aID = inpurid;
                var a = new Array();
                var b = tsid;
                //循环控件ID取得控件
                for(var i = 0; i< aID.length; i++)
                {
                    if (typeof(aID[i]) == "string")
                    {
                       a[i] = document.getElementById(aID[i]);
                    }
                }
                if (typeof(b) == "string") 
                {
                    b = document.getElementById(b);
                    //this.container=b;
                }
                if(isfocus)
                {
                    for(var i = 0; i< a.length; i++)
                    {   
                        function aonfocus()
                        {
                            insertfun(getimg(imagecursor)+ts1);
                        }
                        
                        function aonblur()
                        {
                             validatebegin(a,b);//任何一个被触发都多验证所有空间
                        }
                        
                        if(!window.attachEvent)//Not IE
                        {
                             a[i].addEventListener("onfocus",aonfocus,true); 
                             a[i].addEventListener("onblur",aonblur,true); 
                        }
                        else
                        {
                            a[i].attachEvent("onfocus", aonfocus);
                            a[i].attachEvent("onblur", aonblur);
                        }
                    }
                }
                else
                {
                    for(var i = 0; i< a.length; i++)
                    {
                        function aonmouseup()
                        {
                            insertfun(getimg(imagecursor)+ts1);
                        }
                        
                        function aonkeyup()
                        {
                             validatebegin(a,b);//任何一个被触发都多验证所有空间
                        }
                        
                        if(!window.attachEvent)//Not IE
                        {
                             a[i].addEventListener("onmouseup",aonmouseup,true); 
                             a[i].addEventListener("onkeyup",aonkeyup,true); 
                        }
                        else
                        {
                            a[i].attachEvent("onmouseup", aonmouseup);
                            a[i].attachEvent("onkeyup", aonkeyup);
                        }
                    }
                }
                
//----------------------------------------------------------------------------------------------------------------------------                
                //验证的事件
                function validatebegin(a,b)
                {
                    for(var i = 0; i< a.length; i++)
                    {
                        if (a[i].value.length == 0) 
                        {
                            if(!allownull)
                            {
                            //不能为空
                                insertfun(getimg(imagewrong)+ts5);
                                success=false;
                            }
                            else
                            {
                                insertfun(ts5);
                                success=true;
                            }
                           
                           return;
                        }
                        if (funname == "") 
                        {//没有定义验证方法，不执行用户名合法性验证！
                            for(var i = 0; i< a.length; i++)
                            {
                                if(url!="")
                                {
                                    if(boolAjax)
                                    {
                                        isAjax=true;
                                        makeRequest(url + "?" + parname + "=" + a.value);
                                    }
                                    else
                                    {
                                        success=true;
                                        boolAjax=true;
                                    }
                                }
                                else
                                {
                                    insertfun(getimg(imagewrong)+Validation);
                                    success=false;
                                }
                            }
                        }
                        else 
                        {//有验证合法性方法。先执行
                            var trueorfalse=false;
                            if(associate!="")
                            {
                                var associatecontrol=document.getElementById(associate);
                                trueorfalse=funname + "(\"" + a[i].value + "\",\""+associatecontrol.value+"\")";
                            
                            }
                            else
                            {
                                trueorfalse = funname + "(\"" + a[i].value + "\","+minl+","+maxl+")";
                            }
                            if (eval(trueorfalse)) 
                            {//执行ajax操作
                            
                                if(a[i].defaultValue!=a[i].value)//如果值改变的话
                                {
                                    if(url!="")
                                    {
                                        if(boolAjax)
                                        {
                                            isAjax=true;
                                            makeRequest(url + "?" + parname + "=" + a.value);
                                        }
                                        else
                                        {
                                            success=true;
                                            boolAjax=true;
                                        }
                                    }
                                    else
                                    {
                                        if(showrightimg)
                                        {
                                            insertfun(getimg(imageright)+ts2);
                                        }
                                        else
                                        {
                                            insertfun(ts2);
                                        }
                                        success=true;
                                    }
                                }
                            }
                            else 
                            {
                                insertfun(getimg(imagewrong)+ts4);
                                success=false;
                            }
                        }
                    }
                }
                function insertfun(val)
                {
                    b.innerHTML = val;
                }
                function getimg(val)
                {
                    return "<img src='" + val + "'  class=\'AjaxrightWrongImg\' />"
                }
                function makeRequest(url)
                {
                    insertfun(getimg(imageload));
                    http_request = false;
                    if (window.XMLHttpRequest) 
                    {
                        http_request = new XMLHttpRequest();
                        if (http_request.overrideMimeType) 
                        {
                            http_request.overrideMimeType('text/xml');
                        }
                    }
                    else 
                        if (window.ActiveXObject) 
                        {
                            try 
                            {
                                http_request = new ActiveXObject("Msxml2.XMLHTTP");
                            } 
                            catch (e) 
                            {
                                try 
                                {
                                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                                } 
                                catch (e) 
                                {
                                }
                            }
                        }
                    if (!http_request) 
                    {
                        //alert('Giving up :( Cannot create an XMLHTTP instance');
                        return false;
                    }
                    http_request.onreadystatechange = alertContents;
                    http_request.open('GET', url, true);
                    http_request.send(null);
                }
                function alertContents()
                {
                    if (http_request.readyState == 4) 
                    {
                        if (http_request.status == 200) 
                        {
                            // alert("---"+http_request.responseText+"---");
                            if (http_request.responseText == 0) 
                            {
                                if(showrightimg)
                                {
                                    insertfun(getimg(imageright)+ts2);
                                }
                                else
                                {
                                    insertfun(ts2);
                                }
                                
                                success=true;
                            }
                            else 
                            {
                                if (http_request.responseText == 1) 
                                {
                                    insertfun(getimg(imagewrong) + ts3);
                                }
                                else 
                                {
                                    //alert("---"+http_request.responseText+"---");
                                    insertfun(ServerWrong);
                                }
                                success=false;
                             }
                        }
                        else 
                        {
                            alert('There was a problem with the request.');
                            success=false;
                        }
                    }
                    else
                    {
                        success=false;
                    }
                }
                    
                    //整体验证///////////////////////////////////////////////////////////////////////////////
                this.validate = function(inpurid, tsid)
                {
                    var a = inpurid;
                    var b = tsid;
                    if (typeof(a) == "string") 
                        a = document.getElementById(a);
                    if (typeof(b) == "string") 
                        b = document.getElementById(b);
                    
                    validatebegin(a,b);
                    if(success)
                        successcount++;
    //                funclisttmp[funcount]=this;   //加入到数组
    //                funcount++;                //更新数量
                }
            }
            
            //-------------------------------------------------------------------------------------------
            
     }
//为解决自定义验证与服务器验证的冲突写的
//返回,服务器验证控件是否成功
//
//Example: OnClientClick="if(CheckClientValidate()) { -自定义代码-}"
 function CheckClientValidate(id,validategroup)
 {           
 WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(id, '', true, validategroup, '', false, false))
       if(Page_IsValid)
        return checkAll();
       
//       Page_ClientValidate();
//       if (Page_IsValid){
//           return true;
//       }else{
//           return false;
//       }
     }
     