/**
 * wrapper function for contructing a request object
 * Parameters:
	@reqType:		The HTTP request type: GET/POST
	@url:			The URL of the server app
	@asynch:		Whether to send the request asynchronously or not
	@respHandle:	The name of the function that will handle the response
 * Any 5th params, represented as arguments[4], 
 * are the data a POST request is designed to send
 */
var request = null; // global
function httpRequest(reqType, url, asynch, respHandle)
{
	// Mozilla-based
	if (window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject)
	{
		request = new ACtiveXObject('Msxml2.XMLHTTP');
		if (!request)
			request = new ActiveXObject('Microsoft.XMLHTTP');
	}

	// request could still be null if neither ActiveXObject
	// initialization succeeded
	if (request)
	{
		// if the reqType param is POST, the the
		// 5th argument to the function is the POSTed data
		if (reqType.toLowerCase() != "post")
		{
			initReq(reqType, url, asynch, respHandle);
		}else{
			// the POSTed data
			var args = arguments[4];
			if (args != null && args.length > 0)
			{
				initReq(reqType, url, asynch, respHandle, args);
			}
		}
	}else{
		alert("Your browser does not permit the use of all of this application's features.");
	}
}
/**
 * initialize a request object that is already constructed
 */
function initReq(reqType, url, bool, respHandle)
{
	try {
		// specify the function that will handle the HTTP response
		request.onreadystatechange = respHandle;
		request.open(reqType, url, bool);
		// if the reqType param is POST, then the
		// 5th argument to the function is the POSTed data
		if (reqType.toLowerCase() == "post")
		{
			request.setRequestHeader("Content-Type", "application/x-www-urlencoded; charset=UTF-8");
			request.send(arguments[4]);
		}else{
			request.send(null);
		}
	}catch(e){
		alert("The application cannot contact the server at the moment.\nPlease try again in a few seconds.\nError: "+e.message);
	}
}
// insert free trial
function insertFreeTrial(_email)
{
	url = 'ajaxFreeTrial.php?email='+_email;
	httpRequest('GET', url, true, freeTrialReturnFunc);
}

// return function
function freeTrialReturnFunc()
{
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{
			var returnID;
			if (request.responseText)
			{
			    retval	= request.responseText.split('=');
			    returnID	= retval[1];
			}
			document.getElementById('freeTrialLink').style.display='';
			document.getElementById('btnSubmit').style.display='none';
			document.getElementById('emailField').style.display='none';
		}
	}
}	
// machineID
function captureMachineID(_machineID)
{
	url = 'ajaxCaptureMachineID.php?machineID='+_machineID;
	httpRequest('GET', url, true, captureMachineIDFunc);
}

// return function
function captureMachineIDFunc()
{
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{
			var success;
			if (request.responseText)
			{
			    retval	= request.responseText.split('=');
			    success	= retval[1];
			}
		}
	}
}	

