﻿/// <reference path="reference/jquery-1.4.2-intellisense.js" />
function LoadValidation() {
    //  $.formValidator.initConfig({ formid: "form1", onerror: function(msg) { alert(msg) }, onsuccess: function() { alert('ddd'); return false; } });
    $.formValidator.initConfig({ formid: "form1", onerror: function(msg) { alert(msg) } });
    InitErrorMsg();

    $(".msgTip").each(function() {
        $(this).css("display", "inline");   //显示在同一行
    });

    $(".NotEmpty").each(function() {
        $(this).formValidator({ onshow: "很重要，请真实填写", onfocus: "此项不能为空", oncorrect: "OK" })
     .inputValidator({ min: 1, onerror: "不能为空,请确认" });
    });


    if (!$(".UserName").attr("disabled")) {
        //检查用户名
        $(".UserName").formValidator({
            onshow: "请输入用户名",
            onfocus: "用户名至少6个字符,最多10个字符",
            oncorrect: "该用户名可以注册"
        })
    .inputValidator({ min: 6, max: 10, onerror: "你输入的用户名非法,请确认" })
    .regexValidator({ regexp: "username", datatype: "enum", onerror: "用户名格式不正确" })
    .ajaxValidator({
        type: "get",
        url: "AjaxHandler/UserValidator.ashx",
        // url: "AjaxHandler/UserValidator.ashx",
        //datatype: "json", 
        //data:'UserName='+escape($('.UserName').val()),  后面这个不行
        success: function(data) {           
            return (data == "true");
        },
        buttons: $("input[name*=btnSave]"),
        error: function() { alert("服务器没有返回数据，可能服务器忙，请重试"); },
        onerror: "该用户名不可用，请更换用户名",
        onwait: "正在对用户名进行合法性校验，请稍候..."
    });
    }
    //密码
    $(".Password").formValidator({ onshow: "请输入密码", onfocus: "密码不能为空", oncorrect: "密码合法" })
     .inputValidator({ min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "密码两边不能有空符号" }, onerror: "密码不能为空,请确认" });

    $(".ConfirmPWD").formValidator({ onshow: "请输入重复密码", onfocus: "两次密码必须一致哦", oncorrect: "密码一致" })
    .inputValidator({ min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "重复密码两边不能有空符号" }, onerror: "重复密码不能为空,请确认" })
    .compareValidator({ desid: "Password", operateor: "=", onerror: "2次密码不一致,请确认" });

    $(".CompanyName").formValidator({ onshow: "如果是个人请输入个人姓名", onfocus: "此项不能为空", oncorrect: "OK" })
     .inputValidator({ min: 1, onerror: "公司（个人）名不能为空,请确认" });

    $('input:text[id*=txtPostCode]').formValidator({ onshow: "请输入正确的邮编", onfocus: "邮编不能为空", oncorrect: "OK" })
    .regexValidator({ regexp: "zipcode", datatype: "enum", onerror: "邮编不正确" });

    $(".Email").each(function() {
        $(this).formValidator({ onshow: "请输入邮箱(推荐使用QQ邮箱)", onfocus: "邮箱6-100个字符,输入正确了才能离开焦点", oncorrect: "OK", forcevalid: false })
    .inputValidator({ min: 6, max: 100, onerror: "你输入的邮箱长度非法,请确认" })
    .regexValidator({ regexp: "^([\\w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([\\w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$", onerror: "你输入的邮箱格式不正确" });
    });

    $(".Tel").each(function() {
        $(this).formValidator({ empty: true, onshow: "请输入你的联系电话，可以为空哦", onfocus: "格式例如：0512-88888888", oncorrect: "OK", onempty: "你真的不想留联系电话了吗？" })
        .regexValidator({ regexp: "^[[0-9]{3}-|\[0-9]{4}-]?([0-9]{8}|[0-9]{7})?$", onerror: "你输入的联系电话格式不正确" });
    });

    $(".Fax").each(function() {
        $(this).formValidator({ empty: true, onshow: "请输入你的传真，可以为空哦", onfocus: "格式例如：0512-88888888", oncorrect: "OK", onempty: "确定没有传真？" })
        .regexValidator({ regexp: "^[[0-9]{3}-|\[0-9]{4}-]?([0-9]{8}|[0-9]{7})?$", onerror: "你输入的传真号码格式不正确" });
    });

    $(".QQ").each(function() {
        $(this).formValidator({ onshow: "请输入QQ", onfocus: "QQ号最少4位", oncorrect: "OK" })
    .regexValidator({ regexp: "^\\d{4,12}$", onerror: "输入的QQ不对" });
    })

}

function InitErrorMsg() {
    // $('.NotEmpty').each(function() {
    //        //  $(this).after("<div id='" + $(this).attr("id") + "Tip' class=\"msgTip\" ></div>"); //名称
    GetMsgStr($(this));
    //  });
    //    $('input:text[id*=txtPostCode]').after("<div id='" + $('input:text[id*=txtPostCode]').attr("id") + "Tip' class=\"msgTip\"></div>"); //邮编

    GetMsgStr('.NotEmpty');
    GetMsgStr('.UserName');
    GetMsgStr('.Password');
    GetMsgStr('.ConfirmPWD');
    GetMsgStr('.CompanyName');
    GetMsgStr('input:text[id*=txtPostCode]');
    GetMsgStr('.Tel');
    GetMsgStr('.Email');
    GetMsgStr('.Fax');
    GetMsgStr('.QQ');
}

function GetMsgStr(obj) {
    $(obj).each(function() {
        $(this).after("<div id='" + $(this).attr("id") + "Tip' class=\"msgTip\" ></div>"); //名称
    });
}
