﻿////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                    //
// function MakeHttpRequest(type, url, asynchro, responseHandle, userdata) : Make the XMLHttpRequest. //
//                                                                                                    //
// type           : HTTP request type (GET or POST).                                                  //
// url            : URL of the server program.                                                        //
// asynchro       : If true, send the request asynchronously, else not.                               //
// responseHandle : Name of the function that will handle the response.                               //
// userdata       : User data for the function handling the response.                                 //
// 6th parameters : Represented as arguments[5], are the data a POST request is designed to send.     //
//                                                                                                    //
// return         : true if success else false                                                        //
////////////////////////////////////////////////////////////////////////////////////////////////////////
function MakeHttpRequest(type, url, asynchro, responseHandle, userdata)
{
  var httpRequest = null;
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest)
  {
    httpRequest = new XMLHttpRequest();
	if(httpRequest.overrideMimeType) 
	{
      httpRequest.overrideMimeType('text/xml');
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject)
  {
    try 
	{
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e) 
	{
	  httpRequest = null;
	}
  	if(!httpRequest)
  	{
  	  try
  	  {
  	    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
  	  catch(e) 
  	  {
  	    httpRequest = null;
  	  }
    }
  }
  if(httpRequest) 
  {
    if(type.toLowerCase() != "post") 
	{
      if(initRequest(type, url, asynchro, responseHandle, userdata, httpRequest))
	  {
	    return true;
      }
    } 
	else 
	{
      // If type is POST, the 5th argument of the function is the POSTed data.
      var args = arguments[5];
      if(args != null && args.length > 0)
	  {
        if(initRequest(type, url, asynchro, responseHandle, userdata, httpRequest, args))
	    {
	      return true;
	    }
      }
    }
  } 
  return false;
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function ajaxResponse(httpRequest) : Treat the XMLHttpRequest response. //
//                                                                         //
// httpRequest : XMLHttpRequest object.                                    //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function ajaxResponse(httpRequest)
{
  try
  {
    if(4 == httpRequest.readyState)
	{
	  if(200 == httpRequest.status) 
	  {
	    // Success, continue treatment.
	    var text = httpRequest.responseText;
	    // Can also be "httpRequest.responseXML" if to treat with JavaScript DOM functions.
      } 
	  else 
	  {
        // Failure, problem with the request.
	    // 500 (Internal Server Error).
	    // 503 (Application Not Available).
	    // 404 (Not Found).
	    // ...
	    //alert("A communication problem occured between the XMLHttpRequest object and the server !");
      }
    }
	else
	{
	  // 0 (Uninitialized).
      // 1 (Loading).
      // 2 (Loaded).
      // 3 (Interactive).
	  // Nothing to do : XMLHttpRequest is not completed yet !
	}
  } 
  catch(err)
  {
    //alert("The application cannot contact the server at the moment. Please try again in a few seconds.\nError detail : " + err.message);
  }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                                                     //
// function initRequest(type, url, asynchro, responseHandle, userdata, httpRequest) : Initialize an XMLHttpRequest object that is already constructed. //
//                                                                                                                                           		   //
// type           : HTTP request type (GET or POST).                                                                                                   //
// url            : URL of the server program.                                                                                                         //
// asynchro       : If true, send the request asynchronously, else not.                                                                                //
// responseHandle : Name of the function that will handle the response.                                                                                //
// userdata 	  : User data for the function handling the response.  																				   //
// httpRequest    : XMLHttpRequest object.                                                                                                             //
// 7th parameters : Represented as arguments[6], are the data a POST request is designed to send.                                                      //
//                                                                                                                                                     //
// return         : true if success else false                                                                                                         //
//                                                                                                                                                     //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function initRequest(type, url, asynchro, responseHandle, userdata, httpRequest)
{
  try
  {
    // Specify the function that will handle the HTTP response.
    if(null == responseHandle)
    {
      httpRequest.onreadystatechange = function() { ajaxResponse(httpRequest); };
    }
    else
    {
      httpRequest.onreadystatechange = function() { responseHandle(httpRequest, userdata); };
    }
    httpRequest.open(type, url, asynchro);
    // If type is POST, the 6th argument of the function is the POSTed data.
	if(type.toLowerCase() == "post") 
	{
      httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
      httpRequest.send(arguments[6]);
    } 
	else 
	{
      httpRequest.send(null);
    }
	return true;
  } 
  catch(e) 
  {
  }
  return false;
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function getResponseXML(httpRequest) : Gives XML response.              //
//                                                                         //
// httpRequest : XMLHttpRequest object.                                    //
//                                                                         //
// return      : XML response.                                             //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function getResponseXML(httpRequest)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      var docXML = document.createElement('div');
      docXML.innerHTML = httpRequest.responseText;
    }
    else
    {
      var docXML = httpRequest.responseXML;
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    var docXML = document.createElement('div');
    docXML.innerHTML = httpRequest.responseText;
  }
  return docXML;
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function getElementById(docXML, id) : Gives XML element from id.        //
//                                                                         //
// docXML : XML document.                                                  //
// tag    : element tag.                                                   //
// id     : element id.                                                    //
//                                                                         //
// return : XML element.                                                   //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function getElementById(docXML, tag, id)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      var elements = docXML.getElementsByTagName(tag);
      for(var i=0; i<elements.length; ++i)
      {
        if(elements[i].getAttribute('id') == id)
        {
          return elements[i];
        }
      }
    }
    else
    {
      return docXML.getElementById(id);
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    var elements = docXML.getElementsByTagName(tag);
	for(var i=0; i<elements.length; ++i)
	{
	  if(elements[i].getAttribute('id') == id)
      {
        return elements[i];
      }
    }
  }
  return null;
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function getValue(element) : Gives value from element.                  //
//                                                                         //
// element : XML element.                                                  //
//                                                                         //
// return : value.                                                         //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function getValue(element)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      return element.getAttribute('value');
    }
    else
    {
      return element.value;
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    return element.getAttribute('value');
  }
  return null;
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function getContent(element) : Gives direct content from element.       //
//                                                                         //
// element : XML element.                                                  //
//                                                                         //
// return : content.                                                       //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function getContent(element)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      // Modifié pour ie8, reste à tester avec les autres.
      return element.innerHTML;
      //return element.firstChild.nodeValue;
    }
    else
    {
      return element.innerHTML;
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    // Modifié pour ie6, reste à tester avec les autres.
    return element.innerHTML;
    //return element.firstChild.nodeValue;
  }
  return null;
}

///////////////////////////////////////////////////////////////////////////////////////////
//                                                                                       //
// function getXmlContentById(httpRequest, tag, id) : Gives xml content from tag and id. //
//                                                                                       //
// httpRequest : XMLHttpRequest object.                                                  //
// tag         : element tag.                                                            //
// id          : element id.                                                             //
// return : xml content.                                                                 //
//                                                                                       //
///////////////////////////////////////////////////////////////////////////////////////////
function getXmlContentById(httpRequest, tag, id)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      var xml = httpRequest.responseText;
      var strid = 'id="' + id + '"';
      var strStart = '<' + tag;
      var strEnd = '</' + tag + '>';
      var c1 = xml.indexOf('>', xml.indexOf(strid, 0)) + 1;
      var idxStart = xml.indexOf(strStart, c1 - 1);
      var idxEnd = xml.indexOf(strEnd, c1 - 1);
      while((idxStart > 0) && (idxStart < idxEnd))
      {
        idxStart = xml.indexOf(strStart, idxStart + 1);
        idxEnd = xml.indexOf(strEnd, idxEnd + 3);
      }
      var c2 = idxEnd;
      return xml.substr(c1, c2-c1);
    }
    else
    {
      var element = getElementById(httpRequest.responseXML, tag, id);
      return element.innerHTML;
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    var xml = httpRequest.responseText;
    var strid = 'id="' + id + '"';
    var strStart = '<' + tag;
    var strEnd = '</' + tag + '>';
    var c1 = xml.indexOf('>', xml.indexOf(strid, 0)) + 1;
    var idxStart = xml.indexOf(strStart, c1 - 1);
    var idxEnd = xml.indexOf(strEnd, c1 - 1);
    while((idxStart > 0) && (idxStart < idxEnd))
    {
      idxStart = xml.indexOf(strStart, idxStart + 1);
      idxEnd = xml.indexOf(strEnd, idxEnd + 3);
    }
    var c2 = idxEnd;
    return xml.substr(c1, c2-c1);
  }
  return null;
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function getTitle(httpRequest) : Gives title from document.             //
//                                                                         //
// httpRequest : XMLHttpRequest object.                                    //
//                                                                         //
// return : title.                                                         //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function getTitle(httpRequest)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      var xml = httpRequest.responseText;
      var c1 = xml.indexOf('<title>', 0) + 7;
      var c2 = xml.indexOf('</title>', c1 - 1);
      return xml.substr(c1, c2-c1);
    }
    else
    {
      if(httpRequest.responseXML.title == undefined)
      {
        return httpRequest.responseXML.getElementsByTagName('title').item(0).text;
      }
      return httpRequest.responseXML.title;
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    var xml = httpRequest.responseText;
    var c1 = xml.indexOf('<title>', 0) + 7;
    var c2 = xml.indexOf('</title>', c1 - 1);
    return xml.substr(c1, c2-c1);
  }
  return null;
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function setTitle(title) : Sets title from document.                    //
//                                                                         //
// title : title of the document.                                          //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function setTitle(title)
{
  if(document.title == undefined)
  {
    document.getElementsByTagName('title').item(0).text = title;
  }
  else
  {
    document.title = title;
  }
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function getAlt(element) : Gives alt from element.                      //
//                                                                         //
// element : XML element.                                                  //
//                                                                         //
// return : alt value.                                                     //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function getAlt(element)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      return element.getAttribute('alt');
    }
    else
    {
      return element.alt;
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    return element.getAttribute('alt');
  }
  return null;
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function getAltTitle(element) : Gives title from element.               //
//                                                                         //
// element : XML element.                                                  //
//                                                                         //
// return : title value.                                                   //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function getAltTitle(element)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      return element.getAttribute('title');
    }
    else
    {
      return element.title;
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    return element.getAttribute('title');
  }
  return null;
}

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// function getClass(element) : Gives class from element.                  //
//                                                                         //
// element : XML element.                                                  //
//                                                                         //
// return : class value.                                                   //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////
function getClass(element)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      if(element.getAttribute('class') == null)
      {
        return element.getAttribute('className');
      }
      else
      {
        return element.getAttribute('class');
      }
    }
    else
    {
      return element.className;
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    return element.getAttribute('className');
  }
  return null;
}

////////////////////////////////////////////////////////////////////////////////////
//                                                                                //
// function strcasecmp(f_string1, f_string2) : Compares two strings without case. //
//                                                                                //
// f_string1 : First string.                                                      //
// f_string2 : Second string.                                                     //
//                                                                                //
// return : Egality or not.                                                       //
//                                                                                //
////////////////////////////////////////////////////////////////////////////////////
function strcasecmp(f_string1, f_string2)
{
  var string1 = (f_string1+'').toLowerCase();
  var string2 = (f_string2+'').toLowerCase();
 
  if(string1 > string2) 
  {
    return 1;
  }
  else if(string1 == string2) 
  {
    return 0;
  }
  return -1;
}

//////////////////////////////////////////////////////////////////////////////
//                                                                          //
// function getXmlContent(element) : Gives direct content from XML element. //
//                                                                          //
// element : XML element.                                                   //
//                                                                          //
// return : content.                                                        //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////
function getXmlContent(element)
{
  // For Mozilla-based browsers.
  if(window.XMLHttpRequest) 
  {
    if(navigator.appName == "Microsoft Internet Explorer")
    {
      if(element.nextSibling != undefined)
      {
        return element.nextSibling.nodeValue;
      }
      return null;
    }
    else
    {
      if(element.firstChild != undefined)
      {
        return element.firstChild.nodeValue;
      }
      return null;
    }
  }
  // For Microsoft-based browsers. 
  else if(window.ActiveXObject) 
  {
    if(element.nextSibling != undefined)
    {
      return element.nextSibling.nodeValue;
    }
    return null;
  }
  return null;
}

