//17c5af8f314ebd13c282992007a17ee6
var JsonForm=new Class({Implements:[Options],options:{timelock:10000,url:null,data:{},method:"post",getData:function(){return{}},validate:function(A){return true},form:null,elementSuccess:null,messageSuccess:"Your changes have been saved.",onComplete:$empty,elementError:null,messageError:"A problem occurred. Please try again later",onError:$empty,onFinish:$empty,bubbleError:false},initialize:function(A){this.setOptions(A);this.initForm()},initForm:function(){el=$(this.options.form);if(!el){return null}this.findForm();if(!this.options.url&&this.form){this.options.url=this.form.action}var A=null;if(el.get("tag")=="form"){A="submit"}else{if(el.get("tag")=="input"){if(el.getProperty("type")=="submit"){A="click"}else{if(el.getProperty("type")=="text"){A="blur"}}}else{if(el.get("tag")=="select"){A="change"}}}if(!A){return }el.addEvent(A,function(B){if(B){new Event(B).stop()}this.submit()}.bind(this))},findForm:function(){if(this.options.noForm){return null}var A=$(this.options.form);if(A.get("tag")=="form"){this.form=A;return true}parent=A.getParent();while($type(parent)=="element"){if(parent.get("tag")=="form"){this.form=parent;return true}parent=parent.getParent()}return false},getData:function(){var B=this.getFormElements(this.form);var C={};var A={};B.each(function(F){var E=F.name;var G=F.get("value");if(G===false||!E||F.disabled){return }if(F.type&&F.type=="radio"&&!F.checked){return }if(F.type&&F.type=="checkbox"){if(F.checked){G=1}else{G=0}}var D=function(H){C[E]=H};if($type(G)=="array"){G.each(D)}else{D(G)}});return C},getFormElements:function(A){A=$(A);if(!A||A.get("tag")!="form"){return[]}return $$(A.getElementsByTagName("input"),A.getElementsByTagName("select"),A.getElementsByTagName("textarea"))},submit:function(A,B){if(this.options.timelock&&this.race&&((new Date().getTime()-this.race)<this.options.timelock)){return null}this.clear();if(!this.options.url){return null}A=A||{};B=B||function(){};this._callback=B;if($type(this.options.data)=="object"){$extend(A,this.options.data)}$extend(A,this.getData.attempt([],this));if($type(this.options.validate)=="function"){if(!this.options.validate(A)){return false}}if($type(this.options.getData)=="function"){$extend(A,this.options.getData())}this.race=new Date().getTime();new Request({url:this.options.url,method:this.options.method,onComplete:this.callback.bind(this)}).send({data:A})},post:function(A,B){this.options.method="post";this.submit(A,B)},get:function(A,B){this.options.method="get";this.submit(A,B)},callback:function(A){this.race=0;try{var B=JSON.decode(A)}catch(D){this.throwErrors(this.options.messageError);if($type(this.options.onError)=="function"){this.options.onError.attempt([B])}else{this.fireEvent("error",[B])}if($type(this.options.onFinish)=="function"){this.options.onFinish.attempt([B])}return null}if(B.error||(B.errors&&B.errors.length>0)){var C=[];if(B.error){C=[B.error]}else{C=B.errors}this.throwErrors(C);if($type(this.options.onError)=="function"){this.options.onError.attempt([B])}else{this.fireEvent("error",[B])}if($type(this.options.onFinish)=="function"){this.options.onFinish.attempt([B])}return null}if($(this.options.elementSuccess)&&this.options.messageSuccess){$(this.options.elementSuccess).setStyle("display","block").empty().adopt($EC("p").set("text",this.options.messageSuccess))}if($type(this.options.onComplete)=="function"){this.options.onComplete.attempt([B])}else{this.fireEvent("onComplete",[B])}if($type(this.options.onFinish)=="function"){this.options.onFinish.attempt([B])}if($type(this._callback)=="function"){this._callback.attempt([B])}},clear:function(){[this.options.elementError,this.options.elementSuccess].each(function(A){var A=$(A);if(!A){return null}A.empty();A.setStyles({display:"none"})})},throwErrors:function(B){if($type(B)!="array"){B=[B]}var A=$(this.options.elementError);if(A){A.empty();B.each(function(C){A.adopt($EC("p").set("text",C),$EC("br"))});A.setStyle("display","block")}else{if(this.options.bubbleError){show_generic_bubble("Sorry!",B.join("\n"))}}}});