﻿(function ($) { function fnAjaxSubmit(a, b, c, d, e, f) { var f = f || false; $("input:text[name], input:radio[name]:checked, input:checkbox[name]:checked, textarea[name], select[name], input:hidden[name]", a).each(function (a, b) { var d; if (b.nodeName == "SELECT") { d = $("option:selected", b).val() } else { d = $(this).val() } c[b.name] = d }); if (f) { var g = ""; for (var h in c) { g += '"' + h + '": "' + c[h] + '", ' } if (e) { $(e).html(g).dialog("open") } else { alert(g) } } else { $.ajax({ url: b, type: "POST", dataType: "text", data: c, success: function (b) { if (d) { d(b) } else { $(a).html("<center>" + b + "</center>") } } }) } return false } function fnTodaysDate() { var a = new Date; var b = a.getMonth(); var c = a.getDate(); var d = a.getFullYear(); var e = b + 1 + "/" + c + "/" + d; return e } function fnValSSN(a) { if ($.trim(a) != "") { var b = /((\d{3}-))?\d{2}-\d{4}/; return b.test(a) } return true } function fnValDate(a) { if ($.trim(a) != "") { var b = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/; return b.test(a) } return true } function fnValPhoneNumber(a) { if ($.trim(a) != "") { var b = /((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}/; return b.test(a) } return true } function fnValZipCode(a) { if ($.trim(a) != "") { var b = /\d{5}(-\d{4})?/; return b.test(a) } return true } function fnValEmail(a) { if ($.trim(a) != "") { var b = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; return b.test(a) } return true } $.fn.extend({ Validate: function (a) { var a = a || {}; var b = a.sRequiredFieldMsg || "Required"; var c = a.sRequiredAlert || "Please fill in all required fields and make sure you have entered appropriate values for each field."; var d = a.sAction || null; var e = a.sMethod || "post"; var f = a.sDataUrl || ""; var g = a.oParameters || null; var h = a.fnPostSubmit || null; var i = a.bDebug || false; var j; var k = true; try { j = $("<div>", { id: "ValidateDialog", title: "Required Fields" }).dialog({ autoOpen: false, width: 300, height: 200, modal: true }) } catch (l) { k = false } return this.each(function () { var a = this; var l; if (a.nodeName != "FORM") { $(a).wrap("<form></form>"); l = $(a).parent(); if (d) { $(l).attr({ action: d, method: e }) } } else { l = a } $(":input[name]", l).each(function (a, b) { if ($(b).hasClass("required")) { $(this).Required() } if ($(b).hasClass("email")) { $(this).Validation("Email", "Invalid Email") } if ($(b).hasClass("date")) { $(this).Validation("Date", "Invalid Date") } if ($(b).hasClass("zip")) { $(this).Validation("ZipCode", "Invalid Zip Code") } if ($(b).hasClass("phone")) { $(this).after('<span class="validation"></span>').blur(function () { var a = $(this).val(); var b = a.replace(/[^0-9]/g, ""); var c = b.split(""); var d = ""; for (var e = 0; e < c.length; e++) { d += c[e]; if (e == 2 || e == 5) { d += "-" } } $(this).val(d).next(".validation").text(""); if (!fnValPhoneNumber(d)) { $(this).next(".validation").text("Invalid Phone Number") } }) } if ($(b).hasClass("ssn")) { $(this).after('<span class="validation"></span>').blur(function () { var a = $(this).val(); var b = a.replace(/[^0-9]/g, ""); var c = b.split(""); var d = ""; for (var e = 0; e < c.length; e++) { d += c[e]; if (e == 2 || e == 4) { d += "-" } } $(this).val(d).next(".validation").text(""); if (!fnValSSN(d)) { $(this).next(".validation").text("Invalid SSN") } }) } if ($(b).hasClass("conditional")) { $(this).ConditionalSelect() } }); $(l).submit(function () { var a = 0; $(".required", this).each(function (c, d) { var e = $(d).val(); if ($.trim(e) == "") { $(d).next().text(b); a++ } if (d.type == "checkbox" && !d.checked) { $(d).next().text(b); a++ } }); $(".email", this).each(function () { if (!fnValEmail($(this).val())) { a++ } }); $(".date", this).each(function () { if (!fnValDate($(this).val())) { a++ } }); $(".zip", this).each(function () { if (!fnValZipCode($(this).val())) { a++ } }); $(".phone", this).each(function () { if (!fnValPhoneNumber($(this).val())) { a++ } }); $(".ssn", this).each(function () { if (!fnValSSN($(this).val())) { a++ } }); if (a > 0) { if (k) { $(j).html(c).dialog("open") } else { alert(c) } return false } if (!d) { fnAjaxSubmit(this, f, g, h, j, i); return false } }) }) }, NotRequired: function () { return this.each(function () { if ($(this).hasClass("required")) { $(this).removeClass("required"); $(this).next().remove(".error") } }) }, Required: function () { return this.each(function () { $(this).addClass("required"); $(this).next().remove(".error"); $(this).after('<span class="error">*</span>'); $(this).blur(function () { if ($.trim($(this).val()) != "") { $(this).next().text("*") } }) }) }, Validation: function (_function, errorTxt) { return this.each(function () { $(this).next().remove(".validation"); var err = errorTxt && errorTxt.length > 0 ? errorTxt : "Invalid"; $(this).after('<span class="validation"></span>'); $(this).blur(function () { var txt = eval("fnVal" + _function + "($(this).val())") ? "" : err; $(this).next(".validation").text(txt) }) }) }, ConditionalSelect: function () { return this.each(function () { $(this).bind("change", function () { var a = $("option:selected", this).val(); var b = $(this).attr("rel"); var c = b.search(/|/) > -1 ? b.split("|") : b; for (var d = 0; d < c.length; d++) { var e = c[d]; var f = e.split(":"); var g = f[0]; var h = f[1]; if (h.search(/\[/) > -1) { h = h.replace("[", "").replace("]", ""); var i = h.split(","); $.each(i, function (b, c) { if (a == g) { $("#" + c).Required() } else { $("#" + c).NotRequired() } }) } else { if (a == g) { $("#" + h).Required() } else { $("#" + h).NotRequired() } } } }) }) } }) })(jQuery)
