var xmlHttp = false;
var browserType;
function createXMLHttpRequest() {
// CHECKS IF MOZILLA OR IE
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		xmlHttp = new XMLHttpRequest();
		if (xmlHttp.overrideMimeType) {
			xmlHttp.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
}

var logcount = 0;
function handleStateChange() {
	if (logcount == 10) {
		document.getElementById('testlog').innerHTML = "";
		logcount = 0;
	}
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
		  document.getElementById('statusmsg').innerHTML = "Processing request. Status: " + xmlHttp.status + " ReadyState: " + xmlHttp.readyState;
			var response = xmlHttp.responseText;
			var update = new Array();
			
			if(response.indexOf('|' != -1)) {
			    // SUCCESSFUL RESPONSE
       	     	update = response.split('|');
                for(i=0;i < update.length; i=i+2) {
                   	document.getElementById(update[i]).innerHTML = update[i+1];
                }                
                document.getElementById('statusmsg').innerHTML = "Successful transaction.";
                //document.getElementById('testlog').innerHTML = "(" + getTime() + ") Success - " + calledfunction + "<br />" + document.getElementById('testlog').innerHTML;
				logcount++;

         	} else if (response.indexOf(':' != -1)) {
             	// FATAL ERROR MESSAGE
                document.getElementById('statusmsg').innerHTML = response;
                document.getElementById('testlog').innerHTML = "Error - " + calledfunction + "<br />" + document.getElementById('testlog').innerHTML;
				logcount++;
          	} else {
              	// NO RESPONSE
                document.getElementById('statusmsg').innerHTML = "No response.";
                document.getElementById('testlog').innerHTML = "No response - " + calledfunction + "<br />" + document.getElementById('testlog').innerHTML;
				logcount++;
            }

		} else if (xmlHttp.status == 404) {
        	// RESPONDER FILE NOT FOUND
            document.getElementById('statusmsg').innerHTML = "Unable to connect to responder (404).";
            document.getElementById('testlog').innerHTML = "No connection (404) - " + calledfunction + "<br />" + document.getElementById('testlog').innerHTML;
			logcount++;
		}
	} else {
    	document.getElementById('statusmsg').innerHTML = "Processing request. Status: " + xmlHttp.status + " ReadyState: " + xmlHttp.readyState;
	}
}

function startRequest(functionName,argform) {
    createXMLHttpRequest();
    calledfunction = functionName;
    xmlHttp.onreadystatechange = handleStateChange;
    argelem = "";
    document.getElementById('statusmsg').innerHTML = "Test";
    if(argform) {
    	arg = document.getElementById(argform);
        for(i=0; i < arg.childNodes.length; i++) {
        	if(arg.childNodes[i].type != null && arg.childNodes[i].type != "button" && arg.childNodes[i].type != "span") {
            	if(arg.childNodes[i].type == "checkbox") {
                  	if(arg.childNodes[i].checked) {
                    	argelem += arg.childNodes[i].value + "`";
                  	} else {
                        argelem += "`";
                    }
               	} else {
                  	argelem += arg.childNodes[i].value + "`";	
				}				
          	}
       	}
        argelem = argelem.substring(0,argelem.length - 1);
		//document.getElementById('testlog').innerHTML += "<br />String : " + argelem;
  	}
    //argument = document.getElementById(arg).value;
    //document.getElementById('statusmsg').innerHTML = arguments;
    xmlHttp.open("GET", "respond.php?call=" + functionName + "&args=" + argelem, true);
    //xmlHttp.open("POST", "respond.php?", true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
    sendquery = "call=" + functionName + "&args=" + argelem;
    //xmlHttp.send(sendquery);
    xmlHttp.send(null);
}