/**
 * Copyright(c) 2006-2009, FeyaSoft Inc. All right reserved.
 * ====================================================================
 * Licence
 * ====================================================================
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
Ext.ns("feyaSoft.home.extjsTutor.account");
 
/**
 * This JS is mainly used to handle action in the list
 * health to create.
 *
 * @author Fenqiang Zhuang
 * @Date March 8, 2007
 */
feyaSoft.home.extjsTutor.account.Edit = function(config) {

    this.myOwner = config.myOwner;
	
	Ext.QuickTips.init();
   	Ext.form.Field.prototype.msgTarget = 'side';

    var firstname = new Ext.form.TextField({
        fieldLabel: 'First name',
        allowBlank: false,
        name: 'firstname',
        maxLength: 10,
	    anchor: '90%' 
    });	 
    var lastname = new Ext.form.TextField({
        fieldLabel: 'Last Name',
        allowBlank: false,
        name: 'lastname',
        maxLength: 50,
	    anchor: '90%' 
    });	
    
    var username = new Ext.ux.form.StaticTextField({
        fieldLabel: 'Username',
        name: 'username',
        anchor: '90%' 
    });	    
    var email = new Ext.form.TextField({
        fieldLabel: 'Email',
        name: 'email',
        vtype:'email',
        blankText: 'Please provides your correct email address', 
        maxLength: 50,
	    anchor: '90%' 
    });	
    var active = new Ext.ux.ComboBox({
    	selectOnFocus:true,
        editable: false,
        mode: 'local',
        triggerAction: 'all',    	
        name:'active',
    	fieldLabel: 'Active',
 	    xtype:'combo',
 	    emptyText:'Select a status...',
 	    allowBlank: false,
 	    store:['Yes','No']
    });
    // pre-define fields in the form
	var note = new Ext.form.TextArea({
	    fieldLabel: 'Note',
	    name: 'note',
	    height: 130,
	    anchor: '90%' 
	});
	var password = new Ext.form.TextField({
        fieldLabel: 'Password',
        allowBlank: false,
        inputType: 'password',
        name: 'password',
        minLength: 4,
        anchor: '90%'    
    });
    var confirmPassword = new Ext.form.TextField({
        fieldLabel: 'Confirm Password',
        allowBlank: false,
        inputType: 'password',
        name: 'confirmpassword',
        minLength: 4,
        anchor: '90%'    
    });
	     
    var formPanel = new Ext.form.FormPanel({  
        id: 'formPanel',
        baseCls: 'x-plain',
        labelWidth: 120,
        url:'tutorAccount/update',
        defaultType: 'textfield',
    
        reader: new Ext.data.JsonReader({root: 'data'},['id','firstname','lastname', 'username', 'email','note', 'active']),
        items: [firstname, lastname, username, password, confirmPassword, email, active, note]
    });
    
    formPanel.form.load({url:'tutorAccount/load',
                         params:{id : config.id},
                         waitMsg:'Loading'}); 

    // define window and pop-up - render formPanel
    feyaSoft.home.extjsTutor.account.Edit.superclass.constructor.call(this, {
        title: 'Edit Existing Account',
        width: 550,
        height:400,
        minWidth: 400,
        minHeight: 200,
        layout: 'fit',
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        shim:false,
        animCollapse:false,
        constrainHeader:true,
        modal: true,
        items: formPanel,

        buttons: [{
            text: 'Save/Close',
            handler: function() {
                if (formPanel.form.isValid()) {
            	    if (password.getValue() != confirmPassword.getValue()) {
                		Ext.MessageBox.alert('Error Message', 'Password not match, please reenter again');                		
                	} else {
		 		        formPanel.form.submit({			      
				            waitMsg:'In processing',
				            params:{id : config.id},
				            failure: function(form, action) {
							    Ext.Msg.alert('Error Message', action.result.errorInfo);
							},
							// everything ok...
							success: function(form, action) {
                                var jsonData = Ext.decode(action.response.responseText);
                                if (jsonData.success == 'true') {
                                    Ext.Message.msgStay('Confirm', jsonData.info, 2000);
                                    this.myOwner.reload();
                                    this.close();
                                } else {
                                    Ext.Msg.alert('Error Message', jsonData.errorInfo);
                                }
							},
                            scope: this
				        });
                	}
                } else{
					Ext.MessageBox.alert('Errors', 'Please fix the errors noted.');
				}             
	        },
            scope: this
        },{
            text: 'Cancel',
            handler: function(){this.close();},
            scope: this
        }]
    });

    this.show();
};

Ext.extend(feyaSoft.home.extjsTutor.account.Edit, Ext.Window, {});
