// hold and instance of XMLHttpRequest
var xmlHttp = createXMLHttpRequestObject();

// creates an XMLHttpRequest Instance
function createXMLHttpRequestObject(){
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	//this should work for all browsers except IE6 and older
	try{
		//try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	} catch(e){
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		//try every prog id until on works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
			try{
				// try to create XMLHTTPRequest Object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			} catch (e){}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
		alert("Error creating the XMLHttpRequest Object.");
	else
		return xmlHttp;
}

// called to read a file fromt the server
function process(action, value1, value2){
	// only continue if xmlHttp isn't void
	if (xmlHttp){
	var asc = true;
    var todo;
        switch (action){
            case 'check_the_domain':
				message = '&nbsp;Checking, please wait...';
				try{
				xmlHttp.open("GET", 'check_domain.php?sld=' + value1 + '&tld=' + value2, true);
				xmlHttp.onreadystatechange = handleRequestStateChange;
				xmlHttp.send(null);
				}
				// display the error in case of failure
				catch(e){
					alert("Can't connect to the server:\n" + e.toString());
				}
                break;
			case 'get_load':
				message = '&nbsp;Checking, please wait...';
				try{
				xmlHttp.open("GET", 'display_load.php?action=doit', true);
				xmlHttp.onreadystatechange = handleRequestStateChangeLoad;
				xmlHttp.send(null);
				}
				// display the error in case of failure
				catch(e){
					alert("Can't connect to the server:\n" + e.toString());
				}
                break;
        }
		// try to connect to the server
	}
}

// function that handles the HTTP response
function handleRequestStateChange(){
	// obtain a reference to the <div> element on the page
	myDiv = document.getElementById("myDivElement");
	myDiv.innerHTML = '<img style="vertical-align:middle" src="images/loading.gif">' + message;

     if (xmlHttp.readyState == 4){ 
		// continue only if HTTP status is 200 "ok"
		if (xmlHttp.status == 200){
			try{
				// read the message from the server
				response = xmlHttp.responseText;
				// display the message
				myDiv.innerHTML = response;
			} catch(e){
				// display error message
				alert ("Error reading the reponse: " + e.toString());
			}
		} else {
			// display status message
			alert ("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
}

function handleRequestStateChangeLoad(){
	// obtain a reference to the <div> element on the page
	myDiv = document.getElementById("myDivElement");
	myDiv2 = document.getElementById("myDivElement2");
	//myDiv.innerHTML = '<img style="vertical-align:middle" src="images/loading.gif">' + message;

     if (xmlHttp.readyState == 4){ 
		// continue only if HTTP status is 200 "ok"
		if (xmlHttp.status == 200){
			try{
				// read the message from the server
				response = xmlHttp.responseText;
				var response2=response.split(","); 
				
				// display the message
				myDiv.innerHTML = 'Current Server Load: '+response2[0];
                myDiv2.innerHTML = 'Current Server Load: '+response2[1];
			} catch(e){
				// display error message
				alert ("Error reading the reponse: " + e.toString());
			}
		} else {
			// display status message
			alert ("There was a problem retrieving the data: \n" + xmlHttp.statusText);
		}
	}
  }