﻿/******************************************************************************/
/* OBJECT SECTION */
// definizine dell'oggetto container
//-- variabili
function Child(uid, id){
    this.UID = uid;
    this.ID = id;
}

function ChildCollection(){
    this.Items = new Array();
    
    this.Add = function (child) { this.Items.push(child); };
    
    this.IndexOf = function (id) { 
        var result = -1;
        
        if (this.Items && this.Items.length > 0){
            for (var i = 0; i < this.Items.length; i++){
                if (this.Items[i] && this.Items[i].ID == id) {
                    result = i;
                    break;
                }
            }
        }
        
        return result;
    };
    
    this.GetCount = function () { return (this.Items != null ? this.Items.length : 0); };
    
    this.GetChildById = function (id) { return ((this.IndexOf(id) > -1) ? document.getElementById(id) : null); };
    this.GetChildByIndex = function (index) { 
        if (index > -1 &&
            this.Items &&
            this.Items.length > index)
            return document.getElementById(this.Items[index].ID);
        
        return null;
    };
}

function DDLContainer(){
    // Child Collection
    this.Children = new ChildCollection();
    
    // Dependant not-child element array
    this.DependentControls = new Array();
    
    // Messaggio da visualizzare durante il caricamento delle combo
    DDLContainer.prototype.WaitingMessage = '';
    
    // Aggiunge una combo alla collection
    this.Add = function (UniqueID, ClientID) { this.Children.Add(new Child(UniqueID,ClientID)); };
    
    // Aggiunge gli elementi che dipendono dalle combo asincrone. Questi controlli verranno
    // disabilitati durante la chiamata e riabilitati alla fine del post asincrono
    this.AddDependentControl = function (ClientID) { this.DependentControls.push(ClientID); };
    
    // Funzione richiamata prima dell'invio asincrono: disabilita le combo
    // e scrive "Loading..." in quelle che devono essere aggiornate
    this.OnRequestStart = function (sender, args) {
        var index;
        
        try {
            index = this.Children.IndexOf(args.get_postBackElement().id);
        } catch(err) {
            index=-1;
        }
        for(var i = 0; i < this.Children.GetCount(); i++){     
            var child =  this.Children.GetChildByIndex(i);
            
            if (child != null){                               
                // waiting message solo su quelle che effettivamente si ricaricano
                if (index > -1 &&
                    index < i) {
                    child.options[child.selectedIndex].text = this.WaitingMessage;                               
                }    
                
                // tutti disabilitati
                child.disabled = true;
            }                    
        }               
        
        this.ToggleDependentControls(true);
    };
    
    // funzione richiamata alla fine della chiamata asincrona
    this.OnResponseEnd = function (sender, args) {
        for(var i = 0; i < this.Children.GetCount(); i++){
            if (this.Children.GetChildByIndex(i) != null)
                this.Children.GetChildByIndex(i).disabled = false;
        }
                        
        this.ToggleDependentControls(false);
    };
    
    // Abilita o disabilita i controlli dipendenti
    this.ToggleDependentControls = function (disable){    
        for(var i = 0; i < this.DependentControls.length; i++) {
            var dep = document.getElementById(this.DependentControls[i]);
            
            if (dep) {                
                dep.disabled = disable;                 
                
                if (dep.tagName == "A")
                    dep.onclick = (disable ? "return false;" : "");
            }      
        }
    };
}
/* END OBJECT SECTION */		        
/******************************************************************************/
