/*
 * Retourne l'objet capable d'effectuer des requetes HTTP 
 *	ou faux si le navigateur ne supporte pas cette fonctionnalite
 */
function newXMLHttpRequest() {

  var xmlreq = false;

  if (window.XMLHttpRequest) {

    // Create XMLHttpRequest object in non-Microsoft browsers
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    // Create XMLHttpRequest via MS ActiveX
    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer

      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e1) {
	  alert('Failed to create required ActiveXObject');
      // Failed to create required ActiveXObject

      try {
        // Try version supported by older versions
        // of Internet Explorer

        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {
		alert('Unable to create an XMLHttpRequest with ActiveX');
        // Unable to create an XMLHttpRequest with ActiveX
      }
    }
  }

  return xmlreq;
}
/*
 * Returns a function that waits for the specified XMLHttpRequest
 * to complete, then passes its XML response
 * to the given handler function.
 * req - The XMLHttpRequest whose state is changing
 * responseXmlHandler - Function to pass the XML response to
 */
function getReadyStateHandler(req, responseXmlHandler, returnRequestInResponse) {

  // Return an anonymous function that listens to the 
  // XMLHttpRequest instance
  return function () {

    // If the request's status is "complete"
    if (req.readyState == 4) {
      
      // Check that a successful server response was received
      if (req.status == 200) {

        // Pass the XML payload of the response to the 
        // handler function
        if (returnRequestInResponse)
        	responseXmlHandler(req);
        else
        	responseXmlHandler(req.responseXML);

      } else {

        // An HTTP problem has occurred
        alert("HTTP error: "+req.status);
      }
    }
  }
}
function getRequest(url,handler,method,returnRequestInResponse){
	if(returnRequestInResponse==null) returnRequestInResponse=false;
	req = newXMLHttpRequest();
  	var handlerFunction = getReadyStateHandler(req, handler,returnRequestInResponse);
  	req.onreadystatechange = handlerFunction;
  	req.open(method, url, true);
  	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	return req;
}

function encodeURIComponentPerso(value){
	var valueToEncode =value;
	valueToEncode= valueToEncode.replace(//g,'&eacute;');
	valueToEncode= valueToEncode.replace(//g,'&agrave;');
	valueToEncode= valueToEncode.replace(//g,'&egrave;');
	valueToEncode= valueToEncode.replace(//g,'&icirc;');
	valueToEncode= valueToEncode.replace(//g,'&ucirc;');
	valueToEncode= valueToEncode.replace(//g,'&acirc;');
	valueToEncode= valueToEncode.replace(//g,'&ecirc;');
	valueToEncode= valueToEncode.replace(//g,'&ocirc;');
	valueToEncode= valueToEncode.replace(/\'/g,'&apos;');
	valueToEncode= valueToEncode.replace(/\"/g,'&quot;');
	valueToEncode= valueToEncode.replace(/</g,'&lt;');
	valueToEncode= valueToEncode.replace(/>/g,'&gt;');
	valueToEncode= valueToEncode.replace(/\//g,'&#47;');
	valueToEncode= valueToEncode.replace('/\\/g','&#92;');
	valueToEncode=encodeURIComponent(valueToEncode);
	return valueToEncode;
}
function removeMSWordChars(str) {
    var myReplacements = new Array();
    var myCode, intReplacement;
    myReplacements[8216] = 39;
    myReplacements[8217] = 39;
    myReplacements[8220] = 34;
    myReplacements[8221] = 34;
    myReplacements[8212] = 45;
    for(c=0; c<str.length; c++) {
        var myCode = str.charCodeAt(c);
        if(myReplacements[myCode] != undefined) {
            intReplacement = myReplacements[myCode];
            str = str.substr(0,c) + String.fromCharCode(intReplacement) + str.substr(c+1);
        }
    }
    return replaceMSWordCharsWithString(str);
}
function replaceMSWordCharsWithString (str) {
	var myReplacements = new Array();
	myReplacements[8211] = "-";
	myReplacements[8210] = "-";
	myReplacements[8212] = "-";
	myReplacements[8213] = "-";
	myReplacements[8226] = "*";
	myReplacements[8230] = "...";
	myReplacements[338] = "&OElig;";
	myReplacements[339] = "&oelig;";
	myReplacements[140] = "&OElig;";
	myReplacements[156] = "&oelig;";
	myReplacements[8275] = "&#8275;";
	myReplacements[8364] = "&#8364;";
	for(c=0; c<str.length; c++) {
	    var myCode = str.charCodeAt(c);
	    if(myReplacements[myCode] != undefined) {
	        replacement = myReplacements[myCode];
	        str = str.substr(0,c) + replacement + str.substr(c+1);
	    }
	}
	return str;
}
function getXmlDocumentFromString(text){
	if (window.ActiveXObject){
  		var doc=new ActiveXObject("Microsoft.XMLDOM");
 		 doc.async="false";
  		doc.loadXML(text);
  	}else{
  		var parser=new DOMParser();
  		var doc=parser.parseFromString(text,"text/xml");
  	}
  	return doc;
}
function isInError(xmlResponse){
	var error = true;
	var response = xmlResponse.getElementsByTagName("response")[0];
	if (response.getElementsByTagName("success")!=null){
		if(response.getElementsByTagName("success")[0].firstChild.nodeValue=="true"){
			error=false;
		}
	}
	return error;
}
function displayFromResponse(item,xmlResponse){
	var message =  getResponseItem(xmlResponse,item);
	if(message!=null){
		alert(message);
	}
}
function getResponseItem(xmlResponse,item){
	var response = xmlResponse.getElementsByTagName("response")[0];
	if (response.getElementsByTagName(item)!=null){
		try{
			return response.getElementsByTagName(item)[0].firstChild.nodeValue;
		}catch(e){
			return null;
		}
	}else{
		return null;
	}
}