var FORM=function(selector){return new FormInput.Processor($(selector));};JS2.OO.createClass("FormInput.Processor");(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('method',"initialize",function(jq){this.jq=jq;this.inputs=[];this.form=this.jq;var self=this;if(!this.form.is('form')){this.form=this.jq.find('form:first');}
this.jqSubmit=this.form.first('input[type=submit]');this.form.submit(function(evt){return self.onSubmit();});var fields=jq.find('.field');for(var i=0;i<fields.length;i++){this.inputs.push(this.getInput(fields.eq(i),{toggleSubmit:function(){self.toggleSubmit();}}));}});K.oo('method',"onSubmit",function(){var pass=true;for(var it36=0,input,it36__arr=this.inputs,it36__len=it36__arr.length;(input=it36__arr[it36])||it36<it36__len;it36++){if(!input.onSubmit())pass=false;}
return pass;});K.oo('method',"submittable",function(){for(var it37=0,input,it37__arr=this.inputs,it37__len=it37__arr.length;(input=it37__arr[it37])||it37<it37__len;it37++){if(!input.validated()){this.disableSubmit();return false;}}
this.enableSubmit();return true;});K.oo('method',"getInput",function(jq,options){return new FormInput[jq.attr('datatype')||'String'](jq,options);});K.oo('method',"toggleSubmit",function(){for(var i=0,input;input=this.inputs[i];i++){if(!input.validated()){this.disableSubmit();return;}}
this.enableSubmit();});K.oo('method',"enableSubmit",function(){this.jqSubmit.attr('disabled',false);});K.oo('method',"disableSubmit",function(){this.jqSubmit.attr('disabled',true);});})(FormInput.Processor,FormInput);JS2.OO.createClass("FormInput.Base");(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','REQUIRE_AJAX',true);K.oo('member','NO_AJAX',false);K.oo('method',"initialize",function(jq,options){this.options=options||{};this.toggleSubmit=options.toggleSubmit;this.populate(jq);this.load();this.registerEvents();});K.oo('method',"load",function(){this.parseOpts(this.jqMain);this.min=this.min?parseInt(this.min):null;this.max=this.max?parseInt(this.max):null;this.name=this.name||'';});K.oo('method',"getError",function(ajax){var functs=['contentError','sizeError'];var val=this.val();var error=false;if(val.length!=0){for(var it38=0,funct,it38__arr=functs,it38__len=it38__arr.length;(funct=it38__arr[it38])||it38<it38__len;it38++){var message=this[funct](val);if(message){error=message;break;}}}
this.hasError=!!error;if(this.hasError&&this.err){error=this.err;}
return error;});K.oo('method',"val",function(){return this.jq.val();});K.oo('method',"populate",function(jq){this.jqMain=jq;this.jq=jq.first('input[type!=hidden]');this.messaging=jq.first('.messaging');});K.oo('method',"findMessaging",function(){var next=this.jq.next();if(next.is('.messaging')){this.messaging=next;}});K.oo('method',"focus",function(){if(!this.messaging)return;var m=this.message();if(!this.showingMessage&&m){this.showingMessage=true;this.messaging.show();this.messaging.html(m);this.changeStatus('info');}
this.toggleSubmit();});K.oo('method',"keyup",function(){var error=this.getError(this.NO_AJAX);this.toggleSubmit();return this.showError(error);});K.oo('method',"showError",function(error){if(error){this.showingMessage=true;this.messaging.show();this.messaging.html(error);this.messaging.addClass('error');this.changeStatus('error');return true;}else{this.showingMessage=false;this.messaging.html('');var showPass=this.val().length>0;if(showPass){this.changeStatus(this.val().length==0?'':'pass');this.messaging.show();}else{this.changeStatus();this.messaging.hide();}
return false;}});K.oo('method',"changeStatus",function(status){var className='messaging';if(status)className+=' '+status;if(this.messaging[0])this.messaging[0].className=className;});K.oo('method',"registerEvents",function(){var self=this;this.jq.focus(function(){self.focus();}).keyup(function(){self.keyup();});});K.oo('method',"onSubmit",function(){var reqError=this.requiredError();if(reqError){this.showingMessage=true;this.messaging.show();this.messaging.html(reqError);this.messaging.addClass('error');this.changeStatus('error');return false;}
var error=this.getError(this.NO_AJAX);this.showError(error);if(error){return false;}
return true;});K.oo('method',"validated",function(){if(this.requiredError())return false;if(this.getError(this.NO_AJAX))return false;return true;});K.oo('method',"message",function(){if(this.msg)return this.msg;var message='';if(this.defaultMessage){message+=this.defaultMessage;}
if(this.min){message+=' minimum of '+this.min+' characters.';}
if(this.max){message+=' maximum of '+this.max+' characters.';}
return message.length?this.name+' '+message:'';});K.oo('method',"requiredError",function(){var error=false;if(!this.val()&&this.required){error=this.name+' is a required field.';}
this.hasError=!!error;return error;});K.oo('method',"contentError",function(val){if(this.regex){if(this.regex instanceof Array){for(var i=0,r;r=this.regex[i];i++){var m=val.match(r[0]);if(!m){return r[1]?r[1]:this.message();}}}else{var m=val.match(this.regex);if(!m){return this.message();}}}
return false;});K.oo('method',"sizeError",function(val){if(!this.min||!this.max)return false;var error=[];var l=val.length;if((this.min&&l>=this.min)&&(this.max&&l<=this.max)){return false;}
if(this.min&&this.max){if(this.min==this.max){return'must be '+this.max+' characters.';}
else{return'must be between '+
this.min+' and '+this.max+' characters.';}}else if(this.min){return'must be more than '+
this.min+' characters long.';}else if(this.max){return'must be less than '+
this.max+' characters long.';}});K.oo('method',"parseOpts",function(jq){var optString=jq.attr('valid');this.required=!!jq.attr('required');if(!optString)return;var pairs=optString.split(';');for(var it39=0,pair,it39__arr=pairs,it39__len=it39__arr.length;(pair=it39__arr[it39])||it39<it39__len;it39++){if(pair.length==0)continue;var m=pair.match(/^\s*(\w+)\s*:\s*(.*)\s*$/);var key=m[1];var val=m[2];this[key]=val;}});})(FormInput.Base,FormInput);JS2.OO.createClass("FormInput.Ajax");FormInput.Ajax.oo('extends',FormInput.Base);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','needAjax',true);K.oo('method',"keyup",function(){var error=_super(this);var self=this;if(!error){$.idle(function(){self.ajaxValidate();},500);}});K.oo('method',"validated",function(){var result=_super(this);if(!result)return result;if(this.hasAjaxError)return false;return true;});K.oo('method',"ajaxValidate",function(){return this.getError();});})(FormInput.Ajax,FormInput);JS2.OO.createClass("FormInput.Alphanumeric");FormInput.Alphanumeric.oo('extends',FormInput.Base);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','regex',/\s*\w+\s*/);K.oo('member','defaultMessage',"can contain any text");})(FormInput.Alphanumeric,FormInput);JS2.OO.createClass("FormInput.Username");FormInput.Username.oo('extends',FormInput.Ajax);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','min',6);K.oo('member','max',40);K.oo('member','regex',[[/^\S+$/,'username should not contain spaces'],[/^[a-z](?:[\._]?[a-z0-9]+)*$/i,'Your username can only contain letters, numbers, underscores, and dots (.), where dots and underscores can\'t be consecutive or at the end.']]);K.oo('method',"ajaxValidate",function(){if(_super(this)){return;}
var username=this.val().toLowerCase();if(username=='')return;if(this.ajax)this.ajax.abort();var self=this;this.unique=false;this.changeStatus('loading');this.ajax=AJAX.get('/ajax_valid/valid_unique_username.js',{username:username},function(obj){self.checkedUsername(obj,username)});});K.oo('method',"checkedUsername",function(result,username){this.ajax=null;this.unique=result;this.hasAjaxError=!this.unique;if(this.unique){this.changeStatus('pass');}else{this.showError('The username "'+username+'" has already been taken');}
this.toggleSubmit();});})(FormInput.Username,FormInput);JS2.OO.createClass("FormInput.Password");FormInput.Password.oo('extends',FormInput.Base);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','min',4);K.oo('member','max',40);})(FormInput.Password,FormInput);JS2.OO.createClass("FormInput.Integer");FormInput.Integer.oo('extends',FormInput.Base);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','regex',/\s*\d+\s*/);K.oo('member','defaultMessage',"must be a number");})(FormInput.Integer,FormInput);JS2.OO.createClass("FormInput.String");FormInput.String.oo('extends',FormInput.Base);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','defaultMessage',"can contain any text");})(FormInput.String,FormInput);JS2.OO.createClass("FormInput.Date");FormInput.Date.oo('extends',FormInput.Base);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','regex',/\s*\d{4}-\d{2}-\d{2}\s*/);K.oo('member','defaultMessage',"date muste be in yyyy-mm-dd format");})(FormInput.Date,FormInput);JS2.OO.createClass("FormInput.Checkbox");FormInput.Checkbox.oo('extends',FormInput.Base);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('method',"val",function(){return this.jq.is(':checked')?'checked':'';});})(FormInput.Checkbox,FormInput);JS2.OO.createClass("FormInput.RequiredCheckbox");FormInput.RequiredCheckbox.oo('extends',FormInput.Checkbox);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','defaultMessage',"is required");K.oo('method',"registerEvents",function(){_super(this);var self=this;this.jq.click(function(){self.click();});});K.oo('method',"click",function(){var error=this.getError();this.toggleSubmit();return this.showError(error);});K.oo('method',"getError",function(){var error=false;if(this.jq[0].checked==false)error=this.defaultMessage;this.hasError=!!error;return error;});})(FormInput.RequiredCheckbox,FormInput);JS2.OO.createClass("FormInput.Email");FormInput.Email.oo('extends',FormInput.Ajax);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','regex',[[/^([\w.%+\-]+)@((?:[-a-z0-9]+\.)+[a-z]{2,6})$/i,'must be a valid email address (me@mydomain.com)']]);K.oo('method',"ajaxValidate",function(){if(_super(this)){return;}
var email=this.val().toLowerCase();if(this.ajax)this.ajax.abort();var self=this;this.unique=false;this.changeStatus('loading');this.ajax=AJAX.get('/ajax_valid/valid_unique_email.js',{email:email},function(obj){self.checkedEmail(obj,email)});});K.oo('method',"checkedEmail",function(result,email){this.ajax=null;this.unique=result;this.hasAjaxError=!this.unique;if(this.unique){this.changeStatus('pass');}else{this.showError('There is already a Factual account using this email address.<br/><a href="/forgot_account?email='+encodeURIComponent(email)+'">Click here</a> if you need help recovering your username or password.');}
this.toggleSubmit();});})(FormInput.Email,FormInput);JS2.OO.createClass("FormInput.Captcha");FormInput.Captcha.oo('extends',FormInput.Ajax);(function(K,Package){var self=K;var _super=JS2.OO['super'];K.oo('member','min',6);K.oo('member','max',6);K.oo('method',"ajaxValidate",function(){if(_super(this)){return;}
var self=this;this.correct=false;this.changeStatus('loading');var value=this.val().toLowerCase();this.ajax=AJAX.get('/ajax_valid/valid_captcha_by_ajax.js',{captcha:value},function(obj){self.checkedCaptcha(obj,value);});});K.oo('method',"checkedCaptcha",function(result,value){this.ajax=null;this.correct=result;this.hasAjaxError=!this.correct;if(this.correct){this.changeStatus('pass');}else{this.showError('Your captcha is wrong.');}
this.toggleSubmit();});})(FormInput.Captcha,FormInput);