/*
 * 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;
var clientFieldCT_2;
var clientFieldCT_4;
var clientFieldnew_password;

Ext.onReady(function(){

    clientFieldCT_1 = new Ext.form.TextField({
        id:         'clientFieldCT_1',
        name:       'clientFieldCT_1',
        fieldLabel: lang("First Name"),
        validationEvent:false,
        labelStyle: 'font-weight:bold;font-size:11px;',
        border:     false,
        anchor:     '100%',
        allowBlank: false,
        blankText:  lang("This field is required")
    });

    clientFieldCT_2 = new Ext.form.TextField({
        id:         'clientFieldCT_2',
        name:       'clientFieldCT_2',
        fieldLabel: lang("Last Name"),
        labelStyle: 'font-weight:bold;font-size:11px;',
        border:     false,
        validationEvent:false,
        anchor:     '100%',
        allowBlank: false,
        blankText:  lang("This field is required")
    });

    clientFieldCT_4 = new Ext.form.TextField({
        id:         'clientFieldCT_4',
        name:       'clientFieldCT_4',
        fieldLabel: lang("Email"),
        labelStyle: 'font-weight:bold;font-size:11px;',
        border:     false,
        validationEvent:false,
        vtype:'email',
        anchor:     '100%',
        allowBlank: false,
        blankText:  lang("This field is required")
    });

    var groupstore = new Ext.data.JsonStore({
            id: "groupstore",
            root: 'groups',
            totalProperty: 'totalcount',
            idProperty: 'id',
            remoteSort: true,
            baseParams: {
            filter: "",
            customerid: "0"
        },
        fields: [
                'id','groupname', 'description'
        ],
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
                url: 'index.php?fuse=admin&action=GetAccessGroupList&staffonly=1'
        })
    });
    groupstore.setDefaultSort('id', 'asc');
    groupstore.on('load',function(){
        //toggleButtons(0);
    });

    clientFieldGroup = new Ext.form.ComboBox({
        typeAhead: true,
        triggerAction: 'all',
        lazyRender:true,
        mode: 'local',
        validationEvent:false,
        fieldLabel: lang("Group"),
        labelStyle: 'font-weight:bold;font-size:11px;',
        anchor:     '100%',
        store: groupstore,
        valueField: 'id',
        displayField: 'groupname',
        allowBlank: false,
        blankText:  lang("This field is required"),
        forceSelection: true,
        editable: false
    });
    groupstore.load();


    clientFieldnew_password = new Ext.form.TextField({
        autoCreate:   {tag: "input", type: "password", size: "20", autocomplete: "off"},
        cls:          'x-form-text',
        id:           'clientFieldnew_password',
        name:         'clientFieldnew_password',
        fieldLabel:   lang("Password"),
        labelStyle:   'font-weight:bold;font-size:11px;',
        border:       false,
        validationEvent:false,
        anchor:       '100%',
        allowBlank:   false,
        blankText:    lang("This field is required")
    });

    clientWin = new Ext.Window({
        id:           "clientWindow",
        x:            400,
        y:            200,
        bodyStyle:'padding:8px 15px 8px',
        labelAlign:   'right',
        layout:       'form',
        width:        300,
        autoHeight:   true,
        modal:true,
        defaultType:  'textfield',
        closeAction:  'hide',
        title:        lang("Add Staff:"),
        plain:        true,
        closable:     true,
        buttonAlign:  'right',
        items:        [
                          clientFieldCT_1,
                          clientFieldCT_2,
                          clientFieldCT_4,
                          clientFieldGroup,
                          clientFieldnew_password
                      ],
        buttons:      [
                          {
                              id:       'btnAddClient',
                              text:     lang("Save"),
                              disabled: false,
                              hidden:   false,
                              handler:  function(){
                                   sendClient();
                              }
                          }
                      ]
    });

});

function addStaff(){
    clientFieldCT_1.reset();
    clientFieldCT_2.reset();
    clientFieldCT_4.reset();
    clientFieldnew_password.reset();
    clientWin.show();

    clientFieldCT_1.focus('',300);
}

function sendClient(){
        if(clientFieldCT_1.validate()
           && clientFieldCT_2.validate()
           && clientFieldCT_4.validate()
           && clientFieldGroup.validate()
           && clientFieldnew_password.validate()
          ){

            Ext.Ajax.request({
               url: 'index.php?fuse=admin&action=Admin_SaveAdmin',
               success: function(t) {
                    onCompleteSendClient(t);
               },
               params: {CT_1:clientFieldCT_1.getValue(),
                      CT_2:clientFieldCT_2.getValue(),
                      CT_4:clientFieldCT_4.getValue(),
                      groupid : clientFieldGroup.getValue(),
                      new_password :clientFieldnew_password.getValue(),
                      confirm_password:clientFieldnew_password.getValue()
                      }
            });

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

            if(!clientFieldCT_2.validate()){
                if(message != ''){
                    message += newline;
                }
                message += lang("Last Name")
            }

            if(!clientFieldCT_4.validate()){
                if(message != ''){
                    message += newline;
                }
                message += lang("E-mail")
            }

            if(!clientFieldGroup.validate()){
                if(message != ''){
                    message += newline;
                }
                message += lang("Group")
            }

            if(!clientFieldnew_password.validate()){
                if(message != ''){
                    message += newline;
                }
                message += lang("Password")
            }

            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 onCompleteSendClient(responseObj)
{

    var data = Ext.util.JSON.decode(responseObj.responseText);
    if(data.error){
        if(data.message){
            Ext.MessageBox.alert(lang("Errors"), data.message);
        }else{
            Ext.MessageBox.alert(lang("Errors"), "There was an error performing this action");
        }
        return;
    }else if(data.message){
        msg(data.message);
    }else{
        msg(lang("Action on Note(s) have been performed"));
    }

    clientWin.hide();
    gridstore.load({params:{start:0}});

}