/**-----------------------------------------------------------------------------
*   Ajax init
*/
    //--------------------------------------------------------------------------
    // output html element
        var HTMLELEMENT;
		
	// last called script
		var AJAX_LAST_PHPSCRIPT;

    // holds an instance of XMLHttpRequest
        var xmlHttp = createXmlHttpRequestObject();

    //--------------------------------------------------------------------------
    // creates an XMLHttpRequest instance
        function createXmlHttpRequestObject()
        {
          // will store the reference to the XMLHttpRequest object
          var xmlHttp;
          // this should work for all browsers except IE6 and older
          try
          {
            // try to create XMLHttpRequest object
            xmlHttp = new XMLHttpRequest();
          }
          catch(e)
          {
            // assume IE6 or older
            var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                            "MSXML2.XMLHTTP.5.0",
                                            "MSXML2.XMLHTTP.4.0",
                                            "MSXML2.XMLHTTP.3.0",
                                            "MSXML2.XMLHTTP",
                                            "Microsoft.XMLHTTP");
            // try every prog id until one works
            for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
            {
              try
              {
                // try to create XMLHttpRequest object
                xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
              }
              catch (e) {}
            }
          }
          // return the created object or display an error message
          if (!xmlHttp)
            alert("Error creating the XMLHttpRequest object.");
          else
            return xmlHttp;
        }

/**-----------------------------------------------------------------------------
*   Send question to php script
*/
function ajax(OUTELEMENT,PHPSCRIPT,METHOD)
{
    METHOD = typeof(METHOD) != 'undefined' ? METHOD : "GET";

    AJAX_LAST_PHPSCRIPT = typeof(PHPSCRIPT) != 'undefined' ? PHPSCRIPT : AJAX_LAST_PHPSCRIPT;

    // show progress bar
        HTMLELEMENT = typeof(OUTELEMENT) != 'undefined' ? OUTELEMENT : HTMLELEMENT;
        document.getElementById(HTMLELEMENT).innerHTML = '<img border="0" src="data/img/cmn/pb.gif" />';

    // proceed only if the xmlHttp object isn't busy
        if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
        {
            // execute the page from the server
                xmlHttp.open(METHOD, AJAX_LAST_PHPSCRIPT, false);
            // define the method to handle server responses
                xmlHttp.onreadystatechange = ajax_answer;
            // make the server request
                xmlHttp.send(null);
				
			// update the client display using the data received from the server
				document.getElementById(HTMLELEMENT).innerHTML = xmlHttp.responseText;
        }
        else
            // if the connection is busy, try again after x mseconds
                setTimeout('ajax(\''+OUTELEMENT+'\',\''+PHPSCRIPT+'\',\''+METHOD+'\')', 100);
}


/**-----------------------------------------------------------------------------
*   Get answer from php script
*/
function ajax_answer()
{
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
    {
        // update the client display using the data received from the server
            document.getElementById(HTMLELEMENT).innerHTML = xmlHttp.responseText;
    }
}
