// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var showErrors = true;
var direction = '';

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try
  {
    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
      {
         xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {} // ignore potential error
    }
  }
  // return the created object or display an error message
  if (!xmlHttp){
	 alert("Error creating the XMLHttpRequest object.");
  }
  else
    return xmlHttp;
}


function displayError($message)
{
  if (showErrors)
  {
    showErrors = false;
    alert("Error encountered: \n" + $message);
  }
}





//******************************************************************************//
//**						START SORT WINDOW		  					 		  **//
//******************************************************************************//

function sortOffres(sort)
{

  if (xmlHttp)
  {
	// try to connect to the server
    try
    {
      if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
      {

        // Display "Loading..." message while loading feed
        //document.getElementById("loading").style.display = "block";

        //document.getElementById('sendmessage').messagetext.value = '';

       // Call the server page to execute the server-side operation
        params = "sort=" + sort;
        xmlHttp.open("POST", "sort_offres.php", true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleHttp_sortOffres;
        xmlHttp.send(params);
      }
      else
      {
        // if connection was busy, try again after 1 second
        setTimeout("sortOffres('" + sort + "' );", 1000);
      }
    }
    // display the error in case of failure
    catch (e)
    {
      displayError(e.toString());
    }
  }
}


// function that retrieves the HTTP response
function handleHttp_sortOffres()
{
  // continue if the process is completed
  if (xmlHttp.readyState == 4)
  {
   // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        displayINFO();
      }
      catch(e)
      {
        // display error message
        displayError(e.toString());
      }
    }
    else
    {
      //displayError(xmlHttp.statusText);
    }
  }
}

function displayINFO()
{
  // read server response as text, to check for errors
  var response = xmlHttp.responseText;
  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
    throw(response.length == 0 ? "Void server response." : response);

  // hide the "Loading..." message upon feed retrieval
  //document.getElementById("loading").style.display = "none";

  // append XSLed XML content to existing DOM structure

  var Container = document.getElementById("OfferList");

  Container.innerHTML = response;

}
//******************************************************************************//
//**							END SORT WINDOW		 					  	  **//
//******************************************************************************//






//******************************************************************************//
//**							START OLD NEWS LIST BLOC			 		  **//
//******************************************************************************//
function oldNewsList(page,id,module)
{

  if (xmlHttp)
  {
	// try to connect to the server
    try
    {
      if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
      {

        // Hide arows
        document.getElementById("Nav").style.visibility = "hidden";

        // Display "Loading..." message while loading feed
        document.getElementById("Loader").style.visibility = "visible";


        //document.getElementById('sendmessage').messagetext.value = '';

       // Call the server page to execute the server-side operation
        params = "page=" + page + "&id=" + id + "&module=" +module;
        xmlHttp.open("POST", "oldNewsList.php", true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = handleHttp_oldNewsList;
        xmlHttp.send(params);
      }
      else
      {
        // if connection was busy, try again after 1 second
        setTimeout("oldNewsList('" + sort + "' );", 1000);
      }
    }
    // display the error in case of failure
    catch (e)
    {
      //displayError(e.toString());
    }
  }
}


// function that retrieves the HTTP response
function handleHttp_oldNewsList()
{
  // continue if the process is completed
  if (xmlHttp.readyState == 4)
  {
   // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        displayOldNewsList();
      }
      catch(e)
      {
        // display error message
        displayError(e.toString());
      }
    }
    else
    {
      displayError(xmlHttp.statusText);
    }
  }
}

function displayOldNewsList()
{
  // read server response as text, to check for errors
  var response = xmlHttp.responseText;
  if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
    throw(response.length == 0 ? "Void server response." : response);

  // Show arows
        document.getElementById("Nav").style.visibility = "visible";

  // append XSLed XML content to existing DOM structure

  var Container = document.getElementById("AlteNoutati");

  Container.innerHTML = response;

  // Hide "Loading..." message while loading feed
      //  document.getElementById("Loader").style.visibility = "hidden";

}
//******************************************************************************//
//**							END LD NEWS LIST BLOC					  	  **//
//******************************************************************************//