// Copyright (c) 2009 Felix Nagel for Namics (Deustchland) GmbH
// Licensed under Creative Commens Attribution-Share Alike 3.0 Unported (http://creativecommons.org/licenses/by-sa/3.0/)
(function(A){A.widget("ui.formValidator",{_init:function(){var C=this.options,B=this;B._updateVitualBuffer();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){B.element.find("#ui-formular-info").append('\t<p><a id="ui-formular-live" href="#nogo">'+C.validateOff+"</a></p>\n\t\t");B._updateVitualBuffer();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._updateVitualBuffer();},function(){C.validateLive=true;A(this).attr("aria-live","polite").attr("aria-relevant","text").html(C.validateOff);B._updateVitualBuffer();});}B._makeHover(B.element.find("input:submit, input:reset"));errors=B.options.errorsArray;A.each(C.forms,function(F){errors[F]=[];var D=B.element.find("#"+F);if(!D.length){D=B.element.find("input[name='"+F+"']");if(!D.length){alert("Error: Configuration corrupted!\n\nCan' find element with id or name = "+F);}else{value="group";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{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;if(C.validateLive){if(C.forms[F].type!="group"){var E="keypress";if(C.forms[F].type=="select"){E="change";}}else{var E="change";}C.forms[F].element.bind(E,function(){if(C.validateLive&&!C.disabled){if(C.forms[F].timeout){window.clearTimeout(C.forms[F].timeout);}if(F=="captcha"){var G=C.validateTimeout*3;}else{var G=C.validateTimeout;}C.forms[F].timeout=window.setTimeout(function(){B._validator(C.forms[F].element,F,errors);B._showErrors(false);},G);}});}});C.errorsArray=errors;B._trigger("onInit",0);},_validator:function(D,G,F){var C=this.options,B=this;var E=B._getValue(G);A.each(C.forms[G].rules,function(H,I){if(E==""){if(H!="required"){F[G][H]=B._whichError(true,F[G][H]);}if(H=="required"&&I){F[G][H]=B._whichError(false,F[G][H]);}}else{if(H=="required"&&I){F[G][H]=B._whichError(true,F[G][H]);}switch(H){case"regEx":switch(I){case"number":F[G][H]=B._whichError(B._number(E),F[G][H]);break;case"numberDE":F[G][H]=B._whichError(B._numberDE(E),F[G][H]);break;case"numberISO":F[G][H]=B._whichError(B._numberISO(E),F[G][H]);break;case"email":F[G][H]=B._whichError(B._email(E),F[G][H]);break;case"url":F[G][H]=B._whichError(B._url(E),F[G][H]);break;case"plz":F[G][H]=B._whichError(B._plz(E),F[G][H]);break;case"dateDE":F[G][H]=B._whichError(B._dateDE(E),F[G][H]);break;case"dateISO":F[G][H]=B._whichError(B._dateISO(E),F[G][H]);break;case"captcha":F[G][H]=B._whichError(B._captcha(E),F[G][H]);break;default:F[G][H]=B._whichError(B._regEx(E,I),F[G][H]);break;}break;case"lengthMin":F[G][H]=B._whichError(B._lengthMin(E,I),F[G][H]);break;case"lengthMax":F[G][H]=B._whichError(B._lengthMax(E,I),F[G][H]);break;case"equalTo":F[G][H]=B._whichError(B._equalTo(E,I),F[G][H]);break;}}});B.options.errorsArray=F;},formSubmitted:function(){var C=this.options,B=this;B._trigger("onformSubmitted",0);B.element.find("#ui-formular-success").remove();errors=B.options.errorsArray;A.each(C.forms,function(F){var E=false;var D=C.forms[F].element;if(C.forms[F].type=="single"){B._validator(D,F,errors);}else{if(!E){E=true;B._validator(D,F,errors);}}});B._showErrors(true);},_showErrors:function(K){var N=this.options,M=this;var G,O,D=false;var J=msg="";var L=M.options.errorsArray;for(var B in L){var C=false;for(var I in L[B]){if(L[B][I]=="corrected"){var F=N.forms[B].element;F.attr("aria-invalid",false);if(N.forms[B].type=="group"){F=F.next();}F.removeClass("ui-state-error");D=true;}if(L[B][I]=="new"||L[B][I]=="old"){switch(I){case"required":msg=N.forms[B].msg.required;break;case"regEx":msg=N.forms[B].msg.regEx;break;case"lengthMin":msg=N.forms[B].msg.length;break;case"lengthMax":msg=N.forms[B].msg.length;break;case"equalTo":msg=N.forms[B].msg.equalTo;break;}J+='					<li><a href="#'+B+'">'+msg+"</a></li>\n";G=C=true;}if(L[B][I]=="new"){O=true;}}if(C){var F=N.forms[B].element;F.attr("aria-invalid",true);if(N.forms[B].type=="group"){F=F.next();}F.addClass("ui-state-error");}}var H=' aria-live="assertive"';if(O||D){H+=' aria-relevant="text';}if(O){H+=" additions";}if(D){H+=" removals";}if(O||D){H+='"';}var E="\n";if(G){E+="			<div"+H+' class="info ui-state-highlight ui-state ui-corner-all">'+"\n";E+='				<p id="ui-error-title">'+"\n";E+='					<span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span>'+"\n";E+="					"+N.errorsTitle+"\n";E+="				</p>"+"\n";E+='				<ul aria-labelledby="ui-error-title">'+"\n";E+=J;E+="				</ul>"+"\n";E+="			</div>"+"\n\t\t";}errorElement=M.element.find("#ui-formular-error");errorElement.html(E);if(G){errorElement.find("a").click(function(P){P.preventDefault();var R=A(this).attr("href").split("#");R=R[1];if(N.forms[R].type=="single"){var Q=N.forms[R].element;}else{var Q=N.forms[R].element[0];}Q.focus();});if(K){errorElement.attr("tabindex",-1).focus();}}else{if(K){M._sendForm();}}M._updateVitualBuffer();M._trigger("onShowErrors",0);},_sendForm:function(){var D=this.options,C=this;switch(D.submitHowTo){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;switch(F){case"true":G=C.submitSuccess;E="check";break;case"false":G=C.submitError;E="alert";break;default:G=F;E="alert";}var D="\n";D+='		<div id="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._updateVitualBuffer();B._trigger("onShowSuccess",0);},_whichError:function(B,D){var C="";if(!B){if(D=="new"||D=="old"){C="old";}else{C="new";}}else{if(D=="new"||D=="old"){C="corrected";}else{if(D=="corrected"){C="";}}}return C;},_getValue:function(G){var D=this.options,C=this;var E=D.forms[G].type;var F="";switch(E){case"single":F=D.forms[G].element.val();break;case"group":var B=D.forms[G].element.filter(":checked");F=(B.length)?B:"";break;case"select":var B=D.forms[G].element.find("option").filter(":selected");F=(B.length)?B:"";break;}return F;},_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(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-\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("#virtualBufferForm").parent().remove();},_updateVitualBuffer:function(){var C=A("#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);}}});A.extend(A.ui.formValidator,{version:"1.7.1",defaults:{validateLive:true,validateTimeout:500,validateOff:"Bitte klicken Sie hier um die Live Validierung zu deaktivieren.",validateOn:"Bitte klicken Sie hier um die Live Validierung zu aktivieren.",errorsTitle:"Bitte korrigieren Sie folgende Fehler:",submitHowTo:"ajax",submitUrl:"",submitError:"Bei der Datenübertragung ist ein Fehler aufgetreten. Entschuldigen Sie bitte und versuchen Sie es noch einmal.",submitSuccess:"Die Daten wurden erfolgreich übermittelt. Vielen Dank!",errorsArray:[],originalUrl:""}});})(jQuery);

