/*
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(o)};var h=[];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.set({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(function(a){return(a.value==c)||(a.text==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 i){if(i[b]._b())++a}return a},update:function(){h.forEach(function(a){if(a.isA(Ojay.Forms.Select))a._e();else a.setChecked()})}});var p=new JS.Class({include:JS.Observable,initialize:function(a){this._I=a;this._m={};this._b();this._8={};this._q=[];this._r=[];this._s=[];this._f=new j(this);this._J=new q(this);this._K=new r(this)},_b:function(){if(this._t())return false;this._g={};this._u={};this._h={};this._1=Ojay.byId(this._I);if(!this._t())return false;this._1.on('submit',this.method('_L'));for(var a in this._8)this._8[a]._b();return true},_t:function(){return this._1&&this._1.matches('body form')},_M:function(a){return this._8[a]||(this._8[a]=new s(this,a))},_L:function(b,c){var d=this._N();if(this._v||!d)c.stopDefault();if(!this._v||!d)return;var b=this._1.node;Ojay.HTTP[(b.method||'POST').toUpperCase()](b.action,this._O,{onSuccess:function(a){this._s.forEach({call:[null,a]})}.bind(this)})},_c:function(a){if(this._g[a])return this._g[a];var b=this._1.descendants('input, textarea, select');if(a)b=b.filter(function(element){return element.node.name==a});return this._g[a]=b},_i:function(a){if(a.node)a=a.node;if(a.name)a=a.name;return this._u[a]||(this._u[a]=Ojay.Forms.getLabel(this._c(a)))},_P:function(d){if(this._h[d])return this._h[d];if(this._m[d])return this._h[d]=this._m[d];var e=this._i(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._h[d]=f.charAt(0).toUpperCase()+f.substring(1)},_Q:function(){return this._O=Ojay.Forms.getData(this._1)},_R:function(){this._9=new t(this);var b=this._Q(),c,d;this._r.forEach(function(a){a(b)});for(c in b)Ojay.Forms.setValue(this._c(c),b[c]);Ojay.Forms.update();b=new u(b);for(c in this._8)this._8[c]._S(b.get(c),b);this._q.forEach(function(a){a(b,this._9)},this);var e=this._9._T();for(c in this._g)[this._c(c),this._i(c)].forEach(it()[e.indexOf(c)==-1?'removeClass':'addClass']('invalid'));this.notifyObservers(this)},_N:function(){this._R();return this._9._U()===0},_V:function(){this._c('').forEach(function(a){a.on('focus').addClass('focused')._(this)._i(a).addClass('focused');a.on('blur').removeClass('focused')._(this)._i(a).removeClass('focused')},this)}});var v=function(a){return!Ojay.isBlank(a)||['must not be blank']};var s=new JS.Class({initialize:function(a,b){this._1=a;this._n=b;this._o=[];this._f=new l(this);this._b()},_b:function(){this._2=this._1._c(this._n)},_4:function(a){this._o.push(a)},_S:function(e,f){if(!this._W())return true;var g=[],w=this._o.length?this._o:[v],e=e||'';w.forEach(function(a){var b=a(e,f),c,d;if(b!==true){c=b[0];d=b[1]||this._n;this._1._9.register(this._n);this._1._9.add(d,c)}},this)},_W:function(){return!!this._2&&this._2.reduce(function(a,b){var c=b.node;do{if(c.parentNode&&Ojay(c).getStyle('display')=='none')return false}while(c=c.parentNode)return a},true)}});var u=new JS.Class({initialize:function(b){this.get=function(a){return b[a]===undefined?null:b[a]}}});var t=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._U=function(){var a=g.length;for(var b in f)a+=f[b].length;return a};this._X=function(){var b,c=g.map(function(a){return{field:null,message:a}});for(var d in f){b=e._P(d);f[d].forEach(function(a){c.push({field:d,message:b+' '+a})})}return c};this._T=function(){var a=[];for(var b in f)a.push(b);return a}}});var i={};var k=function(a){return i[a]||(i[a]=new p(a))};var o={form:function(a){return k(a)._f||null},when:function(a){return k(a)._J||null},before:function(a){return k(a)._K||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 j=new JS.Class({initialize:function(a){this._1=a},requires:function(a,b){var c=this._1._M(a);if(b)this._1._m[a]=b;return c._f},validates:function(a){this._1._q.push(a);return this},submitsUsingAjax:function(a){this._1._v=true;return this},highlightsActiveField:function(){this._1._V();return this}});j.include({expects:j.prototype.requires});var x=['requires','expects','validates','submitsUsingAjax','highlightsActiveField'];var l=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}});l.include(x.reduce(function(b,c){b[c]=function(){var a=this._3._1._f;return a[c].apply(a,arguments)};return b},{}));var q=new JS.Class({initialize:function(a){this._1=a},isValidated:function(b,c){this._1.subscribe(function(a){b.call(c||null,a._9._X())})},responseArrives:function(a,b){a=Function.from(a);if(b)a=a.bind(b);this._1._s.push(a)}});var r=new JS.Class({initialize:function(a){this._1=a},isValidated:function(a){this._1._r.push(a)}});var m=new JS.Module({include:Ojay.Observable,_w: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._j);this._6=[this._0,this._5];if(this.getHTML)this._6.push(this.getHTML());this._6.forEach(it().on('mouseover')._(this).setHovered(true));this._6.forEach(it().on('mouseout')._(this).setHovered(false));this._6.forEach(it().addClass('js'));this.setDisabled()},setFocused:function(a){if(this._0.node.checked)this.setChecked();this._k(a,'focused');return this},setHovered:function(a){this._k(a,'hovered');return this},setDisabled:function(a){this.disabled=(a===undefined)?this._0.node.disabled:!!a;this._0.node.disabled=this.disabled;this._k(this.disabled,'disabled');return this},_k:function(b,c){this._7=this._7||[];if(b){if(this._7.indexOf(c)==-1)this._7.push(c);this._7.sort()}else{this._7=this._7.filter(function(a){return a!=c})}this._6.forEach(it()[b?'addClass':'removeClass'](c));var d=this._6[0].node.className.split(/\s+/);var e=this._j,f=new RegExp('^'+e+'-');var g=d.filter({match:f})[0];if(g)this._6.forEach({removeClass:g});if(this._7.length)this._6.forEach({addClass:e+'-'+this._7.join('-')})}});var n=new JS.Module({include:m,_x:function(){this._w();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._y){if(this.checked){this._0.node.checked=true;this._y._Y(this,b)}}else{this._0.node.checked=this.checked;if(b!==false&&c!=this.checked)this.notifyObservers('change')}this._k(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._a=Ojay(b).map(function(a){return new this.klass.Item(this,a)},this);if(this._a.map('_0.node.name').unique().length>1)throw new Error('Attempt to create a RadioButtons object with radios of different names');this._z=this._a.filter('checked')[0]||null},_Y:function(a,b){var c=this._z;if(c&&c!=a)c.setChecked(false);if(b!==false&&c!=a)this.notifyObservers('change');this._z=a},getItem:function(b){return this._a.filter(function(a){return a._0.node.id==b||a._0.node.value==b})[0]},getInput:function(){return Ojay(this._a.map('_0.node'))},getLabel:function(){return Ojay(this._a.map('_5.node'))},getValue:function(){var a=this._a.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,_j:'radio',initialize:function(a,b){h.push(this);if(!b||!b.node||b.node.type!='radio')throw new TypeError('Attempt to create a RadioButtons object with non-radio element');this._y=a;this._0=b;this._x()}})}});Ojay.Forms.Checkbox=new JS.Class({include:n,_j:'checkbox',initialize:function(a){h.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._x()},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,m],_j:'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._A=a;this._13=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._B)return this._B;return this._B=Ojay(Ojay.HTML.li(this._5))},setHovered:function(a){this.hovered=(a!==false);if(this.hovered){this._A._Z(this);this._10()}this.getHTML()[a===false?'removeClass':'addClass']('hovered');return this},_10:function(){var a=this._A._2._d;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){h.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._w();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)._e(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._l.setStyle({position:'relative',cursor:'default'});[e._C,e._11].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._d.setStyle({position:'absolute'})},getHTML:function(){var c=this._2,d=this.klass;if(c._l)return c._l;return c._l=Ojay(Ojay.HTML.div({className:this.klass.CONTAINER_CLASS},function(b){c._C=Ojay(b.div({className:d.DISPLAY_CLASS}));c._11=Ojay(b.div({className:d.BUTTON_CLASS}));c._d=Ojay(b.div({className:d.LIST_CLASS},function(a){c._D=Ojay(a.ul())}))}))},getInput:function(){return this._0},getLabel:function(){return this._5},_E:function(){this._0.node.focus()},updateOptions:function(){this._2._D.setContent('');this._12=Array.from(this._0.node.options).map(function(a){a=new this.klass.Option(this,a);this._2._D.insert(a.getHTML().node);return a},this);this._e();return this},_e:function(a){var b=this._F;var c=this.getSelectedOption();if(!c)return;this._2._C.setContent(c.text.stripTags());this._G(c.value).setHovered(true);if(this.inState('LIST_OPEN')||a===false)return;this._F=c.value;if(b!==undefined&&b!=this._F)this.notifyObservers('change')},_G:function(a){return this._12.filter({value:a})[0]||null},_Z:function(a){if(this._p)this._p.setHovered(false);this._p=a},_H:function(){return Array.from(this._0.node.options)},getSelectedOption:function(){return this._H().filter('selected')[0]||this._0.node.options[0]||null},getOptionsByValue:function(a){return this._H().filter({value:a})},getValue:function(){return this.getSelectedOption().value},setValue:function(a,b){Ojay.Forms.setValue(this._0,a);this._e(b);return this},updateListPosition:function(){var a=this._2._l.getRegion();if(!a)return this;var b=this._2._d;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._d.show();this.setState('LIST_OPEN');this._E();var a=this.getSelectedOption();if(a)this._G(a.value).setHovered(true);return this},toggleList:function(){return this.showList()}},LIST_OPEN:{hideList:function(a){this._2._d.hide();this.setState('LIST_CLOSED');if(a!==false){this.setValue(this._p.value);this._E()}return this},toggleList:function(a){return this.hideList(a)}}}})})();
