/*************************************************************
 * getXhr
 * Create AJAX object instanciation
 *
 * @return	object	AJAX object
 *************************************************************/
function getXhr()
{
    var xhr = null;
	if(window.XMLHttpRequest) // Firefox and co
	   xhr = new XMLHttpRequest();
	else if(window.ActiveXObject){ // Internet Explorer
	   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else { // XMLHttpRequest not supported by the browser
	   xhr = false;
	}
    return xhr;
}

/*************************************************************
 * executeAjaxRequest
 * Execute AJAX request and put the returned string in div
 *
 * @param	string	parameters to send with the request
 * @param	string	id to which the result will be printed
 * @param	string	function that will be called after the response
 * @param	string	parameter to send to the additional function
 *************************************************************/
function executeAjaxRequest(server_process_name, ajax_div, params, additional_function, additional_parameter)
{
	var ajax_request = "ajax=1";
	ajax_request += "&ajax_process=" + server_process_name;
	for(var k in params)
	{
		ajax_request += "&" + k + "=" + params[k];
	}
	
	var xhr = getXhr();
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			ajax_response = xhr.responseText;
			
			
			objdiv=	document.getElementById(ajax_div);
			if(objdiv!=null)
				objdiv.innerHTML = ajax_response;
			if(additional_function)
			{
				// initialize additional_parameter with the ajax response if not exists
				//(additional_parameter != null) ? null : additional_parameter = ajax_response;
				if(additional_parameter != null)
				add_parameter = additional_parameter.split("|");
				add_param1 = add_parameter[0];
				add_param2 = add_parameter[1];
				eval("" + additional_function + "('" + add_param1 + "','"+add_param2+"')");
			}
		}
	}
	xhr.open("POST",  ( typeof(ajax_path) != 'undefined'  ? ajax_path : '') + "ajax_functions.php?no_redirect" ,true);
	xhr.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); 
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send(ajax_request);
}

function doAJAX(server_process_name, el_id, params)
{
	var request = "ajax=1";
	request += "&ajax_process=" + server_process_name;
	for(var k in params)
	{
		request += "&" + k + "=" + params.eval(k);
	}
	executeAjaxRequest(request, el_id, null, null);
}


