/*

	Code permettant de regénérer le module calendrier lorsque l'utilisateur change de jour ou de mois

*/

// holds an instance of XMLHttpRequest
var xmlHttpCalendrier = createXmlHttpRequestObjectCalendrier();
// holds the remote server address
var serverAddressCalendrier = "../xsl/blogPodcast/afficherCalendrier.php";
// when set to true, display detailed error messages
var showErrors = true;
// initialize the validation requests cache
var cache = new Array();


// creates an XMLHttpRequest instance
function createXmlHttpRequestObjectCalendrier()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttpCalendrier;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttpCalendrier = 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 id until one works
    for (var i=0; i<XmlHttpVersions.length && !xmlHttpCalendrier; i++)
    {
      try
      {
        // try to create XMLHttpRequest object
        xmlHttpCalendrier = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {} // ignore potential error
    }
  }
  // return the created object or display an error message
  if (!xmlHttpCalendrier)
    displayErrorCalendrier("Error creating the XMLHttpRequest object.");
  else
    return xmlHttpCalendrier;
}


// function that displays an error message
function displayErrorCalendrier($message)
{
  // ignore errors if showErrors is false
  if (showErrors)
  {
    // turn error displaying Off
    showErrors = false;
    // display error message

    alert("Error encountered: \n" + $message);
    // retry validation after 10 seconds
    setTimeout("afficherCalendrier();", 10000);
  }
}


// the function handles the validation for any form field
function afficherCalendrier(periodeAnneeMois, urlEpisode, operation, podcastID)
{
  // only continue if xmlHttpCalendrier isn't void
  if (xmlHttpCalendrier)
  {
    // if we received non-null parameters, we add them to cache in the
    // form of the query string to be sent to the server for validation
    if (periodeAnneeMois)
    {
      // encode values for safely adding them to an HTTP request query string
	  periodeAnneeMois = encodeURIComponent(periodeAnneeMois);
	  urlEpisode = encodeURIComponent(urlEpisode);	  
	  operation = encodeURIComponent(operation);
	  podcastID = encodeURIComponent(podcastID);
	  
      // Valeur qui seront passées au fichier afficherCalendrier.php
      cache.push("periodeAnneeMois=" + periodeAnneeMois + "&urlEpisode=" + urlEpisode + "&operation=" + operation + "&podcastID=" + podcastID);

    }
    // try to connect to the server
    try
    {
      // continue only if the XMLHttpRequest object isn't busy
      // and the cache is not empty
      if ((xmlHttpCalendrier.readyState == 4 || xmlHttpCalendrier.readyState == 0)
         && cache.length > 0)
      {
        // get a new set of parameters from the cache
        var cacheEntry = cache.shift();
        // make a server request to validate the extracted data
        xmlHttpCalendrier.open("POST", serverAddressCalendrier, true);
        xmlHttpCalendrier.setRequestHeader("Content-Type",
                                 "application/x-www-form-urlencoded");
        xmlHttpCalendrier.onreadystatechange = handleRequestStateChangeCalendrier;
        xmlHttpCalendrier.send(cacheEntry);
      }
    }
    catch (e)
    {
      // display an error when failing to connect to the server
      displayErrorCalendrier(e.toString());
    }
  }
}

// function that handles the HTTP response
function handleRequestStateChangeCalendrier()
{
  // when readyState is 4, we read the server response
  if (xmlHttpCalendrier.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttpCalendrier.status == 200)
    {
      try
      {
        // read the response from the server
        readResponseCalendrier();
      }
      catch(e)

      {
        // display error message
        displayErrorCalendrier(e.toString());
      }
    }
    else
    {
      // display error message
      displayErrorCalendrier(xmlHttpCalendrier.statusText);
    }
  }
}

// read server's response
function readResponseCalendrier()
{
  // retrieve the server's response
  var response = xmlHttpCalendrier.responseText;

  // Régénération complète de la div BlocGeneral avec le response venant du fichier PHP
  document.getElementById('unCalendrier').innerHTML = response;

  setTimeout("afficherCalendrier();", 500);
}


