var Tapestry={FORM_VALIDATE_EVENT:"tapestry:formvalidate",FORM_PREPARE_FOR_SUBMIT_EVENT:"tapestry:formprepareforsubmit",FORM_PROCESS_SUBMIT_EVENT:"tapestry:formprocesssubmit",FIELD_FORMAT_EVENT:"tapestry:fieldformat",FIELD_VALIDATE_EVENT:"tapestry:fieldvalidate",FOCUS_CHANGE_EVENT:"tapestry:focuschange",DEBUG_ENABLED:false,CONSOLE_DURATION:10,onDOMLoaded:function(a){document.observe("dom:loaded",a)},onDomLoadedCallback:function(){Tapestry.ScriptManager.initialize();$$(".t-invisible").each(function(a){a.hide();a.removeClassName("t-invisible")});$$("INPUT","SELECT","TEXTAREA").each(function(b){var a=$T(b);if(!a.observingFocusChange){b.observe("focus",function(){if(b!=Tapestry.currentFocusField){document.fire(Tapestry.FOCUS_CHANGE_EVENT,b);Tapestry.currentFocusField=b}});a.observingFocusChange=true}});$$("INPUT[type=submit]").each(function(b){var a=$T(b);if(!a.trackingClicks){b.observe("click",function(){$T(b.form).lastSubmit=b});a.trackingClicks=true}})},init:function(a){$H(a).each(function(d){var b=d.key;var c=Tapestry.Initializer[b];if(c==undefined){Tapestry.error("Function Tapestry.Initializer.#{name}() does not exist.",{name:b});return}d.value.each(function(e){if(!Object.isArray(e)){e=[e]}c.apply(this,e)})})},error:function(b,a){Tapestry.updateConsole("t-err",b,a)},warn:function(b,a){Tapestry.updateConsole("t-warn",b,a)},debug:function(b,a){if(Tapestry.DEBUG_ENABLED){Tapestry.updateConsole("t-debug",b,a)}},updateConsole:function(a,c,b){if(b!=undefined){c=c.interpolate(b)}if(Tapestry.console==undefined){Tapestry.console=Tapestry.createConsole("t-console")}Tapestry.writeToConsole(Tapestry.console,a,c)},createConsole:function(c){var a=$$("BODY").first();var b=new Element("div",{"class":c});a.insert({top:b});return b},writeToConsole:function(a,c,d,f){var e=new Element("div",{"class":c}).update(d).hide();a.insert({top:e});new Effect.Appear(e,{duration:0.25});var b=new Effect.Fade(e,{delay:Tapestry.CONSOLE_DURATION,afterFinish:function(){e.remove()}});e.observe("click",function(){b.cancel();e.remove()})},updateAjaxConsole:function(a,c,b){if(Tapestry.ajaxConsole==undefined){Tapestry.ajaxConsole=Tapestry.createConsole("t-ajax-console")}if(b!=undefined){c=c.interpolate(b)}Tapestry.writeToConsole(Tapestry.ajaxConsole,a,c)},loadScriptsInReply:function(reply,callback){var redirectURL=reply.redirectURL;if(redirectURL){window.location.pathname=redirectURL;return}Tapestry.ScriptManager.addStylesheets(reply.stylesheets);Tapestry.ScriptManager.addScripts(reply.scripts,function(){callback.call(this);if(reply.script){eval(reply.script)}Tapestry.onDomLoadedCallback()})},ajaxFailureHandler:function(a){var b=a.getHeader("X-Tapestry-ErrorMessage");Tapestry.ajaxError("Communication with the server failed: "+b);Tapestry.debug("Ajax failure: Status #{status} for #{request.url}: "+b,a)},ajaxError:function(b,a){Tapestry.updateAjaxConsole("t-err",b,a)},ajaxRequest:function(b,a){return new Ajax.Request(b,{onSuccess:function(c,d){if(!c.request.success()){Tapestry.ajaxError("Server request was unsuccesful. There may be a problem accessing the server.");return}try{a.call(this,c,d)}catch(f){Tapestry.ajaxError("Client exception processing response: "+f)}},onException:Tapestry.ajaxFailureHandler,onFailure:Tapestry.ajaxFailureHandler})},findZoneManager:function(d){var b=$T(d).zoneId;var a=$(b);if(!a){Tapestry.ajaxError("Unable to locate Ajax Zone '#{id}' for dynamic update.",{id:b});return null}var c=$T(a).zoneManager;if(!c){Tapestry.ajaxError("Ajax Zone '#{id}' does not have an associated Tapestry.ZoneManager object.",{id:b});return null}return c},rebuildURL:function(c){if(c.match(/^https?:/)){return c}if(c.startsWith("/")){var a=window.location;return a.protocol+"//"+a.host+c}var b=this.stripToLastSlash(window.location.href);while(true){if(c.startsWith("../")){b=this.stripToLastSlash(b.substr(0,b.length-1));c=c.substring(3);continue}if(c.startsWith("./")){c=c.substr(2);continue}return b+c}},stripToLastSlash:function(a){var b=a.lastIndexOf("/");return a.substring(0,b+1)}};Element.addMethods({isDeepVisible:function(a){var b=$(a);while(true){if(!b.visible()){return false}if(b.tagName=="FORM"){break}b=$(b.parentNode)}return true}});Element.addMethods("FORM",{getFormEventManager:function(c){c=$(c);var b=$T(c);var a=b.formEventManager;if(a==undefined){a=new Tapestry.FormEventManager(c);b.formEventManager=a}return a},sendAjaxRequest:function(d,c,b){d=$(d);b=Object.clone(b||{});b.onFailure|=Tapestry.ajaxFailureHandler;var e=d.getElements().reject(function(g){return g.tagName=="INPUT"&&g.type=="submit"});var f=Form.serializeElements(e,true);var a=$T(d).lastSubmit;if(a&&a.name){f[a.name]=$F(a)}Object.extend(f,b.parameters);b.parameters=f;return new Ajax.Request(c,b)}});Element.addMethods(["INPUT","SELECT","TEXTAREA"],{getFieldEventManager:function(c){c=$(c);var b=$T(c);var a=b.fieldEventManager;if(a==undefined){a=new Tapestry.FieldEventManager(c);b.fieldEventManager=a}return a},showValidationMessage:function(a,b){a=$(a);$T(a).validationError=true;$T(a.form).validationError=true;a.getFieldEventManager().showValidationMessage(b);return a},removeDecorations:function(a){$(a).getFieldEventManager().removeDecorations();return a},addValidatorAsObserver:function(c,a,b){c.observe(a,function(e){try{b.call(this,$F(c))}catch(d){c.showValidationMessage(d)}});return c},addValidator:function(b,a){return b.addValidatorAsObserver(Tapestry.FIELD_VALIDATE_EVENT,a)},addFormatValidator:function(b,a){return b.addValidatorAsObserver(Tapestry.FIELD_FORMAT_EVENT,a)}});Tapestry.Initializer={ajaxFormLoop:function(a){var b=$(a.rowInjector);$(a.addRowTriggers).each(function(c){$(c).observe("click",function(d){$(b).trigger();Event.stop(d)})})},formLoopRemoveLink:function(a){var b=$(a.link);var c=a.fragment;b.observe("click",function(e){Event.stop(e);var d=function(i){var f=$(c);var g=$T(f).formFragment;if(g!=undefined){g.hideAndRemove()}else{var h=Tapestry.ElementEffect.fade(f);h.options.afterFinish=function(){f.remove()}}};Tapestry.ajaxRequest(a.url,d)})},linkZone:function(c,a,b){c=$(c);$T(c).zoneId=a;if(c.tagName=="FORM"){c.getFormEventManager().preventSubmission=true;c.observe(Tapestry.FORM_PROCESS_SUBMIT_EVENT,function(){var d=Tapestry.findZoneManager(c);if(!d){return}var e=function(f){d.processReply(f.responseJSON)};c.sendAjaxRequest(b,{onSuccess:e})});return}c.observe("click",function(e){Event.stop(e);var d=Tapestry.findZoneManager(c);if(!d){return}d.updateFromURL(b)})},validate:function(a,b){a=$(a);$(a.form).getFormEventManager();$(a).getFieldEventManager();b.each(function(c){var d=c[0];var e=c[1];var f=c[2];var g=Tapestry.Validator[d];if(g==undefined){Tapestry.error("Function Tapestry.Validator.#{name}() does not exist for field '#{fieldName}'.",{name:d,fieldName:a.id});return}g.call(this,a,e,f)})},zone:function(a){new Tapestry.ZoneManager(a)},formFragment:function(a){new Tapestry.FormFragment(a)},formInjector:function(a){new Tapestry.FormInjector(a)},linkTriggerToFormFragment:function(a,b){a=$(a);if(a.type=="radio"){$(a.form).observe("click",function(){$T(b).formFragment.setVisible(a.checked)});return}a.observe("click",function(){$T(b).formFragment.setVisible(a.checked)})}};if(window.console&&!Prototype.Browser.WebKit){var createlog=function(a){return function(c,b){if(b!=undefined){c=c.interpolate(b)}a.call(this,c)}};Tapestry.error=createlog(window.console.error);Tapestry.warn=createlog(window.console.warn);Tapestry.debug=createlog(window.console.debug)}Tapestry.Validator={INT_REGEXP:/^(\+|-)?\d+$/,FLOAT_REGEXP:/^(\+|-)?((\.\d+)|(\d+(\.\d*)?))$/,required:function(b,a){b.addFormatValidator(function(c){if(c.strip()==""){throw a}})},integernumber:function(b,a){b.addFormatValidator(function(c){if(c!=""&&!c.match(Tapestry.Validator.INT_REGEXP)){throw a}})},decimalnumber:function(b,a){b.addFormatValidator(function(c){if(c!=""&&!c.match(Tapestry.Validator.FLOAT_REGEXP)){throw a}})},minlength:function(c,b,a){c.addValidator(function(d){if(d.length<a){throw b}})},maxlength:function(c,b,a){c.addValidator(function(d){if(d.length>a){throw b}})},min:function(c,a,b){c.addValidator(function(d){if(d<b){throw a}})},max:function(c,a,b){c.addValidator(function(d){if(d>b){throw a}})},regexp:function(d,a,c){var b=new RegExp(c);d.addValidator(function(e){if(!b.test(e)){throw a}})}};Tapestry.ErrorPopup=Class.create({BUBBLE_VERT_OFFSET:-34,BUBBLE_HORIZONTAL_OFFSET:-20,BUBBLE_WIDTH:"auto",BUBBLE_HEIGHT:"39px",initialize:function(b){this.field=$(b);this.innerSpan=new Element("span");this.outerDiv=$(new Element("div",{id:this.field.id+":errorpopup","class":"t-error-popup"})).update(this.innerSpan).hide();var a=$$("BODY").first();a.insert({bottom:this.outerDiv});this.outerDiv.absolutize();this.outerDiv.observe("click",function(c){this.ignoreNextFocus=true;this.stopAnimation();this.outerDiv.hide();this.field.activate();Event.stop(c)}.bindAsEventListener(this));this.queue={position:"end",scope:this.field.id};Event.observe(window,"resize",this.repositionBubble.bind(this));document.observe(Tapestry.FOCUS_CHANGE_EVENT,function(c){if(this.ignoreNextFocus){this.ignoreNextFocus=false;return}if(c.memo==this.field){this.fadeIn();return}this.fadeOut()}.bind(this))},showMessage:function(a){this.stopAnimation();this.innerSpan.update(a);this.hasMessage=true;this.fadeIn()},repositionBubble:function(){var a=this.field.cumulativeOffset();this.outerDiv.setStyle({top:(a[1]+this.BUBBLE_VERT_OFFSET)+"px",left:(a[0]+this.BUBBLE_HORIZONTAL_OFFSET)+"px",width:this.BUBBLE_WIDTH,height:this.BUBBLE_HEIGHT})},fadeIn:function(){if(!this.hasMessage){return}this.repositionBubble();if(this.animation){return}this.animation=new Effect.Appear(this.outerDiv,{queue:this.queue,afterFinish:function(){this.animation=null;if(this.field!=Tapestry.currentFocusField){this.fadeOut()}}.bind(this)})},stopAnimation:function(){if(this.animation){this.animation.cancel()}this.animation=null},fadeOut:function(){if(this.animation){return}this.animation=new Effect.Fade(this.outerDiv,{queue:this.queue,afterFinish:function(){this.animation=null}.bind(this)})},hide:function(){this.hasMessage=false;this.stopAnimation();this.outerDiv.hide()}});Tapestry.FormEventManager=Class.create({initialize:function(a){this.form=$(a);this.form.onsubmit=this.handleSubmit.bindAsEventListener(this)},handleSubmit:function(c){var a=$T(this.form);a.validationError=false;var b=null;this.form.getElements().each(function(f){var e=$T(f).fieldEventManager;if(e!=undefined){var d=e.validateInput();if(d&&!b){b=f}}});this.form.fire(Tapestry.FORM_VALIDATE_EVENT,this.form);if(a.validationError){Event.stop(c);if(b){b.activate()}a.lastSubmit=null;return false}this.form.fire(Tapestry.FORM_PREPARE_FOR_SUBMIT_EVENT,this.form);if(this.preventSubmission){Event.stop(c);this.form.fire(Tapestry.FORM_PROCESS_SUBMIT_EVENT);return false}return true}});Tapestry.FieldEventManager=Class.create({initialize:function(a){this.field=$(a);var b=this.field.id;this.label=$(b+":label");this.icon=$(b+":icon");document.observe(Tapestry.FOCUS_CHANGE_EVENT,function(c){if(Tapestry.currentFocusField==this.field&&this.field.form==c.memo.form){this.validateInput()}}.bindAsEventListener(this))},removeDecorations:function(){this.field.removeClassName("t-error");if(this.label){this.label.removeClassName("t-error")}if(this.icon){this.icon.hide()}if(this.errorPopup){this.errorPopup.hide()}},showValidationMessage:function(a){this.field.addClassName("t-error");if(this.label){this.label.addClassName("t-error")}if(this.icon){if(!this.icon.visible()){new Effect.Appear(this.icon)}}if(this.errorPopup==undefined){this.errorPopup=new Tapestry.ErrorPopup(this.field)}this.errorPopup.showMessage(a)},validateInput:function(){if(this.field.disabled){return}if(!this.field.isDeepVisible()){return}var a=$T(this.field);a.validationError=false;this.field.fire(Tapestry.FIELD_FORMAT_EVENT,this.field);if(!a.validationError){var b=$F(this.field);if(b!=""){this.field.fire(Tapestry.FIELD_VALIDATE_EVENT,this.field)}}if(!a.validationError){this.field.removeDecorations()}return a.validationError}});Tapestry.ElementEffect={show:function(a){return new Effect.Appear(a)},highlight:function(a){return new Effect.Highlight(a)},slidedown:function(a){return new Effect.SlideDown(a)},slideup:function(a){return new Effect.SlideUp(a)},fade:function(a){return new Effect.Fade(a)}};Tapestry.ZoneManager=Class.create({initialize:function(a){if(Object.isString(a)){a={element:a}}this.element=$(a.element);this.showFunc=Tapestry.ElementEffect[a.show]||Tapestry.ElementEffect.show;this.updateFunc=Tapestry.ElementEffect[a.update]||Tapestry.ElementEffect.highlight;$T(this.element).zoneManager=this;var b=this.element.select(".t-zone-update");this.updateElement=b.first()||this.element},show:function(b){this.updateElement.update(b);var a=this.element.visible()?this.updateFunc:this.showFunc;a.call(this,this.element)},processReply:function(a){Tapestry.loadScriptsInReply(a,function(){this.show(a.content)}.bind(this))},updateFromURL:function(a){var b=function(c){this.processReply(c.responseJSON)}.bind(this);Tapestry.ajaxRequest(a,b)}});Tapestry.FormFragment=Class.create({initialize:function(a){if(Object.isString(a)){a={element:a}}this.element=$(a.element);$T(this.element).formFragment=this;this.hidden=$(a.element+":hidden");this.showFunc=Tapestry.ElementEffect[a.show]||Tapestry.ElementEffect.slidedown;this.hideFunc=Tapestry.ElementEffect[a.hide]||Tapestry.ElementEffect.slideup;var b=$(this.hidden.form);b.getFormEventManager();$(b).observe(Tapestry.FORM_PREPARE_FOR_SUBMIT_EVENT,function(){if(!this.element.isDeepVisible()){this.hidden.value=""}}.bind(this))},hide:function(){if(this.element.visible()){this.hideFunc(this.element)}},hideAndRemove:function(){var a=this.hideFunc(this.element);a.options.afterFinish=function(){this.element.remove()}.bind(this)},show:function(){if(!this.element.visible()){this.showFunc(this.element)}},toggle:function(){this.setVisible(!this.element.visible())},setVisible:function(a){if(a){this.show();return}this.hide()}});Tapestry.FormInjector=Class.create({initialize:function(a){this.element=$(a.element);this.url=a.url;this.below=a.below;this.showFunc=Tapestry.ElementEffect[a.show]||Tapestry.ElementEffect.highlight;this.element.trigger=function(){var b=function(f){var c=f.responseJSON;var e=new Element(this.element.tagName,{"class":this.element.className});var d={};d[this.below?"after":"before"]=e;Tapestry.loadScriptsInReply(c,function(){this.element.insert(d);e.update(c.content);e.id=c.elementId;this.showFunc(e)}.bind(this))}.bind(this);Tapestry.ajaxRequest(this.url,b);return false}.bind(this)}});Tapestry.ScriptLoadMonitor=Class.create({initialize:function(a,c){this.callback=c;this.loaded=0;this.toload=a.length;var b=this;a.each(function(e){if(Prototype.Browser.IE){var d=false;e.onreadystatechange=function(){if(!d&&(this.readyState=="loaded"||this.readyState=="complete")){d=true;b.loadComplete(e)}}}else{e.onload=b.loadComplete.bindAsEventListener(b,e)}});if(this.toload==0){this.callback.call(this)}},loadComplete:function(){this.loaded++;if(this.loaded==this.toload){this.callback.call(this)}}});Tapestry.ScriptManager={initialize:function(){this.emulated=false;if(!document.scripts){this.emulated=true;document.scripts=new Array();$$("script").each(function(a){document.scripts.push(a)})}},contains:function(b,c,a){return $A(b).any(function(e){var f=e[c];if(f.blank()){return false}var d=Prototype.Browser.IE?Tapestry.rebuildURL(f):f;return d==a});return false},addScripts:function(a,e){var c=new Array();if(a){var d=this.emulated;var b=$$("head").first();a.each(function(h){var g=Tapestry.rebuildURL(h);if(Tapestry.ScriptManager.contains(document.scripts,"src",g)){return}var f=new Element("script",{src:g,type:"text/javascript"});b.insert({bottom:f});c.push(f);if(d){document.scripts.push(f)}})}new Tapestry.ScriptLoadMonitor(c,e)},addStylesheets:function(b){if(!b){return}var a=$$("head").first();$(b).each(function(e){var d=Tapestry.rebuildURL(e.href);if(Tapestry.ScriptManager.contains(document.styleSheets,"href",d)){return}var c=new Element("link",{type:"text/css",rel:"stylesheet",href:d});if(e.media!=undefined){c.writeAttribute("media",e.media)}a.insert({bottom:c})})}};function $T(b){var c=$(b);var a=c._tapestry;if(!a){a={};c._tapestry=a}return a}Tapestry.onDOMLoaded(Tapestry.onDomLoadedCallback);
