/** 
* Send an Ajax GET request to the server. 
* @param handle {function pointer} handler This is a function pointer that is excecuted when the response comes back
* @param url {string} url This is the URL to send the request to.
*/
function getAsyncHttp(url, handler)
{
    var xmlhttp = compat_makeXmlHttp();
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4)
        {
            handler(xmlhttp.status, xmlhttp.responseText);
        }
    }
    
    xmlhttp.open("GET", url, true);
    xmlhttp.setRequestHeader("X-Towersoft-Async", "1");
    xmlhttp.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
    xmlhttp.send("");    
}

/** 
* Send an Ajax POST request to the server. 
* @param handle {function pointer} handler This is a function pointer that is excecuted when the response comes back
* @param url {string} url This is the URL to send the request to.
*/
function postAsyncHttp(url, body, handler)
{
    var xmlhttp = compat_makeXmlHttp();
    
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4)
        {
            handler(xmlhttp.status, xmlhttp.responseText);
        }
    }
    
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("X-Towersoft-Async", "1");
    xmlhttp.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
    xmlhttp.send(body);    
}

/** 
* Parse the JSON (Javascript Object Notation) struct. 
* @param stuctText {javascript object} structText has information about the requested object. Looks something like this {editurl:"/record/edit", metatype:"recordtype"}
*/
function parseStruct(structText)
{
    var result;
	
	if(structText)
	{
		if (structText.startsWith('{') || structText.startsWith('['))
		{
			try
			{
				eval("result = " + structText + ";");
			}
			catch (e)
			{
				// nothing? Oh yearh...
			}
			
			return result;
		}
	
		
		var SEARCH = "<script language='javascript'><!--\ng_content=";
		var ENDSEARCH = "\n--></script>";
		
		var startPosition = structText.indexOf(SEARCH);
		var endPosition = structText.indexOf(ENDSEARCH);
		
		if (startPosition > -1 && endPosition > startPosition + SEARCH.length)
		{
			structText = structText.substring(startPosition + SEARCH.length, endPosition);
			
			try
			{
				eval("result = " + structText + ";");
			}
			catch (e)
			{
				// nothing? Oh yearh...
			}
			
			return result;
		}
		
		return result;
	}
}

/** 
* Send an Ajax POST request to the server. 
* @param handle {function pointer} handler This is a function pointer that is excecuted when the response comes back. 
* @param url {string} url This is the URL to send the request to.
*/
function postAsyncHttpStruct(url, body, handler)
{
    postAsyncHttp(url, body, function(status, text) { handler(status, parseStruct(text)); });
}

/** 
* Send an Ajax GET request to the server. 
* @param handle {function pointer} handler This is a function pointer that is excecuted when the response comes back
* @param url {string} url This is the URL to send the request to.
*/
function getAsyncHttpStruct(url, handler)
{
    getAsyncHttp(url, function(status, text) { handler(status, parseStruct(text)); });
}