﻿


Entegris.ProductCatalog.EmailThisPageDialog = function(){
    this.formBuilt = false;
    
    this.iWidth = 350;
    this.iHeight = 460;
    this.iHeight_collapsed = 150;

	Entegris.ProductCatalog.EmailThisPageDialog.superclass.constructor.call(this, "EmailThisPageDialog", {    modal:true,
                                                                                                        width:this.iWidth,
                                                                                                        height:this.iHeight,
                                                                                                        shadow:true,
                                                                                                        minWidth:300,
                                                                                                        minHeight:300,
                                                                                                        resizable: false,
                                                                                                        collapsible: false,
                                                                                                        draggable: false
                                                                                                    });
    this.addKeyListener(27, this.hide, this);
    
    this.confirmation = Ext.get('EmailThisPageConfirmation');
    this.submission = Ext.get('EmailThisPageSubmission');
    this.confirmation.setVisibilityMode(Ext.Element.DISPLAY);
    this.submission.setVisibilityMode(Ext.Element.DISPLAY);
    this.confirmation.setVisible(false);
    
    
    Ext.get('EmailThisPageSendToAnother').on('click', this.SendToAnotherFriend.createDelegate(this));
    Ext.get('EmailtThisPageConfirmationClose').on('click', this.hide.createDelegate(this));
    
    this.on('beforeshow', this.onBeforeShow.createDelegate(this), this);
    this.on('hide', this.onHide.createDelegate(this), this);
};



Ext.extend(Entegris.ProductCatalog.EmailThisPageDialog, Ext.BasicDialog, {
 
 
    onBeforeShow: function (dialog) {
        if (!this.formBuilt) {
            Ext.QuickTips.init();
            var formDiv = Ext.get('EmailThisPageForm');
            
            this.form = new Ext.form.Form({
                labelAlign: 'top',
                autoCreate: {tag: 'div', id: Ext.id()},
                monitorValid: true
            });
            
            this.form.fieldset(
                {legend: Entegris.Translations.get('lblEmailFriendYourInformation', 'Your Information')},

                new Ext.form.TextField({
                    fieldLabel: Entegris.Translations.get('lblEmailFriendName', "Name"),
                    name: 'yourName',
                    width:225,
                    allowBlank: false,
                    msgTarget: 'side'
                }),
                new Ext.form.TextField({
                    fieldLabel: Entegris.Translations.get('lblEmailFriendEmailAddress', 'Email Address'),
                    name: 'yourEmail',
                    vtype:'email',
                    width:225,
                    allowBlank: false,
                    msgTarget: 'side'
                })
            );         
            this.form.fieldset(   
                {legend: Entegris.Translations.get('lblEmailFriendFriendsInformation', "Your Friend's Information")},
                new Ext.form.TextField({
                    fieldLabel: Entegris.Translations.get('lblEmailFriendName', "Name"),
                    name: 'friendName',
                    width:225,
                    allowBlank: false,
                    msgTarget: 'side'
                }),
                new Ext.form.TextField({
                    fieldLabel: Entegris.Translations.get('lblEmailFriendEmailAddress', 'Email Address'),
                    name: 'friendEmail',
                    vtype:'email',
                    width:225,
                    allowBlank: false,
                    msgTarget: 'side'
                })
            );
            
            this.form.addButton({text: Entegris.Translations.get('lblEmailFriendSend', "Send"), formBind: true}, this.onSend.createDelegate(this), this);
            this.form.addButton({text: Entegris.Translations.get('lblEmailFriendClose', "Close")}, this.hide.createDelegate(this), this);

            this.form.render(formDiv);
        
            this.formBuilt = true;
        } else {
        
        }
    },
    
    SendToAnotherFriend: function(dialog) {
        this.form.findField('friendName').setValue('');
        this.form.findField('friendEmail').setValue('');
        this.form.clearInvalid();
        this.ResetUI(true);
    },
    
    onHide: function(dialog) {
        this.form.reset();
        this.ResetUI(false);
    },
    
    ResetUI: function(anim) {
        if (anim) {
            this.resizeToAnimated(this.iWidth, this.iHeight, this.MakeFormVisible.createDelegate(this));
            this.confirmation.setVisible(false, true);
        } else {
            this.resizeTo(this.iWidth, this.iHeight);
            this.MakeFormVisible();
        }
    },
    
    MakeFormVisible: function() {
        this.submission.setVisible(true, true);
        this.confirmation.setVisible(false);
    },
    
    onSend: function() { 
        if (this.form.isValid()) {
            this.el.mask(Entegris.Translations.get('lblEmailFriendSendingEmail', 'Sending Email...'));
            Entegris.API.ProductCatalog.ProductCatalog.SendEmailAFriend(    document.title, 
                                                                            location.href, 
                                                                            this.form.findField('yourName').getValue(), 
                                                                            this.form.findField('yourEmail').getValue(), 
                                                                            this.form.findField('friendName').getValue(), 
                                                                            this.form.findField('friendEmail').getValue(), this.onSend_process.createDelegate(this));
        }
    },
    
    
    onSend_process: function(response) { 
        this.el.unmask();
        if (typeof(response.value) == 'boolean' && response.value) {
            this.submission.setVisible(false);
            this.confirmation.setVisible(true);
            // Ext.BasicDialog.resizeToAnimated is defined in the Namespace.js file
            this.resizeToAnimated(this.iWidth, this.iHeight_collapsed);
        } else {
            alert(Entegris.Translations.get('lblEmailFriendError', 'An error occurred while processing your request.\nPlease try again at a later time.'));
        }
    }
    
});