//             Ajax functions
// Required to be compliant with XHTML
var xmlHttp=false; // Defines that xmlHttp is a new variable.
// Try to get the right object for different browser, we depend on try/catch method
try {
  // Firefox, Opera 8.0+, Safari
  xmlHttp = new XMLHttpRequest(); // xmlHttp is now a XMLHttpRequest.
  if (xmlHttp.overrideMimeType) {
    xmlHttp.overrideMimeType('text/html');//we request only php scripts with text/html type
  }
}
catch (e) {
  // Internet Explorer
  try {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");//if possible, we use this
  }
  catch (e) {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//alternative method
  }
}

/* Another method to determine whether browser objects are available (without try & catch)
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
  xmlHttp = new XMLHttpRequest();
  if (xmlHttp.overrideMimeType) {
    xmlHttp.overrideMimeType('text/html');
  }
}
else if (window.ActiveXObject) { // IE
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  if (!xmlHttp) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }
}
*/

//In case Ajax not supported...
if (!xmlHttp) {
  alert('Your browser does not support Ajax that is needed to process forms and load content. Please update your browser software or use some other.');
  //return false;
}

//Request function
function makeRequest(requestMethod, url, params, rndSeparator, fnCall) {
  if (xmlHttp) {
    var request_url = url + rndSeparator + 'rnd=' + new Date().getTime();
    xmlHttp.open(requestMethod, request_url, true);
    
    if (requestMethod=='POST') {
      xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      xmlHttp.setRequestHeader('Content-Length',params.length);
      //xmlHttp.setRequestHeader('Cache-Control','no-cache');//... or the server response will be cached and request never re-submitted :(
      xmlHttp.setRequestHeader('Connection','close');
    }
    
    xmlHttp.send(params);
    xmlHttp.onreadystatechange = function() {
      if (xmlHttp.readyState == 4) {
        try { // In some instance, status cannot be retrieve and will produce an error (ex: Port is not responsive)
          if (xmlHttp.status == 200) {
            //eval the js code
            eval(fnCall + '(xmlHttp.responseText)');
          }
        }
        catch (e) {
          alert('Ajax request cannot be completed: ' + e.description);
        }
      }
    }
  }
}

