var MGForm=document.getElementById('contact');
var MGForm_structure=document.getElementById('structure');
var MGForm_fonction=document.getElementById('fonction');
var MGForm_besoin=document.getElementById('besoin');

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
  // public method for url encoding
  encode : function (string) {
    return escape(this._utf8_encode(string));
  },
 
  // public method for url decoding
  decode : function (string) {
    return this._utf8_decode(unescape(string));
  },
 
  // private method for UTF-8 encoding
  _utf8_encode : function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
 
    for (var n = 0; n < string.length; n++) {
 
      var c = string.charCodeAt(n);
 
      if (c < 128) {
        utftext += String.fromCharCode(c);
      }
      else if((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      }
      else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
 
    }
 
    return utftext;
  },
 
  // private method for UTF-8 decoding
  _utf8_decode : function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;
 
    while ( i < utftext.length ) {
 
      c = utftext.charCodeAt(i);
 
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      }
      else if((c > 191) && (c < 224)) {
        c2 = utftext.charCodeAt(i+1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      }
      else {
        c2 = utftext.charCodeAt(i+1);
        c3 = utftext.charCodeAt(i+2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
 
    }
 
    return string;
  }
 
}

InitContact();

function InitContact() {
  for(i=0;i<Structure.length;i++)
    MGForm_structure.options[i] = new Option(Structure[i],Structure[i]);
  MGForm_structure.options.selectedIndex = 0;
  MGForm_fonction.options.selectedIndex = 0;
  
  var lsRegExp = /\+/g;
  var sstructure = Url.decode(getURLParam(Url.encode('formhandler[structure][]')).replace(lsRegExp, " "));
  var pstructure = '';
  if (sstructure != '') {
    for(i=0;i<MGForm_structure.options.length;i++)
      if (sstructure == MGForm_structure.options[i].value)
        MGForm_structure.options.selectedIndex = i;
  } else {
    pstructure = getURLParam('s');
    if (pstructure != '') {
      MGForm_structure.options.selectedIndex = pstructure-1 ;
    } else {
      MGForm_structure.options.selectedIndex = 0 ;
    }
  }
  
  UpdateStructure();  
  
  if (sstructure != '' || pstructure != '') {
    var sfonction = Url.decode(getURLParam(Url.encode('formhandler[fonction][]')).replace(lsRegExp, " "));
    if (sfonction != '') {
      for(i=0;i<MGForm_fonction.options.length;i++)
        if (sfonction == MGForm_fonction.options[i].value)
          MGForm_fonction.options.selectedIndex = i;
    } else {
      pfonction = getURLParam('f');
      if (pfonction != '') {
        MGForm_fonction.options.selectedIndex = pfonction-1 ;
      }
    }
  }
  var refId = getRefererId();
  for (i=0;i<Besoin.length;i++)
    if (Besoin[i][0]==refId)
      MGForm_besoin.value = Besoin[i][1].substr(4);
}

function UpdateStructure()
{
  var i=MGForm_structure.options.selectedIndex;
  for(j=0;j<Fonction[i].length;j++)
    MGForm_fonction.options[j] = new Option(Fonction[i][j],Fonction[i][j]);
  for(j=MGForm_fonction.options.length;j>Fonction[i].length;j--)
    MGForm_fonction.options[j] = null;
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("&") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("&")); //.toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

function getRefererId(){
  var strReturn = "";
  var strHref = document.referrer;
  if ( strHref.lastIndexOf("/") > -1 ){
    var strQueryString = strHref.substr(strHref.lastIndexOf("/")+1).toLowerCase();
    var aQueryString = strQueryString.split(".");
    strReturn = aQueryString[0];
  }
  return strReturn;
}


