// holds an instance of XMLHttpRequest
var xmlHttp2 = createXmlHttpRequestObject2();
// holds the remote server address
var serverAddress = "js/jquery/jmp3/player.php";
// when set to true, display detailed error messages
var showErrors = true;
// initialize the validation requests cache
var cache = new Array();

var globalEpisodeID = '';

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject2()
{
  // will store the reference to the XMLHttpRequest object
  var xmlHttp2;
  // this should work for all browsers except IE6 and older
  try
  {
    // try to create XMLHttpRequest object
    xmlHttp2 = 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 && !xmlHttp2; i++)
    {
      try
      {
        // try to create XMLHttpRequest object
        xmlHttp2 = new ActiveXObject(XmlHttpVersions[i]);
      }
      catch (e) {} // ignore potential error
    }
  }
  // return the created object or display an error message
  if (!xmlHttp2)
    displayError2("Error creating the XMLHttpRequest object.");
  else
    return xmlHttp2;
}

// function that displays an error message
function displayError2($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("afficherPlayer();", 10000);
  }
}


// the function handles the validation for any form field
function afficherPlayer(urlFichier, divID, ID, urlRss, nbreElementsAAfficher)
{
  // only continue if xmlHttp isn't void
  if (xmlHttp2)
  {
    // 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 (urlFichier)
    {
      // encode values for safely adding them to an HTTP request query string
      //urlFichier = encodeURIComponent(urlFichier);
	  divID = encodeURIComponent(divID);
	  urlFichier = encodeURIComponent(urlFichier);
	  ID = encodeURIComponent(ID);
	  urlRss = encodeURIComponent(urlRss);
	  nbreElementsAAfficher = encodeURIComponent(nbreElementsAAfficher);


	  
	  globalDivID = divID;
	  globalUrlFichier = urlFichier;
	  globalID = ID;
      // add the values to the queue
      cache.push("urlFichier=" + urlFichier + "&divID=" + divID + "&urlRss=" + urlRss + "&nbreElementsAAfficher=" + nbreElementsAAfficher);

	  
    }
    // try to connect to the server
    try
    {
      // continue only if the XMLHttpRequest object isn't busy
      // and the cache is not empty
      if ((xmlHttp2.readyState == 4 || xmlHttp2.readyState == 0)
         && cache.length > 0)
      {
        // get a new set of parameters from the cache
        var cacheEntry2 = cache.shift();
        // make a server request to validate the extracted data
        xmlHttp2.open("POST", serverAddress, true);
        xmlHttp2.setRequestHeader("Content-Type",
                                 "application/x-www-form-urlencoded");
        xmlHttp2.onreadystatechange = handleRequestStateChange2;
        xmlHttp2.send(cacheEntry2);
      }
    }
    catch (e)
    {
      // display an error when failing to connect to the server
      displayError2(e.toString());
    }
  }
}

// function that handles the HTTP response
function handleRequestStateChange2()
{
  // when readyState is 4, we read the server response
  if (xmlHttp2.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp2.status == 200)
    {
      try
      {
        // read the response from the server
        readResponse2();
      }
      catch(e)

      {
        // display error message
        displayError2(e.toString());
      }
    }
    else
    {
      // display error message
      displayError2(xmlHttp2.statusText);
    }
  }
}

// read server's response
function readResponse2()
{
  // retrieve the server's response
  var response = xmlHttp2.responseText;



  document.getElementById('blocGeneral').innerHTML = response;


  /*document.getElementById(globalDivID).innerHTML = response;
  document.getElementById("lien"+globalDivID).onclick = "";
  
  for(i=1;i<globalNbItem;i++)
  {
	  uneDiv = "div"+i;
	if(uneDiv != globalDivID)  
	{
		document.getElementById(uneDiv).innerHTML = "";
	}
	  
  }*/
  
  setTimeout("afficherPlayer();", 500);
}

// sets focus on the first field of the form
function setFocus2()
{
  document.getElementById("txtNom").focus();
}
