//*****************************************************************************
// Copyright 2007 Starscale Inc.

ibi.Ajax={ExpectedLoggedIn:null,RequestTimeout:10000,LastKnownServerTime:null}
ibi.Ajax.Request=Class.create(Ajax.Request,{initialize:function($super,url,options){options.encoding=false;options.on403=options.on403||(function(request,object){window.location.reload();}).bind(this);options.requestHeaders={'Accept':'text/javascript, text/html, application/xhtml+xml, application/json, */*'};var onCreate=options.onCreate||Prototype.emptyFunction;options.onCreate=(function(request,object){var onFail=options.onFailure||Prototype.emptyFunction;this.request['timeoutId']=window.setTimeout((function(){handleAjaxTimeout(this,onFail)}).bind(this),options.requestTimeout||ibi.Ajax.RequestTimeout);onCreate(request,object);}).bind(this);var onSuccess=options.onSuccess||Prototype.emptyFunction;options.onSuccess=function(request,object){this.prv_setServerTime(request);onSuccess(request,object);}.bind(this);var onFailure=options.onFailure||Prototype.emptyFunction;options.onFailure=function(request,object){this.prv_setServerTime(request);onFailure(request,object);}.bind(this);var onComplete=options.onComplete||Prototype.emptyFunction;options.onComplete=(function(request,object){window.clearTimeout(this.request['timeoutId']);if(!options.ignoreLoggedIn){var headerMatch=(request.getResponseHeader('X-Starscale-Ajax')||'').match('loggedIn=(.*)');if(!headerMatch){return;}
var serverLoggedIn=headerMatch[1]=="1";if(!ibi.Ajax.ExpectedLoggedIn){ibi.Ajax.ExpectedLoggedIn=serverLoggedIn;}
else{if(ibi.Ajax.ExpectedLoggedIn!=serverLoggedIn){options.on403(request,object);return;}}}
onComplete(request,object);}).bind(this);var onException=options.onException||Prototype.emptyFunction;options.onException=(function(request,ex){if(ex.name){ibi.log("**AjaxException: "+ex.name+": "+ex.message);}else{ibi.log("**AjaxException: "+ex);}
onException(request,ex);options.onFailure(request);});$super(url,options);},prv_setServerTime:function(request){var dateStr=request.transport.getResponseHeader('Date');if(dateStr){ibi.Ajax.LastKnownServerTime=Date.parse(dateStr);}}});ibi.Ajax.PhixRequest=Class.create(ibi.Ajax.Request,{initialize:function($super,phix,onSuccess,onFailure,options){options=options||{};options.onSuccess=function(request){var json=request.responseText.evalJSON();if(json.Status==phix.Status.Success){onSuccess(json);}else{onFailure(json.Status);}}
options.onFailure=function(){onFailure(phix.Status.GeneralFailure);};$super(phix.uriPath,options);}});ibi.Ajax.Updater=Class.create(ibi.Ajax.Request,{initialize:function($super,container,url,options){this.container={success:(container.success||container),failure:(container.failure||(container.success?null:container))}
options=options||{};var onComplete=options.onComplete||Prototype.emptyFunction;options.onComplete=(function(response,param){this.updateContent(response.responseText);if(Object.isFunction(onComplete))onComplete(response,param);}).bind(this);$super(url,options);},updateContent:function(responseText){var receiver=this.container[this.success()?'success':'failure'];var options=this.options;if(!options.evalScripts){responseText=responseText.stripScripts();}
if(receiver=$(receiver)){if(options.insertion){if(Object.isString(options.insertion)){var insertion={};insertion[options.insertion]=responseText;receiver.insert(insertion);}
else options.insertion(receiver,responseText);}
else{if(!Prototype.Browser.IE&&((this.getHeader('Content-Type')||'').match(/application\/xhtml\+xml/)!=null)){receiver.updateXHTML(responseText);}else{receiver.update(responseText);}}}
if(this.success()){if(this.onComplete)this.onComplete.bind(this).defer();}}});function handleAjaxTimeout(request,onFailure){if(callInProgress(request.transport)){if(Prototype.Browser.IE){request.transport.onreadystatechange=Prototype.emptyFunction;}
request.transport.abort();onFailure(request);}}
function callInProgress(transport){if(!transport)return false;switch(transport.readyState){case 0:case 4:return false;break;default:return true;break;}}