﻿// File JScript
function bexHilight(div)
{
    div.className += " bexHilightForeColor ";
    div.parentNode.className += " bexHilightBackColor ";
}

function bexRestore(div)
{
    div.className = div.className.replace (/bexHilightForeColor/g, "");
    div.parentNode.className = div.parentNode.className.replace (/bexHilightBackColor/g, "");
}

function linkHilight(div)
{
    div.className += " linkHilight";
}

function linkRestore(div)
{
    div.className = div.className.replace (/linkHilight/g, "");
}

function onGlButtonEx_click (btn) {
    if (btn == null)
        return false;
        
    if (btn.id == null)
        return false;
    
    if (!CallCustomFunction (btn, "preSubmit"))
        return false;
        
    var data = GetSubmitData (btn);
    
    var res = GLWebLibrary.ButtonEx.OnClick (document.getElementById("sessionState").value, data, btn.id);
    onGlButtonEx_clickCallback (res);

    if (res.value != null && res.value.EndContent == "" && res.value.Value == "true")
    {
        return true;
    }
    
    return false;
}

function GetSubmitData (btn)
{
    var value;
    var data = {};
    var keys = GetStateKeys (btn);
    var state = "";
    var control;
    var submitOnlyVisibles = GetAttribute (btn, "submitOnlyVisibles");
    
    for (var i = 0; i < keys.length; i ++)
    {
        control = document.getElementById(keys [i]);
        
        if (control != null)
        {
            if (submitOnlyVisibles != "true" || IsVisible (control))
            {
                if (GetAttribute (control, "error") == "true")
                {
                    if (CallCustomFunction (control, "validateError"))
                        alert ("uno o più dati inseriti non sono validi");
                    
                    return;
                }
                
                if (control.type == "checkbox")
                {
                    data [keys [i]] = control.checked;
                }
                else if (control.value != null)
                {
                    data [keys [i]] = control.value;
                }
                else
                {
                    data [keys [i]] = GetAttribute (control, "value");
                }
            }
        }
    }
    
    return data;
}

function onGlButtonEx_clickCallback (res)
{
    if (res.value != null)
    {
        if (res.value.TargetID == "sessionEnded")
        {
            window.location = res.value.EndContent;
            return;
        }
        
        // se è un bottone di postback normale allora vediamo come
        // è andata l'operazione
        if (res.value.EndContent == "")
        {
            if (res.value.Value == "true")
            {
                if (GetAttribute (document.getElementById(res.value.TargetID), "saveButton") == "true")
                    alert ("I dati sono stati salvati con successo");
                
                CallCustomFunction (document.getElementById(res.value.TargetID), "CustomButtonExClickOK");
            }
            else
            {
                if (GetAttribute (document.getElementById(res.value.TargetID), "saveButton") == "true")
                    alert ("Errore nel salvataggio dei dati");
                    
                CallCustomFunction (document.getElementById(res.value.TargetID), "CustomButtonExClickCancel");
            }
        }
        else // altrimenti sostituiamo l'html restituitoci e chiamiamo la funzione ok
        {
            if (GetAttribute (document.getElementById(res.value.Value), "saveButton") == "true")
                    alert ("I dati sono stati salvati con successo");

            if (res.value.TargetID == "resval") // è un valore di ritorno, non html
            {
                document.getElementById(res.value.TargetID).value = res.value.EndContent;
            }
            else
                document.getElementById(res.value.TargetID).innerHTML = res.value.EndContent;
            
            if (res.value.TargetID2 != "")
                document.getElementById(res.value.TargetID2).innerHTML = res.value.EndContent2;
            
            if ((res.value.TargetID3 != "") && (res.value.TargetID3 != null))
                document.getElementById(res.value.TargetID3).innerHTML = res.value.EndContent3;
            
            if ((res.value.TargetID4 != "") && (res.value.TargetID4 != null))
                document.getElementById(res.value.TargetID4).innerHTML = res.value.EndContent4;
            
            if ((res.value.TargetID5 != "") && (res.value.TargetID5 != null))
                document.getElementById(res.value.TargetID5).innerHTML = res.value.EndContent5;
                
            CallCustomFunction (document.getElementById(res.value.Value), "CustomButtonExClickOK");
        }
    }
}

function GetStateKeys (ctrl)
{
    var state = GetAttribute (document.getElementById(ctrl.id), "state");
    state = document.getElementById(state);

    if (state == null)
        return "";
        
    var keys = new Array ();
    var strs = state.value.split (';');
    for (var i = 0; i < strs.length; i ++)
    {
        keys [i] = strs [i].split (':')[0];
    }
    
    return keys;
}

//function preSubmit (btn)
//{
//    return true;
//}

//window.attachEvent ("onscroll", OnScroll);
//window.attachEvent ("onscroll", OnScroll1);

//function OnScroll ()
//{
//    alert ("ciao");
//}

//function OnScroll1 ()
//{
//    alert ("ciao1");
//} 
