﻿Type.registerNamespace('JetShop.StoreControls.SendEmailPopup'); 

JetShop.StoreControls.SendEmailPopup = function(element)
{
    JetShop.StoreControls.SendEmailPopup.initializeBase(this, [element]); 
    
    this._informationText = null;
    this._emailAddressText = null;
    this._culture = null;
    this._emailTitle = null;
    this._sendImageUrl = null;
    this._closeImageUrl = null;
    this._sourceEmailID = null;
    
    this._backgroundElement = null;
    this._foregroundElement = null;
    this._ibSend = null;
    this._ibClose = null;
    this._divForm = null;
    this._aLink = null;
    this._isPopupCreated = false;
    this._disableTabsCalled = false;
    
    this._saveTabIndexes = new Array();
    this._saveDisableSelect = new Array();
    this._tagWithTabIndex = new Array('A','AREA','BUTTON','INPUT','OBJECT','SELECT','TEXTAREA','IFRAME');
    
    this._successCallbackRequestAccountInformationHandler = null;
    this._onKeyPressHandler = null;
}

JetShop.StoreControls.SendEmailPopup.prototype =
{
    initialize: function() {
        // initialize the base
        JetShop.StoreControls.SendEmailPopup.callBaseMethod(this, 'initialize');

        this._emailTitle = this._emailAddressText; //"E-postadress";

        this._aLink = $get(this.get_id() + "_aLink");
        $addHandler(this._aLink, "click", Function.createDelegate(this, this._showPopup));

        this._foregroundElement = this.get_element();

        var backgroundElement = document.createElement('div');
        backgroundElement.style.display = 'none';
        backgroundElement.style.position = 'absolute';
        backgroundElement.style.zIndex = 10000;

        if (Sys.Browser.agent == Sys.Browser.InternetExplorer) {
            backgroundElement.style.filter = "alpha(opacity=50)";
        }
        else {
            backgroundElement.style.opacity = 0.5;
        }

        backgroundElement.style.backgroundColor = "#ffffff";

        this._backgroundElement = backgroundElement;
        document.body.appendChild(this._backgroundElement);
        this._foregroundElement.style.position = 'absolute';
        this._foregroundElement.style.zIndex = 10001;

        this._successCallbackRequestAccountInformationHandler = Function.createDelegate(this, this._successCallbackRequestAccountInformation);
        this._onKeyPressHandler = Function.createDelegate(this, this._keyPressed);
    },

    set_InformationText: function(value) {
        this._informationText = value;
    },

    get_InformationText: function() {
        return this._informationText;
    },

    set_EmailAddressText: function(value) {
        this._emailAddressText = value;
    },

    get_EmailAddressText: function() {
        return this._emailAddressText;
    },

    set_SourceEmailID: function(value) {
        this._sourceEmailID = value;
    },

    get_SourceEmailID: function() {
        return this._sourceEmailID;
    },

    set_SendImageUrl: function(value) {
        this._sendImageUrl = value;
    },

    get_SendImageUrl: function() {
        return this._sendImageUrl;
    },

    set_CloseImageUrl: function(value) {
        this._closeImageUrl = value;
    },

    get_CloseImageUrl: function() {
        return this._closeImageUrl;
    },

    _successCallbackRequestAccountInformation: function(result) {
        this._setInfoText(result);
    },

    _getCurrentStyle: function(element, attribute, defaultValue) {
        var currentValue = null;
        if (element) {
            if (element.currentStyle) {
                currentValue = element.currentStyle[attribute];
            } else if (document.defaultView && document.defaultView.getComputedStyle) {
                var style = document.defaultView.getComputedStyle(element, null);
                if (style) {
                    currentValue = style[attribute];
                }
            }

            if (!currentValue && element.style.getPropertyValue) {
                currentValue = element.style.getPropertyValue(attribute);
            }
            else if (!currentValue && element.style.getAttribute) {
                currentValue = element.style.getAttribute(attribute);
            }
        }

        if ((!currentValue || currentValue == "" || typeof (currentValue) === 'undefined')) {
            if (typeof (defaultValue) != 'undefined') {
                currentValue = defaultValue;
            }
            else {
                currentValue = null;
            }
        }
        return currentValue;
    },

    _disableTabs: function() {
        if (!this._disableTabsCalled) {
            var i = 0;
            var tagElements;
            var tagElementsInPopUp = new Array();
            Array.clear(this._saveTabIndexes);
            for (var j = 0; j < this._tagWithTabIndex.length; j++) {
                tagElements = this._foregroundElement.getElementsByTagName(this._tagWithTabIndex[j]);
                for (var k = 0; k < tagElements.length; k++) {
                    tagElementsInPopUp[i] = tagElements[k];
                    i++;
                }
            }
            i = 0;
            for (var j = 0; j < this._tagWithTabIndex.length; j++) {
                tagElements = document.getElementsByTagName(this._tagWithTabIndex[j]);
                for (var k = 0; k < tagElements.length; k++) {
                    if (Array.indexOf(tagElementsInPopUp, tagElements[k]) == -1) {
                        this._saveTabIndexes[i] = { tag: tagElements[k], index: tagElements[k].tabIndex };
                        tagElements[k].tabIndex = "-1";
                        i++;
                    }
                }
            }
            i = 0;
            if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
                var tagSelectInPopUp = new Array();
                tagElements = this._foregroundElement.getElementsByTagName('SELECT');
                for (var k = 0; k < tagElements.length; k++) {
                    tagSelectInPopUp[i] = tagElements[k];
                    i++;
                }
                i = 0;
                Array.clear(this._saveDisableSelect);
                tagElements = document.getElementsByTagName('SELECT');
                for (var k = 0; k < tagElements.length; k++) {
                    if (Array.indexOf(tagSelectInPopUp, tagElements[k]) == -1) {
                        this._saveDisableSelect[i] = { tag: tagElements[k], visib: this._getCurrentStyle(tagElements[k], 'visibility') };
                        //tagElements[k].style.visibility = 'hidden';
                        tagElements[k].disabled = "disabled";
                        i++;
                    }
                }
            }
            this._disableTabsCalled = true;
        }
    },

    _restoreTabs: function() {
        if (this._disableTabsCalled) {
            for (var i = 0; i < this._saveTabIndexes.length; i++) {
                this._saveTabIndexes[i].tag.tabIndex = this._saveTabIndexes[i].index;
            }
            if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
                for (var k = 0; k < this._saveDisableSelect.length; k++) {
                    this._saveDisableSelect[k].tag.style.visibility = this._saveDisableSelect[k].visib;
                    this._saveDisableSelect[k].tag.disabled = "";
                }
            }
            this._disableTabsCalled = false;
        }
    },

    _layout: function() {
        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        var bounds = this._getClientBounds();

        this._backgroundElement.style.left = '0px';
        this._backgroundElement.style.top = '0px';
        this._backgroundElement.style.width = bounds.width + 'px';
        this._backgroundElement.style.height = bounds.height + 'px';
    },

    _getClientBounds: function() {
        var clientWidth;
        var clientHeight;

        switch (Sys.Browser.agent) {
            case Sys.Browser.InternetExplorer:
                clientWidth = document.documentElement.scrollWidth;
                clientHeight = document.documentElement.scrollHeight;
                break;
            case Sys.Browser.Safari:
                clientWidth = window.innerWidth;
                clientHeight = window.innerHeight;
                break;
            case Sys.Browser.Opera:
                clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
                clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
                break;
            default:  // Sys.Browser.Firefox, etc.
                clientWidth = Math.max(window.innerWidth, document.documentElement.scrollWidth);
                clientHeight = Math.max(window.innerHeight, document.documentElement.scrollHeight);
                break;
        }

        return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
    },

    _createPopup: function() {
        if (this._isPopupCreated) {
            this._divForm.style.display = "block";

            return;
        }

        this._divForm = document.createElement("div");
        this._divForm.className = "popupContainer";
        this.get_element().appendChild(this._divForm);

        var objWrapper = document.createElement("div");
        this._divForm.appendChild(objWrapper);

        var objContainer = document.createElement("div");
        objContainer.className = "CustomerInfoBody";
        objWrapper.appendChild(objContainer);

        var objWrapper = document.createElement("div");
        objWrapper.className = "CreateCustomerProfileWrapper";
        objContainer.appendChild(objWrapper);

        var objAreaHeader = document.createElement("div");
        objAreaHeader.className = "ContentAreaHeader";
        objWrapper.appendChild(objAreaHeader);

        var objEmpty = document.createElement("div");
        objAreaHeader.appendChild(objEmpty);

        var objAreaBody = document.createElement("div");
        objAreaBody.className = "ContentAreaBody";
        objWrapper.appendChild(objAreaBody);

        var objAreaContainer = document.createElement("div");
        objAreaContainer.className = "CreateCustomerProfile";
        objAreaBody.appendChild(objAreaContainer);

        var divContent = document.createElement("div");
        divContent.innerHTML = this._informationText;
        objAreaContainer.appendChild(divContent);

        var divEmail = document.createElement("div");
        objAreaContainer.appendChild(divEmail);

        var spnEmailTitle = document.createElement("span");
        spnEmailTitle.innerHTML = this._emailTitle;
        spnEmailTitle.style.paddingRight = "8px";
        divEmail.appendChild(spnEmailTitle);

        var inputEmail = document.createElement("input");
        inputEmail.name = this.get_id() + "_accountInfoEmail";
        inputEmail.id = this.get_id() + "_accountInfoEmail";

        var sourceEmail = $get(this._sourceEmailID);

        if (sourceEmail) {
            inputEmail.value = $get(this._sourceEmailID).value;
        }

        divEmail.appendChild(inputEmail);

        var divSubmit = document.createElement("div");
        divSubmit.style.width = "100%";
        objAreaContainer.appendChild(divSubmit);

        var divResult = document.createElement("div");
        divResult.style.height = "10px";
        divSubmit.appendChild(divResult);

        var spnResult = document.createElement("span");
        spnResult.id = this.get_id() + "_result";
        spnResult.style.color = "red";
        divResult.appendChild(spnResult);

        var divButtons = document.createElement("div");
        divButtons.style.textAlign = "right";
        divSubmit.appendChild(divButtons);

        this._ibClose = document.createElement("img");
        this._ibClose.src = this._closeImageUrl;
        this._ibClose.id = "accountInfoClose";
        this._ibClose.alt = "";
        this._ibClose.style.padding = "3px";
        this._ibClose.style.cursor = "pointer";
        divButtons.appendChild(this._ibClose);
        $addHandler(this._ibClose, "click", Function.createDelegate(this, this._showLink));

        this._ibSend = document.createElement("img");
        this._ibSend.src = this._sendImageUrl;
        this._ibSend.id = this.get_id() + "_accountInfoSend";
        this._ibSend.alt = "";
        this._ibSend.style.padding = "3px";
        this._ibSend.style.cursor = "pointer";
        divButtons.appendChild(this._ibSend);
        $addHandler(this._ibSend, "click", Function.createDelegate(this, this._sendEmail));

        var objAreaFooter = document.createElement("div");
        objAreaFooter.className = "ContentAreaFooter";
        objWrapper.appendChild(objAreaFooter);

        objEmpty = document.createElement("div");
        objAreaFooter.appendChild(objEmpty);

        var objFooter = document.createElement("div");
        objFooter.className = "CustomerInfoFooter";
        objWrapper.appendChild(objFooter);

        this._isPopupCreated = true;
    },

    _showPopup: function() {
        if (!this._isPopupCreated) {
            this._createPopup();
        }

        this._divForm.style.display = "block";
        this._backgroundElement.style.display = '';
        this._disableTabs();

        this._layout();
        this._layout();


        $addHandler(this._divForm, 'keypress', this._onKeyPressHandler);
        this._setFocus('');
    },

    _setFocus: function() {
        $get(this.get_id() + "_accountInfoEmail").focus();
    },

    _setInfoText: function(result) {
        // Empty result text
        var obj = $get(this.get_id() + "_result");
        obj.innerHTML = result;
    },

    _showLink: function() {
        if (this._isPopupCreated) {
            $removeHandler(this._divForm, 'keypress', this._onKeyPressHandler);
        }

        this._setInfoText('');
        this._divForm.style.display = "none";
        this._backgroundElement.style.display = 'none';
        this._restoreTabs();
    },

    _sendEmail: function() {
        JetShop.StoreControls.Services.General.RequestAccountInformation($get(this.get_id() + "_accountInfoEmail").value, this._culture, this._successCallbackRequestAccountInformationHandler);
    },

    _keyPressed: function(e) {
        if (e.charCode == Sys.UI.Key.enter) {
            e.stopPropagation();
            e.preventDefault();

            this._sendEmail();

            return false;
        }
        else if (e.charCode == Sys.UI.Key.esc) {
            e.stopPropagation();
            e.preventDefault();

            this._showLink();

            return false;
        }
        else {
            return true;
        }
    },

    dispose: function() {
        if (this._ibSend != null) {
            $clearHandlers(this._ibSend);
        }

        if (this._ibClose != null) {
            $clearHandlers(this._ibClose);
        }

        if (this._aLink != null) {
            $clearHandlers(this._aLink);
        }

        if (this._divForm != null) {
            $clearHandlers(this._divForm);
        }


        this._successCallbackRequestAccountInformationHandler = null;
        this._onKeyPressHandler = null;

        JetShop.StoreControls.SendEmailPopup.callBaseMethod(this, 'dispose');
    }
}

// register the class
JetShop.StoreControls.SendEmailPopup.registerClass('JetShop.StoreControls.SendEmailPopup', Sys.UI.Control);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();