/*
 * Contrôle de formulaire 1.0 version compressée
 * Copyright(c) 2011, Damien Fayet
 */
(function(b){var a={init:function(c){var d=this;if(!d.data("jqv")||d.data("jqv")==null){a._saveOptions(d,c);b(".formError").live("click",function(){b(this).fadeOut(150,function(){b(this).remove()})})}},attach:function(f){var e=this;var d;if(f){d=a._saveOptions(e,f)}else{d=e.data("jqv")}var c=(e.find("[data-validation-engine*=validate]"))?"data-validation-engine":"class";if(!d.binded){if(d.bindMethod=="bind"){e.find("[class*=validate]:not([type=checkbox])").bind(d.validationEventTrigger,a._onFieldEvent);e.find("[class*=validate][type=checkbox]").bind("click",a._onFieldEvent);e.bind("submit",a._onSubmitEvent)}else{if(d.bindMethod=="live"){e.find("[class*=validate]:not([type=checkbox])").live(d.validationEventTrigger,a._onFieldEvent);e.find("[class*=validate][type=checkbox]").live("click",a._onFieldEvent);e.live("submit",a._onSubmitEvent)}}d.binded=true}},detach:function(){var d=this;var c=d.data("jqv");if(c.binded){d.find("[class*=validate]").not("[type=checkbox]").unbind(c.validationEventTrigger,a._onFieldEvent);d.find("[class*=validate][type=checkbox]").unbind("click",a._onFieldEvent);d.unbind("submit",a.onAjaxFormComplete);d.find("[class*=validate]").not("[type=checkbox]").die(c.validationEventTrigger,a._onFieldEvent);d.find("[class*=validate][type=checkbox]").die("click",a._onFieldEvent);d.die("submit",a.onAjaxFormComplete);d.removeData("jqv")}},validate:function(){return a._validateFields(this)},validateField:function(d){var c=b(this).data("jqv");return a._validateField(b(d),c)},validateform:function(){return a._onSubmitEvent.call(this)},showPrompt:function(d,f,h,e){var g=this.closest("form");var c=g.data("jqv");if(!c){c=a._saveOptions(this,c)}if(h){c.promptPosition=h}c.showArrow=e==true;a._showPrompt(this,d,f,false,c)},hidePrompt:function(){var c="."+a._getClassName(b(this).attr("id"))+"formError";b(c).fadeTo("fast",0.3,function(){b(this).remove()})},hide:function(){var c;if(b(this).is("form")){c="parentForm"+b(this).attr("id")}else{c=b(this).attr("id")+"formError"}b("."+c).fadeTo("fast",0.3,function(){b(this).remove()})},hideAll:function(){b(".formError").fadeTo("fast",0.3,function(){b(this).remove()})},_onFieldEvent:function(){var e=b(this);var d=e.closest("form");var c=d.data("jqv");a._validateField(e,c)},_onSubmitEvent:function(){var e=b(this);var c=e.data("jqv");var d=a._validateFields(e,true);if(d&&c.ajaxFormValidation){a._validateFormWithAjax(e,c);return false}if(c.onValidationComplete){c.onValidationComplete(e,d);return false}return d},_checkAjaxStatus:function(d){var c=true;b.each(d.ajaxValidCache,function(e,f){if(!f){c=false;return false}});return c},_validateFields:function(f,p){var q=f.data("jqv");var g=false;f.trigger("jqv.form.validating");f.find("[class*=validate]").not(":hidden").each(function(){var d=b(this);g|=a._validateField(d,q,p)});f.trigger("jqv.form.result",[g]);if(g){if(q.scroll){var o=Number.MAX_VALUE;var j=0;var l=b(".formError:not('.greenPopup')");for(var k=0;k<l.length;k++){var m=b(l[k]).offset().top;if(m<o){o=m;j=b(l[k]).offset().left}}if(!q.isOverflown){b("html:not(:animated),body:not(:animated)").animate({scrollTop:o,scrollLeft:j},1100)}else{var c=b(q.overflownDIV);var e=c.scrollTop();var h=-parseInt(c.offset().top);o+=e+h-5;var n=b(q.overflownDIV+":not(:animated)");n.animate({scrollTop:o},1100);b("html:not(:animated),body:not(:animated)").animate({scrollTop:c.offset().top,scrollLeft:j},1100)}}return false}return true},_validateFormWithAjax:function(e,d){var f=e.serialize();var c=(d.ajaxFormValidationURL)?d.ajaxFormValidationURL:e.attr("action");b.ajax({type:"GET",url:c,cache:false,dataType:"json",data:f,form:e,methods:a,options:d,beforeSend:function(){return d.onBeforeAjaxFormValidation(e,d)},error:function(g,h){a._ajaxError(g,h)},success:function(l){if(l!==true){var j=false;for(var k=0;k<l.length;k++){var m=l[k];var o=m[0];var h=b(b("#"+o)[0]);if(h.length==1){var n=m[2];if(m[1]==true){if(n==""||!n){a._closePrompt(h)}else{if(d.allrules[n]){var g=d.allrules[n].alertTextOk;if(g){n=g}}a._showPrompt(h,n,"pass",false,d,true)}}else{j|=true;if(d.allrules[n]){var g=d.allrules[n].alertText;if(g){n=g}}a._showPrompt(h,n,"",false,d,true)}}}d.onAjaxFormComplete(!j,e,l,d)}else{d.onAjaxFormComplete(true,e,"",d)}}})},_validateField:function(l,q,m){if(!l.attr("id")){b.error("jQueryValidate: an ID attribute is required for this field: "+l.attr("name")+" class:"+l.attr("class"))}var p=l.attr("class");var f=/validate\[(.*)\]/.exec(p);if(!f){return false}var k=f[1];var o=k.split(/\[|,|\]/);var d=false;var n=l.attr("name");var c="";var j=false;q.isError=false;q.showArrow=true;for(var e=0;e<o.length;e++){var h=undefined;switch(o[e]){case"required":j=true;h=a._required(l,o,e,q);break;case"custom":h=a._customRegex(l,o,e,q);break;case"ajax":if(!m){a._ajax(l,o,e,q);d=true}break;case"minSize":h=a._minSize(l,o,e,q);break;case"maxSize":h=a._maxSize(l,o,e,q);break;case"min":h=a._min(l,o,e,q);break;case"max":h=a._max(l,o,e,q);break;case"past":h=a._past(l,o,e,q);break;case"future":h=a._future(l,o,e,q);break;case"maxCheckbox":h=a._maxCheckbox(l,o,e,q);l=b(b("input[name='"+n+"']"));break;case"minCheckbox":h=a._minCheckbox(l,o,e,q);l=b(b("input[name='"+n+"']"));break;case"equals":h=a._equals(l,o,e,q);break;case"funcCall":h=a._funcCall(l,o,e,q);break;default:}if(h!==undefined){c+=h+"<br/>";q.isError=true}}if(!j){if(l.val()==""){q.isError=false}}var g=l.attr("type");if((g=="radio"||g=="checkbox")&&b("input[name='"+n+"']").size()>1){l=b(b("input[name='"+n+"'][type!=hidden]:first"));q.showArrow=false}if(q.isError){a._showPrompt(l,c,"",false,q)}else{if(!d){a._closePrompt(l)}}l.trigger("jqv.field.result",[l,q.isError,c]);return q.isError},_required:function(f,g,e,d){switch(f.attr("type")){case"text":case"password":case"textarea":case"file":default:if(!f.val()){return d.allrules[g[e]].alertText}break;case"radio":case"checkbox":var c=f.attr("name");if(b("input[name='"+c+"']:checked").size()==0){if(b("input[name='"+c+"']").size()==1){return d.allrules[g[e]].alertTextCheckboxe}else{return d.allrules[g[e]].alertTextCheckboxMultiple}}break;case"select-one":if(!f.val()){return d.allrules[g[e]].alertText}break;case"select-multiple":if(!f.find("option:selected").val()){return d.allrules[g[e]].alertText}break}},_customRegex:function(j,k,f,d){var c=k[f+1];var h=d.allrules[c];if(!h){alert("jqv:custom rule not found "+c);return}var e=h.regex;if(!e){alert("jqv:custom regex not found "+c);return}var g=new RegExp(e);if(!g.test(j.val())){return d.allrules[c].alertText}},_funcCall:function(g,h,d,c){var f=h[d+1];var e=window[f];if(typeof(e)=="function"){return e(g,h,d,c)}},_equals:function(f,g,e,d){var c=g[e+1];if(f.val()!=b("#"+c).val()){return d.allrules.equals.alertText}},_maxSize:function(h,j,f,e){var d=j[f+1];var c=h.val().length;if(c>d){var g=e.allrules.maxSize;return g.alertText+d+g.alertText2}},_minSize:function(h,j,f,d){var e=j[f+1];var c=h.val().length;if(c<e){var g=d.allrules.minSize;return g.alertText+e+g.alertText2}},_min:function(h,j,f,d){var e=parseFloat(j[f+1]);var c=parseFloat(h.val());if(c<e){var g=d.allrules.min;if(g.alertText2){return g.alertText+e+g.alertText2}return g.alertText+e}},_max:function(h,j,f,e){var d=parseFloat(j[f+1]);var c=parseFloat(h.val());if(c>d){var g=e.allrules.max;if(g.alertText2){return g.alertText+d+g.alertText2}return g.alertText+d}},_past:function(j,k,e,c){var h=k[e+1];var d=(h.toLowerCase()=="now")?new Date():a._parseDate(h);var f=a._parseDate(j.val());if(f<d){var g=c.allrules.past;if(g.alertText2){return g.alertText+a._dateToString(d)+g.alertText2}return g.alertText+a._dateToString(d)}},_future:function(j,k,e,c){var h=k[e+1];var d=(h.toLowerCase()=="now")?new Date():a._parseDate(h);var f=a._parseDate(j.val());if(f>d){var g=c.allrules.future;if(g.alertText2){return g.alertText+a._dateToString(d)+g.alertText2}return g.alertText+a._dateToString(d)}},_maxCheckbox:function(h,j,g,f){var d=j[g+1];var e=h.attr("name");var c=b("input[name='"+e+"']:checked").size();if(c>d){f.showArrow=false;if(f.allrules.maxCheckbox.alertText2){return f.allrules.maxCheckbox.alertText+" "+d+" "+f.allrules.maxCheckbox.alertText2}return f.allrules.maxCheckbox.alertText}},_minCheckbox:function(h,j,g,f){var d=j[g+1];var e=h.attr("name");var c=b("input[name='"+e+"']:checked").size();if(c<d){f.showArrow=false;return f.allrules.minCheckbox.alertText+" "+d+" "+f.allrules.minCheckbox.alertText2}},_ajax:function(m,o,h,p){var n=o[h+1];var l=p.allrules[n];var e=l.extraData;var j=l.extraDataDynamic;if(!e){e=""}if(j){var g=[];var k=String(j).split(",");for(var h=0;h<k.length;h++){var c=k[h];if(b(c).length){var d=m.closest("form").find(c).val();var f=c.replace("#","")+"="+escape(d);g.push(f)}}j=g.join("&")}else{j=""}if(!p.isError){b.ajax({type:"GET",url:l.url,cache:false,dataType:"json",data:"fieldId="+m.attr("id")+"&fieldValue="+m.val()+"&extraData="+e+"&"+j,field:m,rule:l,methods:a,options:p,beforeSend:function(){var i=l.alertTextLoad;if(i){a._showPrompt(m,i,"load",true,p)}},error:function(i,q){a._ajaxError(i,q)},success:function(s){var u=s[0];var r=b(b("#"+u)[0]);if(r.length==1){var q=s[1];var t=s[2];if(!q){p.ajaxValidCache[u]=false;p.isError=true;if(t){if(p.allrules[t]){var i=p.allrules[t].alertText;if(i){t=i}}}else{t=l.alertText}a._showPrompt(r,t,"",true,p)}else{if(p.ajaxValidCache[u]!==undefined){p.ajaxValidCache[u]=true}if(t){if(p.allrules[t]){var i=p.allrules[t].alertTextOk;if(i){t=i}}}else{t=l.alertTextOk}if(t){a._showPrompt(r,t,"pass",true,p)}else{a._closePrompt(r)}}}}})}},_ajaxError:function(c,d){if(c.status==0&&d==null){alert("The page is not served from a server! ajax call failed")}else{if(typeof console!="undefined"){console.log("Ajax error: "+c.status+" "+d)}}},_dateToString:function(c){return c.getFullYear()+"-"+(c.getMonth()+1)+"-"+c.getDate()},_parseDate:function(e){var c=e.split("-");if(c==e){c=e.split("/")}return new Date(c[0],(c[1]-1),c[2])},_showPrompt:function(i,g,h,f,e,d){var c=a._getPrompt(i);if(d){c=false}if(c){a._updatePrompt(i,c,g,h,f,e)}else{a._buildPrompt(i,g,h,f,e)}},_buildPrompt:function(g,c,e,i,j){var d=b("<div>");d.addClass(a._getClassName(g.attr("id"))+"formError");if(g.is(":input")){d.addClass("parentForm"+a._getClassName(g.parents("form").attr("id")))}d.addClass("formError");switch(e){case"pass":d.addClass("greenPopup");break;case"load":d.addClass("blackPopup")}if(i){d.addClass("ajaxed")}var k=b("<div>").addClass("formErrorContent").html(c).appendTo(d);if(j.showArrow){var h=b("<div>").addClass("formErrorArrow");switch(j.promptPosition){case"bottomLeft":case"bottomRight":d.find(".formErrorContent").before(h);h.addClass("formErrorArrowBottom").html('<div class="line1"><!-- --></div><div class="line2"><!-- --></div><div class="line3"><!-- --></div><div class="line4"><!-- --></div><div class="line5"><!-- --></div><div class="line6"><!-- --></div><div class="line7"><!-- --></div><div class="line8"><!-- --></div><div class="line9"><!-- --></div><div class="line10"><!-- --></div>');break;case"topLeft":case"topRight":h.html('<div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line2"><!-- --></div><div class="line1"><!-- --></div>');d.append(h);break}}if(j.isOverflown){g.before(d)}else{b("body").append(d)}var f=a._calculatePosition(g,d,j);d.css({top:f.callerTopPosition,left:f.callerleftPosition,marginTop:f.marginTopSize,opacity:0});return d.animate({opacity:0.87})},_updatePrompt:function(h,c,f,g,e,d){if(c){if(g=="pass"){c.addClass("greenPopup")}else{c.removeClass("greenPopup")}if(g=="load"){c.addClass("blackPopup")}else{c.removeClass("blackPopup")}if(e){c.addClass("ajaxed")}else{c.removeClass("ajaxed")}c.find(".formErrorContent").html(f);var i=a._calculatePosition(h,c,d);c.animate({top:i.callerTopPosition,marginTop:i.marginTopSize})}},_closePrompt:function(d){var c=a._getPrompt(d);if(c){c.fadeTo("fast",0,function(){c.remove()})}},closePrompt:function(c){return a._closePrompt(c)},_getPrompt:function(e){var d="."+a._getClassName(e.attr("id"))+"formError";var c=b(d)[0];if(c){return b(c)}},_calculatePosition:function(i,e,l){var c,j,h;var g=i.width();var k=e.height();var d=l.isOverflown;if(d){c=j=0;h=-k}else{var f=i.offset();c=f.top;j=f.left;h=0}switch(l.promptPosition){default:case"topRight":if(d){j+=g-30}else{j+=g-30;c+=-k}break;case"topLeft":c+=-k-10;break;case"centerRight":j+=g+13;break;case"bottomLeft":c=c+i.height()+15;break;case"bottomRight":j+=g-30;c+=i.height()+5}return{callerTopPosition:c+"px",callerleftPosition:j+"px",marginTopSize:h+"px"}},_saveOptions:function(e,d){if(b.validationEngineLanguage){var c=b.validationEngineLanguage.allRules}else{b.error("jQuery.validationEngine rules are not loaded, plz add localization files to the page")}var f=b.extend({validationEventTrigger:"blur",scroll:true,promptPosition:"topRight",bindMethod:"bind",inlineAjax:false,ajaxFormValidation:false,ajaxFormValidationURL:false,onAjaxFormComplete:b.noop,onBeforeAjaxFormValidation:b.noop,onValidationComplete:false,isOverflown:false,overflownDIV:"",allrules:c,binded:false,showArrow:true,isError:false,ajaxValidCache:{}},d);e.data("jqv",f);return f},_getClassName:function(c){return c.replace(":","_").replace(".","_")}};b.fn.validationEngine=function(d){var c=b(this);if(!c[0]){return false}if(typeof(d)=="string"&&d.charAt(0)!="_"&&a[d]){if(d!="showPrompt"&&d!="hidePrompt"&&d!="hide"&&d!="hideAll"){a.init.apply(c)}return a[d].apply(c,Array.prototype.slice.call(arguments,1))}else{if(typeof d=="object"||!d){a.init.apply(c,arguments);return a.attach.apply(c)}else{b.error("Method "+d+" does not exist in jQuery.validationEngine")}}}})(jQuery);
