// Student Add form definitions ------------------------------------------------
// alert("loaded");

var majorDataArr = new Ext.data.Store({
    // url: 'sampleData/majorCodes.json',
    url: '/mvc6c/majorGetAll',
    reader: new Ext.data.JsonReader({
        root:'theArray',
        id:'id'
    }, [
    'id',
    'name'
    ])
});
majorDataArr.load();
var majorComboBox = new Ext.form.ComboBox({
    store: majorDataArr,
    mode: 'local',
    valueField: 'id',
    displayField: 'name',
    fieldLabel: 'Vessel/Airline',
    name: 'major',
    allowBlank: false,
    typeAhead: true,
    triggerAction: 'all'
});

var dormDataArr = new Ext.data.Store({
    // url: 'sampleData/majorCodes.json',
    url: '/mvc6c/dormGetAll',
    reader: new Ext.data.JsonReader({
        root:'theArray',
        id:'id'
    }, [
    'id',
    'editid'
    ])
});
dormDataArr.load();
var dormComboBoxAdd = new Ext.form.ComboBox({
    store: dormDataArr,
    mode: 'local',
    valueField: 'id',
    displayField: 'editid',
    fieldLabel: 'POD',
    name: 'dormName',
    allowBlank: false,
    typeAhead: true,
    triggerAction: 'all'
});

// Student Add Form  -----------------------------------------------------------

var studentAddForm = new Ext.FormPanel({
    url: '/mvc6c/studentAdd',
    autoWidth : true,
    // width: 420,
    labelWidth: 150,
    frame: true,
    title:'Add Job Form',
    items:[{
        layout:'form',
        defaults: {
            width: 230
        },
        defaultType: 'textfield',
        items: [{
            fieldLabel: 'Job no',
            name: 'studentName',
            allowBlank: false
        },{
            fieldLabel: 'Voy no',
            name: 'studentId',
            allowBlank: false
        },{
            fieldLabel: 'Phone',
            name: 'phone'
        },majorComboBox,{
            fieldLabel: 'Create Date',
            name: 'street'
        },{
            fieldLabel: 'File Closing Date',
            name: 'city'
        },{
            fieldLabel: 'Agent',
            name: 'state'
        },{
            fieldLabel: 'Release',
            name: 'zip'
        },dormComboBoxAdd
        ],
        buttonAlign: 'left',
        buttons: [
        {
            text: 'Add',
            listeners:{
                click: clickSubmit
            }
        },{
            text: 'Reset',
            handler: function(){
                studentAddForm.getForm().reset();
                dh.overwrite( 'theMsgtext0', '&nbsp;' );
                dh.overwrite( 'theMsgtext1', '&nbsp;' );
                dh.overwrite( 'divTabStudentAddResults', '&nbsp;' );
            }
        }]
    }]
});

function clickSubmit(){
    dh.overwrite( 'theMsgtext0', '&nbsp;' );
    dh.overwrite( 'theMsgtext1', '&nbsp;' );
    dh.overwrite( 'divTabStudentAddResults', '&nbsp;' );
    studentAddForm.getForm().submit({
        success: function (result,action) {
            dh.overwrite( 'theMsgtext0', 'Ajax call: Success' );
        //    dh.overwrite( 'theMsgtext1', 'Ajax call: responseText: '+action.response.responseText );
            dh.overwrite( 'divTabStudentAddResults', "<b>"+action.result.message+"</b>" );
            studentAddForm.getForm().reset();
        },
        failure: function(result,action){
            if (typeof(action.response)=="undefined") {
                dh.overwrite( 'theMsgtext0', 'Ajax call: not executed, not made' );
                dh.overwrite( 'divTabStudentAddResults', 'Enter required fields which are highlighted in red.' );
                return;
            }
            if (typeof(action.result)=="undefined") {
                dh.overwrite( 'theMsgtext0', 'Ajax call: Failed, HTTP Status: '+action.response.status +" "+action.response.statusText);
            //    dh.overwrite( 'theMsgtext1', 'Ajax call: responseText: '+action.response.responseText );
                dh.overwrite( 'divTabStudentAddResults', '<b>Ajax call: Failed, No JSON data returned by server.</b>' );
                return;
            }
            dh.overwrite( 'theMsgtext0', 'Ajax call: JSON success attribute = false' );
      //      dh.overwrite( 'theMsgtext1', 'Ajax call: responseText: '+action.response.responseText );
            dh.overwrite( 'divTabStudentAddResults', "<b>"+action.result.message+"</b>" );
        }
    });
    // Note, the following executes, only if the form is not submited,
    // ie. Ext JS form field validations failed.
    dh.overwrite( 'theMsgtext1', 'exit: clickSubmit' );
}

// eof
