// Copyright 2009, BMT Defence Services Ltd, All rights reserved.

function bmtAjaxDataService(zMethod,oObject,oOnSuccess,oOnFail){if(!oOnFail)oOnFail=bmtAjaxLoginFail;var zpWebService;if(bmBMTAjaxPage){if(bmBMTAjaxAjaxEnabled)zpWebService="PageMethods";else{if(document.location.search)zpWebService=document.location.href.substring(0,document.location.href.lastIndexOf(document.location.search));else zpWebService=document.location.href;}}else{if(bmBMTAjaxAjaxEnabled)zpWebService=zmBMTAjaxWebService;else zpWebService=bmtWebResolveURL("~/"+zmBMTAjaxWebService+".aspx");}if(bmBMTAjaxAjaxEnabled){if(oObject!=null)window[zpWebService][zMethod].call(null,oObject,oOnSuccess,oOnFail);else window[zpWebService][zMethod].call(null,oOnSuccess,oOnFail);}else{if(!omBMTAjax)omBMTAjax=new Object();var opBMTAjax=new XMLHttpRequest();opBMTAjax.open("POST",zpWebService,zMethod,true);if(oObject||oObject=="")opBMTAjax.send(Sys.Serialization.JavaScriptSerializer.serialize(oObject));else opBMTAjax.send();omBMTAjax[opBMTAjax.id]=new Object();omBMTAjax[opBMTAjax.id].onSuccess=oOnSuccess;}}
function bmtAjaxLoginFail(){window.top.location=bmtWebResolveURL("Default.aspx");}
function bmtAjaxDataServicePage(zMethod,oObject,oOnSuccess,oOnFail){var bpBMTAjaxPage=bmBMTAjaxPage;bmBMTAjaxPage=true;bmtAjaxDataService(zMethod,oObject,oOnSuccess,oOnFail);bmBMTAjaxPage=bpBMTAjaxPage;}
function bmtAjaxSetDataService(zDataService){zmBMTAjaxWebService=zDataService;}
function bmtAjaxUseDataService(zDataService){bmBMTAjaxPage=false;if(zDataService)bmtAjaxSetDataService(zDataService)}
function bmtAjaxUsePage(){bmBMTAjaxPage=true;}
function bmtAjaxSetContainer(oElement){omBMTAjaxContainer=oElement;}var bmBMTAjaxAjaxEnabled=true;var bmBMTAjaxIE7=true;var omBMTAjaxCheck;var omBMTAjax;var lmBMTAjaxIframeNumber=0;var bmBMTAjaxPage=false;var omBMTAjaxContainer=document.body;var zmBMTAjaxWebService="DataService";try{omBMTAjaxCheck=new XMLHttpRequest();}catch(e){try{omBMTAjaxCheck=new ActiveXObject("Microsoft.XMLHTTP");bmBMTAjaxIE7=false;}catch(e){bmtAjaxSetupNonAjax();}}omBMTAjaxCheck=null;bmtAjaxSetupNonAjax();
function bmtAjaxSetupNonAjax(){bmBMTAjaxAjaxEnabled=false;XMLHttpRequest=function(){this.method="POST";this.url=null;this.async=true;this.iframe=null;this.responseText=null;this.header=new Object();this.id="_xmlhttp_"+lmBMTAjaxIframeNumber++;this.service=null;this.serverMethod=null;};XMLHttpRequest.prototype.open=function(method,service,serverMethod,async){this.method=method;this.url="~/"+service+"/"+serverMethod;this.service=service;this.serverMethod=serverMethod;this.async=async;this.readyState=0;this.iframe=document.createElement("IFRAME");this.iframe.style.visibility="hidden";this.iframe.id=this.id;if(document.getElementById(this.id)==null)omBMTAjaxContainer.appendChild(this.iframe);this.setRequestHeader("___xmlhttp","iframe");};XMLHttpRequest.prototype.setRequestHeader=function(name,value){this.header[name]=value;};XMLHttpRequest.prototype.send=function(data){var html=[];html[html.length]='<html><body><form target="'+this.iframe.id+'_submit" method="'+this.method+'" action="'+this.service+'">';html[html.length]='<textarea name="IframeID">'+this.iframe.id+'</textarea>';html[html.length]='<textarea name="service">'+this.serverMethod+'</textarea>';for(name in this.header)html[html.length]='<textarea name="'+name+'">'+this.header[name]+'</textarea>';if(data!=null&&data.length>0)html[html.length]='<textarea name="_data">'+data+'</textarea>';html[html.length]='</form><iframe name="'+this.iframe.id+'_submit"></iframe></body></html>';this.iframe.contentWindow.document.write(html.join('').replace(/\'/g,"\\'").replace(/\r\n/g,"\\r\\n"));this.iframe.contentWindow.document.forms[0].submit();};XMLHttpRequest.prototype._onreadystatechange=function(){};}
function bmtAjaxLoaded(zIframeID){var zpResponseText=window.frames[zIframeID].document.frames[0].document.getElementById("txtData").value;if(omBMTAjax){if(omBMTAjax[zIframeID]){if(omBMTAjax[zIframeID].onSuccess){if(zpResponseText!=""){var opObject=Sys.Serialization.JavaScriptSerializer.deserialize(zpResponseText);omBMTAjax[zIframeID].onSuccess.call(null,opObject);}}delete omBMTAjax[zIframeID];omBMTAjaxContainer.removeChild(document.getElementById(zIframeID));}}}
