﻿$(function() {
    $('input,select,textarea,table').focus(function() {
        var mtt = $.trim($(this).attr("mtt"));
        var err = $.trim($(this).attr("err"));
        var errortype = $.trim($(this).attr($(this).attr("errortype")));
        var x = $(this).offset().left;
        var y = $(this).offset().top;
        var h = $(this).height();
        var w = $(this).width() + 15;
        var cls = $(this).attr("class");

        try {
            if (cls.indexOf("error") >= 0) {
                if (errortype == "" || errortype == null || errortype == "undefined" || errortype == undefined) errortype = err;
                if (errortype == "" || errortype == null || errortype == "undefined" || errortype == undefined) errortype = mtt;
                if (errortype != "" && errortype != null && mtt != "undefined") {
                    var div = $('<div style="position: absolute; left: ' + (x + w) + 'px; top: ' + y + 'px; display: none;" class="errdiv">' + errortype + '</div>');
                    div.appendTo(document.body);
                }
                $(".errdiv").show(100);
            }
            else if (mtt != null && mtt != "" && mtt != "undefined") {
                var div = $('<div style="position: absolute; left: ' + (x + w) + 'px; top: ' + y + 'px; display: none;" class="mttdiv">' + mtt + '</div>');
                div.appendTo(document.body);
                $(".mttdiv").show(100);
            }
        } catch (e) { alert(e); }
    });

    $("input,select,textarea,table").blur(function() {
        $('.errdiv').hide(100);
        $('.mttdiv').hide(100);
        $('.errdiv').remove();
        $('.mttdiv').remove();
    });
});

function validateform(vgroup) {
    $(".msgdiv").remove();
    var Result = true;
    $("input,select,textarea").each(function() {
        if ((vgroup == "" || $(this).attr("vgroup") == vgroup)) {
            var isRequired = $(this).attr("isRequired");
            var isBDate = $(this).attr("isBDate");
            var isDate = $(this).attr("isDate");
            var isEmail = $(this).attr("isEmail");
            var isUrl = $(this).attr("isUrl");
            var minValue = $(this).attr("minValue");
            var maxValue = $(this).attr("maxValue");
            var minLength = $(this).attr("minLength");
            var maxLength = $(this).attr("maxLength");
            var isNumber = $(this).attr("isNumber");
            var isDigit = $(this).attr("isDigit");
            var isEqualTo = $(this).attr("isEqualTo");
            var isGreaterThan = $(this).attr("isGreaterThan");
            var isLessThan = $(this).attr("isLessThan");
            var FileTypes = $(this).attr("FileTypes");
            var isValidPhoneNo = $(this).attr("isValidPhoneNo");
            var isValidPincode = $(this).attr("isValidPincode");
            var res = true;

            try {
                switch (this.nodeName.toLowerCase()) {
                    case "input":
                        if (this.type == "text" || this.type == "password" || this.type == "file") {
                            var value = this.value;

                            if (isDate == "true") {
                                var r = !/Invalid|NaN/.test(new Date(value));
                                res = res && r;
                                setErrorType(r, this, "isDateErr");
                            }

                            if (typeof isBDate == "number") {
                                var d = new Date();
                                d.setDate(d.getDate() - isBDate);
                                var r = (!/Invalid|NaN/.test(new Date(value)) && new Date(value) < d);
                                res = res && r;
                                setErrorType(r, this, "isBDateErr");
                            }

                            if (isDate == "true") {
                                var r = !/Invalid|NaN/.test(new Date(value))
                                res = res && r;
                                setErrorType(r, this, "isDateErr");
                            }

                            if (isEmail == "true") {
                                var r = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);
                                res = res && r;
                                setErrorType(r, this, "isEmailErr");
                            }

                            if (isUrl == "true") {
                                if (value == "") {
                                    r = false;
                                    setErrorType(r, this, "isRequiredErr");
                                }
                                else {
                                    var r = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
                                    res = res && r;
                                    setErrorType(r, this, "isUrlErr");
                                }
                            }


                            if (isEqualTo + "" != "undefined") {
                                var r = (value == $("#" + isEqualTo).val());
                                res = res && r;
                                setErrorType(r, this, "isEqualToErr");
                            }

                            if (isDigit == "true") {
                                var r = /^\d+$/.test(value);
                                res = res && r;
                                setErrorType(r, this, "isDigitErr");
                            }

                            if (minLength + "" != "undefined") {
                                try {
                                    var r = value.length >= parseInt(minLength);
                                    res = res && r;
                                    setErrorType(r, this, "minLengthErr");
                                } catch (err) { }
                            }

                            if (minValue + "" != "undefined") {
                                try {
                                    var r = value > parseInt(minValue);
                                    res = res && r;
                                    setErrorType(r, this, "minValueErr");
                                } catch (err) { }
                            }

                            if (maxValue + "" != "undefined") {
                                try {
                                    var r = value < parseInt(maxValue);
                                    res = res && r;
                                    setErrorType(r, this, "maxValueErr");
                                } catch (err) { }
                            }
                            else if (isGreaterThan + "" != "undefined") {
                                var r = (value <= document.getElementById(isGreaterThan).value);
                                res = res && r;
                                setErrorType(r, this, "isGreaterThanErr");
                            }
                            else if (isLessThan + "" != "undefined") {
                                var r = (value >= document.getElementById(isLessThan).value);
                                res = res && r;
                                setErrorType(r, this, "isLessThanErr");
                            }
                            if (FileTypes + "" != "undefined") {
                                FileTypes = typeof FileTypes == "string" ? FileTypes.replace(/,/g, '|') : "png|jpe?g|gif";
                                var r = value.match(new RegExp(".(" + FileTypes + ")$", "i"));
                                res = res && r;
                                setErrorType(r, this, "FileTypesErr");
                            }

                            if (isNumber == "true") {
                                if (value == "") {
                                    r = false;
                                    setErrorType(r, this, "isNumberErr");
                                }
                                else {
                                    var r = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(value);
                                    res = res && r;
                                    setErrorType(r, this, "isNumberErr");
                                }
                            }
                            if (isRequired == "true") {
                                var r = $.trim(value) != "";
                                res = res && r;
                                setErrorType(r, this, "isRequiredErr");
                            }
                            if (isValidPhoneNo + "" != "undefined") {
                                var r = new RegExp(isValidPhoneNo);
                                if (this.value.match(r)) {
                                    r = true;
                                } else {
                                    r = false;
                                }
                                res = res && r;
                                //                                var r = isValidPhoneNo;
                                //                                r = r.test(value);
                                //                                res = res && r;
                                setErrorType(r, this, "isValidPhoneNoErr");
                            }
                            if (isValidPincode + "" != "undefined") {
                                var r = new RegExp(isValidPincode);
                                if (this.value.match(r)) {
                                    r = true;
                                } else {
                                    r = false;
                                }
                                res = res && r;
                                setErrorType(r, this, "isValidPincodeErr");
                            }
                        }

                        if (this.disabled)
                            res = true;

                        if (!res)
                            $(this).addClass("error");
                        else
                            $(this).removeClass("error");
                        Result = Result && res;

                        break;

                    case "select":
                        var value = this.value;
                        if (isRequired == "true") {
                            var r = this.selectedIndex > 0;
                            res = res && r;
                            setErrorType(r, this, "isRequiredErr");
                        }

                        if (this.multiple == "multiple" && minLength + "" != "undefined") {
                            try {
                                var cnt = parseInt(minLength);
                                var r = $(this).find("option:selected") > minLength;
                                res = res && r;
                            } catch (err) { alert(err); }
                            setErrorType(r, this, "minLengthErr");
                        }

                        if (this.multiple == "multiple" && maxLength + "" != "undefined") {
                            try {
                                var cnt = parseInt(maxLength);
                                var r = $(this).find("option:selected") < maxLength;
                                res = res && r;
                            } catch (err) { alert(err); }
                            setErrorType(r, this, "maxLengthErr");
                        }

                        if (this.disabled)
                            res = true;


                        if (!res)
                            $(this).addClass("error");
                        else
                            $(this).removeClass("error");
                        Result = Result && res;
                        break;

                    case "textarea":
                        var value = this.value;
                        if (isRequired == "true") {
                            var r = $.trim(value) != "";
                            res = res && r;
                            setErrorType(r, this, "isRequiredErr");
                        }

                        if (minLength + "" != "undefined") {
                            try {
                                var r = value.length >= parseInt(minLength);
                                res = res && r;
                                setErrorType(r, this, "minLengthErr");
                            } catch (err) { }
                        }

                        if (maxLength + "" != "undefined" && maxLength > -1) {
                            try {
                                var r = value.length < parseInt(maxLength);
                                res = res && r;
                                setErrorType(r, this, "maxLengthErr");
                            } catch (err) { }
                        }

                        if (this.disabled)
                            res = true;

                        if (!res)
                            $(this).addClass("error");
                        else
                            $(this).removeClass("error");
                        Result = Result && res;
                        break;
                }
            } catch (ex) { alert(ex); }
        }
    });

    $(".error:first").focus();
    if (!Result) {
        ShowError("You have some errors in the form. Please correct it.");
    }
    return Result;
}

function setErrorType(res, elem, atr) {
    if (!res) {
        if ($(elem).attr(atr) + "" != "undefined")
            $(elem).attr("errortype", atr);
        //else if ($(elem).attr("err") + "" != "undefined")
        //    $(elem).attr("errortype", "err");
        //else $(elem).attr("errortype", "mtt");
    }
}

function ShowError(err) {
    $(".showerror").remove();
    $(".showmessage").remove();
    var div = $('<div style="position: absolute; display: block;" class="showerror">' + err + '</div>');
    div.appendTo(document.body);
    $(".showerror").css("left", ($(window).width() / 2 - $(".showerror").width() / 2) + "px");
    var h = ($(".showerror").height() + 5) * -1;
    $(".showerror").css("top", h + "px");
    $(".showerror").css("position", "fixed");
    $(".showerror").animate({ top: 0 }, 500);
    $(".showerror").click(function() { $(".showerror").animate({ top: ($(".showerror").height() + 5) * -1 }, 500, function() { $(".showerror").remove(); }); });
    //setTimeout(function() { $(".msgdiv").animate({ top: ($(".msgdiv").height() + 5) * -1 }, 500, function() { $(".msgdiv").remove(); }); }, 5000);
}

function ShowMessage(msg) {
    $(".showerror").remove();
    $(".showmessage").remove();
    var div = $('<div style="position: absolute; display: block;" class="showmessage">' + msg + '</div>');
    div.appendTo(document.body);
    $(".showmessage").css("left", ($(window).width() / 2 - $(".showmessage").width() / 2) + "px");
    var h = ($(".showmessage").height() + 5) * -1;
    $(".showmessage").css("top", h + "px");
    $(".showmessage").css("position", "fixed");
    $(".showmessage").animate({ top: 0 }, 500);
    $(".showmessage").click(function() { $(".showmessage").animate({ top: ($(".showmessage").height() + 5) * -1 }, 500, function() { $(".showmessage").remove(); }); });
    //setTimeout(function() { $(".msgdiv").animate({ top: ($(".msgdiv").height() + 5) * -1 }, 500, function() { $(".msgdiv").remove(); }); }, 5000);
}

function addMethod(object, name, fn) {
    var old = object[name];
    if (old)
        object[name] = function() {
            if (fn.length == arguments.length)
                return fn.apply(this, arguments);
            else if (typeof old == 'function')
                return old.apply(this, arguments);
        };
    else
        object[name] = fn;
}

function FilterText(val) {

}

