﻿// Dropdowns

///// GENERAL
var dropdowns = new Array();

function FillRelated(relatedID)
{    
    if (relatedID != null)
    {
        var relatedIDs = relatedID.split(',');                    
        for (var i=0; i<relatedIDs.length; i++)
        {
            var ctrl = dropdowns[relatedIDs[i]];    
            if (ctrl != null)
            {  
                ctrl.go();
            }
        }
    }   
}

function ClearRelated(relatedID)
{    
    if (relatedID != null)
    {
        var relatedIDs = relatedID.split(',');                    
        for (var i=0; i<relatedIDs.length; i++)
        {
            var ctrl = dropdowns[relatedIDs[i]];    
            if (ctrl != null)
            {  
                ctrl.clear();
            }
        }
    }   
}

//////////////////////////////////////////////
// Cached Dropdown ///////////////////////////
//////////////////////////////////////////////

function CreateCachedDropDownList(name, array, elementID, parameters, preload, relatedID, value, fnction)
{
    var ctrl       = new CachedDropDown();
    ctrl.name      = name;
    ctrl.array     = array;
    ctrl.element   = $(elementID);    
    ctrl.value     = value;    
    ctrl.relatedID = relatedID;        
    ctrl.fnction = fnction; 
    dropdowns[elementID] = ctrl;
    
    // init filter
    if (parameters != '')
    {
        var filters = parameters.split(',');
        ctrl.parameters = new MultiArray((filters.length - 1)/2, 2);
        
        for (var i=0; i< ctrl.parameters.length; i++)
        {
            ctrl.parameters[i][0] = filters[i*2];
            ctrl.parameters[i][1] = $(filters[i*2 + 1]);            
        }
    }    
    
    
    if (preload)
    {
        ctrl.go();
    }
}

function CachedDropDown() { 
    this.name  = '';
    this.array = null;
    this.element = null; 
    this.parameters = null;   
    this.relatedID = ''; 
    this.value   = ''; 
    this.fnction = null;
    
    // Main callback function
    this.go = function() {    
        
        // Init array from string
        if (this.array == null || this.array.length == 0)
        {
            var groupCount = eval(this.name + 'GroupCount');
            var groupNames = eval(this.name + 'GroupNames');
            var groupString = eval(this.name + 'String')
            
            var groupStringSplit = groupString.split('|');
            var rowCount = groupStringSplit.length / groupCount;
            
            this.array = new MultiArray(rowCount, groupCount)
            
            for (var i=0; i<rowCount; i++)
            {
                for (var j=0; j<groupCount; j++)
                {
                    this.array[i][groupNames[j]] = groupStringSplit[(i * groupCount) + j];
                }
            }
        }
        
        if (this.element != null)
        {
            // Clear elements
            this.clear();
            
            // Init values
            this.element.options[0] = new Option('--','');
            
            // Init option index
            var optionsIndex = 1;
            var error = false;          
            
            // Fill from array
            for (var i=0; i<this.array.length; i++)
            {
                // Empty values we do not include in dropdown
                if (this.array[i]["text"] == null || this.array[i]["text"] == '') continue;
                
                // Filter result
                if (this.parameters != null)
                {
                    error = false;
                    for (var j=0; j<this.parameters.length; j++)
                    {
                        if (this.array[i][this.parameters[j][0]] != this.parameters[j][1].value)
                        {
                            error = true;
                            break;
                        }
                    }
                    if (error) continue;
                }
                
                // Create new option
                this.element.options[optionsIndex] = new Option(this.array[i]["text"], this.array[i]["value"]);
                
                // Set value
                if (this.value == this.array[i]["value"] || this.value == this.array[i]["text"])
                {
                    this.element.options[optionsIndex].selected = true;
                }
                
                // Increment optionsIndex
                optionsIndex++;
            }
            
            // Do post acions
            if (this.element.selectedIndex > 0)
            {
                this.fillRelated();            
            }
            else
            {
                this.clearRelated();
            }
        }
        
        // execute pending function
        if (this.fnction != null)
        {
            this.fnction(res);
        }   
    };
    
    this.clear = function()
    {
        // first clear own
        var cnt = this.element.options.length;        
        for (var i=cnt-1; i>=0; i--)
        {
            this.element.options[i] = null;
        }      
        
        // then we clear related
        ClearRelated(this.relatedID);
    };   
    this.fillRelated = function()
    {
        FillRelated(this.relatedID);
    };
    this.clearRelated = function()
    {
        ClearRelated(this.relatedID);
    };
}


/*********************************************
Asynchronous DRODOWNLIST
*********************************************/

function CreateAsyncDropDownList(name, elementID, parameters, preload, relatedID, value, fnction)
{
    var ctrl       = new AsyncDropDown();
    ctrl.name      = name;
    ctrl.element   = $(elementID);    
    ctrl.value     = value;    
    ctrl.relatedID = relatedID;    
    ctrl.parameters = parameters.split(',');
    ctrl.fnction = fnction;        
        
    dropdowns[elementID] = ctrl;
    
    if (preload)
    {
        ctrl.go();
    }
}

function AsyncDropDown() { 
    this.name = '';    
    this.element = null;   
    this.parameters = '';    
    this.relatedID = ''; 
    this.value   = ''; 
    this.fnction = null;
    
    // Main callback function
    this.callback = function(res) {
        if (FinishAJAXMethod(res))
        {
            // this fills current dropdown
            if (this.element != null)
            {
                this.clear();
                
                var table = res.value;
               
                this.element.options[0] = new Option('--','');
                var inc = 1;
                
                for (var i=0; i< table.Rows.length; i++)
                {
                    // Empty values we don't include
                    if (table.Rows[i]["value"] == '') continue;
                                    
                    this.element.options[inc] = new Option(table.Rows[i]["text"], table.Rows[i]["value"]);                    
                    
                    if (this.value == table.Rows[i]["value"] || this.value == table.Rows[i]["text"])
                    {
                        this.element.options[inc].selected = true;                        
                    }                    
                    inc++;
                }
                
                // Do post acions
                if (this.element.selectedIndex > 0)
                {
                    this.fillRelated();            
                }
                else
                {
                    this.clearRelated();
                }
            }
            
            // execute pending function
            if (this.fnction != null)
            {
                this.fnction(res);
            }           
        }    
    };
    this.go = function() {    
        // Call callback function
        var params = '';        
        for (var i=0; i<this.parameters.length - 1; i++)
        {
            if (this.parameters[i] != '')
            {                
                params += $(this.parameters[i]).value;
                if (i < this.parameters.length - 2) params += ';';
            }
        }            
        SoftwareAG.DataControls.DropDownListAsyncLoad.LoadItems(this.name, params, this.callback.bind(this));
    };
    
    this.clear = function()
    {
        // first clear own
        var cnt = this.element.options.length;        
        for (var i=cnt-1; i>=0; i--)
        {
            this.element.options[i] = null;
        }      
        
        // then we clear related
        this.clearRelated();         
    };
    this.fillRelated = function()
    {
        FillRelated(this.relatedID);
    };
    this.clearRelated = function()
    {
        ClearRelated(this.relatedID);
    };        
}

///////////////////////////////////////////
//// Number Dropdown
///////////////////////////////////////////

function CreateNumberDropDownList(elementID, startNumber, fieldCount, increment, value, mask, decimalPlaces)
{
    var ctrl = $(elementID);
    var startIndex = ctrl.options.length;
    var val;
    var text;    
    
    for (var i=0; i<fieldCount; i++)
    {
        var val = startNumber + i * increment;
        var text = val;
        
        if (decimalPlaces >= 0)
        {
            text = FormatNumber(val, decimalPlaces, true)
        }
        
        if (mask != null && mask != '')
        {
            ctrl.options[startIndex + i] = new Option(String.format(mask, text), val)
        }
        else
        {
            ctrl.options[startIndex + i] = new Option(text, val)
        }
        
        if (value != '' && val == value)
        {
            ctrl.options[startIndex + i].selected = true;
        }
        
        //ctrl.options[startIndex + i].className = 'numberOption';
    }
}
