﻿function BrandLinks()
{
}

BrandLinks.prototype.languageIdHidden = null;
BrandLinks.prototype.oemIdSelect = null;
BrandLinks.prototype.dealerGroupIdHidden = null;
BrandLinks.prototype.websiteIdHidden = null;
BrandLinks.prototype.linkControlSelect = null;
BrandLinks.prototype.optionsChangeEventSource = null;

BrandLinks.getClassInstance = function(controlId)
{
    var control = document.getElementById(controlId);
    
    if (control != null && control.brandLinksClassInstance == null)
    {
        var inst = new BrandLinks();

        inst.languageIdHidden = document.getElementById(control.getAttribute("languageIdControlId"));
        inst.oemIdSelect = document.getElementById(control.getAttribute("oemIdControlId"));
        inst.dealerGroupIdHidden = document.getElementById(control.getAttribute("dealerGroupIdControlId"));
        inst.websiteIdHidden = document.getElementById(control.getAttribute("websiteIdControlId"));
        inst.linkControlSelect = document.getElementById(control.getAttribute("linkControlControlId"));
        
        control.brandLinksClassInstance = inst;
    }
    
    return control == null ? null : control.brandLinksClassInstance;
}

BrandLinks.openLink = function(controlId, eventSource)
{
    var inst = BrandLinks.getClassInstance(controlId);
    
    if (inst != null)
    {
        if (inst.linkControlSelect.value != '-1')
        {
            window.open(inst.linkControlSelect.value);
        }
    }
    
    return false;
}

BrandLinks.optionsChange = function(controlId, eventSource)
{
    var inst = BrandLinks.getClassInstance(controlId);
    
    if (inst != null)
    {
        if (BrandLinksControl != null &&
            inst.languageIdHidden != null &&
            inst.oemIdSelect != null &&
            inst.dealerGroupIdHidden != null &&
            inst.websiteIdHidden != null)
        {
            inst.optionsChangeEventSource = eventSource;
            
            BrandLinksControl.BrandChange(
                inst.languageIdHidden.value,
                inst.oemIdSelect.value,
                inst.dealerGroupIdHidden.value,
                inst.websiteIdHidden.value,
                Delegate.create(inst, inst.optionsChangeCallBack)
            );
        }
    }
}

BrandLinks.prototype.optionsChangeCallBack = function(response)
{
    if (response.error == null)
    {
        BrandLinks.updateSelect(
            this.linkControlSelect,
            response.value.Tables[0],
            "Link",
            "LinkTitle"
        );
    }
}

BrandLinks.updateSelect = function(select, dataTable, valueFieldName, textFieldName)
{
    if (select != null)
    {
        var selectedValue = select.value;
        
        for (var i = 0; i < select.options.length; i++)
        {
            if (select.options[i].value < 0)
                continue;
            
            select.removeChild(select.childNodes[i]);
            i--;
        }
        
        var selectedIndex = 0;
        
        for (var i = 0; i < dataTable.Rows.length; i++)
        {
            var value = dataTable.Rows[i][valueFieldName];
            var text = dataTable.Rows[i][textFieldName];
            
            select.options.add(new Option(text, value));

            /*if (value == selectedValue)
            {
                select.selectedIndex = select.options.length - 1;
            }*/
        }
    }
}


