var request = null;

function sendData(){

	
	if ( !validateInput() ){
		return false;
	}
	else{
		
		var url = "http://www.cameronmoll.com/sandbox/mobile/checker.php5";
		
		httpRequest("POST",url,true, handleResponse, setQueryString() );
	
	}
}

function validateInput(){
  
  return true;

}

function setQueryString(){
	
	var queryString="";
	
	var ele = document.getElementById("userInput");

	var value = encodeURIComponent(ele.value);

	queryString +="q="+value;
	
	alert("queryString "+queryString);

	return queryString;
}

function handleResponse(){
	alert("handleResponse() called");
	if ( request.readyState==4 ){
	alert("response: " + request.responseText);
		if(request.status == 200 ){
		
			var result = document.getElementById("result");
			result.innerHTML = request.responseText;
		}

		else{
			var status_number = request.status;
			alert("Request Status: " + status_number );	
			alert("A problem occurred with communicating between the client and the server.\n" + request.statusText);
		}
	}
	else{
		alert("response: "+request.readyState);
	}//end outer if
}



/*
	wrapper function for constructing a request object
	param:
		reqType: POST only
		url: url of server program
		asynch: send the request asynchronously or not
		respHandle: name of the function that will handle the response
*/
function httpRequest(reqType,url,asynch, respHandle){
	if(window.XMLHttpRequest){

		request = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){
		//IE browsers
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (!request){
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	if (request){

		if (reqType.toLowerCase() != "post"){
			alert("request is not post");
			initReq(reqType,url,asynch);
		}
		else{

			var data = arguments[4];

			if ( data != null && data.length > 0 ){

				initReq(reqType,url,asynch,respHandle,data);
			}
			else{

				}
	
			
		}
	}
	else{
		alert("Please download a morden browser");	
	}

}

function initReq ( reqType,url, asynch, respHandle ){
	try{
		alert("initReq");
		respHandle();
	/*specify the function that will handle the HTTP response*/
		request.onreadystatechange=respHandle;

		request.open(reqType,url,asynch);
		
		if (reqType.toLowerCase() == "post" ){

			//alert("in initReq POST");
			/*Set the Content-Type header for a POST request*/
			request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");

			request.send(arguments[4]);
		}
		else{
			alert("send null");
			request.send(null);
		}
	}
	catch(errv){
		alert("The application cannot contact the server at the moment.\n" +
			  "Error detail: "+errv.message);
	}
}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		alert("on +evType");
		elm['on' + evType] = fn;
	}
}




window.onload = function(){

  //	addEvent(userInput, 'click', sendData, false);

}

