﻿var xmlhttp;

function processXMLHttp(whenFinishedFunction, whenFinishedFunctionArgs, XMLHttpPageURL, stringToPassToProcessingPage, whileProcessingFunction)
{
	if(whileProcessingFunction)
	{
		eval(whileProcessingFunction);
	}
  
	if(window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
	}else if(window.ActiveXObject){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}	
		catch (e) 
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState == 4){
			if(whenFinishedFunction){
				xmlHttpRequestComplete(xmlhttp, whenFinishedFunction, whenFinishedFunctionArgs);
			}else{
				xmlHttpRequestComplete(xmlhttp);
			}
		}
	}
	//if(window.XMLHttpRequest)
	if (document.all)
	{
	    xmlhttp.open("POST", XMLHttpPageURL, false);
	    
	}
	else
	{
	    xmlhttp.open("POST", XMLHttpPageURL, true);
	}
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(stringToPassToProcessingPage);
}

function xmlHttpRequestComplete(xmlhttp, functionToCall, functionArgs){
	doc = xmlhttp.responseXML;
    var root = doc.documentElement;
    if(root && root.hasChildNodes()){
		if(root.childNodes[0].nodeValue.search('ERROR:') != -1){
			var s = root.childNodes[0].nodeValue.replace('ERROR:', '');
			eval(s);
			return;
		}
    }
    if(functionToCall){
		if(functionArgs){
			functionToCall(xmlhttp, functionArgs);
		}else{
			functionToCall(xmlhttp);
		}
	}
}