/*
Copyright (c) 2007-2008 the OTHER media Limited
Licensed under the BSD license, http://ojay.othermedia.org/license.html
*/
// @require ojay/core-min
// @require ojay/pkg/http-min
(function(){JS.extend(Ojay,{isBlank:function(a){return a?false:(String(a).trim()=='')},isNumeric:function(a){return this.NUMBER_FORMAT.test(String(a))},isEmailAddress:function(a){return this.EMAIL_FORMAT.test(String(a))},NUMBER_FORMAT:/^\-?(0|[1-9]\d*)(\.\d+)?(e[\+\-]?\d+)?$/i,EMAIL_FORMAT:/^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b$/i});Ojay.Forms=function(a){a.call(w)};var i=[];JS.extend(Ojay.Forms,{getLabel:function(b){b=Ojay(b);if(!b.node)return Ojay();var c=b.ancestors('label');if(c.node)return c.at(0);var d=b.node.id;c=[].filter.call(document.getElementsByTagName('label'),function(a){return d&&a.htmlFor==d});return Ojay(c.slice(0,1))},getQueryString:function(a){var b=YAHOO.util.Connect.setForm(Ojay(a).node);YAHOO.util.Connect.resetFormState();return b},getData:function(d){return this.getQueryString(d).split('&').reduce(function(a,b){var c=b.split('=').map(decodeURIComponent).map('trim');if(a[c[0]]===undefined)a[c[0]]=c[1];return a},{})},setValue:function(b,c){var d,e,b=Ojay(b);switch(true){case b.every({matches:'[type=radio]'}):d=b.map('node').filter({value:c})[0];if(!d)return;b.setAttributes({checked:false});d.checked=true;break;case b.matches('[type=checkbox]'):b.node.checked=!!(c===true||c==b.node.value);break;case b.matches('select'):e=Array.from(b.node.options);d=e.filter({value:c})[0];if(!d)return;e.forEach(function(a){a.selected=false});d.selected=true;break;case b.matches('input'):case b.matches('[type=hidden]'):case b.matches('textarea'):b.node.value=String(c);break}}.curry(),reattach:function(){var a=0;for(var b in h){if(h[b]._d())++a}return a},update:function(){i.forEach(function(a){if(a.isA(Ojay.Forms.Select))a._k();else a.setChecked()})}});var u=new JS.Class({include:JS.Observable,initialize:function(a){this._T=a;this._m={};this._d();this._9={};this._x=[];this._z=[];this._D=[];this._h=new k(this);this._K=new s(this);this._J=new r(this)},_d:function(){if(this._F())return false;this._f={};this._E={};this._e={};this._1=Ojay.byId(this._T);if(!this._F())return false;this._1.on('submit',this.method('_L'));for(var a in this._9)this._9[a]._d();return true},_F:function(){return this._1&&this._1.matches('body form')},_N:function(a){return this._9[a]||(this._9[a]=new x(this,a))},_L:function(b,c){var d=this._P();if(this._y||!d)c.stopDefault();if(!this._y||!d)return;var b=this._1.node;Ojay.HTTP[(b.method||'POST').toUpperCase()](b.action,this._11,{onSuccess:function(a){this._D.forEach({call:[null,a]})}.bind(this)})},_b:function(a){if(this._f[a])return this._f[a];var b=this._1.descendants('input, textarea, select');if(a)b=b.filter(function(element){return element.node.name==a});return this._f[a]=b},_j:function(a){if(a.node)a=a.node;if(a.name)a=a.name;return this._E[a]||(this._E[a]=Ojay.Forms.getLabel(this._b(a)))},_U:function(d){if(this._e[d])return this._e[d];if(this._m[d])return this._e[d]=this._m[d];var e=this._j(d);var f=((e.node||{}).innerHTML||d).stripTags();f=f.replace(/(\w)[_-](\w)/g,'$1 $2').replace(/([a-z])([A-Z])/g,function(a,b,c){return b+' '+c.toLowerCase()});return this._e[d]=f.charAt(0).toUpperCase()+f.substring(1)},_W:function(){return this._11=Ojay.Forms.getData(this._1)},_M:function(){this._a=new o(this);var b=this._W(),c,d;this._z.forEach(function(a){a(b)});for(c in b)Ojay.Forms.setValue(this._b(c),b[c]);Ojay.Forms.update();b=new p(b);for(c in this._9)this._9[c]._O(b.get(c),b);this._x.forEach(function(a){a(b,this._a)},this);var e=this._a._10();for(c in this._f)[this._b(c),this._j(c)].forEach(it()[e.indexOf(c)==-1?'removeClass':'addClass']('invalid'));this.notifyObservers(this)},_P:function(){this._M();return this._a._Z()===0},_X:function(){this._b('').forEach(function(a){a.on('focus').addClass('focused')._(this)._j(a).addClass('focused');a.on('blur').removeClass('focused')._(this)._j(a).removeClass('focused')},this)}});var q=function(a){return!Ojay.isBlank(a)||['must not be blank']};var x=new JS.Class({initialize:function(a,b){this._1=a;this._o=b;this._n=[];this._h=new m(this);this._d()},_d:function(){this._2=this._1._b(this._o)},_4:function(a){this._n.push(a)},_O:function(e,f){var g=[],t=this._n.length?this._n:[q],e=e||'';t.forEach(function(a){var b=a(e,f),c,d;if(b!==true){c=b[0];d=b[1]||this._o;this._1._a.register(this._o);this._1._a.add(d,c)}},this);return g.length?g:true}});var p=new JS.Class({initialize:function(b){this.get=function(a){return b[a]===undefined?null:b[a]}}});var o=new JS.Class({initialize:function(e){var f={},g=[];this.register=function(a){f[a]=f[a]||[];return this};this.add=function(a,b){this.register(a);if(f[a].indexOf(b)==-1)f[a].push(b);return this};this.addToBase=function(a){g.push(a);return this};this._Z=function(){var a=g.length;for(var b in f)a+=f[b].length;return a};this._S=function(){var b,c=g.map(function(a){return{field:null,message:a}});for(var d in f){b=e._U(d);f[d].forEach(function(a){c.push({field:d,message:b+' '+a})})}return c};this._10=function(){var a=[];for(var b in f)a.push(b);return a}}});var h={};var j=function(a){return h[a]||(h[a]=new u(a))};var w={form:function(a){return j(a)._h||null},when:function(a){return j(a)._K||null},before:function(a){return j(a)._J||null},displayErrorsIn:function(g){return function(c){g=g.setContent?g:Ojay(g);var d=c.length;if(d==0)return g.setContent('');var e=(d==1)?'was':'were',f=(d==1)?'':'s';g.setContent(Ojay.HTML.div({className:'error-explanation'},function(b){b.p('There '+e+' '+d+' error'+f+' with the form:');b.ul(function(a){c.map('message').forEach(a.method('li'))})}))}},displayResponseIn:function(b){return function(a){a.insertInto(b)}},EMAIL_FORMAT:Ojay.EMAIL_FORMAT};var k=new JS.Class({initialize:function(a){this._1=a},requires:function(a,b){var c=this._1._N(a);if(b)this._1._m[a]=b;return c._h},validates:function(a){this._1._x.push(a);return this},submitsUsingAjax:function(a){this._1._y=true;return this},highlightsActiveField:function(){this._1._X();return this}});k.include({expects:k.prototype.requires});var v=['requires','expects','validates','submitsUsingAjax','highlightsActiveField'];var m=new JS.Class({initialize:function(a){this._3=a},toBeChecked:function(c){var d=this._3;this._3._4(function(a){var b=d._2.node;return(a==b.value&&b.checked)||[c||'must be checked']});return this},toBeNumeric:function(b){this._3._4(function(a){return Ojay.isNumeric(a)||[b||'must be a number']});return this},toBeOneOf:function(b,c){this._3._4(function(a){return b.indexOf(a)!=-1||[c||'is not valid']});return this},toBeNoneOf:function(b,c){this._3._4(function(a){return b.indexOf(a)==-1||[c||'is not valid']});return this},toBePresent:function(b){this._3._4(function(a){return!Ojay.isBlank(a)||[b||'must not be blank']});return this},toConfirm:function(c,d){this._3._4(function(a,b){return a==b.get(c)||[d||'must be confirmed',c]});return this},toHaveLength:function(b,c){var d=b.minimum,e=b.maximum;this._3._4(function(a){return(typeof b=='number'&&a.length!=b&&[c||'must contain exactly '+b+' characters'])||(d!==undefined&&a.length<d&&[c||'must contain at least '+d+' characters'])||(e!==undefined&&a.length>e&&[c||'must contain no more than '+e+' characters'])||true});return this},toHaveValue:function(b,c){var d=b.minimum,e=b.maximum;this._3._4(function(a){if(!Ojay.isNumeric(a))return[c||'must be a number'];a=Number(a);return(d!==undefined&&a<d&&[c||'must be at least '+d])||(e!==undefined&&a>e&&[c||'must not be greater than '+e])||true});return this},toMatch:function(b,c){this._3._4(function(a){return b.test(a)||[c||'is not valid']});return this}});m.include(v.reduce(function(b,c){b[c]=function(){var a=this._3._1._h;return a[c].apply(a,arguments)};return b},{}));var s=new JS.Class({initialize:function(a){this._1=a},isValidated:function(b,c){this._1.subscribe(function(a){b.call(c||null,a._a._S())})},responseArrives:function(a,b){a=Function.from(a);if(b)a=a.bind(b);this._1._D.push(a)}});var r=new JS.Class({initialize:function(a){this._1=a},isValidated:function(a){this._1._z.push(a)}});var l=new JS.Module({include:Ojay.Observable,_B:function(){var a=Ojay(Ojay.HTML.span()).setStyle({position:'relative'});this._0.insert(a.node,'before');a.insert(this._0.node,'bottom');this._0.setStyle({position:'absolute',left:'-5000px',top:0});this._0.on('focus')._(this).setFocused(true);this._0.on('blur')._(this).setFocused(false);this._5=Ojay.Forms.getLabel(this._0);if(this._5.node)this._5.addClass(this._l);this._7=[this._0,this._5];if(this.getHTML)this._7.push(this.getHTML());this._7.forEach(it().on('mouseover')._(this).setHovered(true));this._7.forEach(it().on('mouseout')._(this).setHovered(false));this._7.forEach(it().addClass('js'));this.setDisabled()},setFocused:function(a){if(this._0.node.checked)this.setChecked();this._g(a,'focused');return this},setHovered:function(a){this._g(a,'hovered');return this},setDisabled:function(a){this.disabled=(a===undefined)?this._0.node.disabled:!!a;this._0.node.disabled=this.disabled;this._g(this.disabled,'disabled');return this},_g:function(b,c){this._6=this._6||[];if(b){if(this._6.indexOf(c)==-1)this._6.push(c);this._6.sort()}else{this._6=this._6.filter(function(a){return a!=c})}this._7.forEach(it()[b?'addClass':'removeClass'](c));var d=this._7[0].node.className.split(/\s+/);var e=this._l,f=new RegExp('^'+e+'-');var g=d.filter({match:f})[0];if(g)this._7.forEach({removeClass:g});if(this._6.length)this._7.forEach({addClass:e+'-'+this._6.join('-')})}});var n=new JS.Module({include:l,_H:function(){this._B();this._0.on('click')._(this).setChecked()._(this._0.node).focus();this.setChecked()},setChecked:function(a,b){var c=!!this.checked;this.checked=(a===undefined)?this._0.node.checked:!!a;if(this._G){if(this.checked){this._0.node.checked=true;this._G._I(this,b)}}else{this._0.node.checked=this.checked;if(b!==false&&c!=this.checked)this.notifyObservers('change')}this._g(this.checked,'checked');return this},isChecked:function(){return!!this.checked}});JS.MethodChain.addMethod('focus');Ojay.Forms.RadioButtons=new JS.Class({include:Ojay.Observable,initialize:function(b){this._8=Ojay(b).map(function(a){return new this.klass.Item(this,a)},this);if(this._8.map('_0.node.name').unique().length>1)throw new Error('Attempt to create a RadioButtons object with radios of different names');this._q=this._8.filter('checked')[0]||null},_I:function(a,b){var c=this._q;if(c&&c!=a)c.setChecked(false);if(b!==false&&c!=a)this.notifyObservers('change');this._q=a},getItem:function(b){return this._8.filter(function(a){return a._0.node.id==b||a._0.node.value==b})[0]},getInput:function(){return Ojay(this._8.map('_0.node'))},getLabel:function(){return Ojay(this._8.map('_5.node'))},getValue:function(){var a=this._8.filter('_0.node.checked')[0];return a?a._0.node.value:null},setValue:function(a,b){var c=this.getItem(a);if(c)c.setChecked(true,b);return this},extend:{Item:new JS.Class({include:n,_l:'radio',initialize:function(a,b){i.push(this);if(!b||!b.node||b.node.type!='radio')throw new TypeError('Attempt to create a RadioButtons object with non-radio element');this._G=a;this._0=b;this._H()}})}});Ojay.Forms.Checkbox=new JS.Class({include:n,_l:'checkbox',initialize:function(a){i.push(this);this._0=Ojay(a);if(!this._0||!this._0.node||this._0.node.type!='checkbox')throw new TypeError('Attempt to create a Checkbox object with non-checkbox element');this._H()},getInput:function(){return this._0},getLabel:function(){return this._5}});Ojay.Forms.Checkbox.include({getValue:Ojay.Forms.Checkbox.prototype.isChecked,setValue:Ojay.Forms.Checkbox.prototype.setChecked});Ojay.Forms.Select=new JS.Class({include:[JS.State,l],_l:'select',extend:{CONTAINER_CLASS:'select-container',DISPLAY_CLASS:'select-display',BUTTON_CLASS:'select-button',LIST_CLASS:'select-list',Option:new JS.Class({initialize:function(a,b){this._C=a;this._12=Ojay(b);this.value=b.value||'';this._5=b.text.stripTags();this.hovered=false;var c=this.getHTML();[c.on('mouseover'),c.on('mousemove')].forEach(it()._(this).setHovered(true))},getHTML:function(){if(this._A)return this._A;return this._A=Ojay(Ojay.HTML.li(this._5))},setHovered:function(a){this.hovered=(a!==false);if(this.hovered){this._C._Q(this);this._R()}this.getHTML()[a===false?'removeClass':'addClass']('hovered');return this},_R:function(){var a=this._C._2._c;var b=a.getRegion(),c=this.getHTML().getRegion();if(b.contains(c))return;var d=a.node.scrollTop||0,e=(c.top>b.top)?'bottom':'top';a.node.scrollTop=d+c[e]-b[e]}})},initialize:function(d){i.push(this);this._0=Ojay(d);if(!this._0||this._0.node.tagName.toLowerCase()!='select')throw new TypeError('Attempt to create a Select object with non-select element');var e=this._2={};this._0.insert(this.getHTML().node,'after');this._B();this.updateOptions();this.setState('LIST_OPEN');this.hideList(false);this._0.on('blur')._(this).hideList(true);[this._0.on('keydown'),this._0.on('change')].forEach(it().wait(0.001)._(this)._k(false));this._0.on('keydown',function(a,b){var c=b.keyCode||0;if(c.between(48,57)||c.between(65,90)||c.between(37,40))this.wait(0.001).showList()},this);e._i.setStyle({position:'relative',cursor:'default'});[e._w,e._V].forEach(it().on('click')._(this).toggleList());var f=YAHOO.util.KeyListener;new f(this._0.node,{keys:f.KEY.ESCAPE},{fn:this.method('hideList').partial(false)}).enable();new f(this._0.node,{keys:f.KEY.ENTER},{fn:this.method('hideList').partial(true)}).enable();e._c.setStyle({position:'absolute'})},getHTML:function(){var c=this._2,d=this.klass;if(c._i)return c._i;return c._i=Ojay(Ojay.HTML.div({className:this.klass.CONTAINER_CLASS},function(b){c._w=Ojay(b.div({className:d.DISPLAY_CLASS}));c._V=Ojay(b.div({className:d.BUTTON_CLASS}));c._c=Ojay(b.div({className:d.LIST_CLASS},function(a){c._v=Ojay(a.ul())}))}))},getInput:function(){return this._0},getLabel:function(){return this._5},_u:function(){this._0.node.focus()},updateOptions:function(){this._2._v.setContent('');this._Y=Array.from(this._0.node.options).map(function(a){a=new this.klass.Option(this,a);this._2._v.insert(a.getHTML().node);return a},this);this._k();return this},_k:function(a){var b=this._t;var c=this.getSelectedOption();if(!c)return;this._2._w.setContent(c.text.stripTags());this._s(c.value).setHovered(true);if(this.inState('LIST_OPEN')||a===false)return;this._t=c.value;if(b!==undefined&&b!=this._t)this.notifyObservers('change')},_s:function(a){return this._Y.filter({value:a})[0]||null},_Q:function(a){if(this._p)this._p.setHovered(false);this._p=a},_r:function(){return Array.from(this._0.node.options)},getSelectedOption:function(){return this._r().filter('selected')[0]||this._0.node.options[0]||null},getOptionsByValue:function(a){return this._r().filter({value:a})},getValue:function(){return this.getSelectedOption().value},setValue:function(a,b){Ojay.Forms.setValue(this._0,a);this._k(b);return this},updateListPosition:function(){var a=this._2._i.getRegion();if(!a)return this;var b=this._2._c;b.setStyle({width:a.getWidth()+'px',left:0,top:a.getHeight()+'px'});return this},states:{LIST_CLOSED:{showList:function(){if(this.disabled)return this;this.updateListPosition();this._2._c.show();this.setState('LIST_OPEN');this._u();var a=this.getSelectedOption();if(a)this._s(a.value).setHovered(true);return this},toggleList:function(){return this.showList()}},LIST_OPEN:{hideList:function(a){this._2._c.hide();this.setState('LIST_CLOSED');if(a!==false){this.setValue(this._p.value);this._u()}return this},toggleList:function(a){return this.hideList(a)}}}})})();