﻿//welcome to zhValidate
//create by zhouhai
//create on 2009.5
//18795736@qq.com
//first ,you have to take config
var imgPath = "/script/validate/";
var normarlColor = "black";
var wrongColor = "red";
var rightColor = "green";

function CheckForm(formObj)
{
    var tags = formObj.all;
    if (tags.length > 0)
    {
        var shit = 0;
        for (var index = 0; index < tags.length; index++)
        {
            var obj = tags[index];
            var isRequire = attr(obj, "isRequire");
            var zhState = attr(obj, "zhState");
            var group=attr(obj,"group");
            if ((isRequire != null && isRequire =="true") || (group!=null))
            {
                var isValid=false;
                var isZhSateOK=false;
                if(zhState==null)
                {
                    var zhType = attr(obj, "zh");
                    isValid=zhValid(zhType, obj);
                }
                else if(zhState == "ok")
                {
                    isZhSateOK=true;
                }
                if (!isValid &&  !isZhSateOK)
                {
                    shit++;
                    if (shit == 1)
                    {
                        obj.focus();
                    }
                }

            }
            else
            {
                 if (zhState != null)
                {
                    if( zhState == "wrong")
                    {
                        shit++;
                        if (shit == 1)
                        {
                            obj.focus();
                        }
                       var zhType = attr(obj, "zh");
                       if(zhType==null)
                       {
                        continue;
                       }
                        zhValid(zhType, obj);
                    }
                }
            }
        }
        if (shit == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}


//zhouhai html validator 
//18795736@qq.com
//create: 2009.05.26

//some thing for zh.tip
var zh_tip_tag = document.createElement("div");
zh_tip_tag.id = "zh_tip";
zh_tip_tag.style.backgroundColor = "#FFFFFF";
zh_tip_tag.style.fontSize = "13px";
zh_tip_tag.style.lineHeight = "21px";
zh_tip_tag.style.color = "#666666";
zh_tip_tag.style.zIndex = 999;
zh_tip_tag.style.border = "2px solid #CCCCCC";
zh_tip_tag.style.position = "absolute";
zh_tip_tag.style.left = "0px";
zh_tip_tag.style.top = "0px";
zh_tip_tag.style.fontFamily = "";
zh_tip_tag.style.display = "none";

var zh_arrowhead = document.createElement("span");
zh_arrowhead.style.display = "block";
zh_arrowhead.style.border = "10px solid";
zh_arrowhead.style.width = "0px";
zh_arrowhead.style.borderLeft = "0px";
zh_arrowhead.style.position = "absolute";
zh_arrowhead.style.left = "-12px";
zh_arrowhead.style.top = "-2px";
zh_arrowhead.style.lineHeight = "0px";
zh_arrowhead.style.color = "blue";
zh_arrowhead.style.styleFloat = "left";
zh_arrowhead.style.cssFloat = "left";
zh_arrowhead.style.borderColor = " #ffffff #cccccc #ffffff #ffffff";
zh_tip_tag.appendChild(zh_arrowhead);

var zh_tip_title = document.createElement("div");
zh_tip_title.id = "zh_tip_title";
zh_tip_title.innerHTML = "";
zh_tip_title.style.fontWeight = 600;
zh_tip_title.style.lineHeight = "20px";
zh_tip_title.style.padding = "5px 10px";
zh_tip_title.style.display = "block";
zh_tip_title.style.width = "auto";
zh_tip_title.style.backgroundColor = "#efefef";
zh_tip_title.style.color = "#3F3F3F";
zh_tip_title.style.fontSize = "14px";
zh_tip_tag.appendChild(zh_tip_title);

var zh_tip_text = document.createElement("div");
zh_tip_text.id = "zh_tip_text";
zh_tip_text.style.padding = "5px 15px";
zh_tip_text.style.lineHeight = "22px";
zh_tip_tag.appendChild(zh_tip_text);
document.body.appendChild(zh_tip_tag);

//iframe
var objIframe=document.createElement("iframe");
objIframe.style.position="absolute";
objIframe.style.zIndex=zh_tip_tag.style.zIndex-1;
objIframe.style.display="none";
document.body.appendChild(objIframe);


var zh_tags = null;
zh_tags = document.all;

var input_tags_count = (zh_tags.length);
for (var index = 0; index < input_tags_count; index++)
{
    var input_tag = zh_tags[index];
    var zh = attr(input_tag, "zh");
    if (zh != null )
    {
        input_tag.onfocus = zhFocus;
        input_tag.onblur = zhBlur;
        var width= attr(input_tag, "w");
        var height=attr(input_tag, "h");
        if(width!=null)
        {input_tag.style.width = attr(input_tag, "w");}
        if(height!=null)
        {input_tag.style.height = attr(input_tag, "h");}
    }
    //alert(zh);
}


//regular patterns
//author:zhou hai
//18795736@qq.com
//2009-5-26
var zhouhai =
{
    require: /\S/,
    email: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, //邮件地址：18795736@qq.com
    password: /^(\w){6,20}$/,
    userName: /^(\w){6,20}$/, //用户名：以字母开头,6-20位字母、数字、下划线
    mobile: /^(\d){11}$/, //手机号码：数字，11位
    phone: /^(\d){3,4}-(\d){7,8}-(\d){1,4}|(\d){3,4}-(\d){7,8}|(\d){10,12}$/ ,  //固定电话：0757-88330006,0757-88330006-123,075788330006
    chinese:/^[\u4e00-\u9fa5]+$/, //中文
    number:/^(\d+)$/, //数字
    url:"^((https|http|ftp|rtsp|mms)?://)?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"  //网址
}

//zh type
var zhType =
{
    require: "require"
}
//zh prompt type
//author:zhou hai
//18795736@qq.com
//2009-5-26
var zhPromptType =
{
    ok: "ok",
    wrong: "wrong",
    normal: "normal"
}

function zhBlur()
{

    //alert("zhBlur");
    this.style.backgroundColor = "#FFFFFF";
    this.style.border = "1px solid #A7A6AA";
    zh_tip_tag.style.display = "none";
    //zh validate
    var zh = attr(this, "zh");
    if (zh == null)
    { return false; }
    zh = zh.toLowerCase();
   var isValid=  zhValid(zh, this);
   if(isValid)
   {
        var ajax=attr(this,"ajax");
        if(ajax!=null)
        {
            eval(ajax);
        }
   }
    //iframe
     objIframe.style.display="none";
}
function zhFocus()
{
    //alert("zhFocus");
    this.style.backgroundColor = "#FFFDE6";
    this.style.border = "1px solid #F5CAA0";
    var tipText = attr(this, "tiptext");
    var tipTitle = attr(this, "tiptitle");
    if (tipText != null)
    {
        zh_tip_tag.style.display = "block";
        var top = getAbsoluteTop(this)-1;
        var left = getAbsoluteLeft(this) + this.offsetWidth + 14;
        zh_tip_tag.style.left = left + "px";
        zh_tip_tag.style.top = top + "px";
        zh_tip_text.innerHTML = tipText;
        zh_tip_title.innerHTML = tipTitle;
        
        //iframe
        objIframe.style.display="block";
        objIframe.style.width =zh_tip_tag.offsetWidth;
        objIframe.style.height =zh_tip_tag.offsetHeight;
        objIframe.style.top  = zh_tip_tag.style.top;
        objIframe.style.left =zh_tip_tag.style.left;
    }

}
function zhChange()
{
    alert("zhChange");
    alert(this.value);
}

//zh.method.oh yeah. get elements attribute
//author:zhou hai
//18795736@qq.com
//2009-5-26
function attr(obj, name)
{
    var attributeName=name.toLowerCase();
    return obj.getAttribute(attributeName);
}

//get absolute x,y
//author:zhou hai
//18795736@qq.com
//2009-5-26
function getAbsoluteLeft(ob)
{
    if (!ob) { return null; }
    var obj = ob;
    var objLeft = obj.offsetLeft;
    while (obj != null && obj.offsetParent != null && obj.offsetParent.tagName != "BODY")
    {
        objLeft += obj.offsetParent.offsetLeft;
        obj = obj.offsetParent;
    }
    return objLeft;
}

function getAbsoluteTop(ob)
{
    if (!ob) { return null; }
    var obj = ob;
    var objTop = obj.offsetTop;
    while (obj != null && obj.offsetParent != null && obj.offsetParent.tagName!= "BODY")
    {
        objTop += obj.offsetParent.offsetTop;
        obj = obj.offsetParent;
    }

    return objTop;
}
//zh regular match
//author:zhou hai
//18795736@qq.com
//2009-5-26
function zhMatch(pattern, input)
{
    var rx = new RegExp(pattern);
    var isMatch = rx.test(input);
    return isMatch;
}



//zh valid
//author:zhou hai
//18795736@qq.com
//2009-5-26
function zhValid(type, obj)
{
    var isValid=false;
    var pattern = zhouhai.require; 
    switch (type.toLowerCase())
    {
    case("username"):
    pattern=zhouhai.userName;
    break;
        case ("require"):
            pattern = zhouhai.require;
            break;
        case ("email"):
            pattern = zhouhai.email;
            break;
        case ("password"):
            pattern = zhouhai.password;
            break;
        case ("phone"):
            pattern = zhouhai.phone;
            break;
        case ("mobile"):
            pattern = zhouhai.mobile;
            break;
        case ("chinese"):
            pattern=zhouhai.chinese;
            break;
        case ("url"):
            pattern=zhouhai.url;
            break;
    }
    if (obj.value.length == 0)
    {
        zhPrompt(obj, zhPromptType.normal);
        return;
    }
    
    //min,max,group
    var isOK_min_max=true;
    var min=attr(obj,"min");
    var max=attr(obj,"max");

    if(min!=null && max!=null)
    {
        if(parseInt(min)>parseInt(max))
        {alert("属性设置错误：min>max?"); return;}
    }
    //compare
    var isCompareOK=true;
    if(type=="compare")
    {
        var to=attr(obj,"to");
        if(to==null)
        {
            alert("属性设置错误：to=?");
            return;
        }
        var toObj=document.getElementById(to);
        if(toObj==null)
        {
            alert("错误：找不到标签"+to);
         }
        if(document.getElementById(to).value!=obj.value)
        {
            isCompareOK=false;
        }
    }
    
    //select tag
    var isSelectOK=true;
    if(type=="select")
    {   
                var defaultOptionValue=attr(obj,"default");
                if(defaultOptionValue!=null)
                {
                    for(var optionIndex=0;optionIndex<obj.options.length;optionIndex++)
                    {
                        if(obj.options[optionIndex].selected)
                        {
                           if(obj.options[optionIndex].value==defaultOptionValue)
                           {
                               isSelectOK=false;
                           }
                        }
                    }
                }
    }
    else
    {
//input
        switch(obj.type)
        {
              case ("textarea"):
                 if(min!=null)
                {
                    if(obj.value.length<min)
                    {
                        isOK_min_max=false;
                    }
                    if(max!=null)
                    {
                        if(obj.value.length>max)
                        {isOK_min_max=false;}
                    }
                 }
            break;
            case ("input"):
                 if(min!=null)
                {
                    if(obj.value.length<min)
                    {
                        isOK_min_max=false;
                    }
                    if(max!=null)
                    {
                        if(obj.value.length>max)
                        {isOK_min_max=false;}
                    }
                 }
            break;
            default:
            var groupName=attr(obj,"group");
            if(groupName!=null)
            {
               var allTags=document.forms[0].elements;
               var checkedCount=0;
               for(var groupIndex=0; groupIndex <allTags.length;groupIndex++)
               {
                  var thisObj=allTags[groupIndex];
                  var thisGroupName=attr(thisObj,"group");
                  if(thisGroupName!=null)
                  {
                    if(thisGroupName==groupName)
                    {
                        var isChecked=attr(thisObj,"checked");
                        if(isChecked)
                        {
                            checkedCount++;
                        }
                    }
                  }
               }
              //validate min ,max
              if(min!=null)
              {
                   if(checkedCount<min)
                   {
                    isOK_min_max=false;
                   }
              }
              if(max!=null)
              {
                if(checkedCount>max)
                {
                    isOK_min_max=false;
                }
              }
            }
            break;
        }
}



    if (zhMatch(pattern, obj.value) && isOK_min_max && isSelectOK&&isCompareOK)
    {
        isValid=true;
        zhPrompt(obj, zhPromptType.ok);
    }
    else
    {
        zhPrompt(obj, zhPromptType.wrong);
    }

    return isValid;
}
//zh add error
//author:zhou hai
//18795736@qq.com
//2009-5-26
function zhPrompt(obj, promptType)
{
    var promptNode=null;
    var customPromptNodeID=attr(obj,"promptID");
    //alert(customPromptNodeID);
    if(customPromptNodeID!=null)
    {
        promptNode=document.getElementById(customPromptNodeID);
    }
    else
    {
        promptNode = document.getElementById(obj.id + "_prompt");
    }

    if (promptNode == null)
    {
        return false;
    }

    var hidden_tag = document.getElementById(promptNode.id + "_hidden");
    if (hidden_tag == null)
    {
        hidden_tag = document.createElement("input");
        hidden_tag.type = "hidden";
        hidden_tag.value = promptNode.innerHTML;
        hidden_tag.id = promptNode.id + "_hidden";
        document.body.appendChild(hidden_tag);
        promptNode.innerHTML = "";
    }
    

    var zhIMG = document.getElementById(promptNode.id + "_zhIMG");
    if (zhIMG == null)
    {
        zhIMG = document.createElement("img");
        zhIMG.id = promptNode.id + "_zhIMG";
        promptNode.appendChild(zhIMG);
    }
    zhIMG.style.marginRight="5px";
    zhIMG.style.verticalAlign="middle";

    var promptText = document.getElementById(promptNode.id + "_promptText");
    if (promptText == null)
    {
        promptText = document.createElement("span");
        promptText.id = promptNode.id + "_promptText";
        promptNode.appendChild(promptText);
    }
    promptText.style.verticalAlign="bottom";

    var ok = attr(promptNode, "ok");
    var wrong = attr(promptNode, "wrong");
    var normal = hidden_tag.value;
    switch (promptType)
    {
        case (zhPromptType.wrong):
            promptText.innerHTML = wrong;
            promptNode.style.color = wrongColor;
            zhIMG.src = imgPath + "wrong.gif";
            obj.style.border = "1px solid #FFB1B1";
            obj.style.backgroundColor = "#FFECEC";
            obj.setAttribute("zhState", "wrong");
            break;
        case (zhPromptType.normal):
            promptText.innerHTML = normal;
            promptNode.style.color = normarlColor;
            zhIMG.src = imgPath + "normal.gif";
            obj.setAttribute("zhState", "normal");
            break;
        case (zhPromptType.ok):
            obj.setAttribute("zhState", "ok");
            if(ok==null)
            {
                zhIMG.style.display="none";
                return;
            }
            promptText.innerHTML = ok;
            promptNode.style.color = rightColor;
            zhIMG.src = imgPath + "right.gif";
            obj.style.border = "1px solid #AFFE94";
            obj.style.backgroundColor = "#F7FEF5";

            break;
    }
}


