
function ordine(nf){

  var qta=document.forms[nf].quantita.value;

   if(qta.length==0) {
    document.forms[nf].elements['quantita'].value='1';
   }
  qta=document.forms[nf].quantita.value;

  if(checkNumber(nf)){
    if(qta>0){
       var giac=document.forms[nf].elements['giacenza'].value;
       if(parseFloat(qta) > parseFloat(giac)){
         document.forms[nf].elements['quantita'].value=document.forms[nf].elements['giacenza'].value;
         window.alert("quantità massima disponibile in magazzino: " +giac+" pezzi");
         //document.forms[nf].submit();
       }
       document.forms[nf].submit();
    } else {
       window.alert("Introdurre un numero maggiore di zero");
       document.forms[nf].elements['quantita'].value="";
    }
  } else {
     window.alert("Il valore introdotto non è corretto");
     document.forms[nf].elements['quantita'].value="";
  }

}



function annullaOrdine(ssl){
  if (confirm("Confermi l'annullo dell'ordine ?")){
       if(ssl)
         window.location.replace(ssl+"AnnullaOrdine.jsp");
       else
         window.location.replace("AnnullaOrdine.jsp");
  }
}


function CancellaOrdine(idOrdine,idTipoPagamento){
  if (confirm("Sei sicuro di voler annullare l'ordine ?")){
      window.location.replace("CancellaOrdine.jsp?idOrdine=" + idOrdine +"&idTipoPagamento="+idTipoPagamento);
  }
}

function ModificaOrdine(idOrdine){
  window.location.replace("ModificaOrdine.jsp?idOrdine=" + idOrdine);

}

function DettaglioOrdine(idOrdine){
  var page='DettaglioOrdine.jsp?idOrdine='+idOrdine;
  window.open(page,'dettordine','scrollbars=yes,resizable=yes,width=600,height=350,toolbar=no');
}



/* ====== ANOMALIE ========================================*/

function AnnullaAnomalia(idAnomalia,idOrdine){
  if (confirm("Sei sicuro di voler annullare l'anomalia ?")){
      window.location.replace("AnnullaAnomalia.jsp?idAnomalia=" + idAnomalia +"&idOrdineOriginale=" +idOrdine);
  }
}

  function selezionaAnomalia(idform,matchselect,val) {


  var myform = document.forms[idform];
  var campi = myform.elements;
  var ncampi = myform.length;

  for (var i=0;i<ncampi;i++) {
    if(campi[i].type=="select-one"){
      if (campi[i].id.indexOf(matchselect)!=-1) {
        var prod=campi[i].id.substring(11);
        if(document.getElementById('omaggio_'+prod).value=='1' && val=="REC") {
          campi[i].value='';
          document.getElementById('quantita_'+prod).value='';
        } else{
          if(val=="INV") {
            addItem(campi[i].id,'INV','pacco invertito');
          } else {
            removeItem(campi[i].id,'INV','pacco invertito');
          }
          if(val=="COR") {
            addItem(campi[i].id,'COR','mancata consegna');
          } else {
            removeItem(campi[i].id,'COR','mancata consegna');
          }
          campi[i].value=val;
          document.getElementById('quantita_'+prod).value=document.getElementById('qtaOrd_'+prod).value;
        }
      }
    }
  }
  if(val=="REC"){
    for (i=0;i<ncampi;i++) {
    if(campi[i].type=="select-one"){
      if (campi[i].id.indexOf('idTipoOperazioneAnomalia')!=-1) {
        var prod=campi[i].id.substring(25);
        if(document.getElementById('omaggio_'+prod).value=='1') {
           campi[i].value='0';
           campi[i].disabled=false;
        } else {
          removeItem(campi[i].id,'0','----');
          campi[i].value='1';
          campi[i].disabled=true;
        }
      }
    }
   }
  } else {
    for (i=0;i<ncampi;i++) {
   if(campi[i].type=="select-one"){
     if (campi[i].id.indexOf('idTipoOperazioneAnomalia')!=-1) {
       var prod=campi[i].id.substring(25);
       if(document.getElementById('omaggio_'+prod).value=='1'){
         campi[i].value='2';
         campi[i].disabled=true;
       } else {
         removeItem(campi[i].id,'0','----');
         campi[i].disabled=false;
       }
     }
   }
   }
  }
}

function selezionaOperazione(riga,val,om) {
  var campo = document.getElementById('idTipoOperazioneAnomalia_'+riga);
  if(om=='0') {
    if(document.getElementById('idAnomalia_'+riga).value==""){
      addItem(campo.id,'0','=====');
      campo.value='0';
      campo.disabled=false;
    } else {
      if(document.getElementById('idAnomalia_'+riga).value=="REC"){
        removeItem(campo.id,'0','=====');
        campo.value=val;
        campo.disabled=true;
      } else {
        if(document.getElementById('idAnomalia_'+riga).value=="ECC"){
          addItem(campo.id,'0','=====');
          campo.value='0';
          campo.disabled=true;
        } else {
          removeItem(campo.id,'0','====');
          campo.disabled=false;
        }
      }
    }
  } else {
    if(document.getElementById('idAnomalia_'+riga).value==""){
      addItem(campo.id,'0','=====');
      campo.value='0';
      campo.disabled=false;
    } else {
      removeItem(campo.id,'0','=====');
      campo.value=2;
      campo.disabled=true;
    }
  }
}



function checkAnomalie(form,matchselect) {
  var myform = document.forms[form];
  var campi = myform.elements;
  var ncampi = myform.length;
  var s='';

  var ok=false;
  for (i=0;i<ncampi;i++) {
    if(campi[i].type=="select-one"){
      if (campi[i].id.indexOf(matchselect)!=-1) {
         if(campi[i].value!=null && campi[i].value != ""){
           ok=true;
           var id=campi[i].id;
           var ind1=id.indexOf('_');
           var cod=id.substring(ind1+1,id.length);
           var qta=document.getElementById('quantita_'+cod).value;
           if(qta==null || qta==""){
             var desc=document.getElementById('descrizione_'+cod).value;
             alert('Il campo quantità per il prodotto\n' + desc +  '('+cod+')\n non è stato compilato');
             document.getElementById('quantita_'+cod).focus();
             return false;
           } else {
             var qtaOrd=document.getElementById('qtaOrd_'+cod).value;
             if(parseFloat(qta) > parseFloat(qtaOrd)){
               alert('La quantità rotta/mancante del prodotto\n'+ desc +  '('+cod+')\n non può essere superiore alla quantità ordinata');
               document.getElementById('quantita_'+cod).focus();
               return false;
             }
           }
           var tipoOper=document.getElementById('idTipoOperazioneAnomalia_'+cod).value;
           if(tipoOper=="0" && campi[i].value!="ECC"){
             var desc=document.getElementById('descrizione_'+cod).value;
             alert('Il campo "cosa vuoi da noi" per il prodotto\n' + desc +  '('+cod+')\n non è stato selezionato');
             document.getElementById('idTipoOperazioneAnomalia_'+cod).focus();
             return false;
           }
         } else s='Seleziona la voce "problema" della spedizione.';
      }

       if (campi[i].id.indexOf('idTipoOperazioneAnomalia')!=-1) {
          if(campi[i].value!=null && campi[i].value != '0'){
            ok=true;
            var id=campi[i].id;
            var ind1=id.indexOf('_');
            var cod=id.substring(ind1+1,id.length);
            var qta=document.getElementById('quantita_'+cod).value;
            if(qta==null || qta==""){
              var desc=document.getElementById('descrizione_'+cod).value;
              alert('Il campo quantità per il prodotto\n' + desc +  '('+cod+')\n non è stato compilato');
              document.getElementById('quantita_'+cod).focus();
              return false;
            } else {
              var qtaOrd=document.getElementById('qtaOrd_'+cod).value;
			  if(parseFloat(qta) > parseFloat(qtaOrd)){
                alert('La quantità rotta/mancante del prodotto\n'+ desc +  '('+cod+')\n non può essere superiore alla quantità ordinata');
                document.getElementById('quantita_'+cod).focus();
                return false;
              }
            }
            var oper=document.getElementById('idAnomalia_'+cod).value;
            if(oper== "" || oper==null ){
              var desc=document.getElementById('descrizione_'+cod).value;
              alert('Seleziona la voce "problema" della spedizione per il prodotto\n' + desc +  '('+cod+')\n non è stato selezionato');
              document.getElementById('idAnomalia_'+cod).focus();
              return false;
            }

          }

       }

    }
  }

  for(i=1;i<=3;i++){
     var ecc=document.getElementById('descrizione'+i).value;
     if(ecc!=null && ecc!=""){
       ok=true;
       var qtaEcc=document.getElementById('quantita'+i).value;
       if(qtaEcc==null || qtaEcc==""){
         alert('Il campo quantità per la riga' + i + ' dei prodotti ricevuti in più\n non è stato compilato');
         document.getElementById('quantita'+i).focus();
         return false;
       }
     }
  }

  if(!ok){
     alert(s);
     return false;

  }
  return true;

}

function checkQuantita(qta1,idsel) {

  var campo = document.getElementById(idsel);
  var qta2=campo.value;
  if (isNaN(qta2) && qta2!="") {
    alert("La quantità deve essere un valore numerico!!");
    campo.value = "";
    campo.focus();
    return false;
  }
  if (qta2 <= 0 && qta2!="") {
    alert("La quantità deve essere maggiore di zero.");
    campo.value = "";
    campo.focus();
    return false;
  }

  if(qta1 > 0) {
    if(parseFloat(qta2) > parseFloat(qta1)){
      alert('La quantità rotta/mancante non può essere superiore\n alla quantità ordinata');
      campo.value = "";
      campo.focus();
      return false;
    }
  }
}

function requiredCampi(){
  var campo1 = document.getElementById("abi");
  campo1.id="required5";
  var campo2 = document.getElementById("cab");
  campo2.id="required6";
 // var campo3 = document.getElementById("cin");
  //campo3.id="required7";
  //var campo4 = document.getElementById("iban");
 // campo4.id="required8";
  var campo5 = document.getElementById("contoCorrente");
  campo5.id="required9";
}

function notRequiredCampi(){
  var campo1 = document.getElementById("required5");
  campo1.id=campo1.name;
  var campo2 = document.getElementById("required6");
  campo2.id=campo1.name;
 //var campo3 = document.getElementById("required7");
 // campo3.id=campo1.name;
 // var campo4 = document.getElementById("required8");
 // campo4.id=campo1.name;
  var campo5 = document.getElementById("required9");
  campo5.id=campo1.name;
}

/* =====================================================*/



