/**
 * jQuery Form Validation Plugin
 *
 * A jquery plugin which may be used to validate forms.
 *
 * @author     Conor Mac Aoidh <conormacaoidh@gmail.com> http://blog.conormacaoidh.com
 * @license    The BSD License
 * @version    1.1
 */
(function($){var Validate={errors:0,validated:0,required_f:[],email_f:[],pattern_f:[],minlength_f:[],maxlength_f:[],match_f:[],url_f:[],addConds:function(pieces){for(var i in pieces){for(var n in pieces[i]){switch(n){case"required":if(pieces[i][n]==true&&$("input[name="+i+"]").length!=0)
this.required_f.push(i);break;case"pattern":if($("input[name="+i+"]").length!=0)
this.pattern_f[i]=pieces[i][n];break;case"email":if(pieces[i][n]==true&&$("input[name="+i+"]").length!=0)
this.email_f.push(i);break;case"minlength":if($("input[name="+i+"]").length!=0)
this.minlength_f[i]=pieces[i][n];break;case'maxlength':if($("input[name="+i+"]").length!=0)
this.maxlength_f[i]=pieces[i][n];break;case"match":if($("input[name="+i+"]").length!=0)
this.match_f[i]=pieces[i][n];break;case'url':if(pieces[i][n]==true)
this.url_f.push(i);break;}}}},customErrorHandler:null,errorHandler:function(){if(typeof(this.customErrorHandler)=='function')
return this.customErrorHandler(this.errors);alert(this.errors);},required:function(){for(var i=0;i<this.required_f.length;i++){var loc=$("input[name="+this.required_f[i]+"]");loc.removeClass('error');if(loc.val()==''){this.errors='Please do not leave the '+this.required_f[i]+' field blank.';loc.addClass('error');this.errorHandler();return false;}}
return true;},emailFormat:function(){var filter=/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;for(var i=0;i<this.email_f.length;i++){var loc=$("input[name="+this.email_f[i]+"]");loc.removeClass('error');if(filter.test(loc.val())==false){this.errors='Please enter a valid email address';loc.addClass('error');this.errorHandler();return false;}}
return true;},minlength:function(){for(var i in this.minlength_f){var loc=$("input[name="+i+"]");loc.removeClass('error');if(loc.val().length<this.minlength_f[i]){this.errors='The '+i+' field must be at least '+this.minlength_f[i]+' characters long.';loc.addClass('error');this.errorHandler();return false;}}
return true;},maxlength:function(){for(var i in this.maxlength_f){var loc=$("input[name="+i+"]");loc.removeClass('error');if(loc.val().length>this.maxlength_f[i]){this.errors='The '+i+' field must be a maximim of '+this.minlength_f[i]+' characters long.';loc.addClass('error');this.errorHandler();return false;}}
return true;},match:function(){for(var i in this.match_f){var locone=$("input[name="+i+"]");var loctwo=$("input[name="+this.match_f[i]+"]");locone.removeClass('error');loctwo.removeClass('error');if(locone.val()!=loctwo.val()){this.errors='The '+i+' and '+this.match_f[i]+' fields do not match';locone.addClass('error');loctwo.addClass('error');this.errorHandler();return false;}}
return true;},url:function(){var filter=/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;for(var i=0;i<this.url_f.length;i++){var loc=$("input[name="+this.url_f[i]+"]");loc.removeClass('error');if(filter.test(loc.val())==false&&loc.val()!=""){this.errors='Please enter a valid URL.';loc.addClass('error');this.errorHandler();return false;}}
return true;},pattern:function(){for(var i in this.pattern_f){if($.isArray(this.pattern_f[i])){var regex=this.pattern_f[i][0];var message=this.pattern_f[i][1];}
else{var regex=this.pattern_f[i];var message='The '+i+' field is not valid.';}
if(typeof(regex)!='object')
regex=new RegExp(regex);var loc=$("input[name="+i+"]");loc.removeClass('error');if(regex.test(loc.val())==false){this.errors=message;loc.addClass('error');this.errorHandler();return false;}}
return true;},execute:function(){if(this.validated==1)
return true;if(this.required_f.length!=0&&this.required()==false)
return false;if(this.email_f.length!=0&&this.emailFormat()==false)
return false;if(this.pattern()==false)
return false;if(this.minlength()==false)
return false;if(this.maxlength()==false)
return false;if(this.match()==false)
return false;if(this.url_f.lenth!=0&&this.url()==false)
return false;this.validated=1;return true;}};$.fn.validate=function(conds,errorHandler){if(conds=='execute')
return Validate.execute();if(typeof(errorHandler)=='function')
Validate.customErrorHandler=errorHandler;Validate.addConds(conds);var id=this.attr('id');this.unbind('submit.validate-'+id);this.bind('submit.validate-'+id,function(){return Validate.execute();});return this;};})(jQuery);

