/*global SISAL Dialog iframeDocument*/

/**
 * La variabile rappresenta il namespace utilizzato per tutto il codice
 * javascript di giochi numerici.
 */
SISAL.GNTN.REGISTRAZIONE = (function REGISTRAZIONE() {

    var codicefiscale = SISAL.GNTN.CODICEFISCALE,
    messaggio = ""
    ;

    function CalcolaCF(formregistrazione) {
        var CFcalcolato = null;

        CFcalcolato = codicefiscale.CalcolaCodiceFiscale(formregistrazione);
        formregistrazione.id_codicefiscale.style.backgroundColor = "";

        if(CFcalcolato == undefined){
            formregistrazione.id_codicefiscale.value = "";
        }else{
            formregistrazione.id_codicefiscale.value = CFcalcolato;
        }

    }

    function controlloCampiRegistrazione(formId) {
        if (formId !== null && formId !== "") {
            var myCaptcha = null,
                formregistrazione = null,
                campiObbligatori = null,
                CFcalcolato = null,
                messaggio = null
            ;
            myCaptcha = iframeDocument(document.getElementById('portalIFrame_captcha')).captcha;
            formregistrazione = document.getElementById(formId);

            if (!myCaptcha.validate(formregistrazione.id_code.value)) {
                formregistrazione.id_code.style.backgroundColor = "#cb0000";
                messaggio = "Il codice di sicurezza non &egrave; corretto";
                SISAL.FX.showOnElement_TemplatePopup('body', 'mp.alert.buttonok.html', {"{title}" : "Attenzione", "{content}" : messaggio, "{buttonclick}" : '<hideThis>', "{buttontext}" : 'OK', width : '350px'});                
                myCaptcha.refreshImage();
                return false;
            }else{
                formregistrazione.id_code.style.backgroundColor = "";
            }

            campiObbligatori = SISAL.GNTN.REGISTRAZIONE.CHECKCAMPI.controlloCampi(formregistrazione);
            if (campiObbligatori !== "si") {
                // 27-10-2010 ci hanno chiesto di farlo anceh per l'estero
                // if (formregistrazione.nazionenascita.value == "Z000")
                //{
                CFcalcolato = codicefiscale.CalcolaCodiceFiscale(formregistrazione);

                if (CFcalcolato !== formregistrazione.id_codicefiscale.value) {
                    formregistrazione.id_codicefiscale.style.backgroundColor = "#cb0000";
                    messaggio = "Il Codice Fiscale non &egrave; corretto";
                    SISAL.FX.showOnElement_TemplatePopup('body', 'mp.alert.buttonok.html', {"{title}" : "Attenzione", "{content}" : messaggio, "{buttonclick}" : '<hideThis>', "{buttontext}" : 'OK', width : '350px'});                
                    return;
                } else {
                    SISAL.GNTN.REGISTRAZIONE.CHECKCAMPI.controlloCampi(formregistrazione);
                }
                //}
                formregistrazione.submit();
            }
        }
    }

    /** *********************************************************** */
    /* RIEPILOGO */
    /** *********************************************************** */

    function back(formId) {

        document.getElementById("stato").value = "form";

        var form = document.getElementById(formId);
        form.submit();

    }

    function formSubmit(formId) {

        if (document.getElementById("verificadati").checked) {
            var form = document.getElementById(formId);
            form.submit();
        } else {
            messaggio = 'Per proseguire è necessario barrare la casella "Ho letto e verificato con attenzione i miei dati".';
//            messaggio = "Non hai selezionato il flag verifica dati";
            SISAL.FX.showOnElement_TemplatePopup('body', 'mp.alert.buttonok.html', {"{title}" : "Attenzione", "{content}" : messaggio, "{buttonclick}" : '<hideThis>', "{buttontext}" : 'OK', width : '350px'});                
        }

    }

    function getCheckedValue(campo) {
        if (campo.checked)
        {    
            document.getElementById("id_buttonConferma").className = "classButtons";
        }    
        else
        {    
            document.getElementById("id_buttonConferma").className = "classButtons_gray";
        }    
        return;
    }

    // controlla se l'elemento passato  è nullo o no
    function elementIsNotNull(elementName) {
        var element = document.getElementById(elementName);
        if (element !== 'undefined' && element !== null && element !== 'null') {
            return true;
        }

        return false;
    }

    function reg_enableTable(tableId) {
        var tableName = [],
            colorTable = [],
            tableName_sel = null,
            tableRight_id = null,
            colorTable_sel = null,
            tableRight_class = null,
            tableLeftT_id = null,
            tableLeftT_class = null,
            tableLeftC_id = null,
            tableLeftC_class = null,
            tableLeftB_id = null,
            tableLeftB_class = null,
            tableLeft_id = null,
            tableLeft_class = null,
            tableContent_id = null,
            tableContent_class = null,
            tableMiddle_id = null,
            logger = null,
            tableMiddle_class = null,
            tableBottomMiddle_id = null,
            tableBottomMiddle_class = null,
            table_id = null,
            table_class = null,
            i = 0
            ;
        
        tableName[1] = "DatiDiAccesso";
        tableName[2] = "DatiAnagrafici";
        tableName[3] = "AbilitazioneGiochi";
        tableName_sel = tableName[tableId];

        
        colorTable[0] = "grey";
        colorTable[1] = "green";

        // alert(tableName.length);

        for (i = 1; i < tableName.length; i += 1) {
            colorTable_sel = colorTable[0];
            if (i === tableId) {
                colorTable_sel = colorTable[1];
            }

            // alert("id:"+i + "tableId:"+tableId +
            // "colorTable_sel:"+colorTable_sel);

            // sfondo della tabella di destra
            tableRight_id = "tableRight_" + tableName[i];
            tableRight_class = "tableRight_" + tableName[i] + "_" + colorTable_sel;
            if (!SISAL.isNullOrEmpty(document.getElementById(tableRight_id)))
            {    
                document.getElementById(tableRight_id).className = tableRight_class;
            }    

            // mini menu della tabella di sinistra
            tableLeftT_id = tableName[i] + "_tableLeftT";
            tableLeftT_class = "tableLeftT_" + colorTable_sel;
            if (!SISAL.isNullOrEmpty(document.getElementById(tableLeftT_id)))                
            {    
                document.getElementById(tableLeftT_id).className = tableLeftT_class;
            }    

            tableLeftC_id = tableName[i] + "_tableLeftC";
            tableLeftC_class = "tableLeftC_" + colorTable_sel;
            if (!SISAL.isNullOrEmpty(document.getElementById(tableLeftC_id)))            
            {    
                document.getElementById(tableLeftC_id).className = tableLeftC_class;
            }    

            tableLeftB_id = tableName[i] + "_tableLeftB";
            tableLeftB_class = "tableLeftB_" + colorTable_sel;
            if (!SISAL.isNullOrEmpty(document.getElementById(tableLeftB_id)))            
            {    
                document.getElementById(tableLeftB_id).className = tableLeftB_class;
            }    

            // voce del mini menu della tabella di sinistra
            tableLeft_id = "tableLeft_" + tableName[i];
            tableLeft_class = "tableLeft_" + tableName[i] + "_" + colorTable_sel;
            if (!SISAL.isNullOrEmpty(document.getElementById(tableLeft_id)))
            {    
                document.getElementById(tableLeft_id).className = tableLeft_class;
            }   

            // eventuale contenuto aggiuntivo ( tabella o promo o ... )
            tableContent_id = "table_" + tableName[i] + "_content";
            tableContent_class = "table_" + tableName[i] + "_content_" + colorTable_sel;
            if (!SISAL.isNullOrEmpty(document.getElementById(tableContent_id)))
            {    
                document.getElementById(tableContent_id).className = tableContent_class;
            }    

            tableMiddle_id = "tableTopMiddle_" + tableName[i];

            tableMiddle_class = "table_" + tableName[i] + "_middle_" + colorTable_sel;

            if (!SISAL.isNullOrEmpty(document.getElementById(tableMiddle_id)))
            {    
                document.getElementById(tableMiddle_id).className = tableMiddle_class;
            }

            tableLeft_id = "tableTopLeft_" + tableName[i];
            tableLeft_class = "table_" + tableName[i] + "_Left_" + colorTable_sel;
            if (!SISAL.isNullOrEmpty(document.getElementById(tableLeft_id)))
            {    
                document.getElementById(tableLeft_id).className = tableLeft_class;
            }

            tableBottomMiddle_id = "tableBottomMiddle_" + tableName[i];
            tableBottomMiddle_class = "table_" + tableName[i] + "_BottomMiddle_" + colorTable_sel;
            if (!SISAL.isNullOrEmpty(document.getElementById(tableBottomMiddle_id)))
            {    
                document.getElementById(tableBottomMiddle_id).className = tableBottomMiddle_class;
            }

            table_id = "table_" + tableName[i];
            table_class = "table_" + tableName[i] + "_" + colorTable_sel;
            if (!SISAL.isNullOrEmpty(document.getElementById(table_id)))  
            {    
                document.getElementById(table_id).className = table_class;
            }

        }
        // alert(tableRight_background);
    }

    function verificaDati(campo) {
        if (document.getElementById("verificadati").checked === true) {
            document.getElementById("confermaok").style.display = "block";
            document.getElementById("confermako").style.display = "none";
        } else {
            document.getElementById("confermako").style.display = "block";
            document.getElementById("confermaok").style.display = "none";
        }
    }

    function iframeDocument(iframe) {

        var doc = null;

        if (iframe.contentDocument) {
            doc = iframe.contentDocument; // Firefox, Opera
        } else if (iframe.contentWindow) {
            doc = iframe.contentWindow.document; // Internet Explorer
        } else if (iframe.document) {
            doc = iframe.document; // Others?
        }
        return doc;
    }
    
    function sbiancaCampo(campo) {
        if (campo.value !== "") {
            campo.style.backgroundColor = "";
        } else {
            campo.style.backgroundColor = "#CB0000";
        }
    }
    
    return {
        CalcolaCF : CalcolaCF,
        controlloCampiRegistrazione : controlloCampiRegistrazione,
        back : back,
        formSubmit : formSubmit,
        getCheckedValue : getCheckedValue,
        reg_enableTable : reg_enableTable,
        verificaDati : verificaDati,
        iframeDocument : iframeDocument,
        sbiancaCampo : sbiancaCampo
    };
    
}());

if (SISAL.logger.isInfoEnabled()) {
    SISAL.logger.info("SISAL.GNTN.REGISTRAZIONE - namespace creato");
}