﻿/*
* jQuery UI FormValidator (24.12.10)
* http://github.com/fnagel/jQuery-Accessible-RIA
*
* Copyright (c) 2009 Felix Nagel for Namics (Deustchland) GmbH
* Copyright (c) 2010-2011 Felix Nagel
* Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*
* Depends: jQuery UI
*/
(function (a) { a.widget("ui.formValidator", { version: "1.8", options: { validateLive: true, validateLiveMsg: true, validateTimeout: 500, validateTimeoutCaptcha: 3, validateOff: "Please click here to deactivate live validating of this form.", validateOn: "Please clkick here to activate live form validating.", errorSummery: true, errorsTitle: "Please check the following errors:", submitHowTo: "post", submitUrl: "", submitError: "Something wen't wrong while sending your data. Please retry.", submitSuccess: "Your data was succefully submitted, thank you!", selectDefault: "default", noHover: false }, _create: function () { var c = this.options, b = this; b._updateVirtualBuffer(); c.uid = b.element.attr("id") || Math.random().toString(16).slice(2, 10); if (c.submitUrl == "") { c.submitUrl = b.element.attr("action"); } b.element.submit(function (d) { if (!c.disabled) { b.formSubmitted(); } return c.disabled; }); if (c.validateLive && !c.disabled && c.validateLiveMsg) { b.element.find(".ui-formular-info").append('\t<p><a class="ui-formular-live" href="#nogo">' + c.validateOff + "</a></p>\n\t\t"); b._updateVirtualBuffer(); b.element.find(".ui-formular-live").toggle(function () { c.validateLive = false; a(this).attr("aria-live", "polite").attr("aria-relevant", "text").html(c.validateOn); b._updateVirtualBuffer(); }, function () { c.validateLive = true; a(this).attr("aria-live", "polite").attr("aria-relevant", "text").html(c.validateOff); b._updateVirtualBuffer(); }); } if (!c.noHover) { b._makeHover(b.element.find("input:submit, input:reset")); } a.each(c.forms, function (d) { b.initField(d); }); b._trigger("onInit", 0); }, initField: function (f) { var c = this.options, b = this; var d = b.element.find("#" + f); if (!d.length) { d = b.element.find("input." + f); if (!d.length) { alert("Error: Configuration corrupted!\n\nCan't find element with id or class = " + f); } else { value = "group"; if (!c.noHover) { b._makeHover(d.next()); d.bind("mouseenter", function () { a(this).next().addClass("ui-state-hover"); }).bind("mouseleave", function () { a(this).next().removeClass("ui-state-hover"); }).bind("focus", function () { a(this).next().addClass("ui-state-focus"); }).bind("blur", function () { a(this).next().removeClass("ui-state-focus"); }); } } } else { if (!c.noHover) { b._makeHover(d); } if (c.forms[f].rules.required) { d.attr("aria-required", true); } if (d[0].nodeName.toLowerCase() == "select") { value = "select"; } else { value = "single"; } } c.forms[f].element = d; c.forms[f].type = value; var e = "blur "; if (c.validateTimeout != "blur") { if (c.forms[f].type != "group") { e += (c.forms[f].type == "select") ? "click change keyup" : "keyup"; } else { e += "click"; } } c.forms[f].element.bind(e, function (h) { if (c.validateLive && !c.disabled) { if (c.validateTimeout == "blur" || h.type == "blur") { b.validate(f); } else { if (h.keyCode != a.ui.keyCode.TAB) { if (c.forms[f].timeout) { window.clearTimeout(c.forms[f].timeout); } var g = (c.forms[f].rules["regEx"] == "captcha") ? c.validateTimeout * c.validateTimeoutCaptcha : c.validateTimeout; c.forms[f].timeout = window.setTimeout(function () { b.validate(f); }, g); } } } }); }, _validator: function (f) { var c = this.options, b = this; if (!c.forms[f].disabled) { var e = (c.forms[f].errors) ? c.forms[f].errors : []; var d = b._getValue(f); a.each(c.forms[f].rules, function (h, i) { if (d == "") { if (h != "required") { e[h] = b._whichError(true, e[h]); } if (h == "required" && i) { e[h] = b._whichError(false, e[h]); } } else { if (h == "required" && i) { e[h] = b._whichError(true, e[h]); } switch (h) { case "regEx": var g = ""; switch (i) { case "number": g = b._number(d); break; case "numberDE": g = b._numberDE(d); break; case "numberISO": g = b._numberISO(d); break; case "email": g = b._email(d); break; case "url": g = b._url(d); break; case "plz": g = b._plz(d); break; case "dateDE": g = b._dateDE(d); break; case "dateISO": g = b._dateISO(d); break; case "captcha": g = b._captcha(d); break; default: g = b._regEx(d, i); break; } e[h] = b._whichError(g, e[h]); break; case "lengthMin": e[h] = b._whichError(b._lengthMin(d, i), e[h]); break; case "lengthMax": e[h] = b._whichError(b._lengthMax(d, i), e[h]); break; case "equalTo": e[h] = b._whichError(b._equalTo(d, i), e[h]); break; case "custom": e[h] = b._whichError(i(d), e[h]); break; } } }); c.forms[f].errors = e; } c.forms[f]["id"] = f; b._trigger("customError", 0, c.forms[f]); }, formSubmitted: function () { var c = this.options, b = this; b._trigger("onformSubmitted", 0); b.element.find(".ui-formular-success").remove(); a.each(c.forms, function (e) { var d = false; if (c.forms[e].type == "single") { b._validator(e); } else { if (!d) { d = true; b._validator(e); } } }); b._setErrors(true); }, validate: function (d) { var c = this.options, b = this; b._validator(d); b._setErrors(false); }, _setErrors: function (j) { var l = this.options, k = this; var g, m, e = false; var i = "", d = ""; for (var b in l.forms) { var c = false; for (var h in l.forms[b]["errors"]) { if (l.forms[b]["errors"][h] == "corrected" || l.forms[b].disabled) { var f = l.forms[b].element; f.attr("aria-invalid", false); if (l.forms[b].type == "group") { f = f.next(); } f.removeClass("ui-state-error"); e = true; if (!l.forms[b].disabled) { k._trigger("onValid", null, b); } } else { if (l.forms[b]["errors"][h] == "new" || l.forms[b]["errors"][h] == "old") { if (l.errorSummery) { i += '					<li><a href="#' + b + '">' + l.forms[b].msg[h] + "</a></li>\n"; } g = c = true; k._trigger("onError", null, b); } if (l.forms[b]["errors"][h] == "new") { m = true; } } } if (c) { var f = l.forms[b].element; f.attr("aria-invalid", true); if (l.forms[b].type == "group") { f = f.next(); } f.addClass("ui-state-error"); } } if (l.errorSummery === true || (l.errorSummery == "onSubmit" && j)) { k._showErrors({ submitted: j, isError: g, addError: m, removeError: e, msgs: i }); } if (g) { k._trigger("onErrors", 0); } else { if (j) { k._sendForm(); } } k._updateVirtualBuffer(); }, _showErrors: function (f) { var d = this.options, b = this; var c = ' aria-live="assertive"'; if (f["addError"] || f["removeError"]) { c += ' aria-relevant="text'; } if (f["addError"]) { c += " additions"; } if (f["removeError"]) { c += " removals"; } if (f["addError"] || f["removeError"]) { c += '"'; } var e = "\n"; if (f["isError"]) { e += "			<div" + c + ' class="info ui-state-highlight ui-state ui-corner-all">' + "\n"; e += '				<p id="ui-error-title-' + d.uid + '">' + "\n"; e += '					<span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span>' + "\n"; e += "					" + d.errorsTitle + "\n"; e += "				</p>" + "\n"; e += '				<ul aria-labelledby="ui-error-title-' + d.uid + '">' + "\n"; e += f["msgs"]; e += "				</ul>" + "\n"; e += "			</div>" + "\n\t\t"; } errorElement = b.element.find(".ui-formular-error"); errorElement.html(e); if (f["isError"]) { errorElement.find("a").click(function (g) { var i = a(this).attr("href").split("#"); i = i[1]; var h = (d.forms[i].type == "single") ? d.forms[i].element : d.forms[i].element[0]; h.focus(); return false; }); if (f["submitted"]) { errorElement.attr("tabindex", -1).focus(); } b._trigger("onErrors", 0); } b._trigger("onShowErrors", 0); }, _sendForm: function () { var d = this.options, c = this; switch (d.submitHowTo) { default: case "post": d.disabled = true; c.element.submit(); break; case "ajax": a.ajax({ data: c.element.serialize(), type: "post", url: d.submitUrl, error: function (f) { c._showSuccess(f); }, success: function (f) { c._showSuccess(f); } }); break; case "iframe": d.originalUrl = c.element.attr("action"); c.element.attr("action", d.submitUrl); var b = ("upload" + (new Date()).getTime()); var e = a('<iframe name="' + b + '"></iframe>'); e.css("display", "none"); e.load(function (f) { c._showSuccess(a(this).contents().find("body").html()); d.timeout = window.setTimeout(function () { e.remove(); }, 200); }); a("body").append(e); c.element.attr("target", b); d.disabled = true; c.element.submit(); break; } }, _showSuccess: function (f) { var c = this.options, b = this; var g = "", e = ""; c.disabled = false; if (jQuery.browser.safari) { b.element.find(".ui-formular-success").remove(); } switch (f) { case "true": case "1": g = c.submitSuccess; e = "check"; break; default: if (f == "") { g = c.submitError; } else { g = f; } e = "alert"; break; } var d = "\n"; d += '		<div class="ui-formular-success">' + "\n"; d += '			<div aria-live="assertive" class="info ui-state-highlight ui-state ui-corner-all">' + "\n"; d += "				<p>" + "\n"; d += '					<span class="ui-icon ui-icon-' + e + '" style="float: left; margin-right: 0.3em;"></span>' + "\n"; d += "					" + g + "\n"; d += "				</p>" + "\n"; d += "			</div>" + "\n\t\t"; d += "		</div>" + "\n\t\t"; b.element.prepend(d); b.element.find(".ui-formular-success").attr("tabindex", -1).focus(); b._updateVirtualBuffer(); b._trigger("onShowSuccess", null, f); }, _whichError: function (b, d) { var c = ""; if (!b) { c = (d == "new" || d == "old") ? "old" : "new"; } else { if (d == "new" || d == "old") { c = "corrected"; } } return c; }, _getValue: function (e) { var c = this.options, d = ""; switch (c.forms[e].type) { case "single": d = c.forms[e].element.val(); break; case "group": var b = c.forms[e].element.filter(":checked"); if (b.length) { d = b; } break; case "select": var b = c.forms[e].element.children("option").filter(":selected"); if (c.forms[e].element.attr("multiple")) { d = b; } else { d = (b.val() == c.selectDefault) ? 0 : b.val(); } break; } return d; }, _makeHover: function (b) { b.bind("mouseenter", function () { a(this).addClass("ui-state-hover"); }).bind("mouseleave", function () { a(this).removeClass("ui-state-hover"); }).bind("focus", function () { a(this).addClass("ui-state-focus"); }).bind("blur", function () { a(this).removeClass("ui-state-focus"); }); }, _regEx: function (c, b) { b = new RegExp(b); return b.test(c); }, _number: function (b) { return /^\d+$/.test(b); }, _numberDE: function (b) { return /^[-+]?([0-9]*\,)?[0-9]+$/.test(b); }, _numberISO: function (b) { return /^[-+]?([0-9]*\.)?[0-9]+$/.test(b); }, _email: function (b) { return /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/.test(b); }, _url: function (b) { return /^(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(b); }, _plz: function (b) { return /^\b((?:0[1-46-9]\d{3})|(?:[1-357-9]\d{4})|(?:[4][0-24-9]\d{3})|(?:[6][013-9]\d{3}))\b$/.test(b); }, _dateDE: function (b) { return /^\d\d?\.\d\d?\.\d\d\d?\d?$/.test(b); }, _dateISO: function (b) { return /^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/.test(b); }, _lengthMin: function (b, c) { return (b.length >= c) ? true : false; }, _lengthMax: function (b, c) { return (b.length <= c) ? true : false; }, _equalTo: function (b, c) { return (b == a("#" + c).val()) ? true : false; }, _captcha: function (b, c) { return this._trigger("checkCaptcha", null, b); }, destroy: function () { var b = this.options; a.each(b.forms, function (c) { b.forms[c].element.removeClass("ui-state-error").removeClass("ui-state-hover").removeAttr("aria-invalid").removeAttr("aria-required").unbind(); if (b.forms[c].type == "group") { b.forms[c].element.next().removeClass("ui-state-error").removeClass("ui-state-hover").removeAttr("aria-invalid").removeAttr("aria-required").unbind(); } }); this.element.unbind(".formValidator").unbind("submit").removeData("formValidator"); if (b.originalUrl != "") { this.element.attr("action", b.originalUrl); } this.element.find(".ui-formular-live, .ui-formular-error, .ui-formular-success").remove(); a("body>form #virtualBufferForm").parent().remove(); a.Widget.prototype.destroy.apply(this, arguments); }, _updateVirtualBuffer: function () { var c = a("body>form #virtualBufferForm"); if (c.length) { (c.val() == "1") ? c.val("0") : c.val("1"); } else { var b = '<form><input id="virtualBufferForm" type="hidden" value="1" /></form>'; a("body").append(b); } } }); })(jQuery);
