/*
 * 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"),
        labelStyle: 'font-weight:bold;font-size:11px;',
        border:     false,
        validationEvent: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,
        vtype:'email',
        validationEvent:false,
        anchor:     '100%',
        allowBlank: false,
        blankText:  lang("This field is required")
    });

    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,
        labelAlign:   'right',
        layout:       'form',
        width:        300,
        modal:true,
        bodyStyle:'padding:8px 15px 8px',
        autoHeight:   true,
        defaultType:  'textfield',
        closeAction:  'hide',
        title:        lang("Add Client:"),
        plain:        true,
        closable:     true,
        buttonAlign:  'right',
        items:        [
                         clientFieldCT_1,
                          clientFieldCT_2,
                          clientFieldCT_4,
                          clientFieldnew_password
                    ],
        buttons:      [
                          {
                              id:       'btnAddClient',
                              text:     lang("Save"),
                              disabled: false,
                              hidden:   false,
                              handler:  function(){
                                            sendClient();
                              }
                          }
                      ]
    });

});

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

    clientFieldCT_1.focus('',300); // for some reason the focus get lost
}

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

            Ext.Ajax.request({
               url: 'index.php?fuse=clients&action=SaveNewClient',
               success: function(t) {
                    onCompleteSendClient(t);
               },
               params: {CT_1 : clientFieldCT_1.getValue(),
                        CT_2: clientFieldCT_2.getValue(),
                        CT_4: clientFieldCT_4.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(!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)
{
    json = ce.parseAjaxResponse(responseObj);
    if(!json.error) {
        window.location = "index.php?fuse=clients&view=Admin_Profile_Contact&frmClientID=" + json.newid;
    }
}