/*********************************************************************
 * Constants
 *********************************************************************/
var http = null;
http = getXmlHttpRequestObject();
var browser ='';
var file_extension = '.php';
var functionName;
var functionArgs;

/*********************************************************************
 * Sends a POST request and the response is set as innerHTML into 
 * the element identified by 'elementID'
 *********************************************************************/
function replaceResponse(url, postData, elementID)
{
    postPage(url, postData, 'replaceInnerHTML', elementID);
}
/*********************************************************************
 * This function sends an Ajax POST request
 *********************************************************************/
function postPage(url, postData, returnFunctionName, args, isAssync)
{
    if (isAssync==null)
	{
		isAssync=true;
	}
	if(http.readyState != 4 && http.readyState !=0)
    {
        setTimeout("postPage(\'"+url+"\', \'"+postData+"\', \'"+returnFunctionName+"\', \'"+args+"\')",600);
        return;
    }
    else
    {
        functionName = returnFunctionName;
        functionArgs = args;
        postData = 'url=' + url + '&' + postData+'&uniqueId='+(new Date()).getTime(); 
        http.open("POST",'controller.php',isAssync);
        http.onreadystatechange = handleResponse;
        http.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        http.setRequestHeader("ajax-header","true");
        http.send(postData);
    }
}
/*********************************************************************
 * Sets the innerHTML of the element identified by 'elementID'
 *********************************************************************/
function replaceInnerHTML(html, elementID)
{
    if (html == 'associe') {
		associese();
	}
	else {
		var element = get(elementID);
		if (element) {
			element.innerHTML = '';
			element.innerHTML = html;
		}
	}
}
/*********************************************************************
 * Returns the element identified by 'id'
 *********************************************************************/
function get(id)
{
    return document.getElementById(id);
}
/*********************************************************************
 * Returns an instance of the XML HTTP Request Object 
 *********************************************************************/
function getXmlHttpRequestObject()
{
    if(http != null) return http;
    var http_request = false;
    if (window.XMLHttpRequest)
    { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType)
        {
            http_request.overrideMimeType('text/xml');
        }
    }
    else if (window.ActiveXObject)
    { // IE
        try
        { http_request = new ActiveXObject("Msxml2.XMLHTTP");}
        catch (e)
        {
            try
            {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e2) 
            { 
                return;
            }
        }
    }
    if (!http_request)
    {
        alert('Error : Cannot instantiate XMLHTTP.');
        return false;
    }
    return http_request;
}
/*********************************************************************
 * This is the function that is called when the XML HTTP Object 
 * change state it will call the function passed as parameter with 
 * the arguments also passed as parameters
 *********************************************************************/
function handleResponse()
{
    if(http.readyState == 4)
    {
        loadingMsg(1);			
        
        if (http.status == 200)
        {
            var evalValue =  '';
            evalValue =	functionName + '(http.responseText, functionArgs);';
            eval(evalValue);
        }
		else if (http.status == 403)
		{
			window.location="index.php";
		}
        else
        {
            alert(http.status + ' - Error Loading page.', functionArgs);
        }
    }
    else
    {
        loadingMsg(0);
    }
}

function loadingMsg(toggle)
{
    var loadingElement='loadingelement';
	if(toggle == 0)
	{
		get(loadingElement).style.visibility='visible';
	}
	else
	{
		get(loadingElement).style.visibility='hidden';
	}
}
