/*
 * 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"),
        labelStyle: 'font-weight:bold;font-size:11px;',
        border:     false,
        validationEvent: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,
        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:        [
                          clientFieldFirstName,
                          clientFieldLastName,
                          clientFieldEmail,
                          clientFieldnew_password
                    ],
        buttons:      [
                          {
                              id:       'btnAddClient',
                              text:     lang("Save"),
                              disabled: false,
                              hidden:   false,
                              handler:  function(){
                                            sendClient();
                              }
                          }
                      ]
    });

});

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

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

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

            Ext.Ajax.request({
               url: 'index.php?fuse=clients&action=SaveNewClient',
               success: function(t) {
                    onCompleteSendClient(t);
               },
               params: {firstName : clientFieldFirstName.getValue(),
                        lastName: clientFieldLastName.getValue(),
                        email: clientFieldEmail.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(!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;
    }
}
