﻿Hardcore.Customer = function()
{
    var _customerKey;
    var _customerName;
    var _cookie;
    var _dialog;
    Ext.onReady(this.Initialize.createDelegate(this));    
}

Ext.extend(Hardcore.Customer, Ext.util.Observable,
{
    Initialize : function()
    {
        this.addEvents({"onData": true});
        this._loading = Ext.get('loading');        
    },

    Dialog: function(d)
    {
        this._dialog = d;
    },
    
    OverviewTab : function()
    {
        ShowTab('divOverviewContent','tabGroup');
        return false;
    },
    
    CartTab : function()
    {
        cart.Get(false, false);
        ShowTab('divShoppingCartContent','tabGroup');
        return false;
    },

    ProfileTab : function()
    {
        Functions.ShowLoader(true);
        
        cart.GetCartCookie(); 
        Hardcore.AjaxAPI.Customer.InfoGet(cart._key, this._customerKey, this.ProfileTab_process.createDelegate(this));
    },
    ProfileTab_process : function(response)
    {
        if (response.error)
        {
            this.HandleError(response);
            
        }
        else
        {
            Form.PopulateUserFields(response.value.documentElement);
            this.ToggleProfileUIFields(0);
            ShowTab('divProfileContent','tabGroup');         
        }
        Functions.ShowLoader(false);
    },

    OrderHistoryTab : function()
    {
        Functions.ShowLoader(true);
        
        this.GetCustomerCookie();
        if(this._customerKey != null)
            Hardcore.AjaxAPI.Customer.OrderHistory(this._customerKey, this.OrderHistoryTab_process.createDelegate(this));
        else
            Functions.ShowLoader(false);
        
    },
    OrderHistoryTab_process : function(response)
    {
        if (response.error)
        {
            this.HandleError(response);
        }
        else
        {
            xmlCart = response.value.documentElement;

            var divOrdHist = Ext.get('divOrderHistory');
            divOrdHist.update(xmlCart.childNodes[0].nodeValue);
            
            ShowTab('divOrderHistoryContent', 'tabGroup');        
        }
        
        Functions.ShowLoader(false);
        return false;
    },

    ToggleOrderDetail : function(orderNumber)
    {
        Functions.ShowLoader(true);
        
        var divName = 'div' + orderNumber;
        var divContent = Ext.get(divName);
        var anchor = Ext.get('a' + orderNumber);
        
        if (divContent.isVisible())
        {
            //hide
            divContent.dom.className = 'hide';
            anchor.dom.className = 'Plus';
            Functions.ShowLoader(false);
        }
        else
        {
            //get and show
            divContent.dom.className = '';
            anchor.dom.className = 'Minus';
            
            if (divContent.dom.firstChild == null)
                this.OrderGet(orderNumber);       
        }
        
        //Functions.ShowLoader(false);
    },
    
    OrderGet : function(orderNumber)
    {
        var contextObj = {varOrdNumber: orderNumber}
        
        this.GetCustomerCookie();
        Hardcore.AjaxAPI.Customer.OrderGet(this._customerKey, orderNumber, this.OrderGet_process.createDelegate(this), contextObj);
    },
    OrderGet_process : function(response)
    {
        if (response.error)
        {
            this.HandleError(response);
        }
        else
        {
            resXml = response.value.documentElement;
            //replace the specified div
            this.BuildOrderDetail(resXml, response.context.varOrdNumber);
        }
        Functions.ShowLoader(false);
        return;
    },

    BuildOrderDetail : function (xml, orderNumber)
    {
        var divName = 'div' + orderNumber;
        var divCName = 'divC' + orderNumber;
        
        var divMainContent = Ext.get(divName);

        var divContent = new Ext.Element(document.createElement('div'));
        divContent.update('<div id="' + divCName + '"></div>');
        divMainContent.appendChild(divContent.dom);
        
        divContent.update(xml.firstChild.nodeValue);
    },
   
    Logout : function()
    {
        Hardcore.AjaxAPI.Customer.Logout(this.Logout_process.createDelegate(this));        
    },
    Logout_process : function(response)
    {
        if (response.error)
        {
            this.HandleError(response);
        }
        else
        {
            //delete cookies
            if (!this._cookie)
            {
               this._cookie = new Utility.Cookie();
            }
            this._cookie.Delete("HardcoreCustomer");
            this._cookie.Delete("HardcoreLoginName");
            
            window.location.reload(true);
        }
    },

    GetCustomerCookie : function()
    {
        if (!this._cookie)
        {
           this._cookie = new Utility.Cookie();
        }
        this._customerKey = this._cookie.Read("HardcoreCustomer");
    },
    
    GetCustomerNameCookie : function()
    {
        if (!this._cookie)
        {
           this._cookie = new Utility.Cookie();
        }
        this._customerName = this._cookie.Read("HardcoreLoginName");        
    },
    
    SignUp : function(fields)
    {
        Utility.Functions.SetVis(this._loading, true);
        
        //validate fields
        if (Validator.SignInScreen(fields) == false)
        {
            Utility.Functions.SetVis(this._loading, false);
            return false;
        }

        Utility.Functions.SetVis(this._loading, false);
        
        return true;
    },
    
    ToggleProfileUIFields : function(onOff)
    {
        //for each formfield, lock or unlock it
        aFields = new Array();
        aFields = Form.getFormElementsArray('formfield', 'true');
        
        for(var i = 0; i < aFields.length; i++)
        {
            var c = document.getElementById(aFields[i]);
            if (onOff == 1)
                c.disabled = false;
            else
                c.disabled = true;
        }
        
        //handle the non formfields
        if (onOff == 1)
        {
            document.getElementById('aEdit').className = 'hide';
            document.getElementById('aCancel').className = '';
            document.getElementById('divSave').className = '';            
        }
        else
        {
            document.getElementById('aEdit').className = '';
            document.getElementById('aCancel').className = 'hide';
            document.getElementById('divSave').className = 'hide';
        }        
    },
    
    
    EnableProfileUIFields : function()
    {
        //for each formfield, unlock it
        aFields = new Array();
        
        aFields = Form.getFormElementsArray('formfield', 'true');
        
        for(var i = 0; i < aFields.length; i++)
        {
            var c = document.getElementById(aFields[i]);
            c.disabled = false;
        }
    },

    ProfileUpdate : function()
    {
        Functions.ShowLoader(true);
        var x = Form.getFormElementsXML('formfield', 'true');
        this.GetCustomerCookie();
        Hardcore.AjaxAPI.Customer.InfoUpdate(this._customerKey, x, this.ProfileUpdate_process.createDelegate(this));
    },
    ProfileUpdate_process : function(response)
    {
        if(response.error)
        {
            Functions.ShowLoader(false);
            this.HandleError(response);            
        }
        else
        {
            Functions.ShowLoader(false);
            d.Message('Profile Updated', 'Your Profile has been updated.');
            this.ToggleProfileUIFields(0);
        }        
    },
     
    HandleError: function(response)
    {
        _dialog.Message('Error', 'There was an error message from the AJAX call:\n\n' + response.error.Message);
    }
});
