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

var clientWin;
var clientFieldFirstName;
var clientFieldLastName;
var clientFieldEmail;
var clientFieldnew_password;

Ext.onReady(function(){

    clientFieldFirstName = new Ext.form.TextField({
        id:         'clientFieldFirstName',
        name:       'clientFieldFirstName',
        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")
    });

    clientFieldLastName = new Ext.form.TextField({
        id:         'clientFieldLastName',
        name:       'clientFieldLastName',
        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")
    });

    clientFieldEmail = new Ext.form.TextField({
        id:         'clientFieldEmail',
        name:       'clientFieldEmail',
        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:        [
                          clientFieldFirstName,
                          clientFieldLastName,
                          clientFieldEmail,
                          clientFieldGroup,
                          clientFieldnew_password
                      ],
        buttons:      [
                          {
                              id:       'btnAddClient',
                              text:     lang("Save"),
                              disabled: false,
                              hidden:   false,
                              handler:  function(){
                                   sendClient();
                              }
                          }
                      ]
    });

});

function addStaff(){
    clientFieldFirstName.reset();
    clientFieldLastName.reset();
    clientFieldEmail.reset();
    clientFieldnew_password.reset();
    clientWin.show();

    clientFieldFirstName.focus('',300);
}

function sendClient(){
        if(clientFieldFirstName.validate()
           && clientFieldLastName.validate()
           && clientFieldEmail.validate()
           && clientFieldGroup.validate()
           && clientFieldnew_password.validate()
          ){

            Ext.Ajax.request({
               url: 'index.php?fuse=admin&action=Admin_SaveAdmin',
               success: function(t) {
                    onCompleteSendClient(t);
               },
               params: {firstName:clientFieldFirstName.getValue(),
                      lastName:clientFieldLastName.getValue(),
                      email:clientFieldEmail.getValue(),
                      groupid : clientFieldGroup.getValue(),
                      new_password :clientFieldnew_password.getValue(),
                      confirm_password:clientFieldnew_password.getValue()
                      }
            });

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

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

            if(!clientFieldEmail.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}});

}
