﻿var errImg = "<span style='\"padding-right:40px\"'><img border=0 src='/images/1000216.gif' ></span>";
var validImg = "<span style='\"padding-right:40px\"'><img border=0 src='/images/1000716.gif' ></span>";
var validMsg = validImg + "填写正确!";

function ZSUSEValidator(validatorId, chkEmptyText, s, activate) {
    this.validatorId = validatorId;

    this.chkEmptyText = chkEmptyText;
    this.custFunction = null;
    this.isValid = true;
    this.color = "green";
    this.msg = validMsg;
    this.emptyMsg = '';
    this.custMsg = '';
    this.value = s;
    this.activate = activate;  //onblur,onvalidate
    //this.ZSUSEValidator = null;

    this.Check = function() {

        this.isValid = true;
        this.color = "green";
        this.msg = validMsg;

        oSrc = document.getElementById(validatorId);
        if (oSrc == null) {
            return;
        }

        if (this.chkEmptyText) {
            if (this.value == '') {
                this.msg = errImg + this.emptyMsg;
                this.isValid = false;
            }
            else {
                if (IsTextMalicious(this.value)) {
                    this.msg = errImg + "请不要输入敏感信息";
                    this.isValid = false;
                }
                else {
                    if (this.custFunction != null) {

                        this.isValid = this.custFunction(this.value);
                        if (!this.isValid) {
                            this.msg = errImg + this.custMsg;
                        }
                    }
                }
            }


        }
        else {
            if (this.value == '') {
                return true;
            }
            else {
                if (IsTextMalicious(this.value)) {
                    this.msg = errImg + "请不要输入敏感信息";
                    this.isValid = false;
                }
                else {
                    if (this.custFunction != null) {

                        this.isValid = this.custFunction(this.value);
                        if (!this.isValid) {
                            this.msg = errImg + this.custMsg;
                        }
                    }
                }
            }
        }

        if (this.isValid) {
            this.color = "green";
        }
        else {
            this.color = "red";
        }
        oSrc.style.color = this.color;
        oSrc.style.visibility = "visible";
        oSrc.innerHTML = this.msg;
        //alert(validatorId + 'finally:' + this.isValid);
        return this.isValid;
    }
}




function GetValidator(msgId, chkEmptyText, s, activate) {
    oSrc = document.getElementById(msgId);
    if (oSrc.ZSUSEValidator ==null) {
        oSrc.ZSUSEValidator = new ZSUSEValidator(msgId, chkEmptyText, s, activate);

    }
    oSrc.ZSUSEValidator.activate = activate;
    oSrc.ZSUSEValidator.value = s;
    return oSrc.ZSUSEValidator;
}


/*
验证方法,通过返回true
*/

function IsMatch(exp, s) {
    var re = new RegExp(exp, "igm");
    var v = re.exec(s);
    //alert(s);

    if (v != null && v.length > 0) {
        if (v[0] == s) {
            return true;
        }
    }

    return false;
}


function Ltrim(s) {

    var ss = "";
    var index = 0;
    if (s != null && s != '') {

        for (i = 0; i < s.length; i++) {
            if (s[i] != ' ') {
                index = i;
                break;
            }
        }

        ss = s.substring(index, s.length);

    }
    return ss;
}

function Rtrim(s) {
    var ss = "";
    var index = 0;
    if (s != null && s != '') {

        for (i = s.length - 1; i >= 0; i--) {
            if (s[i] != ' ') {
                index = i;
                break;
            }
        }

        ss = s.substring(0, index + 1);
    }
    return ss;
}

function Trim(s) {
    return s.replace(/^ +/, "").replace(/ +$/, "");
}


function IsMobilePhoneValid(s) {
    
    var exp = "1\\d{10}";
    return IsMatch(exp,s);
}

function IsDateValid(s) {
    var iDay, iMonth, iYear;
    var arrValues;
    arrValues = s.split("-");
    iYear = arrValues[0];
    iMonth = arrValues[1];
    iDay = arrValues[2];
    

    var testDate = new Date(iYear, iMonth - 1, iDay);
    if ((testDate.getDate() != iDay) ||
      (testDate.getMonth() != iMonth - 1) ||
      (testDate.getFullYear() != iYear)) {
        return false;
        
    }

    return true;
}


function IsTimeValid(s) {
    var iDay, iMonth, iYear,iHour,iMinute,iSecond;
    var arrValues;

    arrValues = s.split(" ");
    arr1 = arrValues[0].split("-");
    arr2 = arrValues[1].split(":");
    
    
    iYear = arr1[0];
    iMonth = arr1[1];
    iDay = arr1[2];

    iHour = arr2[0];
    iMinute = arr2[1];
    iSecond = arr2[2];


    var testDate = new Date(iYear, iMonth - 1, iDay,iHour,iMinute,iSecond);
    if ((testDate.getDate() != iDay) ||
      (testDate.getMonth() != iMonth - 1) ||
      (testDate.getFullYear() != iYear) ||
      (testDate.getHours() != iHour) ||
      (testDate.getMinutes() != iMinute) ||
      (testDate.getSeconds() != iSecond)) {
        return false;
      
    }

    return true;
}


function IsDigitValid(s) {
    var exp = "\\d+";
    return IsMatch(exp,s);
}

function IsHanziValid(s) {
    var exp = "^(?:[\u4e00-\u9fa5a-zA-Z]*\w*\s*)+$";
    return IsMatch(exp,s);
}


function IsEmailValid(s) {
    var exp = "\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    return IsMatch(exp,s);
}

function IsQQValid(s) {
    var exp = "[1-9][0-9]{4,}";
    return IsMatch(exp, s);
}
function IsPostalCodeValid(s) {
    var exp = "\\d{6}";
    return IsMatch(exp,s);
}

function IsAreaValid(s) {
    var exp = "\\d{3,4}";
    return IsMatch(exp, s);
}

function IsPersonalIdValid(s) {
    var exp = "\\d{17}[\\d|X]|\\d{15}";
    return IsMatch(exp, s);
}

function IsUrlValid(s) {
    var exp = "(http(s)?://)?([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
    return IsMatch(exp, s);
}

//正确格式：(86)0769-22222222-33，支持多个
function IsTelValid(s) {
    var exp = "(((\\(\\d{2,3}\\))?\\d{2,5}-)?\\d{6,8}(-\\d{1,4})?[,\\s]?)*";
    return IsMatch(exp, s);
}


function IsHtmlValid(s) {
    var exp = "<\s*(\S+)(\s[^>]*)?>[\s\S]*<\s*\/\1\s*>";
    return IsMatch(exp, s);
}

///通用验证
function IsTongYongValid(s) {
    var exp = "(.*?)";
    return IsMatch(exp, s);
}


function IsTextMalicious(s) {
    var s1 = "select,from";
    var s2 = "and,=" ;
    var s3 = "or,=" ;
    var s4 = "update,set,=";
    var s5 = "truncate,table";
    var s6 = "cast,as";
    var s7 = "convert,as";
    var s8 = "<,script,>";
    
    var arr1 = s1.split(",");
    var arr2 = s2.split(",");
    var arr3 = s3.split(",");
    var arr4 = s4.split(",");
    var arr5 = s5.split(",");
    var arr6 = s6.split(",");
    var arr7 = s7.split(",");
    var arr8 = s8.split(",");

    var arr = new Array();
    arr.push(arr1);
    arr.push(arr2);
    arr.push(arr3);
    arr.push(arr4);
    arr.push(arr5);
    arr.push(arr6);
    arr.push(arr7);
    arr.push(arr8);

    var err = false;
    for (i = 0; i < arr.length; i++) {
        count = 0;
        for (j = 0; j < arr[i].length; j++) {
            if (s.indexOf(arr[i][j]) != -1) {
                count++;        
            }
        }
        if (count > 1) {
            err = true;
            break;
        }
    }
    return err;
}

//通用验证

function Check_CompanyName(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入公司名';
    return v.Check();
}

function Check_PostalCode(msgId, chkEmptyText, s, activate) {

    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入邮编';
    v.custFunction = IsPostalCodeValid;
    v.custMsg = '格式错误';
    return v.Check();
}

function Check_area(msgId, chkEmptyText, s, activate) {

    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入区号';
    v.custFunction = IsAreaValid;
    v.custMsg = '格式错误';
    return v.Check();
}

function Check_Tel(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入联系电话/传真';
    v.custFunction = IsTelValid;
    v.custMsg = '格式错误';
    return v.Check();
}

function Check_MobilePhone(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请填写手机号码';
    v.custFunction = IsMobilePhoneValid;
    v.custMsg = '格式错误';
    return v.Check();
}

function Check_WebSite(msgId, chkEmptyText, s, activate) {

    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入网址';
    v.custFunction = IsUrlValid;
    v.custMsg = '格式错误';
    return v.Check();
}

function Check_Date(msgId, chkEmptyText, s, activate) {

    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入日期';
    v.custFunction = IsDateValid;
    v.custMsg = '格式错误';
    return v.Check();
}
function Check_Number(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入数字';
    v.custFunction = IsDigitValid;
    v.custMsg = '输入格式错误，请输入数字!';
    return v.Check();
}

function Check_PersonalId(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入身份证号';
    v.custFunction = IsPersonalIdValid;
    v.custMsg = '格式错误，请重新输入';
    return v.Check();
}

function Check_QiYeFaRen(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请填写法人代表的姓名';
    v.custFunction = IsHanziValid;
    v.custMsg = '格式错误，参考格式如：张三或zhangsan';
    return v.Check();
}

function Check_JobTitle(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请填写您的职位';
    v.custFunction = IsHanziValid;
    v.custMsg = '格式错误，参考格式如：总经理或zongjingli';
    return v.Check();
}

function Check_ContactPerson(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请填写您姓名';
    v.custFunction = IsHanziValid;
    v.custMsg = '格式错误，只能输入英文和汉字';
    return v.Check();
}

function Check_Email(msgId, chkEmptyText, s, activate) {

    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = "请输入电子邮件";
    v.custFunction = IsEmailValid;
    v.custMsg = '格式错误';
    return v.Check();
}
function Check_WorkingAge(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入工作年龄';
    v.custFunction = IsDigitValid;
    v.custMsg = '输入格式错误，请输入数字!';
    return v.Check();
}
function Check_CurAreaId(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入现所在地ID';
    v.custFunction = IsDigitValid;
    v.custMsg = '输入格式错误，请输入数字!';
    return v.Check();
}
function Check_ExpectAreaId(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    v.emptyMsg = '请输入期望工作地ID';
    v.custFunction = IsDigitValid;
    v.custMsg = '输入格式错误，请输入数字!';
    return v.Check();
}
//function Check_Age(msgId, chkEmptyText, s, activate) {
//    v = GetValidator(msgId, chkEmptyText, s, activate);
//    v.emptyMsg = '请输入您的年龄';
//    v.custFunction = IsDigitValid;
//    v.custMsg = '输入格式错误，请输入数字!';
//    return v.Check();
//}
function Check_Float(msgId, chkEmptyText, s, activate) {
    v = GetValidator(msgId, chkEmptyText, s, activate);
    //v.emptyMsg = "";
    v.custFunction = IsFloatValid;
    v.custMsg = '格式错误，参考格式：整型表示:10；小数表示:10.2/10.22';
    return v.Check();
}

//验证小数类型数据
function IsFloatValid(s) {
    var exp1 = "([0-9]+)(\.?[0-9]{1,2})";
    var exp2 = "\\d+";
    if (IsMatch(exp1, s) || IsMatch(exp2, s)) {
        return true;
    }
    else {
        return false;
    }
    //    return IsMatch(exp, s);
}
