// Global Variables ------------------------------------------------------------------------------------

var xhr = null; 	// XMLHttpRequest Object
var isIE; // True if browser is Internet Explorer
var responseTime;
var responseStart;
var showResponseTime = false;

// End Global Variables --------------------------------------------------------------------------------

// Functions -------------------------------------------------------------------------------------------

// Handles ReadyStateChange
function processReadyStateChange(){
  // check readyState for 'loaded' status
	if(xhr.readyState == 4){
		//check status for 'OK'
		if(xhr.status == 200){
			if(xhr.responseText != ""){
	      eval(xhr.responseText);
	      responseTime = Math.abs((new Date()).getUTCMilliseconds() - responseTimeStart);
	      if(showResponseTime){
	        alert("Response Time: " + responseTime + "ms");
	      }// end if
	      initXhr();
			}// end if
		}
		else{
			alert("An error occurred while retrieving the XML data: " + xhr.statusText);
		}// end if/else
	}// end if readyState is 'loaded'
	
}// end processReadyStateChange

function getReturnObject(url){
  responseTimeStart = (new Date()).getUTCMilliseconds();
	url = new String(url);
	var myPage = url.substring(0, url.indexOf('?'));
	var myVars = url.substring(url.indexOf('?')+1, url.length);

	var myReturnObject = null;
	
	xhr.open("POST", myPage, true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
  xhr.send(myVars);

}// end getReturnObject

function getReturnCode(url){
  responseTimeStart = (new Date()).getUTCMilliseconds();
  url = new String(url);
	var myPage = url.substring(0, url.indexOf('?'));
	var myVars = url.substring(url.indexOf('?')+1, url.length);
	
	if(xhr == null){
	  initXhr();
	}// end if
	
	if(xhr.readyState != 0){
		xhr.abort();
	}// end if
	

	xhr.open("POST", myPage, true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.send(myVars);
  
}// end getReturnObject

// Executes initialization code for this module
function initCommonServices(){

  initXhr();
	
}// end initCommonServices

// Creates the global xhr object
function initXhr(){
	
	try{
		xhr = null;
		
		if(window.XMLHttpRequest){
			
			isIE = false;
			
			// Handle object creation for Mozilla
			xhr = new XMLHttpRequest();
			xhr.onreadystatechange = processReadyStateChange;
			//xhr.open("POST", url, true);
			//xhr.send(null);
			
		}
		else{
			
			isIE = true;
			
			// Handle object creation for IE
			try{
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e){
				try{
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e2){
					xhr = null;
				}// end try/catch
			}// end try/catch
			
			if(xhr){
				xhr.onreadystatechange = processReadyStateChange;
				//xhr.open("POST", url, true);
				//xhr.send();
			}
			else{
				alert("An error occurred while attempting to create XMLHttpRequest.");
			}// end if/else xhr is not null
			
		}// end if/else
	}
	catch(e){
		alert("An error occurred while attempting to create XMLHttpRequest: " + e.description + ".\nPlease ensure your browser is compatible.");
	}// end try/catch
	
}// end initXhr

// End Functions ---------------------------------------------------------------------------------------

initCommonServices(); // Executes initialization code for this module

