/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

Ext.onReady(function(){

    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';



    var personal =  [{
            layout:'column',
            border:false,
            items:[{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textfield',
                    fieldLabel: '*Name',
                    name: 'name',
                    allowBlank:false,
                    anchor:'90%'
                },
                    new Ext.form.DateField({
                    fieldLabel: '*Birth Date',
                    name: 'birth_date',
                    value: new Date("8/1/1990"),
                    allowBlank:false,
                    anchor:'90%'
                }),
                    new Ext.form.ComboBox({
                    //store: store,
                    store: [['0', 'Male'], ['1', 'Female']],
                    fieldLabel:'*Gender',
                    name: 'female',
                    typeAhead: true,
                    mode: 'local',
                    triggerAction: 'all',
                    emptyText:'Select a gender...',
                    allowBlank:false,
                    selectOnFocus:true,
                    anchor:'90%'
                }),
                    new Ext.form.ComboBox({
                    //store: store,
                    store: [['single', 'single'], ['married', 'married'], ['widow(er)', 'widow(er)'], ['separated', 'separated'], ['divorced', 'divorced']],
                    fieldLabel:'*Marital Status',
                    name: 'marrital_status',
                    typeAhead: true,
                    mode: 'local',
                    triggerAction: 'all',
                    emptyText:'Select current marital status...',
                    allowBlank:false,
                    selectOnFocus:true,
                    anchor:'90%'
                })]
            },{
                columnWidth:.5,
                layout: 'form',
                border:false
            }]
    }]

    var contact =  [{
            layout:'column',
            border:false,
            items:[{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textfield',
                    fieldLabel: '*Email Address',
                    name: 'email',
                    vtype:'email',
                    allowBlank:false,
                    anchor:'90%'
                },{
                    xtype:'textfield',
                    fieldLabel: 'Phone',
                    name: 'phone',
                    anchor:'90%'
                },{
                    xtype:'textfield',
                    fieldLabel: 'Cell Phone',
                    name: 'cellphone',
                    anchor:'90%'
                },{
                    xtype:'textarea',
                    fieldLabel: '*Mailing Label',
                    name: 'mailing_label',
                    allowBlank:false,
                    anchor:'90%'
                }]
            },{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textfield',
                    fieldLabel: 'Parent/Guardian Contact',
                    name: 'parent',
                    anchor:'90%'
                },{
                    xtype:'textfield',
                    fieldLabel: 'Parent/Guardian Phone',
                    name: 'parent_phone',
                    anchor:'90%'
                },{
                    xtype:'textarea',
                    fieldLabel: '*Permanent Mailing Label',
                    name: 'permanent_mailing_label',
                    allowBlank:false,
                    anchor:'90%'
                }]
            }]
    }]

    var education =  [{
            layout:'column',
            border:false,
            items:[{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [
                    new Ext.form.ComboBox({
                    store: [['Public High School', 'Public High School'], ['Private High School', 'Private High School'], ['Home Schooled', 'Home Schooled'], ['GED', 'GED'], ['', 'Other']],
                    fieldLabel:'*High School Education Type',
                    name: 'graduated',
                    typeAhead: true,
                    mode: 'local',
                    triggerAction: 'all',
                    emptyText:'Choose type of education...',
                    allowBlank:false,
                    selectOnFocus:true,
                    anchor:'90%'
                }),{
                    xtype:'textfield',
                    fieldLabel: 'High School Name',
                    name: 'graduated_school',
                    anchor:'90%'
                },{
                    xtype:'textfield',
                    fieldLabel: 'High School Location',
                    name: 'graduated_location',
                    anchor:'90%'
                },{
                    xtype:'textfield',
                    fieldLabel: 'When Graduated',
                    name: 'graduated_date',
                    anchor:'90%'
                }]
            },{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textarea',
                    fieldLabel: 'Please describe any college experience',
                    name: 'college',
                    height: 172,
                    anchor:'90%'
                }]
            }]
    }]

    var religion =  [{
            layout:'column',
            border:false,
            items:[{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textfield',
                    fieldLabel: '*Please enter the name of your religion',
                    allowBlank:false,
                    name: 'religion',
                    anchor:'90%'
                },{
                    xtype:'textfield',
                    fieldLabel: '*Ward, Parish, Congregation',
                    allowBlank:false,
                    name: 'religion_unit',
                    anchor:'90%'
                }]
            },{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textfield',
                    fieldLabel: '*Religious Leader Name',
                    allowBlank:false,
                    name: 'religion_leader',
                    anchor:'90%'
                },{
                    xtype:'textarea',
                    fieldLabel: '*Religious Leader Contact Information',
                    name: 'religion_contact',
                    allowBlank:false,
                    height: 72,
                    anchor:'90%'
                }]
            }]
    }]

    var specialty =  [{
            xtype:'tabpanel',
            //border:false,
            plain:true,
            activeTab: 0,
            height:250,
            defaults:{bodyStyle:'padding:10px'},
            deferredRender: false,
            items:[{
                title:'Physical Education',
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textarea',
                    name:'physical_education',
                    fieldLabel: 'Describe in detail your experience and talents in team sports, individual sports, aerobics, outdoor activities, etc',
                    height:200,
                    anchor:'98%'
                }]
            },{
                title:'Science/Technology',
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textarea',
                    name:'science_technology',
                    fieldLabel: 'Describe in detail your experience and talents in computer, audio/visual, lighting, data entry, etc',
                    height:200,
                    anchor:'98%'
                }]
            },{
                title:'Fine Arts',
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textarea',
                    name:'fine_arts',
                    fieldLabel: 'Describe in detail your experience and talents in painting, drawing, singing, instruments, dancing, writing, drama, etc',
                    height:200,
                    anchor:'98%'
                }]
            },{
                title:'Domestic Engineering',
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textarea',
                    name:'domestic_engineering',
                    fieldLabel: 'Describe in detail your experience and talents in crafts, mechanics, scrapbooking, sewing, cooking, graphics design, etc',
                    height:200,
                    anchor:'98%'
                }]
            },{
                title:'Communication',
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textarea',
                    name:'communication',
                    fieldLabel: 'Describe in detail your experience and talents in journalism, writing, public speaking, etc',
                    height:200,
                    anchor:'98%'
                }]
            },{
                title:'Other Skills',
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textarea',
                    name:'other_skills',
                    fieldLabel: 'Describe in detail your experience and talents in any other areas',
                    height:200,
                    anchor:'98%'
                }]
            }]
    }]

    var login =  [{
            layout:'column',
            border:false,
            items:[{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items: [{
                    xtype:'textfield',
                    fieldLabel: '*Username',
                    name: 'username',
                    allowBlank:false,
                    anchor:'90%'
                },{
                    xtype:'textfield',
                    fieldLabel: '*Password',
                    name: 'password',
                    inputType:'password',
                    allowBlank:false,
                    anchor:'90%'
                },{
                    xtype:'button',
                    text: 'Load an existing application.',
                    hidden:true,
                    disabled: false,
                    scope:this,
                    style:'margin-bottom:10px',
                    handler: function(){
                        Ext.Msg.alert('Under Construction','This feature is not working yet. Please call 217-453-2222 for assistance.');
                        tab2.getForm().load({url:'get.php?username=cumom', waitMsg:'Loading'});

                    }
                },{
                    xtype:'textfield',
                    fieldLabel: '*Password again',
                    name: 'password2',
                    inputType:'password',
                    allowBlank:false,
                    anchor:'90%'
                }]
            },{
                columnWidth:.5,
                layout: 'form',
                border:false,
                items:[{
                    xtype:'button',
                    text: 'Send me my username as a reminder.',
                    hidden:true,
                    disabled: false,
                    scope:this,
                    handler: function(){
                        Ext.Msg.alert('Under Construction','This feature is not working yet. Please call 217-453-2222 for assistance.');
                    }
                },{
                    xtype:'textfield',
                    labelSeparator: " ",
                    hidden:true,
                    name: 'id',
                    anchor:'90%' 
                }]
            }]
    }]

    /*
     * ================  Form 5  =======================
     */
    
    var tab2 = new Ext.form.FormPanel({
        //standardSubmit:true,
        reader : new Ext.data.JsonReader({
            totalProperty: "results",    // The property which contains the total dataset size (optional)
            successProperty: "success",
            root: "applications",        // The property which contains an Array of row objects
            id: "id"
        }, [
            {name: 'id'},
            {name: 'username'},
            {name: 'password'},
            {name: 'name'},
            {name: 'female'},
            {name: 'birth_date'},
            {name: 'marital_status'},
            {name: 'email'},
            {name: 'phone'},
            {name: 'cellphone'},
            {name: 'mailing_label'},
            {name: 'parent'},
            {name: 'parent_phone'},
            {name: 'permanent_mailing_label'},
            {name: 'religion'},
            {name: 'religion_unit'},
            {name: 'religion_contact'},
            {name: 'mission'},
            {name: 'mission_start'},
            {name: 'mission_end'},
            {name: 'church_discipline'},
            {name: 'graduated'},
            {name: 'graduated_school'},
            {name: 'graduated_date'},
            {name: 'graduated_location'},
            {name: 'college'},
            {name: 'physical_education'},
            {name: 'science_technology'},
            {name: 'fine_arts'},
            {name: 'domestic_engineering'},
            {name: 'communication'},
            {name: 'other_skills'},
            {name: 'updated'}
        ]),
        labelAlign: 'top',
        url:"put.php",
        title: 'Nauvoo University Student Application',
        bodyStyle:'padding:5px',
        width: 700,
        items: [{
            xtype:'tabpanel',
            plain:true,
            activeTab: 0,
            height:285,
            defaults:{bodyStyle:'padding:10px'},
            deferredRender: false,
            items:[{
                title:'Personal Details',
                layout:'fit',
                items: personal
            },{
                title:'Contact Details',
                layout:'fit',
                items: contact
            },{
                title:'Education',
                layout:'fit',
                items: education
            },{
                title:'Religion',
                layout:'fit',
                items: religion
            },{
                //xtype:'tabpanel',
                title:'Skills and Talents',
                layout:'fit',
                items: specialty
            },{
                //xtype:'tabpanel',
                title:'Essay',
                layout:'form',
                items:[{
                    xtype:'textarea',
                    name:'essay',
                    fieldLabel:'Please write a short essay on why you want to attend Nauvoo University',
                    allowBlank:false,
                    height: 200,
                    anchor:'98%'
                }]
            },{
                title:'Login',
                layout:'fit',
                items: login
            }]
        }],

        buttons: [{
            text: 'Submit application now',
            //disabled: true,
            handler: function(){
                //Ext.Msg.alert('Hi!','Hi!');
                tab2.getForm().submit({
                    url: 'put.php',
                    method: 'POST',
                    waitMsg: "Submitting your application.",
		    success: function(form,e){
                        Ext.Msg.alert("Success", e.result.success_message);
                    },
                    failure: function( form, e ){
                        if (e.failureType == 'server') {
                            Ext.Msg.alert('Server failure', 'Your application did not go through. Send an email to peterivie@gmail.com. Please keep this window open and try to submit again later. Sorry for the inconvenience.');
                        }
                        else if (e.result){
                            alert("Your application did not go through. Send an email to peterivie@gmail.com. Please keep this window open and try to submit again later. Sorry for the inconvenience. Error: " + e.result.error[0]);
                        }
                        else {
                            Ext.Msg.alert("Application Incomplete", "Please check for unfinished fields under each tab.");
                        }
                        
                    }
                });
            }
            
        /*},{
            text: 'Save Draft',
            handler: function(){
                fs.getForm().submit({url:'save.php', waitMsg:'Saving Data...'});
            }
        },{
            text: 'Cancel'*/
        }]
    });

    tab2.render(document.body);
    //var bd = Ext.getBody();
    //bd.createChild({tag: '', html: '<br><br>If  you are having trouble submitting this application, go to NauvooUniversity.com and send us an email using the "Contact Us" form on the bottom right of the home page.'});


});
