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

var clientWin;
var clientFieldCT_1;

Ext.onReady(function(){

    new Ext.Button({
        id:"addcustomfield",
        renderTo:"addcustomfield",
        text: lang('Add Custom Field'),
        iconAlign: 'left',
        handler:function(){
            addCustomField()
        }
    });

    clientFieldCT_1 = new Ext.form.TextField({
        id:         'clientFieldCT_1',
        name:       'clientFieldCT_1',
        fieldLabel: lang("Field Name"),
        labelStyle: 'font-weight:bold;font-size:11px;',
        border:     false,
        anchor:     '100%'
    });

    clientWin = new Ext.Window({
        id:           "customFieldWindow",
        x:            400,
        y:            200,
        labelAlign:   'right',
        layout:       'form',
        width:        300,
        autoHeight:   true,
        defaultType:  'textfield',
        closeAction:  'hide',
        title:        lang("Add Custom Field"),
        plain:        true,
        closable:     true,
        buttonAlign:  'right',
        items:        clientFieldCT_1,
        buttons:      [
                          {
                              id:       'btnAddClient',
                              text:     lang("Add Custom Field"),
                              disabled: false,
                              hidden:   false,
                              handler:  function(){
                                            sendCustomField();
                              }
                          }
                      ]
    });

});

function addCustomField(){
    clientFieldCT_1.reset();
    clientWin.show();

    clientFieldCT_1.focus('',300);
}

function sendCustomField() {
    if (clientFieldCT_1.getValue() != '') {
        
        Ext.Ajax.request({
           url: 'index.php?fuse=admin&action=AddCustomField',
           success: function(t) {
                onCompleteSendCustomField(t);
           },
           params: {customfieldname : clientFieldCT_1.getValue(),
                   type : customFieldType
                  }
        });

    } else {
        message = '';
        newline = "<br>";
        if (clientFieldCT_1.getValue() == '') {
            if (message != '') {
                message += newline;
            }
            message += lang("Field Name");
        }

        if (message != '') {
            Ext.MessageBox.alert(lang("Errors"), lang("The following form field(s) were incomplete or incorrect") + ":<br><br>" + message + "<br><br>" + lang("Please complete or correct the form and submit again."));
            return;
        }
    }
}

function onCompleteSendCustomField(responseObj)
{
    clientWin.hide();
    tree.root.reload();
}