// Gaia Ajax Widgets Copyright (C) 2007 - 2008 Gaiaware AS. details at http://ajaxwidgets.com/
if(!window.Gaia)
Gaia=Class.create();Gaia.Control=Class.create();Gaia.Control._itsRegisteredInvisibleControls=new Array();Gaia.Control._itsRegisteredControls=$H({});Gaia.Control._updateControl=null;Gaia.Control.setUpdateControl=function(el,isCallback){Gaia.Control._updateControl=el;if(el){Element.hide(el);}}
Gaia.Control.registerControl=function(control){if(Gaia.Control._itsRegisteredControls.get(control.element.id)){Gaia.Control._itsRegisteredControls.get(control.element.id).destroy();}
Gaia.Control._itsRegisteredControls.set(control.element.id,control);return control;}
Gaia.Control.destroyAllContaining=function(inId,except){var arr=new Array();Gaia.Control._itsRegisteredControls.values().each(function(idx){if(idx.element.id!=except&&idx.element.id.indexOf(inId)!=-1)
arr.push(idx);});arr.each(function(idx){idx.destroy();});}
$G=function(control){return Gaia.Control._itsRegisteredControls.get(control);}
$FC=function(field){if($(field))
return $(field);var input=document.createElement('input');input.type='hidden';input.name=field;input.id=field;document.getElementsByTagName('form')[0].appendChild(input);return input;}
$FChange=function(field,unchangedTo,value){var el=$FC(field);el.value=el.value.substr(0,unchangedTo)+value;}
Gaia.Control._defaultUrl=null;Gaia.Control._activeRequest=false;Gaia.Control._activeRequests=new Array();Gaia.Control._onSuccessfulAjaxRequestFinished=function(callback,t){if(Gaia.Control._updateControl)
Element.hide(Gaia.Control._updateControl);if(callback){callback(t);}
Gaia.Control._activeRequests.splice(0,1);Gaia.Control._activeRequest=false;}
Gaia.Control._userSpecificErrorHandler=null;Gaia.Control._errorHandlerIFrame=null;Gaia.Control._closeErrorHandler=function(){document.body.removeChild(Gaia.Control._errorHandlerIFrame);}
Gaia.Control._onFailedAjaxRequestFinished=function(callback,err){if(Gaia.Control._hasShownErrorForCurrentRequest)
return;Gaia.Control._hasShownErrorForCurrentRequest=true;if(Gaia.Control._userSpecificErrorHandler){if(!Gaia.Control._userSpecificErrorHandler((err&&err.status)?err.status:-1,(err&&err.statusText)?err.statusText:'CONNECTION_PROBLEM',(err&&err.responseText)?err.responseText:'Unknown problem in page, probably caused by a connection failure to the server'))
this._onSuccessfulAjaxRequestFinished(callback);return;}
if(!err.status){alert('Something went wrong with the communication, probably reason is connection breakdown or something similar');}else if(confirm('Error from server: '+err.status+', \''+err.statusText+'\', Ajax Engine shutdown for security reasons, refresh page to enable application again...\nDo you wish to see the debug results?')){var dbg=document.createElement('iframe');dbg.width='100%';dbg.height='100%';dbg.style.position='absolute';dbg.style.left='0px';dbg.style.top='0px';dbg.style.zIndex=10000;document.body.appendChild(dbg);dbg.contentWindow.document.open();dbg.contentWindow.document.write(err.responseText);dbg.contentWindow.document.write('<div onclick="window.parent.Gaia.Control._closeErrorHandler();" style="cursor:pointer;z-index:10001;right:5px;top:5px;width:100px;height:40px;border:Solid 3px Black;background-color:Red;position:absolute;margin:auto;text-align:center;font-size:18px;">Close</div>');dbg.contentWindow.document.close();Gaia.Control._errorHandlerIFrame=dbg;}
this._onSuccessfulAjaxRequestFinished(callback);}
Gaia.Control._addUpParameter=function(formInputName,ajaxOptions){var value=$(formInputName);if(value){ajaxOptions.postBody+='&'+formInputName+'='+encodeURIComponent($F(formInputName));}}
Gaia.Control._addUpCommonParameters=function(ajaxOptions){Gaia.Control._addUpParameter('__GAIA_FILES',ajaxOptions);Gaia.Control._addUpParameter('__EVENTVALIDATION',ajaxOptions);Gaia.Control._addUpParameter('__VIEWSTATE',ajaxOptions);Gaia.Control._addUpParameter('__VIEWSTATEENCRYPTED',ajaxOptions);}
Gaia.Control._hasShownErrorForCurrentRequest=false;Gaia.Control._createNewRequestImplementation=function(controlToCallFor,methodAfter,evt,extraParams,url){Gaia.Control._activeRequest=true;if(Gaia.Control._updateControl)
Element.show(Gaia.Control._updateControl);var opt={method:'post',postBody:'GaiaCallback=true',onSuccess:Gaia.Control._onSuccessfulAjaxRequestFinished.bind(this,methodAfter),onFailure:Gaia.Control._onFailedAjaxRequestFinished.bind(this,methodAfter),onException:Gaia.Control._onFailedAjaxRequestFinished.bind(this,methodAfter),evalJS:false};Gaia.Control._addUpCommonParameters(opt)
if(extraParams){opt.postBody+='&gaiaParams='+encodeURIComponent(extraParams);}
var allControlsInPage=Gaia.Control._itsRegisteredControls.values();for(var idx=0;idx<allControlsInPage.length;idx++){var ctrl=allControlsInPage[idx];if(ctrl==controlToCallFor)
opt.postBody+=ctrl._getElementPostValueEvent(evt);else
opt.postBody+=ctrl._getElementPostValue();}
Gaia.Control._hasShownErrorForCurrentRequest=false;new Ajax.Request((url||(controlToCallFor?controlToCallFor.options.url:null)||Gaia.Control._defaultUrl),opt);}
Gaia.Control._dispatchNextRequest=function(){if(Gaia.Control._activeRequests.length==0)
return;if(Gaia.Control._activeRequest){setTimeout(function(){Gaia.Control._dispatchNextRequest()},50);}else{var request=Gaia.Control._activeRequests[0];Gaia.Control._createNewRequestImplementation(request.control,request.method,request.evtIn,request.xtraParams,request.urlToCall,request.evaluate);}}
Gaia.Control._createNewRequest=function(controlToCallFor,methodAfter,evt,extraParams,url,evalScripts){var retVal={control:controlToCallFor,method:methodAfter,evtIn:evt,xtraParams:extraParams,urlToCall:url,evaluate:evalScripts};Gaia.Control._activeRequests.push(retVal);Gaia.Control._dispatchNextRequest();return retVal;}
Gaia.Control.returnValue=null;Gaia.Control._onSuccessServerSideMethodCalled=function(onFinished,t){if(t)
Gaia.Control.evalServerCallback('Gaia.Control.returnValue = '+t.responseText);else
Gaia.Control.returnValue=false;if(onFinished)
onFinished(Gaia.Control.returnValue);}
Gaia.Control._parseParametersForServerSideMethod=function(method,params,methodType){var parsedParams=method+','+methodType;if(params){params.each(function(param){var idxParam=param;if(typeof param=='string'){while(idxParam.indexOf(',')!=-1){idxParam=idxParam.replace(',','|$|');}}
parsedParams+=','+idxParam;});}
return parsedParams;}
Gaia.Control._callServerSideMethod=function(method,params,onFinished,methodType){var parsedParams=Gaia.Control._parseParametersForServerSideMethod(method,params,methodType);Gaia.Control._createNewRequest(null,Gaia.Control._onSuccessServerSideMethodCalled.bind(this,onFinished),null,parsedParams,null,false);}
Gaia.Control.callPageMethod=function(method,params,onFinished){Gaia.Control._callServerSideMethod(method,params,onFinished,'PageMethod');}
Gaia.Control.callControlMethod=function(method,params,onFinished,passId){Gaia.Control._callServerSideMethod(method,params,onFinished,'ControlMethod,'+(passId||this.element.id));}
Gaia.Control.callAspectMethod=function(method,params,onFinished,passId){Gaia.Control._callServerSideMethod(method,params,onFinished,'AspectMethod,'+(passId||this.element.id));}
Gaia.Control.currentScriptFragmentExecutingIdx=0;Gaia.Control.lastLoadedListOfScripts=null;Gaia.Control.evalServerCallback=function(script){Gaia.javaScriptFilesToWaitFor=0;Gaia.Control.lastLoadedListOfScripts=script.split(';;;;;');Gaia.Control.currentScriptFragmentExecutingIdx=0;return Gaia.Control._executeScript(true);}
Gaia.Control._executeScript=function(shouldExecute){var retVal=null;if(Gaia.Control.currentScriptFragmentExecutingIdx<Gaia.Control.lastLoadedListOfScripts.length){if(shouldExecute){retVal=eval(Gaia.Control.lastLoadedListOfScripts[Gaia.Control.currentScriptFragmentExecutingIdx]);setTimeout('Gaia.Control._executeScript(false);',100);}else{if(Gaia.javaScriptFilesToWaitFor==0){Gaia.Control.currentScriptFragmentExecutingIdx+=1;Gaia.Control._executeScript(true);}else{setTimeout('Gaia.Control._executeScript(false);',100);}}}
return retVal;}
Gaia.Control.prototype={initialize:function(){throw"You must override the initialize function in your derived class";},initializeControl:function(element,options){this.element=$(element);if(!this.element)
this.element=element;this.options=Object.extend({url:Gaia.Control._defaultUrl},options||{});if(this.options.hasSetFocus)
this.setFocus();},getControlId:function(){return this.element.id;},setVisible:function(value){value?Element.show(this.element):Element.hide(this.element);return this;},setFocus:function(){try{this.element.focus();}
catch(err){}
return this;},destroy:function(){this._destroyImpl();},_destroyImpl:function(){if(this._subscribedEvents){for(var idx=0;idx<this._subscribedEvents.length;idx++){var evt=this._subscribedEvents[idx];Element.stopObserving(this.element,evt.name,evt.evt);}}
if(this.options.aspects){for(var idx=0;idx<this.options.aspects.length;idx++){this.options.aspects[idx].destroy();}}
Gaia.Control._itsRegisteredControls.unset(this.element.id);},observe:function(evtName,bubbleUp){this._observeImpl(evtName,(bubbleUp?true:false));return this;},_observeImpl:function(evtName,bubbleUp){if(this._subscribedEvents==null)
this._subscribedEvents=new Array();var _onEventEvent=this._onEvent.bindAsEventListener(this,evtName,bubbleUp);Element.observe(this.element,evtName,_onEventEvent);this._subscribedEvents.push({name:evtName,evt:_onEventEvent});},_onEventImpl:function(evt,evtName,skipDisable){if(typeof(WebForm_OnSubmit)=='function'){if(WebForm_OnSubmit()!=true)
return;}
if(window['_spFormOnSubmitCalled']!=null&&window['_spFormOnSubmitCalled']!='undefined'){_spFormOnSubmitCalled=false;}
if(Gaia.Control._updateControl==null&&!skipDisable)
this.setEnabled(false);this._updaterControl=Gaia.Control._updateControl;Gaia.Control._createNewRequest(this,this._afterEvent.bind(this,skipDisable),evt);},_getElementPostValue:function(){throw"Abstract _getElementPostValue not overridet";},_getElementPostValueEvent:function(){throw"Abstract _getElementPostValueEvent not overridet";},getCallbackName:function(){if(this.options.callbackName&&this.options.callbackName.length>0)
return this.options.callbackName;if(this.element.name&&this.element.name.length>0){this.options.callbackName=this.element.name;return this.options.callbackName;}
var retVal=this.element.id;while(retVal.indexOf('_')!=-1){retVal=retVal.replace('_','$');}
this.options.callbackName=retVal;return retVal;},_onEvent:function(evt,evtName,bubbleUp){try{this._onEventImpl(evt,evtName);}catch(err){alert('Gaia Error: _onEvent/Gaia - Control.js fire\n\nError Message:\n\n'+err);}
if(!bubbleUp)
Event.stop(evt);},_afterEvent:function(skipDisable,t){if($(this.element.id)){if(this._updaterControl==null&&!skipDisable)
this.setEnabled(true);}
if(t)
Gaia.Control.evalServerCallback(t.responseText);}};Gaia.Control.browserFinishedLoading=true;