/*
  * Ajax call function to call remote page.
  * @param url url to call
  * @param formIdToPost form id to post with the call of the url
  * @param spanIdToModify span id to modify
  */
  function ajaxCallRemotePageRetour(url) {

    var req = null;

    //Do the Ajax call
    if (window.XMLHttpRequest) { // Non-IE browsers
        req = new XMLHttpRequest();
    } else
    if (window.ActiveXObject) { // IE
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }

    req.onreadystatechange = function() { processStateChangeRetour(req); };
    req.open("GET", url, true);


    req.send(null);
    //alert(url);
  }

  /*
  * Set as the callback method for when XmlHttpRequest State Changes
  * used by retrieveUrl
  */
  function processStateChangeRetour(req) {

    if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response
      sResponse = req.responseText;
      if (sResponse.length > 10){
       document.getElementById("retour-moteur").innerHTML = sResponse;
       }
      } else {
        alert("Problem with server response:\n " + req.statusText);
      }
    }
  }

/*
  * Ajax call function to call remote page.
  * @param url url to call
  * @param formIdToPost form id to post with the call of the url
  * @param spanIdToModify span id to modify
  */
  function ajaxCallRemotePageRetour2(url) {

    var req = null;
    if (url.lastIndexOf("?") < 0) {
        url = url + "?";
    }
    //get the (form based) params to push up as part of the get request

    url=url+getFormAsString('filter-retour');
    url=url+"&"+document.getElementById("searchEngineUrl").value;

    //Do the Ajax call
    if (window.XMLHttpRequest) { // Non-IE browsers
        req = new XMLHttpRequest();
    } else
    if (window.ActiveXObject) { // IE
        req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.onreadystatechange = function() { processStateChangeRetour2(req,url); };
    req.open("GET", url, true);

    req.send(null);

    //alert(url);
  }

  /*
  * Set as the callback method for when XmlHttpRequest State Changes
  * used by retrieveUrl
  */
  function processStateChangeRetour2(req,url) {

    if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response

      sResponse = req.responseText;
      if (sResponse.length > 10){
       document.getElementById("retour-moteur").innerHTML = sResponse;

       onloadFilter(url);

       }
      } else {
        alert("Problem with server response:\n " + req.statusText);
      }
      scroll(0,0);
    }
  }

function onloadFilter(url){
var mmp=getQueryString(url,'mmp');
var pension=getQueryString(url,'c.pe');
var theme=getQueryString(url,'c.th');
var st=getQueryString(url,'st');
var show=getQueryString(url,'show');

document.getElementById("mmp").value=mmp;
document.getElementById("c.pe").value=pension;
document.getElementById("c.th").value=theme;
document.getElementById("st").value=st;
document.getElementById("show").value=show;

}
/**
  * gets the contents of the form as a URL encoded String
  * suitable for appending to a url
  * @param formName to encode
  * @return string with encoded form values , beings with &
  */
 function getFormAsString(formId){

    var formElements;
    //Setup the return String
    returnString ="";

    //Get the form values
    formElements=document.getElementById(formId).elements;

    //loop through the array , building up the url
    //in the form /strutsaction.do&name=value
    for ( var i=formElements.length-1; i>=0; --i ){
        //we escape (encode) each value
        returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
    }

    //return the values
    return returnString;
 }

function getQueryString(url,name){
if(url.indexOf("?")==-1 || url.indexOf(name+'=')==-1)
{
return '';
}

var queryString = url.substring(url.indexOf("?")+1);

var parameters = queryString.split("&");

var pos, paraName, paraValue;

for(var i=0; i<parameters.length; i++){
pos = parameters[i].indexOf('=');

if(pos == -1) { continue; }

paraName = parameters[i].substring(0, pos);

paraValue = parameters[i].substring(pos + 1);

if(paraName == name){
return unescape(paraValue.replace(/\+/g, " "));
}
}
return '';

};
