﻿
var LOADING_HTML = "<div class=\"loading\" title=\"caricamento in corso\"></div>";
var PREFIX = "master_cpBody_";


var utils = {};

utils.calendar = {};

utils.calendar.months = {};

utils.calendar.callCalendar = function(y, m, d, calendarDivId, currentY, currentM) {
    var div = document.getElementById(calendarDivId);
    var monthName = y + "" + m;
    var curMonthName = currentY + "" + currentM;
    if (!utils.calendar.months[curMonthName]) {
        utils.calendar.months[curMonthName] = document.getElementById(calendarDivId).innerHTML;
    }
    if (utils.calendar.months[monthName]) {
        document.getElementById(calendarDivId).innerHTML = utils.calendar.months[monthName];
        return false;
    }
    var load = utils.controls.createLoadingMessage("calLoad", div);
    //div.appendChild(load);

    $.get("services/calendar.aspx", { Y: y, M: m, D: d }, function(data) {
        document.getElementById(calendarDivId).innerHTML = data;
        utils.calendar.months[monthName] = data;
        load.parentNode.removeChild(load);
    }
    );

}

utils.calendar.showDetail = function(detailTitle, detailTextId, calendarDivId) {
    var detailText = document.getElementById(detailTextId).innerHTML;
    if (detailText == '') {
        detailText = '<p>Nessun evento previsto.</p><p>Vuoi inserire il tuo?<br/><a class=\'lnkSegnala\' href=\'segnala.aspx\'>Compila il modulo!</a></p>';
    }
    document.getElementById(calendarDivId + '_text').innerHTML = detailText;
    document.getElementById(calendarDivId + '_title').innerHTML = detailTitle;

}

utils.gallery = {};

utils.gallery.swapFotoProperty = function(idFoto, property) {
    var div = document.getElementById("dv_" + property + "_" + idFoto);
    div.innerHTML = "...";
    $.get('../services/GalleryButton.aspx', { Tipo: property, IdFoto: idFoto },
        function(res) {
            div.innerHTML = res;
        });
};

utils.gallery.browseSlideShow = function(idOldPhoto, idNewPhoto) {
    document.getElementById(idOldPhoto).style.display = "none";
    document.getElementById(idNewPhoto).style.display = "block";
};

utils.controls = {};

utils.controls.createLoadingMessage = function(id, dest, customText) {
    if (!customText) {
        customText = "caricamento in corso";
    }
    var d = document.createElement("DIV");
    d.className = "dvInfoPanel";
    var s = document.createElement("DIV");
    s.className = "loadingIcon";
    d.appendChild(s);
    var p = document.createElement("P");
    p.innerHTML = customText;
    d.appendChild(p);
    d.id = id;
    var w = dest.clientWidth - 10;
    var x = $(dest).offset().left + 5;
    var h = 38;
    var y = (dest.clientHeight - h) / 2 + $(dest).offset().top;
    d.style.width = w + "px";
    d.style.height = h + "px";
    d.style.overflow = "hidden";
    d.style.position = "absolute";
    d.style.display = "block";
    d.style.left = x + "px";
    d.style.top = y + "px";
    document.body.appendChild(d);
    return d;
}

function $id(id) {
    return document.getElementById(id);
}

var CAN_CHANGE_VOTE = true;

function votaMiss(id) {
    if (!id) {
        $id("butVota").setAttribute("disabled", "disabled");
        return false;
    }
    if (!CAN_CHANGE_VOTE) {
        return false;
    }
    $id("imgVotaMiss").src = $id("Miss" + id + "Img").value;
    $id("votaId").value = id;
    $id("lblVotaNome").innerHTML = $id("Miss" + id + "Nome").value;
    $id("lblVotaVoti").innerHTML = $id("Miss" + id + "Voti").value + " voti";
    $id("butVota").removeAttribute("disabled");
}


function vota() {
    var txtNome = $id("txtNome");
    var txtCognome = $id("txtCognome");
    var txtMail = $id("txtMail");
    var errMessage = $id("errMessage");
    var dvEsitoVoto = $id("dvEsitoVoto");
    
    txtNome.className = "textbox";
    txtCognome.className = "textbox";
    txtMail.className = "textbox";
    errMessage.style.display = "none";
    var err = false;
    if (txtNome.value == "") {
        err = true;
        txtNome.className = "textboxErr";
    }
    if (txtCognome.value == "") {
        err = true;
        txtCognome.className = "textboxErr";
    }
    if (txtMail.value == "") {
        err = true;
        txtMail.className = "textboxErr";
    }
    if (err) {
        errMessage.style.display = "block";
    
    }
    else {
        $("#dvEsitoVoto").ajaxError(function(event, request, settings) {
            this.innerHTML = "<p>Si &egrave; verificato un errore.<br/>Il voto non &egrave; stato registrato.</p>";
        });
        dvEsitoVoto.innerHTML = LOADING_HTML;
        $id("butVota").setAttribute("disabled", "disabled");
        CAN_CHANGE_VOTE = false;
        $.get("services/vote.aspx", { id: $id("votaId").value, nome: txtNome.value, cognome: txtCognome.value, mail: txtMail.value }, function(data) {
            $id("butVota").removeAttribute("disabled");
            CAN_CHANGE_VOTE = true;
            var m = $.parseJSON(data);
            if (m["STATUS"] == "OK") {
                CAN_CHANGE_VOTE = false;
                $id("dvVota").style.display = "none";
                $id("dvConfermaVoto").style.display = "block";
            }
            dvEsitoVoto.innerHTML = m["MSG"];
        });
    }
}

function confermaVoto() {
    var dvEsitoVoto = $id("dvEsitoVoto");
    var txtMail = $id("txtMail");
    var txtCodice = $id("txtCodice");
    $("#dvEsitoVoto").ajaxError(function(event, request, settings) {
        this.innerHTML = "<p>Si &egrave; verificato un errore.<br/>Il voto non &egrave; stato confermato.</p>";
    });
    dvEsitoVoto.innerHTML = LOADING_HTML;
    $.get("services/vote.aspx", { mail: txtMail.value, cod: txtCodice.value }, function(data) {
        var m = $.parseJSON(data);
        if (m["STATUS"] == "OK") {
            $id("dvConfermaVoto").style.display = "none";
        }
        dvEsitoVoto.innerHTML = m["MSG"];
    });
}

function controlloModulo() {
    try {
        var ctl = null;
        var ctlFocus = null;
        var isErr = false;
        var m = ["txtNome", "txtCognome", "txtMail", "txtTelefono", "txtLuogoNascita", "cmbGiornoNascita",
        "cmbMeseNascita", "cmbAnnoNascita", "cmbAltezza", "txtProfessione"];
        for (var i = 0; i < m.length; i++) {
            ctl = $id(PREFIX + m[i]);
            if (ctl.value == "") {
                isErr = true;
                ctl.className = "textboxErr";
                if (!ctlFocus) {
                    ctlFocus = ctl;
                }
            }
            else {
                ctl.className = "textbox";
            }
        }
        m = ["chkFoto", "chkLiberatoria", "chkPrivacy"];
        for (i = 0; i < m.length; i++) {
            ctl = $id(PREFIX + m[i]);
            if (!ctl.checked) {
                isErr = true;
                ctl.parentNode.className = "checkBoxErr";
                if (!ctlFocus) {
                    ctlFocus = ctl;
                }
            }
            else {
                ctl.parentNode.className = "checkBox";
            }
        }
        m = ["flFotoPrimoPiano", "flFotoFiguraIntera", "flFoto1", "flFoto2", "flFoto3"];
        var totalSize = 0;
        for (i = 0; i < m.length; i++) {
            var nome = "";
            var ctl = $id(PREFIX + m[i]);
            if (ctl.files) {
                if (ctl.files[0]) {
                    totalSize += ctl.files[0].fileSize;
                    nome = ctl.files[0].fileName;
                }
            }
            else if (ctl.fileSize) {
                totalSize += ctl.fileSize;
                nome = ctl.fileName;
            }
            else {
                nome = ctl.value;
            }
            var ext = nome.substring(nome.length - 4, nome.length).toUpperCase();
            ctl.className = "file";
            if ((i < 2 && ext == "") || (ext != "" && ext != ".JPG" && ext != "JPEG")) {
                ctl.className = "fileErr";
                isErr = true;
                if (!ctlFocus) {
                    ctlFocus = ctl;
                }
            }
        }
        if (totalSize > 3000000) {
            alert("La dimensione totale dei files supera i 3MB");
            return false;
        }
        if (ctlFocus) {
            ctlFocus.focus();
        }
        if (isErr) {
            alert("Per favore, riempi tutti i campi obbligatori");
            return false;
        }
        return confirm("Inviare la richiesta?"); 
        }
    catch (ex) {return false;}
}
