DEBUG = false;
/**
* Cette classe sert à faire la gestion des garnitures d'une pizza.
*/

/**
* Class constructor pour une instance de SpecialToppingManager.
*/
function SpecialToppingManager(html_index, toppings, topping_prices, standard_toppings, category_names,  pizzaManager) {

  /*VARIABLES*/
  /*Pizza manager associated to this topping manager*/
  this.pizzaManager = pizzaManager ;

  /*Les garnitures disponible et leur quantité total sur la pizza*/
  this.toppings = new Array();

  /**Liste des toppings standard de la pizza*/
  this.standard_topping_list = standard_toppings ;

  for (var i in toppings){
    this.toppings[i] = new Array();
    for (var j in toppings[i]){
      this.toppings[i][j] = toppings[i][j] ;
    }
  }

  /*Les prix des garniture par format*/
  this.topping_prices = topping_prices ;

  /**Liste des noms de catégories*/
  this.category_names = category_names ;

  /*Liste contenant les garnitures sélectionnées*/
  this.toppings_left ;
  this.toppings_right ;
  this.toppings_all ;

  //calcul le nombre de garniture sur la pizza
  this.topping_count ;

  //nombre standard de garnitures
  this.standard_toppings ;

  //calcul le nombre de garniture maximum
  this.max_toppings ;

  this.initialize();
  //--------------------
  // Variable pour les nom des formulaires et des liste de bouton radio de sélection dans
  // la page HTML directement.
  //--------------------
  /*Référence à l'ID de la liste d'affichage des toppings */

  this.selection_lists = new Array();
  for (var i in this.category_names){
    this.selection_lists[i] = 'pizza[' + html_index + '][topping_list_' + i + ']' ;
  }

  /*Référence à l'ID du div utilisé pour l'affichage des garniture sélectionnés */
  this.selection_display = "pizza[" + html_index + "][topping_display]" ;

  this.topping_list = "pizza[" + html_index + "][toppings]" ;

  this.id = "topping[" + html_index + "]";

}//Fin SpecialToppingManager

/**
* Cette fonction initialise toutes les valeurs des toppings. Utilisé lors de la création d'un toppign manager
* et lors du changement d'une recette.
*/

SpecialToppingManager.prototype.initialize = function(){
  /*Vide les array de toppings sélectionnés*/
  this.toppings_left = new Array();
  this.toppings_right = new Array();
  this.toppings_all = new Array();

  //calcul le nombre de garniture sur la pizza
  this.topping_count = new Array();
  for(var t in this.toppings){
    if (!this.topping_count[this.toppings[t]['category']])
      this.topping_count[this.toppings[t]['category']] = 0; //empèche la conversion undefined -> NaN

    if (this.toppings[t]['A']){
      var i = this.toppings[t]['A'];
      while(i > 0){
        this.topping_count[this.toppings[t]['category']] += 1;
        this.toppings_all.push(t);
        i--;
      }
    }
    if (this.toppings[t]['L']){
      var i = this.toppings[t]['L'];
      while (i>0){
        this.topping_count[this.toppings[t]['category']] += 0.5 ;
        this.toppings_left.push(t);
        i-=0.5;
      }
    }
    if (this.toppings[t]['R']){
      var i = this.toppings[t]['R'] ;
      while (i>0){
        this.topping_count[this.toppings[t]['category']] += 0.5 ;
        this.toppings_right.push(t) ;
        i-=0.5 ;
      }
    }
  }

  //calcul le nombre standard de garnitures
  this.standard_toppings = new Array() ;
  for (var t in this.standard_topping_list){
    if (!this.standard_toppings[this.toppings[t]['category']])
      this.standard_toppings[this.toppings[t]['category']] = 0 ;
    this.standard_toppings[this.toppings[t]['category']] += 1 ;
  }

  //avec les noms de catégorie, vérifie l'existance de chacune des catégorie
  for (var t in this.category_names){
    if (!this.standard_toppings[t])
      this.standard_toppings[t] = 0 ;
  }


  //calcul le nombre de garniture maximum
  this.max_toppings = new Array();
  //génère le nombre de topping par categorie

  for (var t in this.standard_toppings){
    if (this.standard_toppings[t])
      this.max_toppings[t] = this.standard_toppings[t] + 3 ;
    else
      this.max_toppings[t] = 3 ;

  }//*/
}

/**
* Cette fonction vérifie qu'un certain pourcentage des garnitures initiales
* sont toujours sélectionnée.
*/
SpecialToppingManager.prototype.getStandardToppingCount = function(){
  var count = 0;
  for (var tid in this.toppings_all){
    if (this.standard_topping_list[this.toppings_all[tid]])
      count++ ;
  }
  for (var tid in this.toppings_left){
    if (this.standard_topping_list[this.toppings_left[tid]])
      count+=0.5 ;
  }
  for (var tid in this.toppings_right){
    if (this.standard_topping_list[this.toppings_right[tid]])
      count+=0.5 ;
  }

  return count ;
}

/******************* DÉFINITION DES FONCTIONS DE SpecialToppingManager *******************/
/**
* Fonction pour obtenir le prix total de toute les garniture
* @return int le prix de toute les garnitures
*/
SpecialToppingManager.prototype.getPrice = function(){
  if (!this.pizzaManager.selected_size)
    return 0;
  var price = 0;

  //enlève les quantité standard et calcul le prix
  for (var cat in this.topping_count){
    var qty = this.topping_count[cat] - this.standard_toppings[cat] ;
    if (qty > 0)
      price += qty * this.topping_prices[this.pizzaManager.selected_size][cat];
  }

  return price;
}

/**
* Charge les garnitures dans leur liste de sélection respective.
*/
SpecialToppingManager.prototype.loadToppings = function(){
  //ajoute le premier élément
  var index = new Array  ;
  var form_list = new Array() ;
  for (var i in this.selection_lists){
    form_list[i] = document.forms[this.pizzaManager.form_name][this.selection_lists[i]] ;
    index[i] = 0 ;
    form_list[i].options[index[i]++] = new Option(TEXT_SELECT_TOPPING[i], -1, false, false );
  }

  for (var t in toppings){
    form_list[this.toppings[t]['category']].options[index[this.toppings[t]['category']]++] = new Option(this.toppings[t]['name'], t, false, false );
  }

  //peuple la liste de garnitures
  //var list = document.forms[this.pizzaManager.form_name][this.selection_list] ;
  //var i = 0;
  //list.options[i++] = new Option(TEXT_SELECT_TOPPING, -1, false, false );
  //for (var t in toppings){
  //  list.options[i++] = new Option(this.toppings[t]['name'], t, false, false );
  //}

  this.refreshDisplay();
}

/**
* Fonction pour rafraichir l'affichage de la sélection
*/
SpecialToppingManager.prototype.refreshDisplay = function(){
   if (!this.pizzaManager.selected_size)
    return ;
   var html = "" ;

  //var list = document.forms[this.pizzaManager.form_name][this.selection_list] ;

  var topping_display = document.getElementById(this.selection_display) ;

  var count = new Array();
  var included = new Array();
  var extra = new Array();
  for (var i in this.category_names){
    count[i] = 0 ;
    included[i] = {'A' : new Array(), 'L' : new Array() ,'R' : new Array()};
    extra[i] = {'A' : new Array(), 'L' : new Array() ,'R' : new Array()};
  }

  //passe à travers de la liste des garnitures et les ajoutes aux bon array
  for (var k in this.toppings_all){
    var t = this.toppings_all[k] ;
    if (this.toppings_all[k] == null)
      continue;
    if (count[this.toppings[t]['category']] >= this.standard_toppings[this.toppings[t]['category']]) {
      extra[this.toppings[t]['category']]['A'].push(t) ;
    }
    else{
      included[this.toppings[t]['category']]['A'].push(t) ;
    }
    //Ajoute 1 au compte pour cette garniture
    count[this.toppings[t]['category']] += 1;
  }

  for (var k in this.toppings_left){
    var t = this.toppings_left[k] ;
    if (this.toppings_left[k] == null)
      continue;
    if (count[this.toppings[t]['category']] >= this.standard_toppings[this.toppings[t]['category']]) {
      extra[this.toppings[t]['category']]['L'].push(t) ;
    }
    else{
      included[this.toppings[t]['category']]['L'].push(t);
    }
    //Ajoute 1 au compte poru cette garniture
    count[this.toppings[t]['category']] += 0.5;
  }

  for (var k in this.toppings_right){
    var t = this.toppings_right[k] ;
    if (this.toppings_right[k] == null)
      continue;
    if (count[this.toppings[t]['category']] >= this.standard_toppings[this.toppings[t]['category']]) {
      extra[this.toppings[t]['category']]['R'].push(t) ;
    }
    else{
      included[this.toppings[t]['category']]['R'].push(t);
    }
    //Ajoute 1 au compte poru cette garniture
    count[this.toppings[t]['category']] += 0.5;
  }

  //------------  */

  if (DEBUG){
     html = "<table><tr>Included</tr><tr><td>" ;
     for (var i in included){
        html += i + " all : " + included[i]['A'].toString() + "<br>";
        html += i + " left : " + included[i]['L'].toString() + "<br>";
        html += i + " right : " + included[i]['R'].toString() + "<br>";
     }

     html += "</td></tr></table>";

     html += "<table><tr>Extra</tr><tr><td>" ;
     for (var i in extra){
        html += i + " all : " + extra[i]['A'].toString() + "<br>";
        html += i + " left : " + extra[i]['L'].toString() + "<br>";
        html += i + " right : " + extra[i]['R'].toString() + "<br>";
     }

     html += "</td></tr></table>";

  }//End debug string

  //AFFICHAGE DES TOPPINGS DE LA PIZZA
  html += "<table width='380'><tr><td class='text12pizzainfo' colspan='5'><b>" + HEADING_STANDARD_TOPPINGS + "</td></tr></table><table>" ;
  for (var i in included){
    html += "<tr>" ;
    html += " <td width='10'><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
    html += " <td colspan=3 class='text12pizzainfo' align='left'>" + this.category_names[i] ;
    //affiche nb topping restant
    var tr = this.standard_toppings[i] - this.topping_count[i] ;
    if (tr > 0)
      html += "&nbsp;&nbsp;&nbsp;&nbsp;<small>(" + tr + ") " + ((tr==1) ? TEXT_TOPPING_REMAINING : TEXT_TOPPINGS_REMAINING) + "</small>" ;
    html += " </td>" ;
    html += "</tr>" ;

    //Garniture incluse sur toute la pizza
    for (var k in included[i]['A']){
      html += "<tr>" ;
      html += " <td width='10'><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
      html += " <td width='10'><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
      html += " <td class='text12pizzainfo' width='40'>" ;
      html += "   <img src='images/button_down.gif' style='cursor:pointer;' onmouseup='" + this.id + ".removeTopping(" + included[i]['A'][k] + ", \"A\");'>" ;
      html += "   <img src='images/pizza_full.gif' width='20' height='20'>" ;
      //html += "   <input type='hidden' name='topping_A_" + included[i]['A'][k] + "' value='" + included[i]['A'][k] + "'>" ;
      html += " </td>" ;
      html += " <td class='text12pizzainfo' align='left' width='300'>" ;
      html +=     toppings[included[i]['A'][k]]['name'] ;
      html += " </td>" ;
      html += "</tr>" ;
    }

    //Garniture incluse sur la partie gauche
    for (var k in included[i]['L']){
      html += "<tr>" ;
      html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
      html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
      html += " <td class='text12pizzainfo'>" ;
      html += "   <img src='images/button_down.gif' style='cursor:pointer;' onmouseup='" + this.id + ".removeTopping(" + included[i]['L'][k] + ", \"L\");'>" ;
      html += "   <img src='images/pizza_left.gif' width='10' height='20'>" ;
      //html += "   <input type='hidden' name='topping_L_" + included[i]['L'][k] + "' value='" + included[i]['L'][k] + "'>" ;
      html += " </td>" ;
      html += " <td class='text12pizzainfo' align='left'>" ;
      html +=     toppings[included[i]['L'][k]]['name'] ;
      html += " </td>" ;
      html += "</tr>" ;
    }

    //Garniture incluse sur la partie droite
    for (var k in included[i]['R']){
      html += "<tr>" ;
      html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
      html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
      html += " <td class='text12pizzainfo'>" ;
      html += "   <img src='images/button_down.gif' style='cursor:pointer;' onmouseup='" + this.id + ".removeTopping(" + included[i]['R'][k] + ", \"R\");'>" ;
      html += "   <img src='images/pizza_right.gif' width='10' height='20'>" ;
      //html += "   <input type='hidden' name='topping_R_" + included[i]['R'][k] + "' value='" + included[i]['R'][k] + "'>" ;
      html += " </td>" ;
      html += " <td class='text12pizzainfo'>" ;
      html +=     toppings[included[i]['R'][k]]['name'] ;
      html += " </td>" ;
      html += "</tr>" ;
    }
  }//fin affichage garniture incluses

  html += "</table><table><tr><td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='10'></td></tr></table>" ;

  //AFFICHAGE DES GARNITURES EN EXTRA
  //seulement si des garniture extra sont présente.
  if (this.hasExtra()){
    html += "<table><tr><td class='text12pizzainfo'><b>" + HEADING_EXTRA_TOPPINGS + "</b></td></tr></table><table>" ;
    for (var i in extra){

      if (extra[i]['A'].length > 0 || extra[i]['L'].length > 0 || extra[i]['R'].length > 0){
        html += "<tr>" ;
        html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
        html += " <td colspan=3 class='text12pizzainfo'>" + this.category_names[i] ;

        html += " </td>" ;
        html += "</tr>" ;

        //Garniture incluse sur toute la pizza
        for (var k in extra[i]['A']){
          html += "<tr>" ;
          html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
          html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
          html += " <td class='text12pizzainfo'>" ;
          html += "   <img src='images/button_down.gif' style='cursor:pointer;' alt='A' onmouseup='" + this.id + ".removeTopping(" + extra[i]['A'][k] + ", \"A\");'>" ;
          html += "   <img src='images/pizza_full.gif' width='20' height='20'>" ;
          //html += "   <input type='hidden' name='topping_A_" + extra[i]['A'][k] + "' value='" + extra[i]['A'][k] + "'>" ;
          html += " </td>" ;
          html += " <td class='text12pizzainfo'>" ;
          html +=     toppings[extra[i]['A'][k]]['name'] ;
          html += "&nbsp;&nbsp;&nbsp;&nbsp;<small>(+" + LEFT_DOLLARD_SIGN + (this.topping_prices[this.pizzaManager.selected_size][this.toppings[extra[i]['A'][k]]['category']]) + RIGHT_DOLLARD_SIGN + ")</small>" ;
          html += " </td>" ;
          html += "</tr>" ;
        }

        //Garniture incluse sur la partie gauche
        for (var k in extra[i]['L']){
          html += "<tr>" ;
          html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
          html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
          html += " <td class='text12pizzainfo'>" ;
          html += "   <img src='images/button_down.gif' style='cursor:pointer;' alt='L' onmouseup='" + this.id + ".removeTopping(" + extra[i]['L'][k] + ", \"L\");'>" ;
          html += "   <img src='images/pizza_left.gif' width='10' height='20'>" ;
          //html += "   <input type='hidden' name='topping_L_" + extra[i]['L'][k] + "' value='" + extra[i]['L'][k] + "'>" ;
          html += " </td>" ;
          html += " <td class='text12pizzainfo'>" ;
          html +=     toppings[extra[i]['L'][k]]['name'] ;
          html += "&nbsp;&nbsp;&nbsp;&nbsp;<small>(+" + LEFT_DOLLARD_SIGN + (this.topping_prices[this.pizzaManager.selected_size][this.toppings[extra[i]['L'][k]]['category']]/2).toFixed(2) + RIGHT_DOLLARD_SIGN + ")</small>" ;
          html += " </td>" ;
          html += "</tr>" ;
        }

        //Garniture incluse sur la partie droite
        for (var k in extra[i]['R']){
          html += "<tr>" ;
          html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
          html += " <td><img src='images/pixel_trans.gif' border='0' alt='' width='10' height='100%'></td>" ;
          html += " <td class='text12pizzainfo'>" ;
          html += "   <img src='images/button_down.gif' style='cursor:pointer;' alt='R' onmouseup='" + this.id + ".removeTopping(" + extra[i]['R'][k] + ", \"R\");'>" ;
          html += "   <img src='images/pizza_right.gif' width='10' height='20'>" ;
          //html += "   <input type='hidden' name='topping_R_" + extra[i]['R'][k] + "' value='" + extra[i]['R'][k] + "'>" ;
          html += " </td>" ;
          html += " <td class='text12pizzainfo'>" ;
          html +=     toppings[extra[i]['R'][k]]['name'] ;
          html += "&nbsp;&nbsp;&nbsp;&nbsp;<small>(+" + LEFT_DOLLARD_SIGN + (this.topping_prices[this.pizzaManager.selected_size][this.toppings[extra[i]['R'][k]]['category']]/2).toFixed(2) + RIGHT_DOLLARD_SIGN + ")</small>" ;
          html += " </td>" ;
          html += "</tr>" ;
        }
      }
    }//fin affichage garniture incluses
  }

  html += "</table>" ;
  html += "<table>" ;
  html += "<tr>" ;
  html += " <td>" ;

  for (var tid in this.toppings){

    var cat = this.toppings[tid]['category'] ;
    var qty= "" ;
    var show = false ;
    if (this.toppings[tid]['A'] > 0){
      qty += this.toppings[tid]['A'] + "-A:" ;
      show = true ;
    }
    if (this.toppings[tid]['L'] > 0) {
      qty += this.toppings[tid]['L'] + "-L:" ;
      show = true ;
    }
    if (this.toppings[tid]['R'] > 0) {
      qty += this.toppings[tid]['R'] + "-R:" ;
      show = true ;
    }

    qty = qty.substring(0, qty.length-1);

    if (this.toppings[tid]['A']>0 || this.toppings[tid]['L']>0 || this.toppings[tid]['R']>0){
      html += "<input type='hidden' name='" + this.topping_list + "[" + cat + "][" + tid +"]' value='" + qty + "'>" ;
    }

  }
  html += " </td>"    ;
  html += "</tr>"     ;
  html += "</table>"  ;

  if (DEBUG){
    html = this.debug(html);
  }

  var disp = document.getElementById(this.selection_display) ;
  disp.innerHTML = html ;

  this.pizzaManager.refreshPrice();


}//END FUNCTION refreshDisplay()


/**
* Détermine si cette pizza possède des garniture en extra ou non.
*/
SpecialToppingManager.prototype.hasExtra = function (){
  //vérifie si il y'a des extra
  var ext = false ;
  for (var i in this.topping_count){
    if (this.topping_count[i] > this.standard_toppings[i])
      ext = true ;
  }
  return ext ;
}

/**
* Fonction pour ajouter un topping
*/
SpecialToppingManager.prototype.addTopping = function(type){
  for (var i in this.selection_lists){
    var list = document.forms[this.pizzaManager.form_name][this.selection_lists[i]] ;
    if (list.selectedIndex > 0) ;
      this.addTopping_helper(type, list);
  }
}

/**
* Fonction pour ajouter un topping
*/
SpecialToppingManager.prototype.addTopping_helper = function(type, list){
  var selectedIndex = list.selectedIndex ;
  if (selectedIndex < 1)
    return;

  var tid = list[selectedIndex].value ; //topping id
  var cat = this.toppings[tid]['category'] ; //categorie du topping

  var selectionArray ;

  //avant d'ajouter, vérifie si la limite de garniture est atteinte

  if (this.topping_count[cat] < this.max_toppings[cat]){
    switch (type){
      case 'A' :
      default:
        selectionArray = this.toppings_all ;
        this.topping_count[cat]  = 1 * this.topping_count[cat] + 1;
        this.toppings[tid]['A']  = 1 * this.toppings[tid]['A'] + 1 ;
        break;
      case 'L':
        selectionArray = this.toppings_left ;
        this.toppings[tid]['L'] = 1* this.toppings[tid]['L'] + 0.5 ;
        this.topping_count[cat] = 1*this.topping_count[cat] + 0.5;
        break;
      case 'R' :
        selectionArray = this.toppings_right ;
        this.toppings[tid]['R'] = 1* this.toppings[tid]['R'] +  0.5 ;
        this.topping_count[cat] = 1* this.topping_count[cat] + 0.5;
        break;
    }  //*/
    //Ajouté la garniture sélectionnée à la liste appropriée
    selectionArray.push(list[selectedIndex].value);

    //effacer l'élément de la liste si sa quantité est supérieure ou égale à 2
    if ((1*this.toppings[tid]['A']+1*this.toppings[tid]['L']+1*this.toppings[tid]['R']) >= 2){
      list.remove(list.selectedIndex);
      list.selectedIndex = 0;
    }
    this.regroupTopping();
    this.refreshDisplay();
  } else{
    alert (TEXT_ERROR_MAX_TOPPING_REACHED[this.toppings[tid]['category']]);
  }
  list.selectedIndex = 0;
  this.getStandardToppingCount();
  //alert ("initialBasic = " + this.initialBasic + ", currentBasic = " + this.currentBasic + "\ninitialDeluxe = " + this.initialDeluxe + ", currentDeluxe = " + this.currentDeluxe);
}//END FUNCTION addTopping(type);

/**
* Fonction pour obtenir retirer un topping
*/
SpecialToppingManager.prototype.removeTopping = function(tid, type){
  //vérifie qu'on ne va pas sous le nombre minimale de garniture standard
  var std = 0;
  for (var c in this.standard_toppings)
    std += this.standard_toppings[c] ;
  if (this.standard_topping_list[tid] && this.getStandardToppingCount() <=  std - 2){
    alert (TEXT_ERROR_MIN_STANDARD_TOPPING);
    return ;
  }

  var cat = this.toppings[tid]['category'] ; //categorie du topping
  var list = document.forms[this.pizzaManager.form_name][this.selection_lists[cat]] ;

  switch (type){
    case 'A' :
    case 'a' :
      var mbr = contain(this.toppings_all,tid) ;
      this.toppings_all.splice(mbr,1) ;
      this.topping_count[cat] --;
      this.toppings[tid]['A'] -- ;
      break ;
    case 'L' :
    case 'l' :
      var mbr = contain(this.toppings_left,tid) ;
      this.toppings_left.splice(mbr,1) ;
      this.topping_count[cat] -= 0.5 ;
      this.toppings[tid]['L'] -= 0.5 ;
      break;
    case 'R' :
    case 'r' :
      var mbr = contain(this.toppings_right,tid) ;
      this.toppings_right.splice(mbr,1) ;
      this.topping_count[cat] -= 0.5 ;
      this.toppings[tid]['R'] -= 0.5 ;
      break;
  }

  var inList = false ;
  for (var i=0 ; i<list.options.length ; i++){
    if (list.options[i].value == tid)
      inList = true;
  }

  if (!inList){

    list.options[list.options.length] = new Option(this.toppings[tid]['name'], tid, false, false);

    //si l'élément n'était pas dans la liste, on le rajoute.
    var toppingOptions = list.options ;
    var toppingLength = toppingOptions.length - 1 ;

    //sort the selection list
    while (toppingLength > 0 && toppingOptions[toppingLength].text < toppingOptions[toppingLength-1].text) {
      tempText = toppingOptions[toppingLength-1].text;
      tempValue = toppingOptions[toppingLength-1].value;
      toppingOptions[toppingLength-1].text = toppingOptions[toppingLength].text;
      toppingOptions[toppingLength-1].value = toppingOptions[toppingLength].value;
      toppingOptions[toppingLength].text = tempText;
      toppingOptions[toppingLength].value = tempValue;
      toppingLength = toppingLength - 1;
    }
    if (list.selectedIndex > toppingLength)
      list.selectedIndex++;
  }

  this.refreshDisplay();   //*/
}//END FUNCTION removeTopping(toppingId)


/**
* Fonction simplifiant la liste de toppings.
* Si un toppings se retrouve sur la partie droite et gauche, on le retire des deux et
* on l'ajoute à toute la pizza.
*/
SpecialToppingManager.prototype.regroupTopping = function(){
  //pour chaque élément dans toppings_left
  for (var k in this.toppings_left){
    var tid = this.toppings_left[k] ;
    var onRight = contain(this.toppings_right,tid) ;
    if (onRight){
      this.toppings_right.splice(onRight,1) ;
      this.toppings_left.splice(k, 1);
      this.toppings[tid]['L'] = 1 * this.toppings[tid]['L'] - 0.5 ;
      this.toppings[tid]['R'] = 1 * this.toppings[tid]['R'] - 0.5 ;
      this.toppings[tid]['A']  = 1 * this.toppings[tid]['A'] + 1 ;
      this.toppings_all.push(tid);
    }
  }
}

SpecialToppingManager.prototype.debug = function(html){
     html += "<table><tr>Toppings Array</tr></td></tr></table>" ;

     html += "<table>" ;
     for (var i in this.toppings){
          html += "<tr><td width='40%'>" + this.toppings[i]['name'] + "</td>" + "<td>" ;
          html += "A > " + this.toppings[i]['A'] + ", ";
          html += "L > " + this.toppings[i]['L'] + ", ";
          html += "R > " + this.toppings[i]['R'] + ", ";
          html +=  "</td></tr>" ;
        //html += "<br>" ;
     }

     //html += "<tr><td>" + this.toppings_all.toString() + "<br>";
     //html += this.toppings_left.toString() + "<br>";
     //html += this.toppings_right.toString() + "<br>";

     html += "</table>";

     return html
     //End debug string
}

SpecialToppingManager.prototype.changeRecipe = function(new_toppings){

  for (var i in this.toppings){
    this.toppings[i]['A'] = 0 ;
    this.toppings[i]['L'] = 0 ;
    this.toppings[i]['R'] = 0 ;
  }

  for (var i in new_toppings){
    this.toppings[i]['A'] = new_toppings[i]['A'];
    this.toppings[i]['L'] = new_toppings[i]['L'];
    this.toppings[i]['R'] = new_toppings[i]['R'];
  }

  this.standard_topping_list = new_toppings ;
  this.initialize();//*/
}

SpecialToppingManager.prototype.setToppings = function(toppings){
  this.toppings = toppings ;
  this.initialize();//*/
}
