
if(!window.sis){window.sis={};}
sis.ajax=function(clientObject,ajaxUrl)
{if(ajaxUrl&&ajaxUrl.charAt(0)!="/")
{var url=location.href.substring(0,location.href.indexOf("/",9));if(ajaxUrl.substring(0,url.length)!=url)
{if(window.console)console.error("sis.ajax error: the remote server URL does not use the same domain name!");}}
this._ajaxUrl=ajaxUrl;this._clientObject=clientObject;var myself=this;this._handleAjaxResponse=function(data){return myself._ajaxResponse(data);};this._handleAjaxError=function(x,s,e){return myself._ajaxError(x,s,e);};this._handleShowSpinner=function(){return myself._showSpinner();}}
jQuery.extend(sis.ajax.prototype,{version:1.1,_debug:(window.console!=null),_clientObject:null,_ajaxUrl:"",_inResponse:false,_spinnerTimer:0,_events:{showSpinner:[],hideSpinner:[],replaceHtml:[]},_lastRequest:null,callMethod:function(methodName,params)
{if(this._ajaxUrl=="")
{if(this._debug)
console.error("sis.ajax.callMethod() - no ajaxUrl defined!");alert("Helaas...\n\n"
+"Er is een fout opgetreden bij het communiceren met de server.\n"
+"Probeer het later a.u.b. nogmaals");return;}
clearTimeout(this._spinnerTimer);this._spinnerTimer=setTimeout(this._handleShowSpinner,150);var postdata="$call="+methodName+"&$lang="+document.documentElement.lang;if(params!=null)
{postdata+="&"+(typeof(params)=="string"?params:jQuery.param(params));}
this._lastRequest=jQuery.ajax({type:"POST",url:this._ajaxUrl,data:postdata,success:this._handleAjaxResponse,error:this._handleAjaxError,dataType:"json"});},getFormData:function(form)
{if(typeof(form)=="object"&&form.jquery)
form=form[0];var values=[];for(var i=0;i<form.elements.length;i++)
{var el=form.elements[i]
if(!el.type||el.disabled)continue;switch(el.type)
{case"select-multiple":for(var j=0;j<el.options.length;j++)
{var op=el.options[j];if(op.selected)
values.push({name:el.name,value:op.value});}
break;case"submit":case"button":case"reset":break;case"radio":case"checkbox":if(!el.checked)break;default:values.push({name:el.name,value:el.value});break;}}
return values;},onShowSpinner:function(callback)
{this._events.showSpinner.push(callback);},onHideSpinner:function(callback)
{this._events.hideSpinner.push(callback);},onReplaceHtml:function(elementId,callback,initNow)
{this._events.replaceHtml.push([elementId,callback]);if(initNow)
{callback.call(this._clientObject,elementId);}},executeCalls:function(calls)
{for(var i in calls)
{var call=calls[i]
var args=[];for(var j in call.args)
{args.push(call.args[j].v);}
var targetObject=(call.type=="s"?this:this.clientObject);if(targetObject[call.method]!=null||typeof(targetObject[call.method])!="function")
{targetObject[call.method].apply(targetObject,args);}
else if(this._debug)
{console.error("Unable to handle response, no such method: clientObject."+call.method+"()");}}},isProcessingResponse:function()
{return this._inResponse;},_ajaxResponse:function(data)
{this._inResponse=true;this._lastRequest=null;clearTimeout(this._spinnerTimer);this._spinnerTimer=0;this.executeCalls(data.calls);this._hideSpinner();this._inResponse=false;},_ajaxError:function(xhr,status,error)
{this._lastRequest=null;if(xhr.status==500)
{var response=xhr.responseText;response=response.replace(/\r?\n/g,"").replace(/(<\/)/g,"\n$1").replace(/<[^>]+>/g,"").replace(/^\s+/,"").replace(/\s+$/,"");if(this._debug)
console.error(response);}
this._hideSpinner();alert("Helaas...\n\n"
+"Er is een fout opgetreden bij het communiceren met de server.\n"
+"Probeer het later a.u.b. nogmaals");},_showSpinner:function()
{var callbacks=this._events.showSpinner
for(i in callbacks)
callbacks[i].call(this._clientObject);},_hideSpinner:function()
{var callbacks=this._events.hideSpinner;for(i in callbacks)
callbacks[i].call(this._clientObject);},alert:function(message)
{this._hideSpinner();window.alert(message);},debug:function(type,message)
{if(!this._debug)return;switch(type)
{case"print":console.log("** SERVER PRINT OUTPUT **\n\n"+message);break;default:console.log(message);}},replaceHtml:function(elementId,newHtml)
{var el=document.getElementById(elementId);if(el==null)
{if(this._debug)
console.error("sis.ajax.replaceHtml() - the element '"+elementId+"' does not exist.");return;}
$(el).replaceWith(newHtml);var callbacks=this._events.replaceHtml;for(i in callbacks)
{var slot=callbacks[i];if(slot[0]==elementId)
slot[1].call(this._clientObject,elementId);}}});