﻿
jQuery.fn.extend({
    validate: function () {
        if ($(this).attr("tagName") == "FORM") {
            $(this).submit(function () {
                return $("#form1").validateInput($(this).attr("id"));
            })
        }
        else {
            this.click(function () {
                return $("#form1").validateInput($(this).attr("id"));
            });
        }
    },
    getTags: function () {
        var form = $("form");
        if (!form)
            return;
        return $("[reg]");
    },
    validateInput: function (objectId) {
        hidMessageDiv();

        $(".validateCss").remove();
        var ipts = this.getTags();
        var IsValidate = true;
        ipts.each(function (i) {
            if ($(this).attr("objId") == objectId || objectId == "form1") {
                var regType = $(this).attr("reg");
                var regValue = $("#form1").rule[regType];
                if ($.isFunction(regValue)) {
                    var obj = $("#" + $(this).attr("eqvalue"));
                    var funValidate = true;
                    if (obj.length > 0)
                        funValidate = regValue($(this).val(), obj.val());
                    else
                        funValidate = regValue($(this).val());

                    if (!funValidate) {
                        $("#form1").showMsg(this, $("#form1").messages[regType]);
                        IsValidate = false;
                    }
                }
                else {
                    var reg = new RegExp(regValue);
                    var value = $("#form1").trim($(this).val());
                    if (!reg.test(value)) {
                        $("#form1").showMsg(this, $("#form1").messages[regType]);
                        IsValidate = false;
                    }
                }
            }
        });
        return IsValidate;
    },
    trim: function (str) {
        return str;
    },
    showMsg: function (sender, msg) {
        showErrorMessage(msg);
    },
    trim: function (str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },
    getWidth: function (object) {
        return object.offsetWidth;
    },

    getLeft: function (object) {
        var go = object;
        var oParent, oLeft = go.offsetLeft;
        while (go.offsetParent != null) {
            oParent = go.offsetParent;
            oLeft += oParent.offsetLeft;
            go = oParent;
        }
        return oLeft;
    },
    getTop: function (object) {
        var go = object;
        var oParent, oTop = go.offsetTop;
        while (go.offsetParent != null) {
            oParent = go.offsetParent;
            oTop += oParent.offsetTop;
            go = oParent;
        }
        return oTop + 22;
    },
    rule: {
        // 正则规则
        "required": /\S/,
        "eng": /^[A-Za-z]+$/,
        "chn": /^[\u0391-\uFFE5]+$/,
        "code": /\S/,
        "mail": /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
        "url": /^http[s]?:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
        "int": /^[0-9]{1,30}$/,
        "firstname": /\S/,
        "lastname": /\S/,
        "address": /\S/,
        "name": /\S/,
        "cityname": /\S/,
        "comments": /\S/,
        "captcha": /\S/,

        "postcode": /\S/,
        "companyname": /\S/,
        "username": /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){1,19}$/,
        "password": /^(\w){6,20}$/,
        "oldpassword": /^(\w){6,20}$/,
        "newpassword": /^(\w){6,20}$/,
        "safe": />|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i,
        "date": /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/,
        "year": /^(19|20)[0-9]{2}$/,
        "month": /^(0?[1-9]|1[0-2])$/,
        "day": /^((0?[1-9])|((1|2)[0-9])|30|31)$/,
        "hour": /^((0?[1-9])|((1|2)[0-3]))$/,
        "minute": /^((0?[1-9])|((1|5)[0-9]))$/,
        "second": /^((0?[1-9])|((1|5)[0-9]))$/,
        "mobile": /\S/,
        "phone": /\S/,
        "zipcode": /^[1-9]\d{5}$/,
        "bodycard": /^((1[1-5])|(2[1-3])|(3[1-7])|(4[1-6])|(5[0-4])|(6[1-5])|71|(8[12])|91)\d{4}((19\d{2}(0[13-9]|1[012])(0[1-9]|[12]\d|30))|(19\d{2}(0[13578]|1[02])31)|(19\d{2}02(0[1-9]|1\d|2[0-8]))|(19([13579][26]|[2468][048]|0[48])0229))\d{3}(\d|X|x)?$/,
        "ip": /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
        "file": /^[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/,
        "image": /.+\.(jpg|gif|png|bmp)$/i,
        "word": /.+\.(doc|rtf|pdf)$/i,
        // 函数规则 
        "eqEmail": function (arg1, arg2) { return arg1 == arg2 ? true : false; },
        "eqPwd": function (arg1, arg2) { return arg1 == arg2 ? true : false; },
        "gt": function (arg1, arg2) { return arg1 > arg2 ? true : false; },
        "gte": function (arg1, arg2) { return arg1 >= arg2 ? true : false; },
        "lt": function (arg1, arg2) { return arg1 < arg2 ? true : false; },
        "lte": function (arg1, arg2) { return arg1 <= arg2 ? true : false; }
    },
    messages: {
        required: "This field is required.",
        eng: "",
        chn: "",
        code: "Please enter characters shown in Picutre.",
        mail: "Please enter a valid email address.",
        url: "Please enter a valid URL.",
        int: "Please enter a valid number.",
        firstname: "Please enter your first name.",
        lastname: "Please enter your last name.",
        address: "Please enter your address",
        name: "Please enter your name.",
        cityname: "Please enter your city name.",
        comments: "Please enter additional comments.",
        captcha: "Please enter the captcha.",

        postcode: "Please enter your postcode",
        companyname: "Please enter your company name.",
        username: "Please enter your user name.",
        password: "Please enter 6 characters Password.",
        oldpassword: "Please enter 6 characters old Password.",
        newpassword: "Please enter 6 characters new Password.",
        safe: "",
        date: "Please enter a valid date.",
        year: "",
        month: "",
        day: "",
        hour: "",
        minute: "",
        second: "",
        mobile: "Please enter your mobile",
        phone: "Please enter your phone",
        zipcode: "",
        bodycard: "",
        ip: "",
        file: "",
        image: "",
        word: "",
        // 函数规则
        eqEmail: "Invalid Confirm Email.",
        eqPwd: "Invalid Confirm Password.",
        gt: "",
        gte: "",
        lt: "",
        lte: ""
    }
});

function showErrorMessage(msg) {
    var divMsg = $("#div_ErrorMssage");
    if (divMsg.length == 0) {
        var dh = $('.nav_list');
        dh.append("<div class=\"signin_message\" id=\"div_ErrorMssage\" style=\"display: none\"></div>");
        divMsg = $("#div_ErrorMssage");
    }
    divMsg.attr("class", "signin_message").css("display", "block");
    if (divMsg.children().length == 0) {
        divMsg.append("<ul></ul>");
    }
    var ul = divMsg.children().first();
    if (ul.children('.note').length == 0) {
        ul.append("<li class=\"note\">We’re sorry, we are unable to process your request because of following errors. Please rectify them:</li>");
    }
    ul.append("<li class=\"note_r\">" + msg + "</li>");

    window.scrollTo(0, divMsg.offset().top - 5);
}
function showOtherMessage(msg) {
    hidMessageDiv();
    var MsgDiv = $("#div_OtherMssage");
    if (MsgDiv.length > 0) {
        MsgDiv.attr("class", "msgbox theme-bg4").html("<TABLE border=0 cellSpacing=0 cellPadding=0 width=\"100%\"><TR><TD class=\"align-t pd10-r\" width=35><B class=\"icon ico-user\">&nbsp;</B></TD><TD><H6 class=\"h4\">Thank you</H6><P id=\"spnErrorMsg\"><SPAN>" + msg + "</SPAN></P></TD></TR></TBODY></TABLE>");
        MsgDiv.show();
        window.scrollTo(0, MsgDiv.offset().top - 5);
    }
    else {
        alert(msg);
    }
}
function hidMessageDiv() {
    if ($("#div_ErrorMssage").length > 0) {
        $("#div_ErrorMssage").html('').css("display", "none");
    }
    if ($("#div_OtherMssage").length > 0) {
        $("#div_OtherMssage").html('').css("display", "none");
    }
}
