function getHTTPObject() {
	var xhr = false;
	if(window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
			}
		}
	}
	return xhr;
}

function prepareForm() {
	if(!document.getElementById) { //check we can use getElementById in this browser
		return;
	}
	if(!document.getElementById("domainform")) { //check the form is present
		return;
	}
	document.getElementById("domainform").onsubmit = function() { // set private function on the onsubmit event
		var data = "";
		for (var i=0; i<this.elements.length; i++) {
			if (this.elements[i].value == "")
			{
				return false; // break out if the fields are blank
			}
			// build the query string
			data+= this.elements[i].name;
			data+= "=";
			data+= escape(this.elements[i].value);
			data+= "&";
		}
		return !sendData(data); // call the send request function
	};
}

function sendData(data) {
	var phpscript = "/domLookup.php"; // this is the php file doing the lookup

	var request = getHTTPObject(); // instantiate the getHTTPObject
	if (request) {
		request.onreadystatechange = function() {  // set event handler for onreadystatechange
			parseResponse(request); 
		};
		request.open("POST", phpscript, true);  // prepare to post
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // this is required for POST requests
		request.send(data);  // send the request
		return true;
	} else {
		return false;
	}
}

function parseResponse(request) {
	if (request.readyState == 4) {  // readyState 4 means it has completed
		if (request.status == 200 || request.status == 304) {  // check we got the correct http status back
			//alert(request.responseText); // for testing
			var container = document.getElementById("whois_result"); // this is where we want to put the response info
			container.innerHTML = request.responseText;  // pop it in the HTML
			//alert(openWhois);
			prepareForm(); // re-prep the form
		}
	}
}


