/* formHandling // 1.0.3 // jQuery 1.2.3 // Twist Image */
(function($){$.fn.formHandling=function(params){params=$.extend({},$.fn.formHandling.defaults,params);$('label:has(span.max) ~ textarea',this).addClass('v-maxlength').keyup(function(){var maxlength=$(this).siblings('label').find('.max').html();var $counter=$(this).siblings('label').find('.counter');var currentLength=this.value.length;$counter.html(currentLength.toString()+' / ');if(currentLength>maxlength){$counter.addClass('error');}
else{$counter.removeClass('error');}});return this.submit(function(event){if(!params.ajax){return $(this).isValid(params);}
event.preventDefault();var form=this;var $form=$(this);var $container=$form.parents(params.formContainer);if(params.skipValidation||$form.isValid(params)){var $buttons=$(':image, :submit, :reset',$form).attr('disabled','disabled');var $loading=$('<div class="ajax_loading"></div>').appendTo($container);$.ajax({type:form.method,url:form.action,data:$form.serialize()+'&is_ajax=true'+params.extraData,dataType:"xml",complete:function(){$loading.remove();$buttons.attr('disabled','');},success:function(xml){if(params.ga&&undefined!==window.pageTracker){console.log('ga');pageTracker._trackPageview(form.action);}
var response={result:$('result',xml).text(),title:$('title',xml).text(),msg:$('msg',xml).text(),url:$('url',xml).text()};params.ajaxSuccess(response,$form);},error:function(msg){params.ajaxError(msg,$form);}});}
if(params.callback){params.callback($form);}});};$.fn.isValid=function(params){params=$.extend({},$.fn.formHandling.defaults,params);var error=0;$('.error',this).removeClass('error');$('span.error_desc',this).remove();$(".v-mandatory",this).each(function(){if(!$(this).val()){error++;flagError(this,params,' is mandatory');}});$(".v-email",this).each(function(){if(!checkEmail($(this).val())){error++;flagError(this,params,' is invalid');}});$(".v-phone",this).each(function(){if(!checkPhone($(this).val())){error++;flagError(this,params,' is invalid');}});$(".v-postal",this).each(function(){if(!checkPostal($(this).val())){error++;flagError(this,params,' is invalid');}});$(".v-password",this).each(function(){if(!checkPassword($(this).val())){error++;flagError(this,params,' is too short');}});$(".v-maxlength",this).each(function(){if(!checkLength(this)){error++;flagError(this,params,' is too long');}});$(".v-date",this).each(function(){if(!checkDate($(this).val())){error++;flagError(this,params,' is invalid');}});$(".v-hour",this).each(function(){if(!checkHour($(this).val())){error++;flagError(this,params,' is invalid');}});$(params.errorWrapper).similars().remove();if(error){var $errEl=$(params.errorWrapper).addClass('error').html(params.invalidMsg);$(params.errorContainer,this).append($errEl);return false;}
return true;};$.fn.similars=function(){var str=this.attr('tagName').toLowerCase();if(this.attr('id')){str="#"+this.attr('id');}
else if(this.attr('class')){str+='.'+this.attr('class').replace(' ','.');}
return $(str);};function flagError(self,params,msg){var $label=$(self).parents('form').find('label[@for='+$(self).attr('id')+']');$label.addClass('error');if(params.explicitError){var $msg=$('<span class="error_desc"></span>').html(msg);if($('.label_info',$label).size()){$('.label_info',$label).before($msg);}else{$label.append($msg);}}}
function checkEmail(to_check){return(/^(\w+([-.+]?\w+)*@\w+([.-]?\w+)*\.(\w{2,5}|museum))?$/.test(to_check));}
function checkPhone(to_check){var rePhone=/^(?:(?:\+?(\d{1,2})[ -.])?[(]?(\d{3})[ -.)]*(\d{3})[ -.]?(\d{4})(?:[ ;]?(?:x|ext\.?|poste|#)[ =]?(\d{1,5}))?)?$/i;return rePhone.test(to_check);}
function checkPostal(to_check){return(/^([a-zA-z]\d[a-zA-Z][-\s]?\d[a-zA-Z]\d|\d{5})?$/.test(to_check));}
function checkPassword(to_check){return to_check.length>=4||to_check.length===0;}
function checkLength(obj){var $limit=$(obj).parents('form').find('label[@for='+$(obj).attr('id')+'] span.max').html();return obj.value.length<=$limit;}
function checkDate(to_check){if(!to_check){return true;}
return(/^(?:\d{4}([-\/. ])(\d{1,2})\1(\d{1,2}))?$/.test(to_check)&&RegExp.$2!=0&&RegExp.$3!=0&&RegExp.$2<=12&&RegExp.$3<=31);}
function checkHour(to_check){var reHour=/^(?:(\d{1,2})[:hH](\d{2}))?$/;return(reHour.test(to_check)&&RegExp.$1<24&&RegExp.$2<60);}
$.fn.formHandling.ajaxSuccess=function(response,$form){var $popup=$form.parents(this.formContainer);if(response.result==1){$(this.titleHandler,$popup).html(response.title);$(this.contentHandler,$popup).html(response.msg);if(response.url=='self'){location.reload();}
else if(response.url){location.href=response.url;}}else{$(this.errorContainer,$popup).empty().append($(this.errorWrapper).html(response.msg));}};$.fn.formHandling.ajaxError=function(msg,$form){var $popup=$form.parents(this.formContainer);$(this.titleHandler,$popup).html('Sorry!');$(this.contentHandler,$popup).html('We are unable to process your request at this time.');};$.fn.formHandling.defaults={explicitError:false,invalidMsg:'Looks like you\'ve missed a field, or some of the information you entered is invalid. Please try again.',errorContainer:'ol',errorWrapper:'<li class="error_msg></li>',ajax:true,callback:null,ajaxSuccess:$.fn.formHandling.ajaxSuccess,ajaxError:$.fn.formHandling.ajaxError,extraData:'',skipValidation:false,formContainer:'div.form_wrapper',titleHandler:'h1',contentHandler:'div.content',ga:false};})(jQuery);
