//costruiamo la formula 1
//var sped6bott = false;
//var sped12bott = false;
//var sped24bott = false;
//var spedO24bott = false;

function somma(num,punit,totunit) {
  var wnum = parseInt(document.getElementById(num).value);
  var wpunit = document.getElementById(punit).value;
  wpunit = parseFloat(wpunit.replace(",","."));
  var TextTotP = document.getElementById(totunit);
  var totbott = 0;
  var totord = 0;
  //alert(wpunit);
  if (document.getElementById(num).value != "") {
    if (document.getElementById(totunit).value == "") {
        //alert((wnum*wpunit));
        //alert(formatta(wnum*wpunit));
        document.getElementById(totunit).value = formatta(wnum*wpunit);
        if (document.getElementById("totale_complessivo").value == "") {
          totord = 0;
        } else {
          var totord = document.getElementById("totale_complessivo").value;
          totord = parseFloat(totord.replace(",","."));
        }
        //        alert(totord);
    } else {
      var totord = document.getElementById("totale_complessivo").value;
      totord = parseFloat(totord.replace(",","."));
      //alert(totord);
      var totale_unita = document.getElementById(totunit).value;
      totale_unita = parseFloat(totale_unita.replace(",","."));
      totord = totord - totale_unita;
      //alert(totale_unita);
      //alert(totord);
      //alert(formatta(wnum*wpunit));
      document.getElementById(totunit).value = formatta(wnum*wpunit);
        //        alert(totord);
    }
    totord = totord + (wnum*wpunit);
    if (document.getElementById("totale_bottiglie").value == "") {
      totbott = totbott + wnum;
    } else {
      totbott = parseInt(document.getElementById("totale_bottiglie").value);

      if (document.getElementById(num+"_old").value !="") {
        totbott = totbott + wnum - parseInt(document.getElementById(num+"_old").value);
      } else {
        totbott = totbott + wnum;
      }
    }
    //-----------------
    // modifiche marco 2007.12.27
    //-----------------
    y=0;
    nSpedOld=0;
    for(y=0;y<aSpedizioni.length;y++){
      aSpedizione=aSpedizioni[y];
      if ( (totbott <= aSpedizione[0] && nSpedOld<totbott) && (aSpedizione[2] == false) ) {
        for(x=0;x<aSpedizioni.length;x++){
          aSpedizConfr=aSpedizioni[x];
          if ( (aSpedizione[0] != aSpedizConfr[0]) && (aSpedizConfr[2] == true ) ) {
            totord = totord - parseInt(aSpedizConfr[1]);
            aSpedizioni[x][2] = false;
            document.getElementById("costo_spedizione_"+aSpedizConfr[0]).checked = false;
          }
        }
        aSpedizioni[y][2] = true;
        document.getElementById("costo_spedizione_"+aSpedizione[0]).checked = true;
        totord = totord + aSpedizione[1];
      }
      nSpedOld=aSpedizioni[y][0];
    }
    //-----------------
    // modifiche marco 2007.12.27
    //-----------------
    //
    //alert(totord);
    //alert(formatta(totord));
    document.getElementById("totale_complessivo").value = formatta(totord);
    document.getElementById("totale_bottiglie").value = totbott;
  }
}

function ControllaNumBott(num) {
  document.getElementById(num+"_old").value = document.getElementById(num).value;
}

function ControllaTotBott(chk) {
  if ((document.getElementById(chk).value == "10") && (parseInt(document.getElementById("totale_bottiglie").value) > 6))   {
    alert("Selezionare Spedizione appropriata al numero di bottiglie");
    document.getElementById(chk).checked = false;
  }

  if ((document.getElementById(chk).value == "18") && ( (parseInt(document.getElementById("totale_bottiglie").value) > 12) || (parseInt(document.getElementById("totale_bottiglie").value) < 7))) {
    alert("Selezionare Spedizione appropriata al numero di bottiglie");
    document.getElementById(chk).checked = false;
  }

  if ((document.getElementById(chk).value == "30") && (  (parseInt(document.getElementById("totale_bottiglie").value) > 24) || (parseInt(document.getElementById("totale_bottiglie").value) < 13))) {
    alert("Selezionare Spedizione appropriata al numero di bottiglie");
    document.getElementById(chk).checked = false;
  }

  if ((document.getElementById(chk).value == "0") && (parseInt(document.getElementById("totale_bottiglie").value) < 24)) {
    alert("Selezionare Spedizione appropriata al numero di bottiglie");
    document.getElementById(chk).checked = false;
  }
}

function formatta(valore) {
  var n_intero = Math.floor (valore);
  var n_decimale = valore - n_intero;

  // Costruiamo la stringa per la parte intera
  // partendo dalla fine e inserendo un punto ogni 3 caratteri
  var s_intero = new String (n_intero);
  var length = s_intero.length;
  var n_punti = Math.floor ((length - 1) / 3);
  var mod = (length - 1) % 3;
  for (i = 0; i < n_punti; i++) {
    var primaparte = s_intero.substring(0, mod + 3*(n_punti - i - 1) + 1);
    var secondaparte = s_intero.substring(mod + 3*(n_punti - i - 1) + 1);
    s_intero = primaparte + '.' + secondaparte;
  }

  // Costruiamo la stringa per la parte decimale
  var s_decimale = '';
  if (n_decimale == 0) {
    s_decimale = '00';
  } else {
    s_decimale = new String (Math.round (n_decimale * 100));
    if (10 > s_decimale) {
    s_decimale = '0'+s_decimale;
    }
  }
  return (s_intero + ',' + s_decimale);
}
