/*
Copyright (c) 2007-2008 the OTHER media Limited
Licensed under the BSD license, http://ojay.othermedia.org/license.html
Version: 0.4.1
Build:   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(),submit:function(a){a=Ojay(a);i(a.node.id)._K()},reattach:function(){var a=0;for(var b in j){if(j[b]._b())++a}return a},update:function(){h.forEach(function(a){if(a.isA(Ojay.Forms.Select))a._f();else a.setChecked()})}});var p=new JS.Class('Ojay.Forms.FormDescription',{include:JS.Observable,initialize:function(a){this._L=a;this._n={};this._b();this._8={};this._r=[];this._s=[];this._t=[];this._g=new k(this);this._M=new q(this);this._N=new r(this)},_b:function(){if(this._u())return false;this._h={};this._v={};this._i={};this._1=Ojay.byId(this._L);if(!this._u())return false;for(var c in this._8)this._8[c]._b();this._1.on('submit',function(a,b){if(!this._w())b.stopDefault()},this);return true},_u:function(){return this._1&&this._1.ancestors('body').node},_O:function(a){return this._8[a]||(this._8[a]=new s(this,a))},_K:function(){if(this._w())this._1.node.submit()},_w:function(){var b=true;valid=this._P();if(this._x||!valid)b=false;if(!this._x||!valid)return b;var c=this._1.node;Ojay.HTTP[(c.method||'POST').toUpperCase()](c.action,this._Q,{onSuccess:function(a){this._t.forEach({call:[null,a]})}.bind(this)});return b},_c:function(a){if(this._h[a])return this._h[a];var b=this._1.descendants('input, textarea, select');if(a)b=b.filter(function(element){return element.node.name==a});return this._h[a]=b},_j:function(a){if(a.node)a=a.node;if(a.name)a=a.name;return this._v[a]||(this._v[a]=Ojay.Forms.getLabel(this._c(a)))},_R:function(d){if(this._i[d])return this._i[d];if(this._n[d])return this._i[d]=this._n[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._i[d]=f.charAt(0).toUpperCase()+f.substring(1)},_S:function(){return this._Q=Ojay.Forms.getData(this._1)},_T:function(){this._9=new t(this);var b=this._S(),c,d;this._s.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]._U(b.get(c),b);this._r.forEach(function(a){a(b,this._9)},this);var e=this._9._V();for(c in this._h)[this._c(c),this._j(c)].forEach(it()[e.indexOf(c)==-1?'removeClass':'addClass']('invalid'));this.notifyObservers(this)},_P:function(){this._T();return this._9._W()===0},_X:function(){this._c('').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 v=function(a){return!Ojay.isBlank(a)||['must not be blank']};var s=new JS.Class('Ojay.Forms.FormRequirement',{initialize:function(a,b){this._1=a;this._o=b;this._p=[];this._g=new m(this);this._b()},_b:function(){this._2=this._1._c(this._o)},_4:function(a){this._p.push(a)},_U:function(e,f){if(!this._Y())return true;var g=[],w=this._p.length?this._p:[v],e=e||'';w.forEach(function(a){var b=a(e,f),c,d;if(b!==true){c=b[0];d=b[1]||this._o;this._1._9.register(this._o);this._1._9.add(d,c)}},this)},_Y: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('Ojay.Forms.FormData',{initialize:function(b){this.get=function(a){return b[a]===undefined?null:b[a]}}});var t=new JS.Class('Ojay.Forms.FormErrors',{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._W=function(){var a=g.length;for(var b in f)a+=f[b].length;return a};this._Z=function(){var b,c=g.map(function(a){return{field:null,message:a}});for(var d in f){b=e._R(d);f[d].forEach(function(a){c.push({field:d,message:b+' '+a})})}return c};this._V=function(){var a=[];for(var b in f)a.push(b);return a}}});var j={};var i=function(a){return j[a]||(j[a]=new p(a))};var o={form:function(a){return i(a)._g||null},when:function(a){return i(a)._M||null},before:function(a){return i(a)._N||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('Ojay.Forms.FormDSL',{initialize:function(a){this._1=a},requires:function(a,b){var c=this._1._O(a);if(b)this._1._n[a]=b;return c._g},validates:function(a){this._1._r.push(a);return this},submitsUsingAjax:function(a){this._1._x=true;return this},highlightsActiveField:function(){this._1._X();return this}});k.include({expects:k.prototype.requires});var x=['requires','expects','validates','submitsUsingAjax','highlightsActiveField'];var m=new JS.Class('Ojay.Forms.RequirementDSL',{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(x.reduce(function(b,c){b[c]=function(){var a=this._3._1._g;return a[c].apply(a,arguments)};return b},{}));var q=new JS.Class('Ojay.Forms.WhenDSL',{initialize:function(a){this._1=a},isValidated:function(b,c){this._1.subscribe(function(a){b.call(c||null,a._9._Z())})},responseArrives:function(a,b){a=Function.from(a);if(b)a=a.bind(b);this._1._t.push(a)}});var r=new JS.Class('Ojay.Forms.BeforeDSL',{initialize:function(a){this._1=a},isValidated:function(a){this._1._s.push(a)}});var l=new JS.Module('Ojay.Forms.Inputable',{include:Ojay.Observable,extend:{DEFAULT_WRAPPER_POSITION:'relative'},_y:function(){var a=Ojay(Ojay.HTML.span()).setStyle({position:this._d.wrapperPosition||l.DEFAULT_WRAPPER_POSITION});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._k);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._l(a,'focused');return this},setHovered:function(a){this._l(a,'hovered');return this},setDisabled:function(a){this.disabled=(a===undefined)?this._0.node.disabled:!!a;this._0.node.disabled=this.disabled;this._l(this.disabled,'disabled');return this},_l: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._k,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('Ojay.Forms.Checkable',{include:l,_z:function(){this._y();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._A){if(this.checked){this._0.node.checked=true;this._A._10(this,b)}}else{this._0.node.checked=this.checked;if(b!==false&&c!=this.checked)this.notifyObservers('change')}this._l(this.checked,'checked');return this},isChecked:function(){return!!this.checked}});JS.MethodChain.addMethod('focus');Ojay.Forms.RadioButtons=new JS.Class('Ojay.Forms.RadioButtons',{include:Ojay.Observable,initialize:function(b,c){this._a=Ojay(b).map(function(a){return new this.klass.Item(this,a,c)},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._B=this._a.filter('checked')[0]||null},_10:function(a,b){var c=this._B;if(c&&c!=a)c.setChecked(false);if(b!==false&&c!=a)this.notifyObservers('change');this._B=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('Ojay.Forms.RadioButtons.Item',{include:n,_k:'radio',initialize:function(a,b,c){this._d=c||{};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._A=a;this._0=b;this._z()}})}});Ojay.Forms.Checkbox=new JS.Class('Ojay.Forms.Checkbox',{include:n,_k:'checkbox',initialize:function(a,b){this._d=b||{};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._z()},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('Ojay.Forms.Select',{include:[JS.State,l],_k:'select',extend:{CONTAINER_CLASS:'select-container',DISPLAY_CLASS:'select-display',BUTTON_CLASS:'select-button',LIST_CLASS:'select-list',Option:new JS.Class('Ojay.Forms.Select.Option',{initialize:function(a,b){this._C=a;this._14=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._D)return this._D;return this._D=Ojay(Ojay.HTML.li(this._5))},setHovered:function(a){this.hovered=(a!==false);if(this.hovered){this._C._11(this);this._12()}this.getHTML()[a===false?'removeClass':'addClass']('hovered');return this},_12:function(){var a=this._C._2._e;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,e){this._d=e||{};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 f=this._2={};this._0.insert(this.getHTML().node,'after');this._y();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)._f(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);f._m.setStyle({position:'relative',cursor:'default'});[f._E,f._13].forEach(it().on('click')._(this).toggleList());var g=YAHOO.util.KeyListener;new g(this._0.node,{keys:g.KEY.ESCAPE},{fn:this.method('hideList').partial(false)}).enable();new g(this._0.node,{keys:g.KEY.ENTER},{fn:this.method('hideList').partial(true)}).enable();f._e.setStyle({position:'absolute'})},getHTML:function(){var c=this._2,d=this.klass;if(c._m)return c._m;return c._m=Ojay(Ojay.HTML.div({className:this.klass.CONTAINER_CLASS},function(b){c._E=Ojay(b.div({className:d.DISPLAY_CLASS}));c._13=Ojay(b.div({className:d.BUTTON_CLASS}));c._e=Ojay(b.div({className:d.LIST_CLASS},function(a){c._F=Ojay(a.ul())}))}))},getInput:function(){return this._0},getLabel:function(){return this._5},_G:function(){this._0.node.focus()},updateOptions:function(){this._2._F.setContent('');this._d=Array.from(this._0.node.options).map(function(a){a=new this.klass.Option(this,a);this._2._F.insert(a.getHTML().node);return a},this);this._f();return this},_f:function(a){var b=this._H;var c=this.getSelectedOption();if(!c)return;this._2._E.setContent(c.text.stripTags());this._I(c.value).setHovered(true);if(this.inState('LIST_OPEN')||a===false)return;this._H=c.value;if(b!==undefined&&b!=this._H)this.notifyObservers('change')},_I:function(a){return this._d.filter({value:a})[0]||null},_11:function(a){if(this._q)this._q.setHovered(false);this._q=a},_J:function(){return Array.from(this._0.node.options)},getSelectedOption:function(){return this._J().filter('selected')[0]||this._0.node.options[0]||null},getOptionsByValue:function(a){return this._J().filter({value:a})},getValue:function(){return this.getSelectedOption().value},setValue:function(a,b){Ojay.Forms.setValue(this._0,a);this._f(b);return this},updateListPosition:function(){var a=this._2._m.getRegion();if(!a)return this;var b=this._2._e;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._e.show();this.setState('LIST_OPEN');this._G();var a=this.getSelectedOption();if(a)this._I(a.value).setHovered(true);return this},toggleList:function(){return this.showList()}},LIST_OPEN:{hideList:function(a){this._2._e.hide();this.setState('LIST_CLOSED');if(a!==false){this.setValue(this._q.value);this._G()}return this},toggleList:function(a){return this.hideList(a)}}}})})();
