﻿Type.registerNamespace('JetShop.StoreControls.NewsLetterSignUp'); 

JetShop.StoreControls.NewsLetterSignUp = function(element)
{
    JetShop.StoreControls.NewsLetterSignUp.initializeBase(this, [element]);
    
    this._clickHandler = null;
    this._successCallback = null;
    this._errorCallback = null;
    
    this._shopTheme = null;
}

JetShop.StoreControls.NewsLetterSignUp.prototype = 
{
    initialize : function() 
    {
        // initialize the base
        JetShop.StoreControls.NewsLetterSignUp.callBaseMethod(this,'initialize');
        
        this._clickHandler = Function.createDelegate(this, this._onClick);
        this._successCallback = Function.createDelegate(this, this._success);
        this._errorCallback = Function.createDelegate(this, this._error);
        
        $addHandler($get(this.get_id() + "_ibEmail"), 'click', this._clickHandler);
    },
    
    get_ShopTheme : function()
    {
		return this._shopTheme;
    },
    
    set_ShopTheme : function(value)
    {
		this._shopTheme = value;
    },
    
    _onClick : function(e)
    {
        e.stopPropagation();
        e.preventDefault();
        
        var email = $find(this.get_id() + "_tbwe").get_Text();
        var subscribe = this._getSubscribed();
        
        if (email.length > 0)
        {
		    JetShop.StoreControls.Services.General.SignUpForNewsletter(email, subscribe, this._successCallback, this._errorCallback);
		}
	
            
		return false;
    },
    
    _getSubscribed : function()
    {
        if (this._shopTheme == "FrameworkBasic")
        {
            var nameID = this.get_id()+ "_register";
            var objItems = document.getElementById(nameID);
            
            if (objItems.checked == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            var nameID = this.get_id()+ "_mailStatus";
            var objItems = document.getElementsByName(nameID.replace(/_/g, '$'));
        
            for (var cbIndex = 0; cbIndex < objItems.length; cbIndex++)
            {
                if (objItems[cbIndex].checked == true)
                {
                    if (objItems[cbIndex].value == "0")
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }            
            }
        }
        
        return true;
    },
    
    _success : function(result)
    {
       $get(this.get_id() + "_lblResult").innerHTML = result;
    },
    
    _error : function(result)
    {
        $get(this.get_id() + "_lblResult").innerHTML = result;
    },
	    
    dispose : function() 
    {
        $clearHandlers($get(this.get_id() + "_ibEmail"));

        JetShop.StoreControls.NewsLetterSignUp.callBaseMethod(this,'dispose'); 
    }
}

// register the class
JetShop.StoreControls.NewsLetterSignUp.registerClass('JetShop.StoreControls.NewsLetterSignUp', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();