var BaseSignupForm=new Class({Extends:Form,initialize:function(){this.dialog=$("signup-dialog");this.element={name:new Field("signup-name",this.dialog),email:new Field("signup-email",this.dialog),password:new Field("signup-password",this.dialog),password_confirm:new Field(["signup-password-confirm","signup-password"],this.dialog),acept_user:new Field('acept-user',this.dialog)}
this.element.name.addError("CRuleNotEmpty").addRule(new CRuleNotEmpty("signup-name"));this.element.email.addError("CRuleEmail").addRule(new CRuleEmail("signup-email"));this.element.email.addError("ExDbUnique");this.element.password.addError("CRuleNotEmpty").addRule(new CRuleNotEmpty("signup-password",6));if(!$defined(short_signup_form)||false==short_signup_form){this.element.password_confirm.addError("CRulePasswordMatch").addRule(new CRulePasswordMatch("signup-password-confirm","signup-password"));}
this.element.acept_user.addError("CRuleNotEmpty").addRule(new CRuleNotEmpty("acept-user"));this._initButtons();},_initButtons:function(){this.button={signup:this.dialog.getElement("#signup-button")}
var self=this;this.button.signup.addEvent("click",function(){self.onSignup();});this.initBindings();},initBindings:function(){var self=this;this.field("name").dom.input.addEvent("blur",function(){self.field("name").validate();});this.field("email").dom.input.addEvent("blur",function(){self.field("email").validate();});this.field("password").dom.input.addEvent("blur",function(){self.field("password").validate();});this.field("password_confirm").dom.input.addEvent("blur",function(){self.field("password_confirm").validate();});},onSignup:function(){this.hideErrors();if(this.validate()==true){var self=this;var data=this.storeToHash();$URL.ModUser.onSignup({data:data,onComplete:function(json){self.onSignup_cb(json);}}).send();}},onSignup_cb:function(response){return response.status=="OK"?this._statusOk(response):this._statusError(response);},storeToHash:function(){var hash=this.parent();var role=$ROLES.CUSTOMER;hash.merge(new Hash({role:role}));return hash;}});var SignupForm=new Class({Extends:BaseSignupForm,initialize:function(){this.parent();this.element['role']=new Field("role-user",this.dialog);this.element['learn_about']=new Field("learn-about",this.dialog);this.element['phone_code']=new Field("phone-code",this.dialog);this.element['phone']=new Field("phone",this.dialog);var self=this;this.field("role").dom.input.addEvent("change",function(){self.togglePhone();});this.field("learn_about").addError("CRuleNotEmpty").addRule(new CRuleNotEmpty("learn-about"));this.message={signup_complete:$("signup-complete-message")};},togglePhone:function(){var style=this._isAuthor()?"block":"none";$("phone_field").setStyle("display",style);},_isAuthor:function(){return this.field("role").dom.input.checked;},onSignup:function(){var self=this;this.hideErrors();var acept_user=this.field('acept_user').dom.input.get('checked');if(this._isAuthor()==true){this.field("phone_code").addError("CRuleNotEmpty").addRule(new CRuleNotEmpty("phone-code"));this.field("phone_code").addError("CRuleNumberFormat").addRule(new CRuleNumberFormat("phone-code"));this.field("phone").addError("CRuleNotEmpty").addRule(new CRuleNotEmpty("phone"));this.field("phone").addError("CRuleNumberFormat").addRule(new CRuleNumberFormat("phone"));}else{this.field("phone").removeErrors();this.field("phone_code").removeErrors();}
if(this.validate()==true&&acept_user){var data=this.storeToHash();$URL.ModUser.onSignup({data:data,onComplete:function(json){self.onSignup_cb(json);}}).send();}
if(!acept_user){this.field('acept_user').error('CRuleNotEmpty').show();}},_statusOk:function(json){$getInstance($Application).location(json.redirect);},_statusError:function(json){if(json.error.class_name==$E.ExValidateFails){this.showErrors(json);}else if(json.error.class_name==$E.ExDbUnique){this.field("email").error("ExDbUnique").show();}},hideErrors:function(){this.parent();}});var ShowWorkSignupForm=new Class({Extends:BaseSignupForm,initialize:function(){this.parent();this.events=[];},addEvent:function(fn){this.events.push(fn);},_statusOk:function(json){this.events.each(function(fn){fn();});},_statusError:function(response){if(response.error.class_name==$E.ExValidateFails){return this.showErrors(response);}
if(response.error.class_name==$E.ExDbUnique){this.field("email").error($E.ExDbUnique).show();}}});