/*
 * Ext JS Library 3.0 RC1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 *
 * http://extjs.com/license
 */

var recordEmailInfo;
var simpleForm;

var emailWin;
var emailName;
var emailSubject;
var emailType;
var emailHtml;
var emailTxt;

var defaultEmailName = 'Template Name';
var defaultEmailSubject = 'Template Subject';

Ext.onReady(function(){

//hiddens
    emailId = new Ext.form.Hidden({
        id:               'emailId',
        name:             'emailId',
        value: '0'
    });


//  adminEmail
    emailName = new Ext.form.TextField({
        id:               'emailName',
        name:             'emailName',
        fieldLabel:       lang("E-Mail Name"),
        value:            defaultEmailName,
        validationEvent:  false,
        anchor:           '98%',
        allowBlank:       false
    });

//  emailSubject
    emailSubject = new Ext.form.TextField({
        id:               'emailSubject',
        name:             'emailSubject',
        fieldLabel:       lang("E-Mail Subject"),
        value:            defaultEmailSubject,
        validationEvent:  false,
        anchor:           '100%',
        allowBlank:       false
    });

    emailType = new Ext.form.ComboBox({
        id:               'emailType',
        name:             'emailType',
        typeAhead:        true,
        triggerAction:    'all',
        lazyRender:       true,
        mode:             'local',
        validationEvent:  false,
        anchor:           '100%',
        store: new Ext.data.SimpleStore({
            	fields: ['id','name'],
            	data : [
                     ['2','Custom E-Mail'],
                     ['1','Autoresponder E-Mail']
                    ]
            }),
        fieldLabel:       lang("Type"),
        valueField:       'id',
        displayField:     'name',
        hiddenName:       'emailTypeId',
        allowBlank:       false,
        blankText:        lang("This field is required"),
        forceSelection:   true,
        editable:         false,
        disabled: true
    });

    //emailType.on('show', processResult(0));
    emailType.on('select', function(c,r,i){
        if(i==0){
            $('emailCustomTag').innerHTML = "";
        }else{
            $('emailCustomTag').innerHTML = '<div style="padding-bottom:8px;">( <a href="#" onclick="newOpenHelpWindow(60);">click to view available tags</a> )</div>';
        }
    });


//  emailMessage
    emailHtml = new Ext.form.HtmlEditor({
        id:         'emailHtml',
        name:       'emailHtml',
        value:      '',
        hideLabel:  true,
        anchor:     '100%',
        height:     200,
        x:          5,
        y:          40,
        hideMode:   'offsets'
    });

    emailTxt = new Ext.form.TextArea({
        id:         'emailTxt',
        name:       'emailTxt',
        value:      '{ADMINSIGNATURE}',
        hideLabel:  true,
        anchor:     '100%',
        height:     200
    });

// form
    simpleForm = new Ext.FormPanel({
        //labelAlign:   'top',
        id:           'simpleForm',
        name:         'simpleForm',
        frame:        true,
        hideBorders:  true,
        labelAlign: "top",
        shadow:       false,
        floating:     false,
        autoHeight:   true,
        title:        '',
        bodyStyle:    'padding:5px 5px 0',
        items:        [
                          {
                              layout: 'column',
                              border: false,
                              items:  [
                                          {
                                              columnWidth:  .7,
                                              layout:       'form',
                                              border:       false,
                                              items:        [
                                                                emailName
                                                            ]
                                          },
                                          {
                                              columnWidth:  .3,
                                              layout:       'form',
                                              border:       false,
                                              items:        [
                                                                emailType
                                                            ]
                                          }
                                      ]
                          },
                          emailSubject,
                          {
                                id: 'emailCustomTag',
                                xtype:'label',
                                name: 'creatinvoicelabel'
                          },
                          {
                                id: 'emailtabs',
                                xtype: 'tabpanel',
                                deferredRender : false,
                                activeTab: 0,
                                height: 350,
                                anchor: '100%',
                                plain: true,
                                items:[{
                                    id: 'panel1',
                                    cls: 'x-plain',
                                    title: lang('HTML E-mail'),
                                    layout:'fit',
                                    items: emailHtml
                                },{
                                    id: 'panel2',
                                    forceLayout:true,
                                    cls: 'x-plain',
                                    title: lang('Plain-Text E-mail'),
                                    layout:'fit',
                                    items: emailTxt
                                }

                                ]
                           },
                           emailId
                      ]
    });

    simpleForm.on({
        actioncomplete: function(form, action){
            if(action.type == 'load'){
                //can now save
                if(action.result.data.emailTypeId==1 || action.result.data.emailTypeId ==2)
                {
                    emailName.setDisabled(false);
                    if(action.result.data.emailTypeId==2){
                        $('emailCustomTag').innerHTML = "";
                    }else{
                        $('emailCustomTag').innerHTML = '<div style="padding-bottom:8px;">( <a href="#" onclick="newOpenHelpWindow(60);">click to view available tags</a> )</div>';
                    }
                }else{
                    $('emailCustomTag').innerHTML = '<div style="padding-bottom:8px;">( <a href="#" onclick="newOpenHelpWindow('+action.result.data.emailHelpId+');">click to view available tags</a> )</div>';
                }
                Ext.getCmp('btnSaveEmail').setDisabled(false);
            }else if(action.type == 'submit'){
                emailWin.hide();
                if(action.result.nameupdated){
                    gridstore.reload();
                }
            }
        }
    });


// Window
    emailWin = new Ext.Window({
        id:           "emailWindow",
        y:            100,
        layout:       'fit',
        width:        650,
        autoHeight:   true,
        modal:        true,
        closeAction:  'hide',
        title:        lang("E-Mail Customer"),
        plain:        true,
        shadow:       false,
        items:        [simpleForm],
        buttonAlign:  'right',
        buttons:      [
                          {
                              text:     'Close',
                              handler:  function(){
                                            emailWin.hide();
                                        }
                          },
                          {
                              id:       'btnSaveEmail',
                              text:     lang("Save"),
                              disabled: true,
                              hidden:   false,
                              handler:  function(){
                                            simpleForm.getForm().submit({url:'index.php?fuse=admin&action=Admin_SaveEmailTemplate', waitMsg:'Saving Data...'});
                                        }
                          }
                      ]
    });

    emailWin.on("hide",function(){
        closeHelpWindow();
    });

});

function ShowEditEmail(emailId)
{
    //alert(emailId);
    emailWin.enable();
    emailWin.show();
    
    if(emailId==0){
        //show the dropdown for type
        emailType.setDisabled(false);
        emailName.setDisabled(false);
        $('emailId').value = "0";
        emailType.setValue(2);
        emailName.setValue(defaultEmailName);
        emailSubject.setValue(defaultEmailSubject);
        emailHtml.setValue('');
        emailTxt.setValue('');
        Ext.getCmp('btnSaveEmail').setDisabled(false);
    }else{
        Ext.getCmp('btnSaveEmail').setDisabled(true);
        emailType.setDisabled(true);
        emailName.setDisabled(true);
        simpleForm.getForm().load({url:'index.php?fuse=admin&action=Admin_GetEmailTemplate&templateid='+emailId, waitMsg:'Loading'});
    }
}