var MGForm=document.all.MGBesoins;

var selectStructureMessage = '';
var selectFonctionMessage = '';
var selectBesoinMessage = '';

InitMGB();

function InitMGB() {
  MGForm.Structure.options[0] = new Option(selectStructureMessage,0);
  MGForm.Fonction.options[0] = new Option(selectFonctionMessage,0);
  MGForm.Besoin.options[0] = new Option(selectBesoinMessage,0);
  for(i=1;i<=Structure.length;i++)
    MGForm.Structure.options[i] = new Option(Structure[i-1],i);
  ResetAll();
  pstructure = getURLParam('s');
  if (pstructure != '') {
  MGForm.Structure.options.selectedIndex = pstructure ;
    UpdateStructure(pstructure);
    pfonction = getURLParam('f');
    if (pfonction != '') {
      MGForm.Fonction.options.selectedIndex = pfonction ;
      UpdateFonction(pfonction );
    }
  }
}

function ResetAll() {
  MGForm.Structure.options.selectedIndex = 0;
  MGForm.Fonction.disabled = true;
  ResetStructure();
}
function ResetStructure() {
  MGForm.Fonction.options.selectedIndex = 0;
  MGForm.Besoin.disabled = true;
  ResetFonction();
}
function ResetFonction() {
  MGForm.Besoin.options.selectedIndex = 0;
  ResetBesoin();
}
function ResetBesoin() {
}

function UpdateStructure()
{
  var i=MGForm.Structure.options.selectedIndex;
  ResetStructure();
  if (i==0) {
    MGForm.Fonction.disabled = true;
    for(j=MGForm.Fonction.options.length;j>0;j--)
      MGForm.Fonction.options[j] = null;
  } else {
    MGForm.Fonction.disabled = false;
    for(j=1;j<=Fonction[i-1].length;j++)
      MGForm.Fonction.options[j] = new Option(Fonction[i-1][j-1],j);
    for(j=MGForm.Fonction.options.length;j>Fonction[i-1].length;j--)
      MGForm.Fonction.options[j] = null;
  }
  UpdateFonction();
}

function UpdateFonction()
{
  var i=MGForm.Structure.options.selectedIndex;
  var j=MGForm.Fonction.options.selectedIndex;
  ResetFonction();
  if (j==0) {
    MGForm.Besoin.disabled = true;
    for(k=MGForm.Besoin.options.length;k>0;k--)
      MGForm.Besoin.options[k] = null;
  } else {
    MGForm.Besoin.disabled = false;
    var k=1;
    for (var l=0;l<Besoin.length;l++) {
      if (Besoin[l][0]=='---' || IsElement(Besoin[l][0],SelectionBesoin[i-1][j-1])) {
        MGForm.Besoin.options[k] = new Option(Besoin[l][1],Besoin[l][0]);
        k++;
      } 
    }
    for(var l=MGForm.Besoin.options.length;l>=k.length;l--)
      MGForm.Besoin.options[l] = null;
  }
}

function UpdateBesoin()
{
  var i=MGForm.Structure.options.selectedIndex;
  var j=MGForm.Fonction.options.selectedIndex;
  var k=MGForm.Besoin.options.selectedIndex;
  if (k!=0) {
    if (MGForm.Besoin.value == "---")
      alert("\""+MGForm.Besoin.options[k].text+"\" est une catégorie de besoin. Veuillez sélectionner un besoin...");
    else
      window.location.href = "/"+MGForm.Besoin.value+".html?&s="+i+"&f="+j;
  } 
}

function IsElement(alias,liste) {
  for (i=0;i<liste.length;i++)
    if (alias==liste[i])
      return true;
  return false;
} 

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;
}