﻿Hardcore.Configurator = function()
{
    var _ProductID;
    var _Line;
    var _divMainContent;
    var xmlConfigComponents;
    var _summary;
    var _valid;
    Ext.onReady(this.Initialize.createDelegate(this));
}

Ext.extend(Hardcore.Configurator, Ext.util.Observable,
{
    Initialize : function()
    {
        this.addEvents({"onData": true});
        
        _divMainContent = Ext.get('divMC');
        _summary = Ext.get('divSummary');
    },
    
    ProductID: function(ProductID)
    {
        this._ProductID = ProductID;
    },
    
    Line: function(Line)
    {
        if (Line == null || (typeof Line == "undefined"))
            this._Line = "";
        else
            this._Line = Line;
    },
    
    
    update: function(ComponentID, NewValue)
    {
        Hardcore.AjaxAPI.Configurator.UpdateConfigComponents(this._ProductID, ComponentID, NewValue, Xml.ToString(xmlConfigComponents), this.Load_process.createDelegate(this));
    },
    
    AddToCart : function()
    {
        if (this._valid == "1")
        {
            cart.UpdateConfigured(this._ProductID, xmlConfigComponents, this._Line);
        }
        else
        {
            d.Message('Configuration', 'Please correct the noted configuration errors before saving to the cart.');
        }
    },

    LoadFromCart : function(CartID, Line)
    {
        Hardcore.AjaxAPI.Configurator.LoadFromCart(CartID, Line, this.Load_process.createDelegate(this)); 
    },
    
    Load : function()
    {
        Hardcore.AjaxAPI.Configurator.ConfigComponents(this._ProductID, this.Load_process.createDelegate(this));        
    },
    
    Load_process: function (response)
    {
        if (response.error)
        {
            alert('There was an error message from the AJAX call:\n\n' + response.error.Message);
        }
        else
        {
            xmlConfigComponents = response.value.documentElement;
            
            this._valid = Xml.GetNamedValue(xmlConfigComponents, 'Valid', '');
            
            _summary.update(Xml.GetNamedValue(xmlConfigComponents, 'Summary', ''));
            HeaderPrice = Ext.get('lblPrice');
            HeaderPrice.update(Xml.GetNamedValue(xmlConfigComponents, 'Price', ''));
            
            Shipping = Ext.get('lblShipping');
            Shipping.update(Xml.GetNamedValue(xmlConfigComponents, 'ShipDate', ''));
            
            
            for (var i = 0, child; child = xmlConfigComponents.childNodes[i]; i++)
            {
                if (child.nodeName == 'Category')
                {
                    this.BuildComponentCategory(child);
                }
            }
        }
    },
    
    BuildComponentCategory: function (xml)
    {
        var parent;
        var UpdateUI = Xml.GetNamedValue(xml, 'UpdateUI', '');
        var ID = Xml.GetNamedValue(xml, 'ID', '');
        var divCategory = Ext.get('divCategory' + ID);

        if (UpdateUI == '-1') //not visible, empty placeholder
        {
            if (divCategory == null)
            {
                divCategory = new Ext.Element(document.createElement('div'));
                divCategory.update('<div id="divCategory' + ID + '"></div>');
                _divMainContent.appendChild(divCategory.dom);
            }
            else
            {
                parent = divCategory.up('div');
                parent.update('<div id="divCategory' + ID + '"></div>');
            }        
        }
        
        if (UpdateUI == '1') //visible
        {
            if (divCategory == null)
            {
                divCategory = new Ext.Element(document.createElement('div'));
                divCategory.update(Xml.GetNamedValue(xml, 'content', ''));
                _divMainContent.appendChild(divCategory.dom);
            }
            else
            {
                parent = divCategory.up('div');
                parent.update(Xml.GetNamedValue(xml, 'content', ''));
            }
        }
    },
    
    ToggleCategoryLine : function(CategoryID)
    {
        var divCD = Ext.get('divCD' + CategoryID);
        var divCDC = Ext.get('divCDC' + CategoryID);
        var divCDI = Ext.get('divCDI' + CategoryID);
        var divCDT = Ext.get('divCDT' + CategoryID);
        var divCDB = Ext.get('divCDB' + CategoryID);
        var divCDM = Ext.get('divCDM' + CategoryID);
        
        if (divCD.isVisible())
        {      
            Utility.Functions.SetVis(divCD, false);
            divCDB.removeClass('ConfigLeftFormBottom');
            divCDB.addClass('ConfigLeftFormBottomBlack');
            divCDM.removeClass('ConfigLeftFormMiddle');
            divCDM.addClass('ConfigLeftFormMiddleBlack');            
        }
        else
        {
            Utility.Functions.SetVis(divCD, true);
            divCDB.removeClass('ConfigLeftFormBottomBlack');
            divCDB.addClass('ConfigLeftFormBottom');
            divCDM.removeClass('ConfigLeftFormMiddleBlack');
            divCDM.addClass('ConfigLeftFormMiddle');             
        }
    }
});
