// performs an AJAX query
function _ajaxGetRequest( url, funcSnippet ) {
	showLoadingDialog();
	if(!window._ajaxRequests){window._ajaxRequests = Object();}
	var theTime = (new Date()).getTime();
	while(_ajaxRequests[theTime]){
		theTime -= 1;
	}
	var theFunction = "var xmlhttp = _ajaxRequests['" + theTime + "'];"
	theFunction += "if( xmlhttp.readyState == 4 ) {";
	theFunction += "if( xmlhttp.status == 200 ) {";
	theFunction += "var responseObject = parseResponseData(xmlhttp.responseText);";
	theFunction += "if( responseObject.error ) {";
	theFunction += " showMessage('ERROR', responseObject.error );";
	theFunction += "} else{";
	theFunction += funcSnippet;
	theFunction += "} } else{  showMessage('ERROR', 'Error (' + xmlhttp.status + '): could not load the dynamic content' ); }";
	theFunction += "}";
	
	try {
		_ajaxRequests[theTime] = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); 
		_ajaxRequests[theTime].onreadystatechange = new Function( theFunction );
		_ajaxRequests[theTime].open("GET", url); 
		_ajaxRequests[theTime].setRequestHeader('referer',document.location.href);
		_ajaxRequests[theTime].setRequestHeader('If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT');
		_ajaxRequests[theTime].send(null); 
	} catch(e) { 
		showMessage("ERROR", "Error: could not load the dynamic content: " + e.message);
	}
}

function _ajaxPostRequest( url, data, funcSnippet ) {
	showLoadingDialog();
	if (url.indexOf('?') < 0)
		url = url + '?';

	if(!window._ajaxRequests){window._ajaxRequests = Object();}
	var theTime = (new Date()).getTime();
	while(_ajaxRequests[theTime]){
		theTime -= 1;
	}
	var theFunction = "var xmlhttp = _ajaxRequests['" + theTime + "'];"
	theFunction += "if( xmlhttp.readyState == 4 ) {";
	theFunction += "if( xmlhttp.status == 200 ) {";
	theFunction += "var responseObject = parseResponseData(xmlhttp.responseText);";
	theFunction += "if( responseObject.error ) {";
	theFunction += " showMessage('ERROR', responseObject.error );";
	theFunction += "} else{";
	theFunction += funcSnippet;
	theFunction += "} } else{  showMessage('ERROR', 'Error (' + xmlhttp.status + '): could not load the dynamic content' ); }";
	theFunction += "}";
	
	try {
		_ajaxRequests[theTime] = window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP"); 
		_ajaxRequests[theTime].onreadystatechange = new Function( theFunction );
		_ajaxRequests[theTime].open("POST", url, true); 
		_ajaxRequests[theTime].setRequestHeader('referer',document.location.href);
		_ajaxRequests[theTime].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		_ajaxRequests[theTime].setRequestHeader('If-Modified-Since','Wed, 15 Nov 1995 00:00:00 GMT');
		_ajaxRequests[theTime].send(data); 
	} catch(e) { 
		showMessage("ERROR", "Error: could not load the dynamic content: " + e.message);
	}
}


function parseResponseData( data ){
	try{
		var newData = data;
		if ( typeof( newData ) == 'string' ) {
			newData = newData.replace( /^\s+/m, '' );
			newData = newData.replace( /\s+$/m, '' );
			data = newData;
			newData = eval( newData );
		}

		if( typeof( newData ) == 'object' ){
			return newData;
		}
	}catch(ex){
		var x = new Object();
		x.error = ex.message + "\n<br/>\n" + data
		return x;
	}
}

