document.onkeydown = function (evt)
{
if(navigator.appName == "Microsoft Internet Explorer"){
  if (window.event.keyCode == 13 &&  window.event.srcElement.type != 'textarea') {
  return false;
  }

} else {
  if (evt.keyCode == 13 && evt.target.type != 'textarea') { return false; }

}
}

function loadCat0(site) {

    if(!document.getElementById('Cat0'))
    setTimeout('loadCat0("'+ site +'")', 20);

  site = site*(-1);
 //alert(site);
  if(!C[site])
  site=-1;

  for (i = 0; i < (C[site].length); i++) { // -1 because arrays end with ",]"
    val = C[site][i];
      if(debugMode)  alert(S[val]);
     if (C[val] != null) {
      document.getElementById('Cat0').options[i] = new Option(S[val] + ' >', val);
    } else {
      document.getElementById('Cat0').options[i] = new Option(S[val],        val);
    }
  }
  document.getElementById('Cat0').options[0].selected = true;
  document.getElementById('Cat1').length = 0;
  document.getElementById('Cat2').length = 0;
  document.getElementById('Cat3').length = 0;
  changeCat(0, 0);
}

/********************************************************************************/

   function changeCat(columnNr, modified) {
     // reset sub-categorie
    for(i = columnNr+1; i < maxLevels; i++) {
      document.getElementById('Cat'+i).length = 0;
    }

  if (columnNr == 0) {
    val = document.getElementById('Cat0').options[document.getElementById('Cat0').selectedIndex].value;
    if (C[val] == null ) {
      if (modified == 1) boxUpdated(val);
     return;
    }
    for(i = 0; i < (C[val].length); i++) { // -1 because arrays end with ",]"
      val1 = C[val][i];
      if (C[val1] == null){
       if(debugMode)  alert(S[val1]);
        document.getElementById('Cat1').options[i] = new Option(S[val1]       , val1);
      }
      else{
        document.getElementById('Cat1').options[i] = new Option(S[val1] + ' >', val1);
      }
    }
    if(selectSheet){
    document.getElementById('Cat1').options[0].selected = true;
    columnNr = 1;
    }

  }

  if (columnNr == 1) {
    val = document.getElementById('Cat1').options[document.getElementById('Cat1').selectedIndex].value;
    if (C[val] == null) {
      if (modified == 1) boxUpdated(val);
      return;
    }
    for (i = 0; i < (C[val].length); i++) { // -1 because arrays end with ",]"
      val1 = C[val][i];
      if (C[val1] == null)
        document.getElementById('Cat2').options[i] = new Option(S[val1]       ,val1);
      else
        document.getElementById('Cat2').options[i] = new Option(S[val1] + ' >', val1);
    }
    if(selectSheet){
    document.getElementById('Cat2').options[0].selected = true;
    columnNr = 2;
    }
  }

  if (columnNr == 2) {
    val = document.getElementById('Cat2').options[document.getElementById('Cat2').selectedIndex].value;
    if (C[val] == null) {
      if (modified == 1) boxUpdated(val);
      return;
    }
    for (i = 0; i < (C[val].length); i++) { // -1 because arrays end with ",]"
      val1 = C[val][i];
      if (C[val1] == null)
        document.getElementById('Cat3').options[i] = new Option(S[val1]       , val1);
      else
        document.getElementById('Cat3').options[i] = new Option(S[val1] + ' >', val1);
    }
    if(selectSheet){
    document.getElementById('Cat3').options[0].selected = true;
    columnNr = 3;
    }
  }

  if (columnNr == 3) {
    val = document.getElementById('Cat3').options[document.getElementById('Cat3').selectedIndex].value;
    if (C[val] == null) {
      if (modified == 1) boxUpdated(val);
      return;
    }
    for (i = 0; i < (C[val].length); i++) { // -1 because arrays end with ",]"
      val1 = C[val][i];
      if (C[val1] == null)
        document.getElementById('Cat4').options[i] = new Option(S[val1]       , val1);
      else
        document.getElementById('Cat4').options[i] = new Option(S[val1] + ' >', val1);
    }
    if(selectSheet){
    document.getElementById('Cat4').options[0].selected = true;
    columnNr = 4;
    }
  }

  if (columnNr == 4) {
    val = document.getElementById('Cat4').options[document.getElementById('Cat4').selectedIndex].value;
    if (modified == 1) boxUpdated(val);
    return;
  }


  }

/********************************************************************************/

function searchInCategories(){
  word = noaccent(document.getElementById('wordToSearch').value.toLowerCase());
  resultat = '';
  resultID = new Array();
  resultString = new Array();
  document.getElementById('searchCategoryResult').innerHTML = "<b>" + txt_3108 + "</b><br><br>";
  for (var i in S) {
    chaine = noaccent(S[i].toString().toLowerCase());
    if ( chaine.indexOf(word) != -1) {
     resultID.push(i);
    }
  }
    if(resultID.length ==0){
      document.getElementById('searchCategoryResult').innerHTML  += txt_3110;
    } else if (resultID.length >20){
      document.getElementById('searchCategoryResult').innerHTML  += txt_3109 +' ('+ resultID.length +')' ;
    } else {
      for(j = 0 ; j < resultID.length ; ++j){
        i = resultID[j];
        document.getElementById('searchCategoryResult').innerHTML += '<a href="javascript:;" onClick=setResult(' + i + ')>['+txt_0283+'] ' +  getCatStrFromID(i) +  '</a>' +  "<br>";
       }
    }


}
/********************************************************************************/

   function setResult(myCat){
     setMyCat(myCat);
     if(selectSheet)myCat = forceCatToSheet();
     boxUpdated(myCat);
   }
/********************************************************************************/

function findCatPath(catnum, catpath) { // RECURSIVE !
  if (catnum > 0) {
    catpath += catnum + '~';
    found = 0;
    for (key in C) { // Searches parent of catnum
      for (key2 in C[key]) {
        if (C[key][key2] == catnum) {
         found = 1;
         catpath = findCatPath(key, catpath);
         break;
        }
      }
    }
  }
  if (found == 0) catpath = '';
  return(catpath);
}

/********************************************************************************/

function setCat(columnNr, myValue) {
  for (i = 0; i < (document.getElementById('Cat'+columnNr).length); i++) {
    if (document.getElementById('Cat'+columnNr).options[i].value == myValue) {
      document.getElementById('Cat'+columnNr).options[i].selected = true;
      break;
    }
  }
  changeCat(columnNr, 0); // 0?
}

/********************************************************************************/

function setMyCat(myCat) {

  catpath = findCatPath(myCat, '');
  if (catpath != '') {
    catpath = catpath.split('~');
    nbr_cols = catpath.length - 1;
    col_counter = 0;
    for (key = nbr_cols - 1; key >= 0; key--) setCat(col_counter++, catpath[key]);
    document.getElementById(catTextInput).value = getCatStr();
    document.getElementById(catIdInput).value = myCat;
  } else {
    alert(txt_0333);
    //document.getElementById(catIdInput).value = '';
    //document.getElementById('Cat0').value = 0;
    setResult(document.getElementById('Cat0').value);
  }
}
/********************************************************************************/

function getCatStr() {
  catStr = '';
  if (document.getElementById('Cat0').value != 0) catStr += ' ' + document.getElementById('Cat0').options[document.getElementById('Cat0').selectedIndex].text;
  if (document.getElementById('Cat1').value != 0) catStr += ' ' + document.getElementById('Cat1').options[document.getElementById('Cat1').selectedIndex].text;
  if (document.getElementById('Cat2').value != 0) catStr += ' ' + document.getElementById('Cat2').options[document.getElementById('Cat2').selectedIndex].text;
  if (document.getElementById('Cat3').value != 0) catStr += ' ' + document.getElementById('Cat3').options[document.getElementById('Cat3').selectedIndex].text;
  if (document.getElementById('Cat4').value != 0) catStr += ' ' + document.getElementById('Cat4').options[document.getElementById('Cat4').selectedIndex].text;
  return(catStr);
}
/********************************************************************************/

function getCatStrFromID(myCat) {
  catStr = '';
  catpath = findCatPath(myCat, '');
  if (catpath != '') {
    catpath = catpath.split('~');
    catpath.reverse();

    for(i=1 ; i<(catpath.length) ; ++i){
      catStr +=  S[catpath[i]];
      if(i < (catpath.length-1)) catStr += ' > ';
    }
  }
  return(catStr);
}
/********************************************************************************/

function forceCatToSheet(){

  cat = '';
  for(i=0 ; i<maxLevels ; ++i){
    if(document.getElementById('Cat' + i).length !=0 ) cat =  document.getElementById('Cat' + i).value;
  }

  return cat;
}

/********************************************************************************/

function checkForbiddenCats(cat) {
 if (checkForForbiddenCats){
    found = false;
    i = 0;

    while (i < arrForbiddenCats.length) {
        if (arrForbiddenCats[i] == cat) {
          found = true;
          break;
        }
        else i++;
    }

    if (found == true) {
       if(document.getElementById(option1)) document.getElementById(option1).checked = false;
       if(document.getElementById(option1)) document.getElementById(option1).disabled = true;

       if(document.getElementById(option2))document.getElementById(option2).checked = false;
       if(document.getElementById(option2))document.getElementById(option2).disabled = true;

       if(document.getElementById(option3))document.getElementById(option3).checked = false;
       if(document.getElementById(option3))document.getElementById(option3).disabled = true;


       document.getElementById('alert_cat').innerHTML = txt_alert_cat;
    }
    else {
       if(document.getElementById(option1)) document.getElementById(option1).disabled = false;
       if(document.getElementById(option2)) document.getElementById(option2).disabled = false;
       if(document.getElementById(option3)) document.getElementById(option3).disabled = false;
       document.getElementById('alert_cat').innerHTML = '';
    }
  }
}

function boxUpdated(cat) {


  checkForbiddenCats(cat);

  document.getElementById(catIdInput).value = cat;
  document.getElementById(catTextInput).value = getCatStr();
}

/*
function maj_liste(cat) {
  alert(findCatPath(cat, '')+'!');
  if (findCatPath(cat, '') != '') {
    document.getElementById('cat').value = cat;
  } else {
    alert("<? echo txt_0333;?>");
  }
}*/
