
JHXScriptHelper=Class.create({version:'1.5',scrollerDiff:null,initialize:function(options){this.options={idPrefix:'',formId:'',hideOnLoad:false,showOnLoad:false,autoSubmit:false,scrollTables:true,focusInError:false,disableEnterKey:false};Object.extend(this.options,options||{});this.options.idPrefix='view'+this.options.idPrefix+':';this.options.form=$(this.options.idPrefix+this.options.formId)||document;this.options.idPrefix=this.options.idPrefix+this.options.formId+':';if(this.options.autoSubmit){this.temp=this.options.form.action;this.options.form.action=this.options.autoSubmit;this.options.form.submit();this.options.form.action=this.temp;}
$$('.jhxscripthelper_hideonload').each(function(el){el.hide('hide');if(el.up(0).hasClassName('panel')){el.up(0).addClassName('panel-hide');}});if(this.options.hideOnLoad!==false){this.options.hideOnLoad.each(function(id){var el=this.getElement(id);if(el!==null){el.hide();}}.bind(this));}
if(this.options.showOnLoad!==false){this.options.showOnLoad.each(function(id){var el=this.getElement(id);if(el!==null){el.show();}}.bind(this));}
$$('.jhxTableDynamic .jhxscripthelper_dynamic_cell').each(function(cell){if(cell.tagName!="TD"){cell=cell.up('td').addClassName(cell.className);}
cell.up(0).addClassName('heading click');if(cell.hasClassName('jhxscripthelper_dynamic_cell_collapsed')){cell.up(0).addClassName('toggle-hide').next(0).addClassName('hide');cell.select('.jhxscripthelper_dynamic_cell_show_text').invoke('removeClassName','hide');cell.select('.jhxscripthelper_dynamic_cell_hide_text').invoke('addClassName','hide');}else{cell.select('.jhxscripthelper_dynamic_cell_show_text').invoke('addClassName','hide');cell.select('.jhxscripthelper_dynamic_cell_hide_text').invoke('removeClassName','hide');}
var ath=new Element('th',{className:'first toggle jhxscripthelper_dynamic_cell'}).insert(cell.innerHTML);cell.replace(ath);ath.observe('click',function(ev){ev.stop();this.up(0).toggleClassName('toggle-hide').next(0).toggleClassName('hide');this.select('.jhxscripthelper_dynamic_cell_show_text').invoke('toggleClassName','hide');this.select('.jhxscripthelper_dynamic_cell_hide_text').invoke('toggleClassName','hide');});});$$('.jhxTableDynamic tr').invoke('removeClassName','zebra');this.options.form.observe('keypress',this.onkeypress.bindAsEventListener(this));this.options.form.observe('keydown',this.onkeydown.bindAsEventListener(this));this.options.form.observe('keyup',this.onkeyup.bindAsEventListener(this));this.options.form.observe('click',this.onclick.bindAsEventListener(this));if(this.options.scrollTables&&Prototype.Browser.IE){this.maximizeScrollingTables();Event.observe(window,'resize',function(ev){this.maximizeScrollingTables();}.bind(this));}
if(this.options.focusInError){var errorRows=this.options.form.select('tr.error');if(errorRows&&errorRows.length>0){errorRows[0].descendants().each(function(el){if(el.type&&el.focus){el.focus();throw $break;}});}}
if(this.options.disableEnterKey){this.options.form.observe('keypress',function(ev){if(ev.keyCode==Event.KEY_RETURN&&['text','password','checkbox','radio'].include(ev.findElement().type)){ev.stop();}});}
return this;},addBehaviours:function(id,options){var element=this.getElement(id);if(element!==null){element.hsbc_options=options||{};if(element.tagName=="SELECT"){element.observe('change',this.onchange.bindAsEventListener(this));if(Prototype.Browser.Gecko){element.observe('keyup',this.onchange.bindAsEventListener(this));}}
this.actionBehaviours(null,element,0);}
return this;},addValidators:function(id,validators){var element=this.getElement(id);if(element!==null){element.hsbc_validators=validators||{};element.observe('blur',this.onblur.bindAsEventListener(this));this.actionValidators(element);}
return this;},addTimer:function(id,options){if(typeof options.interval!="undefined"){var element=this.getElement(id);element.hsbc_timer_options=options||{};element.hsbc_timer_options.timer=new PeriodicalExecuter(function(pe){this._updateTimer(pe,element);}.bind(this),element.hsbc_timer_options.interval);}else{element=new Element('div');element.hsbc_timer_options=options||{};element.hsbc_timer_options.timer=window.setTimeout(function(){this._afterTimer(element);}.bind(this),element.hsbc_timer_options.timeout*1000);}
return this;},_updateTimer:function(timer,element){var newVal=element.innerHTML-element.hsbc_timer_options.interval;element.innerHTML=(newVal<0)?0:newVal;if(element.innerHTML==0){timer.stop();this._afterTimer(element);}},_afterTimer:function(element){if(typeof element.hsbc_timer_options.afterFinish.hide!="undefined"){element.hsbc_timer_options.afterFinish.hide.each(function(el){this.getElement(el).hide();}.bind(this));}
if(typeof element.hsbc_timer_options.afterFinish.show!="undefined"){element.hsbc_timer_options.afterFinish.show.each(function(el){this.getElement(el).show();}.bind(this));}},onchange:function(ev){var element=ev.element();if(typeof element.hsbc_options=='undefined'){return;}
this.actionBehaviours(ev,element,0);this.linkedSelect(element);},onclick:function(ev){var element=ev.element();if(typeof element.hsbc_options=='undefined'){return;}
this.actionBehaviours(ev,element,0);},onblur:function(ev){var element=ev.element();if(typeof element.hsbc_validators=='undefined'){return;}
this.actionValidators(element);},actionBehaviours:function(ev,element,depth){var stopEvent=false;if(depth>1){return;}
depth++;if(ev&&typeof element.hsbc_options.print!='undefined'){window.print();stopEvent=true;}
if(ev&&(typeof element.hsbc_options.toggle!='undefined')){element.hsbc_options.toggle.each(function(id){var el=this.getElement(id);if(el!==null)el.toggle();}.bind(this));stopEvent=this.stopEvent(element);}
if(typeof element.hsbc_options.hide!='undefined'){if(element.hsbc_options.hide[element.getValue()]){element.hsbc_options.hide[element.getValue()].each(function(id){var el=this.getElement(id);if(el!==null){el.hide();el.descendants().each(function(el){if(typeof el.hsbc_options!='undefined'){this.actionBehaviours(ev,el,depth);}}.bind(this));}}.bind(this));}else{if((typeof element.hsbc_options.showAll!='undefined')){element.hsbc_options.showAll.each(function(id){var el=this.getElement(id);if(el!==null)el.show();}.bind(this));}}}
if(typeof element.hsbc_options.show!='undefined'){if(element.hsbc_options.show[element.getValue()]){element.hsbc_options.show[element.getValue()].each(function(id){var el=this.getElement(id);if(el!==null){el.show();el.descendants().each(function(el){if(typeof el.hsbc_options!='undefined'){this.actionBehaviours(ev,el,depth);}}.bind(this));}}.bind(this));}else{if((typeof element.hsbc_options.hideAll!='undefined')){element.hsbc_options.hideAll.each(function(id){var el=this.getElement(id);if(el!==null)el.hide();}.bind(this));}}}
if(typeof element.hsbc_options.enable!='undefined'){if(element.hsbc_options.enable[element.getValue()]){element.hsbc_options.enable[element.getValue()].each(function(id){var el=this.getElement(id);if(el!==null){el.writeAttribute('disabled',false);el.descendants().each(function(el){if(typeof el.hsbc_options!='undefined'){this.actionBehaviours(ev,el,depth);}}.bind(this));}}.bind(this));}
stopEvent=this.stopEvent(element);}
if(typeof element.hsbc_options.disable!='undefined'){if(element.hsbc_options.disable[element.getValue()]){element.hsbc_options.disable[element.getValue()].each(function(id){var el=this.getElement(id);if(el!==null){el.writeAttribute('disabled',true);el.descendants().each(function(el){if(typeof el.hsbc_options!='undefined'){this.actionBehaviours(ev,el,depth);}}.bind(this));}}.bind(this));}
stopEvent=this.stopEvent(element);}
if(typeof element.hsbc_options.write!='undefined'){if(element.hsbc_options.write[element.getValue()]){element.hsbc_options.write[element.getValue()].each(function(id){var el=this.getElement(id);if(el!==null){el.writeAttribute('readonly',false);el.descendants().each(function(el){if(typeof el.hsbc_options!='undefined'){this.actionBehaviours(ev,el,depth);}}.bind(this));}}.bind(this));}
stopEvent=this.stopEvent(element);}
if(typeof element.hsbc_options.read!='undefined'){if(element.hsbc_options.read[element.getValue()]){element.hsbc_options.read[element.getValue()].each(function(id){var el=this.getElement(id);if(el!==null){el.writeAttribute('readonly',true);el.descendants().each(function(el){if(typeof el.hsbc_options!='undefined'){this.actionBehaviours(ev,el,depth);}}.bind(this));}}.bind(this));}
stopEvent=this.stopEvent(element);}
if(typeof element.hsbc_options.checkAll!='undefined'){this.options.form.select('input[type=checkbox][id^='+this.options.idPrefix+element.hsbc_options.checkAll+']').each(function(el){el.checked=true;});stopEvent=this.stopEvent(element);}
if(typeof element.hsbc_options.checkNone!='undefined'){this.options.form.select('input[type=checkbox][id^='+this.options.idPrefix+element.hsbc_options.checkNone+']').each(function(el){el.checked=false;});stopEvent=this.stopEvent(element);}
if(ev&&(typeof element.hsbc_options.clearForm!='undefined'||typeof element.hsbc_options.clearSection!='undefined')){var els=this.options.form.getElements();if(typeof element.hsbc_options.clearSection!='undefined'){els=els.findAll(function(el){return(el.up('.jhxscripthelper_clear_section')==element.up('.jhxscripthelper_clear_section'));});}
els.each(function(el){if(el.tagName=='BUTTON'||el.readOnly||el.disabled||['hidden','button','reset','submit'].include(el.type)){return;}
if(el.tagName=='SELECT'){el.selectedIndex=(el.size>1)?-1:0;}else if(['checkbox','radio'].include(el.type)){el.checked=false;}else{el.setValue('');}});if(typeof element.hsbc_options.clearForm!='undefined'&&element.hsbc_options.clearForm==true){this.options.form.focusFirstElement();}else if(typeof element.hsbc_options.clearSection!='undefined'&&element.hsbc_options.clearSection==true){els.find(function(el){if('hidden'!=el.type&&!el.disabled&&/^(?:input|select|textarea)$/i.test(el.tagName)){el.focus();return true;}});}
stopEvent=true;}
if(ev&&stopEvent){ev.stop();}},actionValidators:function(element){var value=element.getValue();element.removeClassName('error');if(!value){return;}
if(typeof element.hsbc_validators.minLength!='undefined'){if(value.length<element.hsbc_validators.minLength){element.addClassName('error');return;}}
if(typeof element.hsbc_validators.maxLength!='undefined'){if(value.length>element.hsbc_validators.maxLength){element.addClassName('error');return;}}
if(typeof element.hsbc_validators.integer!='undefined'&&element.hsbc_validators.integer==true){if(parseInt(value)!=value){element.addClassName('error');return;}}
if(typeof element.hsbc_validators.decimal!='undefined'&&element.hsbc_validators.decimal==true){if(parseFloat(value)!=value){element.addClassName('error');return;}}
if(typeof element.hsbc_validators.minValue!='undefined'){if(parseInt(value)<element.hsbc_validators.minValue){element.addClassName('error');return;}}
if(typeof element.hsbc_validators.maxValue!='undefined'){if(parseInt(value)>element.hsbc_validators.maxValue){element.addClassName('error');return;}}
if(typeof element.hsbc_validators.email!='undefined'&&element.hsbc_validators.email==true){if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value))){element.addClassName('error');return;}}},linkedSelect:function(parent){if(parent.hsbc_lastvalue==parent.getValue()){return;}
if(typeof parent.hsbc_options.linkedSelect!='undefined'){var parentVal=parent.getValue();$H(parent.hsbc_options.linkedSelect).each(function(to){var child=this.getElement(to[0]);if(child!==null){child.options.length=0;$H(to[1]).each(function(optionData){if(optionData[1].parents=='*'||optionData[1].parents.indexOf(parentVal)!=-1){child.options[child.options.length]=new Option(optionData[1].text,optionData[0]);}});if(typeof child.hsbc_options!='undefined'&&typeof child.hsbc_options.linkedSelect!='undefined'){this.linkedSelect(child);}}}.bind(this));}
parent.hsbc_lastvalue=parent.getValue();},onkeydown:function(ev){var element=ev.element();if(typeof element.hsbc_options=='undefined'){return;}
if(typeof element.hsbc_options.textCount!='undefined'&&element.hsbc_options.textCountId){var count=(typeof element.hsbc_options.textCountUp!='undefined')?element.getValue().length:element.hsbc_options.textCount-element.getValue().length;if((typeof element.hsbc_options.textCountUp!='undefined'&&count>=element.hsbc_options.textCount)||(typeof element.hsbc_options.textCountUp=='undefined'&&count<=0))
{if(this.isCharKey(ev)){ev.stop();}}}},onkeypress:function(ev){this.keyBlocker(ev);},onkeyup:function(ev){var element=ev.element();if(typeof element.hsbc_options=='undefined'){return;}
this.keyBlocker(ev);if(typeof element.hsbc_options.textCount!='undefined'&&element.hsbc_options.textCountId){var count=(typeof element.hsbc_options.textCountUp!='undefined')?element.getValue().length:element.hsbc_options.textCount-element.getValue().length;var countEl=this.getElement(element.hsbc_options.textCountId);if(countEl!==null){if(typeof countEl.value=="undefined"){countEl.innerHTML=count;}else{countEl.setValue(count);}}}},keyBlocker:function(ev){var element=ev.element();if(typeof element.hsbc_options=='undefined'){return;}
if(element.hsbc_options.charSet&&element.hsbc_options.charSet){if(!ev.ctrlKey&&!ev.altKey&&this.getCharCode(ev)!==0&&element.hsbc_options.charSet.indexOf(String.fromCharCode(this.getCharCode(ev)))==-1){ev.stop();}}},maximizeScrollingTables:function(){$$('div.scroll-table').each(function(el){var scrollerParent=el.up();var scrollerDiv=el.down();var scrollerTable=scrollerDiv.down();if(scrollerDiv.hasClassName('scroller')&&scrollerTable.tagName=='TABLE'){var scrollerWidth=parseInt(el.getWidth());var scrollerTableWidth=parseInt(scrollerTable.getWidth());if(!this.scrollerDiff){this.scrollerDiff=scrollerWidth-scrollerTableWidth;}
el.setStyle({width:'100%'});scrollerDiv.setStyle({width:'100%'});var scrollerWidth=parseInt(el.getWidth());el.select('div.scroller table')[0].setStyle({width:(scrollerWidth-this.scrollerDiff)+'px'});}});},stopEvent:function(element){return(element.tagName=="A"||(element.tagName=="INPUT"&&element.type=="submit"));},getElement:function(id){return $(this.options.idPrefix+id);},getCharCode:function(ev){return typeof ev.charCode!='undefined'?ev.charCode:ev.keyCode;},getKeyCode:function(ev){return ev.keyCode;},isCharKey:function(ev){var code=ev.charCode||ev.keyCode;return((code==13||code==32||code>=48)&&(code<112||code>123)&&!ev.altKey&&!ev.ctrlKey);}});
