var m_AjaxLoader = null;
var m_AjaxAsync = true;
var m_AjaxContainer = "";
var m_AjaxWorker = "";

function GetFormValues(input)
{
	var values = "";
	var AND = "";
	var field;
	for(var i=0;i<input.elements.length; i++)
	{
		field = input.elements[i];
		if(field != null &&	((field.type != "checkbox" && field.type != "radio") ||	field.checked))
		{
			values += (AND + field.name + "=" + escape(field.value));
			AND = "&";
		}
	}
	return values;
}

function SetWorker(w, msg)
{
	m_AjaxWorker = w;
	var aw = document.getElementById(m_AjaxWorker);
	if(aw)
	{
		if(!msg)
			msg = "<img src='images/loading.gif' align='middle'>";
		aw.innerHTML = msg;
	}
}

function LoadPage(input, target, async)
{
	var url;
	var method = "GET";
	if(typeof(input) == "object")
	{
		if(input.tagName == "FORM")
		{
			url = input.action;
			if(input.method != "")
				method = input.method;
			method = method.toUpperCase();
			if(method == "GET")
				url += "?"+GetFormValues(input);
		}
	 	else if(input.tagName == "A")
		{
			url = input.href;
		}
	}
	else if(typeof(input) == "string")
	{
		url = input;
	}
	else
	{
		return;
	}
	try
	{
		// Moz supports XMLHttpRequest. IE uses ActiveX.
		// browser detction is bad. object detection works for any browser
		m_AjaxLoader = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e)
	{
		// browser doesn't support ajax. handle however you want 
		location.href = "index.php?page="+url;
		return;
	}
	m_AjaxContainer = target;
	if(m_AjaxWorker != "")
	{
		var sr = document.getElementById(m_AjaxWorker);
		if(sr)sr.style.display = "block";
	}
	// the m_AjaxLoader object triggers an event everytime the status changes
	// AjaxCatchResult() function handles the events
	if(typeof(async) == "undefined")
		m_AjaxAsync = true;
	else
		m_AjaxAsync = async;

	if(m_AjaxAsync)
		m_AjaxLoader.onreadystatechange = OnStateChange;
	else
 		m_AjaxLoader.onreadystatechange = function(){return false;};
    //m_AjaxLoader.setRequestHeader("Content-type", "text/html");
	// open takes in the HTTP method and url.  
	try
	{
		m_AjaxLoader.open(method, url, m_AjaxAsync);
		// send the request. if this is a POST request we would have
		// sent post variables:	send("name=aleem&gender=male)
		if(method == "POST")
		{
			m_AjaxLoader.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			m_AjaxLoader.send(GetFormValues(input));
		}
		else
		// IE expects a value here, hence we do send(null);
		m_AjaxLoader.send(null);
	}
	catch(e)
	{
		alert(e);
	}
	return false;
}

function OnStateChange()
{
	// if the readyState code is 4 (Completed)  
	// and http status is 200 (OK) we go ahead and get the responseText  
	// other readyState codes:  
	// 0=Uninitialised 1=Loading 2=Loaded 3=Interactive 
	var result = false;
	if (m_AjaxLoader.readyState == 4)
	{
		if(m_AjaxWorker != "")
			document.getElementById(m_AjaxWorker).style.display = "none";
		switch(m_AjaxLoader.status)
		{
		case 200:
		case 304:
			if(m_AjaxLoader.responseText.substring(0,6) == "ERREUR")
			{
				alert(m_AjaxLoader.responseText);
				result = false;
				break;
			}
			else
			{
				if(m_AjaxContainer != "")
					document.getElementById(m_AjaxContainer).innerHTML = m_AjaxLoader.responseText;
				result = true;
			}
			break;
		default:
			alert("La page démandée est introuvable!");
		}
	}
	//alert(m_AjaxLoader.readyState + " - " + m_AjaxLoader.status + " - " + result);
	return result;
}
