var xmlHttp = null;
var debug = false;

function format00(n) {
    var len;
    if (n.indexOf(',') > 0) {
        len = n.length - (n.indexOf(',') + 1)
        if (len == 1) n = n + '0'
    }
    else { n = n + ',00'; }
    return n;
}

function rigaOver(e) {

    for (i = 0; i < 5; i++) {

        e.childNodes[i].style.backgroundColor = "#FFAAAA"
    }
}

function rigaOut(e) {
    for (i = 0; i < 5; i++) {

        e.childNodes[i].style.backgroundColor = "white"
    }
}
function checkDates(mode) {
    da = document.getElementById("dateArr").value
    dp = document.getElementById("datePart").value
    // operazione in corso...
    if (xmlHttp != null) {
        if (xmlHttp.readyState != 0 || xmlHttp.readyState != 4) {
            xmlHttp.abort()
            xmlHttp = null
        }
    }

    if (setXmlHttp() == true) {
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                s = xmlHttp.responseText;
                // formato: 0=ok,1=err @@ descrizione errore @@ dataArr @@ dataPart @@ sistemazione calendario partenza (elenco delle date 'disabled') @@ sistemazione calendario arrivi(elenco date 'disabled')
                if (debug == true) alert(s)
                v = s.split("@@")
                if (v.length == 4) {

                }
            }
        }

        if (debug == true) alert("checkDates.asp?mode=" + mode + "&dp=" + dp + "&da=" + da)

        xmlHttp.open("GET", "checkDates.asp?mode=" + mode + "&dp=" + dp + "&da=" + da, true);
        xmlHttp.send(null);

    }

}

function checkDispo() {
    da = document.getElementById("dateArr").value
    dp = document.getElementById("datePart").value
    // operazione in corso...
    if (xmlHttp != null) {
        if (xmlHttp.readyState != 0 || xmlHttp.readyState != 4) {
            xmlHttp.abort()
            xmlHttp = null
        }
    }

    if (setXmlHttp() == true) {
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                s = xmlHttp.responseText;
                // formato: 0=ok,1=err @@ descrizione errore @@ dataArr @@ dataPart @@ sistemazione calendario partenza
                if (debug == true) alert(s)
                v = s.split("@@")
                //if(v[0]=="0"){
                document.getElementById("col2-booking").innerHTML = v[1]

                //}
            }
        }
        d = new Date();
        t = d.getTime();

        if (debug == true) alert("findDispo.asp?dp=" + dp + "&da=" + da + "&ID=" + document.getElementById("ID").value + "&dummy=" + t)

        xmlHttp.open("GET", "findDispo.asp?dp=" + dp + "&da=" + da + "&ID=" + document.getElementById("ID").value + "&dummy=" + t, true);
        xmlHttp.send(null);

    }


}

function checkRiepil(tipopag) {


    // verifica javascript: nome, indirizzo, CAP, citta', telefono, email correttamente compilati
    if (document.getElementById("nome").value == '' ||
      document.getElementById("indirizzo").value == '' ||
      document.getElementById("CAP").value == '' ||
      document.getElementById("citta").value == '' ||
      document.getElementById("tel").value == '' ||
      document.getElementById("email").value == '' ||
      document.getElementById("email").value.indexOf("@") < 1 ||
      document.getElementById("email").value.indexOf(".") < 1) {
        alert("Inserire correttamente i dati della prenotazione prima di procedere")
        return false;

    }

    // verifico di avere scelto almeno una camera e un trattamento
    if (parseFloat(document.getElementById("htot").value) <= 0) {
        alert("Occorre selezionare almeno un tipo di camera ed il relativo trattamento")
        return false;
    }


    // visualizzo il riepilogo (stessa finestra, sovrapposto!)
    r = document.getElementById("divR")
    //costruisco il riepilogo

    if (document.getElementById("hda") != null) {
        da = document.getElementById("hda").value
        da2 = da.split("/")
        da = da2[1] + "/" + da2[0] + "/" + da2[2]
        dp = document.getElementById("hdp").value
        dp2 = dp.split("/")
        dp = dp2[1] + "/" + dp2[0] + "/" + dp2[2]

        s = "</b>Data di arrivo:<b> " + da + "<br>"
        s = s + "</b>Data di partenza:<b> " + dp

        //aggiungo tabella
        s = s + "<table cellpadding=2 style='border-collapse:collapse;border:1px solid black;margin-top:19px;width:500px;'>"
        s = s + "<thead><tr><th style='border:1px solid black;font-size:12px;font-family:Verdana;'>Tipologia Camera</th>"
        s = s + "<th style='border:1px solid black;font-size:12px;font-family:Verdana;padding:2 2 2 2;'>Capienza</th>"
        s = s + "<th style='border:1px solid black;font-size:12px;font-family:Verdana;padding:2 2 2 2;'>Trattamento</th>"
        s = s + "<th style='border:1px solid black;font-size:12px;font-family:Verdana;padding:2 2 2 2;'>Numero camere</th>"
        s = s + "<th style='border:1px solid black;font-size:12px;font-family:Verdana;padding:2 10 2 10;'>Tariffa</th>"
        s = s + "</tr></thead><tbody>"

        ptot = 0;
        if (document.getElementsByName("qta").length == 1) {
            tip = document.getElementById("tipo").value;
            nmP = document.getElementById("numP").value;
            pid = document.getElementById("punit");
            pu = pid.value.split(":")[0];
            trt = pid.options[pid.selectedIndex].text;

            qu = document.getElementById("qta").value;
            tot = parseInt(100 * qu * pu) / 100;
            ptot = tot;
            t = format00(tot.toString().replace('.', ','));
            s = s + addRiga(tip, nmP, trt, qu, t)

        }
        else {
            for (id = 0; id <= document.getElementsByName("tipo").length - 1; id++) {
                tip = document.getElementsByName("tipo").item(id).value;
                nmP = document.getElementsByName("numP").item(id).value;

                pid = document.getElementsByName("punit").item(id)
                pu = pid.value.split(":")[0];
                trt = pid.options[pid.selectedIndex].text;
                qu = document.getElementsByName("qta").item(id).value;

                tot = parseInt(100 * qu * pu) / 100;
                t = tot.toString()
                t = t.replace('.', ',');
                t2 = format00(t);

                if (tot > 0)
                    s = s + addRiga(tip, nmP, trt, qu, t2)
                ptot = ptot + tot;
            }
        }

        s = s + "</tbody></table><br>"

        // inserisco dati prenotazione
        s = s + "<table cellpadding='2' border='0' width='500'>"
        s = s + "<tr><td width='250'>Cognome Nome:</td><td width='250'>" + document.getElementById("nome").value + "</td></tr>";
        s = s + "<tr><td>Indirizzo:</td><td>" + document.getElementById("indirizzo").value + "</td></tr>";
        s = s + "<tr><td>CAP / Citt&agrave;:</td><td>" + document.getElementById("CAP").value + " " + document.getElementById("citta").value + "</td></tr>";
        prv = document.getElementById("prov")
        s = s + "<tr><td>Provincia:</td><td>" + prv.options[prv.selectedIndex].text + "</td></tr>";
        naz = document.getElementById("naz")
        s = s + "<tr><td>Nazione:</td><td>" + naz.options[naz.selectedIndex].text + "</td></tr>";
        s = s + "<tr><td>Telefono:</td><td>" + document.getElementById("tel").value + "</td></tr>";
        s = s + "<tr><td>Email:</td><td>" + document.getElementById("email").value + "</td></tr>";
        s = s + "<tr><td>Note:</td><td>" + document.getElementById("note").value + "</td></tr></table><br><br>";

        s = s + "Totale complessivo prenotazione: " + format00(ptot.toString().replace('.', ',')) + "&euro;" + "<br>"
        pcap = parseInt(100 * ptot * 0.3) / 100;
        document.getElementById("Caparra").value = format00(pcap.toString().replace('.', ','))

        if (tipopag == 'Nessuno') {
            // s = s + "<br>Premendo il tasto 'Prenota' si accede alla pagina di inserimento dei dati della carta di credito<br>"
            s = s + "<br>Premendo il tasto 'Prenota' si procede con l'invio della mail di richiesta prenotazione<br>"
            s = s + "<div style='border-top:1px solid dotted;padding-top:5px;font-size:10px;font-weight:normal;'><b>Attenzione</b><br>"
            /*
            s = s + "(1) Nessun addebito viene effettuato sulla carta di credito<br>"
            s = s + "(2) La prenotazione pu&ograve; essere disdetta senza penali fino a 30gg prima del soggiorno<br>"
            s = s + "(3) A seguito del pagamento, verr&agrave; inviata una e-mail di conferma della prenotazione<br></div>"
            */
            if (document.getElementById("ID").value!='14') {
              //s = s + "(1) Nessun addebito viene effettuato sulla carta di credito<br>" 
              s = s + "(1) A seguito della prenotazione, riceverai una mail di conferma<br>"
              s = s + "(2) Per cancellare o modificare la tua prenotazione, contattaci via mail<br>"
              s = s + "(3) Le cancellazioni effettuate fino a 2 giorni prima della data prevista di arrivo non comportano alcuna penale<br>"
              s = s + "(4) Le cancellazioni o le modifiche tardive e la mancata presentazione comportano l'addebito del costo della prima notte<br></div>"
            }
            else {
              s = s + "(1) Nessun addebito viene effettuato sulla carta di credito, l'importo verr&agrave; addebitato solo 14 giorni (21 in caso di prenotazioni Family Suite) prima della data di arrivo<br>"
              s = s + "(2) A seguito della prenotazione, riceverai una mail di conferma<br>"
              s = s + "(3) Per cancellare o modificare la tua prenotazione, contattaci via mail<br>"
              s = s + "(4) Le cancellazioni effettuate fino a 14 giorni (21 in caso di prenotazioni Family Suite) prima della data prevista di arrivo non comportano alcuna penale<br>"
              s = s + "(5) Le cancellazioni o le modifiche tardive e la mancata presentazione comportano l'addebito del costo totale del soggiorno<br></div>"
            }
        }
        else {
            s = s + "Caparra per confermare la prenotazione: <font color=red>" + format00(pcap.toString().replace('.', ',')) + "&euro;</font><br>"
            s = s + "<div style='border-top:1px solid dotted;padding-top:5px;font-size:10px;font-weight:normal;'><b>Note</b><br>(1) La prenotazione pu&ograve; essere disdetta con rimborso della caparra fino a 30gg prima del soggiorno<br>"
            s = s + "(2) Premendo il tasto 'Prenota' si accede al pagamento on-line con carta di credito<br>"
            s = s + "(3) A seguito del pagamento, verr&agrave; inviata una e-mail di conferma della prenotazione<br></div>"
        }

        ri = document.getElementById("riepil")
        ri.innerHTML = s


        //alert(document.getElementById("prenotazione").offsetLeft);
        //alert(document.getElementById("prenotazione").offsetTop);
        //r.style.Top = document.getElementById("prenotazione").offsetTop;
        //r.style.Left = document.getElementById("prenotazione").offsetLeft;
        //alert(r.style.Top);

        r.style.visibility = "visible"

    }
    else {
        // mancano le date
        alert("Selezionare il periodo del soggiorno")
        return false;
    }
}

function addRiga(tipo, numP, tratt, numC, tot) {
    s = "<tr><td style='border:1px solid black;font-size:12px;font-family:Verdana;padding:2 2 2 2;background-color:white;'>" + tipo + "</td>"
    s = s + "<td style='border:1px solid black;font-size:12px;font-family:Verdana;padding:2 2 2 2;background-color:white;'>" + numP + "</td>"
    s = s + "<td style='border:1px solid black;font-size:12px;font-family:Verdana;padding:2 2 2 2;background-color:white;'>" + tratt + "</td>"
    s = s + "<td style='border:1px solid black;font-size:12px;font-family:Verdana;padding:2 2 2 2;background-color:white;'>" + numC + "</td>"
    s = s + "<td style='border:1px solid black;font-size:12px;font-family:Verdana;padding:2 2 2 2;background-color:white;text-align:right'>" + tot + " &euro;</td></tr>"
    return s;
}

function setXmlHttp() {
    xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Il browser non supporta AJAX!")
                return false;
            }
        }
    }
    return true;
}

function getDispo() {
    // operazione in corso...
    if (xmlHttp != null) {
        if (xmlHttp.readyState != 0 || xmlHttp.readyState != 4) {
            xmlHttp.abort()
            xmlHttp = null
        }
    }

    if (setXmlHttp() == true) {
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                s = xmlHttp.responseText;
                // formato: 0=ok,1=err @@ descrizione errore @@ dataArr @@ dataPart @@ sistemazione calendario partenza
                if (debug == true) alert(s)

                document.getElementById("col4-booking").innerHTML = s

                //}
            }
        }
        d = new Date();
        t = d.getTime();

        if (debug == true) alert("getDispo.asp?ID=" + document.getElementById("ID").value + "&dummy=" + t)

        xmlHttp.open("GET", "getDispo.asp?ID=" + document.getElementById("ID").value + "&dummy=" + t, true);
        xmlHttp.send(null);

    }


}

function getPren(msg) {
    // operazione in corso...
    if (xmlHttp != null) {
        if (xmlHttp.readyState != 0 || xmlHttp.readyState != 4) {
            xmlHttp.abort()
            xmlHttp = null
        }
    }

    if (setXmlHttp() == true) {
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                s = xmlHttp.responseText;
                // formato: 0=ok,1=err @@ descrizione errore @@ dataArr @@ dataPart @@ sistemazione calendario partenza
                if (debug == true) alert(s)

                document.getElementById("col4-booking").innerHTML = s

                //}
            }
        }
        d = new Date();
        t = d.getTime();

        if (debug == true) alert("getPren.asp?ID=" + document.getElementById("ID").value + "&dummy=" + t + "&msg=" + msg)

        xmlHttp.open("GET", "getPren.asp?ID=" + document.getElementById("ID").value + "&dummy=" + t + "&msg=" + msg, true);
        xmlHttp.send(null);

    }


}

function delPren(idreg) {
    // operazione in corso...
    if (xmlHttp != null) {
        if (xmlHttp.readyState != 0 || xmlHttp.readyState != 4) {
            xmlHttp.abort()
            xmlHttp = null
        }
    }

    if (setXmlHttp() == true) {
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                s = xmlHttp.responseText;
                // formato: 0=ok,1=err @@ descrizione errore @@ dataArr @@ dataPart @@ sistemazione calendario partenza
                if (debug == true) alert(s)

                getPren(s)

                //}
            }
        }
        d = new Date();
        t = d.getTime();

        if (debug == true) alert("delPren.asp?IDReg=" + idreg + "&dummy=" + t)

        xmlHttp.open("GET", "delPren.asp?IDReg=" + idreg + "&dummy=" + t, true);
        xmlHttp.send(null);
    }
}

function seeDetail(idreg) {
    // operazione in corso...
    if (xmlHttp != null) {
        if (xmlHttp.readyState != 0 || xmlHttp.readyState != 4) {
            xmlHttp.abort()
            xmlHttp = null
        }
    }

    if (setXmlHttp() == true) {
        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {

                s = xmlHttp.responseText;
                // formato: 0=ok,1=err @@ descrizione errore @@ dataArr @@ dataPart @@ sistemazione calendario partenza
                if (debug == true) alert(s)

                document.getElementById("col5-booking").innerHTML = s;

                //}
            }
        }
        d = new Date();
        t = d.getTime();

        if (debug == true) alert("seeDetail.asp?IDReg=" + idreg + "&dummy=" + t)

        xmlHttp.open("GET", "seeDetail.asp?IDReg=" + idreg + "&dummy=" + t, true);
        xmlHttp.send(null);
    }
}
