﻿function Init() {
    CenterContainer();
}

function CenterContainer() {
    var MainTable = $('#MainTable');
    var Left = ($(window).width() - 1000) / 2;
    var Top = ($(window).height() - 650) / 2;
    MainTable.css("left", Left.toString() + "px");
    MainTable.css("top", Top.toString() + "px");
}

function ChangePage(Page) {
    //Visibilità del box delle domande
    $('.PaginaSelezionata').removeClass().addClass('Pagina');
    $('#MasterPage_ContentPlaceHolderDomande_Pagina' + Page).removeClass().addClass('PaginaSelezionata');
    //Stile dell'indice della pagina
    $('.BoxPaginaSelezionata').removeClass().addClass('BoxPagina');
    $('#MasterPage_ContentPlaceHolderPagine_' + Page).removeClass().addClass('BoxPaginaSelezionata');
}

function ValidateRadios(sender, args) {
    var GroupName = sender.getAttribute('GroupName');
    var RadioList = $(":radio[name='" + GroupName + "']:checked");
    if (RadioList.length <= 0) {
        args.IsValid = false
    }
    else {
        args.IsValid = true
    }
    return false;
}

function ValidateCheckBoxes(sender, args) {
    var GroupName = sender.getAttribute('GroupName');
    var RadioList = $(":checkbox[name='" + GroupName + "']:checked");
    if (RadioList.length <= 0) {
        args.IsValid = false
    }
    else {
        args.IsValid = true
    }
    return false;
}

$(document).ready(function () {
    try {
        $('#MasterPage_BoxAlert').dialog({ autoOpen: false, bgiframe: true, resizable: false, width: 350, height: 150, modal: true });
        window.alert = CustomAlert;
    } catch (err) {
        window.alert('Your browser does not support overloading window.alert. ' + err);
    }
});

function CustomAlert(message) {
    $('#MasterPage_BoxAlert').dialog("open");
    $('#MasterPage_BoxAlert').html(message);
    return true;
}

function SwitchDomanda(value, type, hash, showsubcontrol) {
    //Nascondo tutto
    $.each(hash, function (indice, valore) {
        $.each(valore, function (idx, val) {
            var Controls = $("[IDDOMANDA='" + val + "']");
            Controls.hide();

            Controls.attr('checked', false);
            $("SELECT[IDDOMANDA='" + val + "']").val('-');


            //Ciclo che permette a cascata di nascondere anche le domande figlio di figlio
            $.each(Controls, function (ci, co) {
                var OnChangeEventScript = (co).getAttribute('onchange');
                if (OnChangeEventScript != undefined) {
                    var JSToExec = OnChangeEventScript.toString();
                    JSToExec = JSToExec.replace("true", "false");
                    eval(OnChangeEventScript);
                }
            });

            //TEMPO FA' HO TROVATO CHE NELLA RIGA SOTTO C'ERA valore INVECE DI val
            //valore PERO E' UNA LISTA DI IDDOMANDA MENTRE val E' L'ATTUALE IDDOMANDA
            //ORA E' CORRETTO E TESTATO CON RADIO VERTICALE, SE CAMBIATE QUALCOSA
            //DESCRIVETE COSA AVETE CAMBIATO E PERCHE'
            var Control = $("[IDDOMANDA='" + val + "'][IDVALIDATOR]")
            if (Control.attr('IDVALIDATOR') != undefined) {
                var Validator = document.getElementById(Control.attr('IDVALIDATOR'));
                if (Validator != null) {
                    ValidatorEnable(Validator, false);
                }
            }
        });
    });

    if (showsubcontrol) {
        if (hash[value] != undefined) {
            $.each(hash[value], function (indice, valore) {
                var Controls = $("[IDDOMANDA='" + valore + "']");
                Controls.show();
                var Control = $("[IDDOMANDA='" + valore + "'][IDVALIDATOR]:not([ISFIGLIO])")
                if (Control.attr('IDVALIDATOR') != undefined) {
                    var Validator = document.getElementById(Control.attr('IDVALIDATOR'));
                    if (Validator != null) {
                        ValidatorEnable(Validator, true);
                    }
                }
            });
        }
    }
}


function SwitchDomandaStandard(who, type, hash) {
    //alert(who.checked);
    if (who.checked) SwitchDomandaStandardOn(who, type, hash);
    else SwitchDomandaStandardOff(who, type, hash);
}


function SwitchDomandaStandardOn(value, type, hash) {
    //alert(hash);
    var chiave = value.value;
    var AlreadyChecked = value.checked;

    $.each(hash, function (indice, valore) {
        $.each(valore, function (idx, val) {
            var Controls = $("[IDDOMANDASTD='" + indice + "'][IDRISPOSTASCALA='" + val + "']");
            Controls.removeAttr('disabled');
            //alert(Controls.attr('IDVALIDATOR'));
            if (Controls.attr('IDVALIDATOR') != undefined) {
                var ValidatorControl = document.getElementById(Controls.attr('IDVALIDATOR'));
                if (ValidatorControl != null) {
                    ValidatorEnable(ValidatorControl, true);
                }
            }
        });
    });
}

function SwitchDomandaStandardOff(value, type, hash) {

    $.each(hash, function (indice, valore) {
        $.each(valore, function (idx, val) {
            var Controls = $("[IDDOMANDASTD='" + indice + "'][IDRISPOSTASCALA='" + val + "']");
            Controls.attr('disabled', 'disabled');

            if (Controls.attr('IDVALIDATOR') != undefined) {
                //alert(Controls.attr('IDVALIDATOR'));
                var ValidatorControl = document.getElementById(Controls.attr('IDVALIDATOR'));
                if (ValidatorControl != null) {
                    ValidatorEnable(ValidatorControl, false);
                }
            }

            $.each(Controls, function (idxctl, ctl) {
                ctl.checked = false;
            });
        });
    });
}

function CheckMaxRisposteRadio(Gruppo, MaxRisposte, Control, MsgLocalizzato) {
    var ElencoDiRadioNelGruppo = $(":radio[GruppoDomanda='" + Gruppo + "']:checked");
    if (ElencoDiRadioNelGruppo.length > MaxRisposte) {
        alert(MsgLocalizzato.replace('[1]', MaxRisposte.toString()));
        Control.checked = false;
    }
}

function CheckMaxRisposteCheckbox(Gruppo, MaxRisposte, Control, MsgLocalizzato) {
    var ElencoDiCheckNelGruppo = $(":checkbox[GruppoDomanda='" + Gruppo + "']:checked");
    if (ElencoDiCheckNelGruppo.length > MaxRisposte) {
        alert(MsgLocalizzato.replace('[1]', MaxRisposte.toString()));
        Control.checked = false;
        //Control.onclick();
    }
}

function ValidateMinRisposteRadio(sender, args) {
    var GroupName = sender.getAttribute('GroupName');
    var MinRisposte = sender.getAttribute('MinRisposte');
    args.IsValid = CheckMinRisposteRadio(GroupName, MinRisposte, null);
    return false;
}

function ValidateMinRisposteCheckbox(sender, args) {
    var GroupName = sender.getAttribute('GroupName');
    var MinRisposte = sender.getAttribute('MinRisposte');
    args.IsValid = CheckMinRisposteCheckbox(GroupName, MinRisposte, null);
    return false;
}



function CheckMinRisposteRadio(Gruppo, MinRisposte, Control) {
    var ElencoDiRadioNelGruppo = $(":radio[GruppoDomanda='" + Gruppo + "']:checked");
    if (ElencoDiRadioNelGruppo.length >= MinRisposte) {
        return true;
    } else {
        return false;
    }
}

function CheckMinRisposteCheckbox(Gruppo, MinRisposte, Control) {
    var ElencoDiCheckboxNelGruppo = $(":checkbox[GruppoDomanda='" + Gruppo + "']:checked");
    if (ElencoDiCheckboxNelGruppo.length >= MinRisposte) {
        return true;
    } else {
        return false;
    }
}



function SwitchRispostaFiglio(IdRisposta, type, ElencoIdRisposte, showsubcontrol) {
    //Nascondo tutto
    //NOTA: Come potete vedere ho remmato la parte che nasconde il controllo grafico
    //e quindi agisco solo sull'obbligatorietà, quello che dovremmo fare è inserire un nuovo flag
    //nel DB (IsFiglioSempreVisibile) e sulla base di questo decidere se far apparire il controllo
    //figlio solo nel caso in qui sia stata selezionata quella risposta o sempre (come è adesso)

    $.each(ElencoIdRisposte, function (idx, val) {
        //var Controls = $("[IDRISPOSTA='" + val + "']");
        //Controls.hide();
        var Control = $("[IDRISPOSTA='" + val + "'][IDVALIDATOR]")
        if (Control.attr('IDVALIDATOR') != undefined) {
            ValidatorEnable(document.getElementById(Control.attr('IDVALIDATOR')), false);
        }
    });

    if (showsubcontrol) {
        if (IdRisposta != undefined) {
            //var Controls = $("[IDRISPOSTA='" + valore + "']");
            //Controls.show();
            var Control = $("[IDRISPOSTA='" + IdRisposta + "'][IDVALIDATOR]")
            if (Control.attr('IDVALIDATOR') != undefined) {
                ValidatorEnable(document.getElementById(Control.attr('IDVALIDATOR')), true);
            }
        }
    }
}


function PreventMultipleSubmit() {
    //Faccio uso del try perchè nel caso non ci siano controlli da validare la
    //variabile Page_IsValid non esiste
    try {
        if (Page_IsValid) { $('.Navigator').hide(); }
    }
    catch (Error) {
        $('.Navigator').hide();
    }
}
