
var acquisti = {id_acquisto:0, id_utente:0, nome:"", cognome:"", codfis:"",
              partita_iva:"", indirizzo:"", n_civ:"", cap:"", comune:"", provincia:"",
              email:"", telefono:"", fax:"", nomesp:"", cognomesp:"", 
              indirizzosp:"", n_civsp:"", capsp:"", comunesp:"", provinciasp:"", 
              id_cliente:"", modpag:"", esito_pagamento:"", modspe:""};

function clearAcquisti()
{
    acquisti = {id_acquisto:0, id_utente:0, nome:"", cognome:"", codfis:"",
              partita_iva:"", indirizzo:"", n_civ:"", cap:"", comune:"", provincia:"",
              email:"", telefono:"", fax:"", nomesp:"", cognomesp:"", 
              indirizzosp:"", n_civsp:"", capsp:"", comunesp:"", provinciasp:"", 
              id_cliente:"", modpag:"", esito_pagamento:"", modspe:""};
}


function aggiornaCarrello()
{
    retrieveURL('./Controller.do?query=__CARRELLO_TEMPLATE');
}

function setQuantity(id, qt)
{
    BI.setQuantityItem(null, id, qt);

retrieveURL('./Controller.do?query=__CARRELLO_UPDATE_TEMPLATE'); 
}

//AE promozione
function setCodice(id,sconto)
{
    BI.setCodicePro(id,sconto);

retrieveURL('./Controller.do?query=__CARRELLO_UPDATE_TEMPLATE'); 
}



function aggiungiAlCarrello(id)
{
    BI.addLibroAlCarrello(id);
}

function carrello_addBook1(id)
{
    //alert("carrello_addBook1");
    retrieveURLtoSet("./Controller.do?query=__CARRELLO_ADD_BOOK_TOSET&typecall=3&idBook=" + id);
    open_popup_acquisto();
}

function carrello_addBook(id)
{
    BI.setIdItemDaAcquistare(do_carrello_addBook, id);
}

function do_carrello_addBook()
{
    BI.addCarrelloBookDWR(do_carrello_acquisti);
    //retrieveURL('./Controller.do?query=__CARRELLO_ADD_BOOK'); 

}

function carrello_addRivista(id)
{
    BI.setIdItemDaAcquistare(do_carrello_addRivista, id);
}

function do_carrello_addRivista(id)
{
    retrieveURL('./Controller.do?query=__CARRELLO_ADD_RIVISTA');
}

function carrello_addFascicolo(idFascicolo,dest)
{ 
    var idDestinazione = dest;
    
    /**
    alert('carrello_addFascicolo');
    if (idDestinazione!=-1) {
        BI.setIdDestinazioneAcquistoAbbonamentoDWR(idDestinazione);
    }
    else {
        alert('INTERNAL ERROR: selezionare una delle destinazioni proposte');
        return false;
    } 
    if (idFascicolo!='') {
        BI.setIdItemDaAcquistare(idFascicolo);
        BI.addAcquistoFascicoloRivistaDWR(do_carrello_acquisti);
    } else {
        alert("INTERNAL ERROR: si è verificato un errore durante l'aggiunta del fascicolo al carrello");
    }
    **/

    // Ini Nuova versione
    retrieveURLtoSet("./Controller.do?query=__CARRELLO_ADD_FASCICOLO_TOSET&typecall=3&idFasc=" + 
                        idFascicolo + "&idDest=" +idDestinazione);
    open_popup_acquisto();
    // End Nuova Versione
}

function carrello_addAbbonamentoRivista(idRivista,dest )
{
    var idDestinazione = dest;
    
/*
    if (idDestinazione!=-1) {
        BI.setIdDestinazioneAcquistoAbbonamentoDWR(idDestinazione);
    }
    else {
        alert('INTERNAL ERROR: selezionare una delle destinazioni proposte');
        return false;
    }
    
    if (idRivista!='') {
        BI.setIdItemDaAcquistare(idRivista);
        BI.addAbbonamentoRivistaDWR(do_carrello_acquisti);
    } else {
        alert("INTERNAL ERROR: si è verificato un errore durante l'aggiunta dell'abbonamento della rivista al carrello");
    }

*/
    // Ini Nuova versione
    retrieveURLtoSet("./Controller.do?query=__CARRELLO_ADD_ABBONAMENTO_RIVISTA_TOSET&typecall=3&idRiv=" + 
                        idRivista + "&idDest=" +idDestinazione);
    open_popup_acquisto();
    // End Nuova Versione
}

function do_carrello_acquisti(f)
{
    if (!f){
        alert("INTERNAL ERROR: impossibile aggiungere il prodotto al carrello");
    } else {
        //alert("CH = " + document.getElementById("carrelloHome"));
        if (document.getElementById("carrelloHome")!=null) {
            DWRUtil.setValue("carrelloHome", f);
    }

        open_popup_acquisto();
    }
}

function carrello_addAllegato(id)
{
    BI.setIdItemDaAcquistare(do_carrello_addAllegato, id);
}

function do_carrello_addAllegato(id)
{
    retrieveURL('./Controller.do?query=__CARRELLO_ADD_ALLEGATO');
}

////////////////////////////////////////////////////////////////////
//     funzioni per la scrittura nel DB degli acquisti 
////////////////////////////////////////////////////////////////////

function item_enable(id, check){

//alert("carrello.js   item_enable    id= "+id+ "     check="+check);
    
BI.setItemEnable(do_item_enable(id, check), id, check); 
BI.setItemEnable(do_item_enable(id, check), id, check); 
retrieveURL('./Controller.do?query=__CARRELLO_UPDATE_TEMPLATE'); 
}

function do_item_enable(id, check)
{
    if (!check)
    {
        document.getElementById('titolo_'+id).color = '#AAAAAA';
        document.getElementById('prezzo_'+id).color = '#AAAAAA';
        document.getElementById('qta_'+id).disabled = true;
        document.getElementById('tot_'+id).color = '#AAAAAA';

        //popup acquisti
    //    document.getElementById('titolo_popup_'+id).color = '#AAAAAA';
    //    document.getElementById('prezzo_popup_'+id).color = '#AAAAAA';
    //    var qta = document.getElementById('qta_popup_'+id).value;        
    //    DWRUtil.setValue("item_qta_"+id, "<font style=\"font-size: 10px; color: #AAAAAA\">"+qta+"</font>");        
     //   document.getElementById('tot_popup_'+id).color = '#AAAAAA';
    }else
    {
        document.getElementById('titolo_'+id).color = '#000000';
        document.getElementById('prezzo_'+id).color = '#000000';
        document.getElementById('qta_'+id).disabled = false;
        document.getElementById('tot_'+id).color = '#000000';

        //popup acquisti
     //   document.getElementById('titolo_popup_'+id).color = '#000000';
     //   document.getElementById('prezzo_popup_'+id).color = '#000000';
     //   var qta = document.getElementById('qta_popup_'+id).value;        
     //   DWRUtil.setValue("item_qta_"+id, "<input style=\"font-size: 10px; text-align: right;\" size=\"3\" value=\""+qta+"\" readonly/></div>");       
     //   document.getElementById('tot_popup_'+id).color = '#000000';
    }  
}

function changeModSped(id, check)
{  
    if (check)
    {   
        BI.setModSpedizione(do_changeModSped, id);
    }
   changeModSpedColor(id, check);
}

function changeModSpedColor(id, check){ 

    if ((check && id==1) || (!check && id==2)){ 
//alert("THEN   check="+check+"  id="+id);
        document.getElementById('titsped2').disabled = true;  //.color = '#AAAAAA'; 
        document.getElementById('eurotitsped2').disabled = true; //.color = '#AAAAAA'; 
    //    document.getElementById('sped2').disabled = true; 

        document.getElementById('titsped1').disabled = false; //.color = '#000000'; 
    document.getElementById('eurotitsped1').disabled = false; //.color = '#000000'; 
     //   document.getElementById('sped1').disabled = false;  

        } else{  
//alert("ELSE   check="+check+"  id="+id);

        document.getElementById('titsped1').disabled = true; //.color = '#AAAAAA'; 
        document.getElementById('eurotitsped1').disabled = true; //.color = '#AAAAAA'; 
    //    document.getElementById('sped1').disabled = true; 

        document.getElementById('titsped2').disabled = false;  //.color = '#000000'; 
        document.getElementById('eurotitsped2').disabled = false; //.color = '#000000'; 
     //   document.getElementById('sped2').disabled = false;  
        } 
}

function do_changeModSped(costoSp)
{
    if (costoSp.length > 0)
        DWRUtil.setValue("costo_sped", costoSp);
    else
    {
        alert("Errore nel calcolo del costo della spedizione");
        DWRUtil.setValue("costo_sped", "0,00");
    }
    BI.getCostoTolale(do_setCostoTotale);
}

function do_setCostoTotale(costo_tot)
{
    if (costo_tot.length > 0)
        DWRUtil.setValue("costo_totale", costo_tot);
}

function update_carrello()
{
    retrieveURL('./Controller.do?query=__CARRELLO_UPDATE_TEMPLATE')
}

function go_carrelloPag()
{
    
    var ok = true;
    var msg = "Riscontrati i seguenti problemi:\n";

    DWRUtil.useLoadingMessage();
    DWRUtil.getValues(acquisti);
    
    if (acquisti.nome == ""){ ok = false; msg += "   -  inserire il campo nome;\n"; }
    if (acquisti.cognome == ""){ ok = false; msg += "   -  inserire il campo cognome;\n"; }
    if (acquisti.codfis == ""){ ok = false; msg += "   -  inserire il campo codice fiscale;\n"; }
    if (acquisti.indirizzo == ""){ ok = false; msg += "   -  inserire il campo indirizzo;\n"; }
    if (acquisti.n_civ == ""){ ok = false; msg += "   -  inserire il campo numero civico;\n"; }
    if (acquisti.cap == ""){ ok = false; msg += "   -  inserire il campo cap;\n"; }
    if (acquisti.comune == ""){ ok = false; msg += "   -  inserire il campo comune;\n"; }
    if (acquisti.email == ""){ ok = false; msg += "   -  inserire il campo e-mail;\n"; }
    else { if (!validateEmail(acquisti.email)){ ok = false; msg += "   -  il campo email non valido\n"; } }
    
    if (!ok)
        alert(msg);
    else
    {
        BI.setAcquisto(acquisti);
        retrieveURL('./Controller.do?query=__CARRELLO_M_PAGAMENTO_TEMPLATE');
    }
}
/*
function check_modPag()
{
    var ok = true;
    var msg = "Riscontrati i seguenti problemi:\n";

    // check modalità di pagamento
    for (var i=0; i< document.getElementById('num_modPag').value; ++i)
    {
        if (document.getElementById("pag"+i).checked)
            acquisti.modpag = document.getElementById("pag"+i).value;
    }
    if ( acquisti.modpag.length == 0)
    {
        ok = false; 
        msg += "   -  selezionare una modalità di pagamento;\n"; 
    }
    
    if (!ok)
        alert(msg);
    else
        {
            BI.setAcquisto(do_addAcquisti, acquisti);
        }  
}
*/
function addAcquisti()
{
    var ok = true;
    var msg = "Riscontrati i seguenti problemi:\n";
    var pagId = "";
    // check modalità di pagamento
    for (var i=0; i< document.getElementById('num_modPag').value; ++i)
    {
        pagId = "pag"+i;
        
        if (document.getElementById(pagId).checked)
            acquisti.modpag = document.getElementById(pagId).value;
    }
    if ( acquisti.modpag.length == 0)
    {
        ok = false; 
        msg += "   -  selezionare una modalità di pagamento;\n"; 
    }
    
    if (!ok)
        alert(msg);
    else
        {
            BI.setAcquisto(do_addAcquisti, acquisti);
        }  
}

function do_addAcquisti(f)
{
   if (f) {
        clearAcquisti();
//alert("acquisti.modpag="+acquisti.modpag);
 
        
        if (document.getElementById("pag4").checked){
             //carta di credito quindi chiamo Banca Sella 
             //   retrieveURL('./Controller.do?query=__CARRELLO_BANCA_SELLA');
            executor('./Controller.do?query=__CARRELLO_BANCA_SELLA&jscr=0')  
        }else{ 
  // devo discriminare tra acquisto OK e acquisto KO
            retrieveURL('./Controller.do?query=__CARRELLO_ADD_ACQUISTI');
        }
    } else {
        alert("INTERNAL ERROR: Impossibile eseguire l'azione di acquisto");
    }
}

function conferma_acquisto() {
    if (confirm('Prodotto aggiunto al carrello.\n\nCompleti l\'acquisto?')) {
        retrieveURL('./Controller.do?query=__CARRELLO_TEMPLATE');
    }
}

function cambiaStatoPopUpAcquisto(stato) {
    if (stato == "show") {          
        retrieveURL2('./Controller.do?query=__CARRELLO_POPUP_UPDATE'); 
        //   document.getElementById("popup_acquisto").style.visibility = 'visible';          
    } 
    if (stato == "hide") {
        document.getElementById("popup_acquisto").style.visibility = 'hidden';
    }
}


function abilita_accetta(check){
    if (check){ 
       document.getElementById('conferma_ordine').disabled = false;
    }else{ 
// alert("document.getElementById('conferma_ordine').disabled="+document.getElementById('conferma_ordine').disabled);
         document.getElementById('conferma_ordine').disabled = true;
    }
}

//function go_and_remember_page2(str){
//    if(BI.getConta_item() >0){
//            if (str != "")  go_and_remember_page(str);
//    } 
//}

