Ext.namespace('utc.calendar.event');

var form;
var date;
var day;
var userType;
var action;

/**
 * Initializes an event. This can be thought of as a constructor if you'd like.
 * Sets up instance variables and the event form.
 */
utc.calendar.event.initialize = function(aUserType, aDate, aDay, anAction) {
    userType = aUserType;
    date = aDate;
    day = aDay;
    action = anAction;
    form = new Ext.form.BasicForm(action + 'EventForm', {
        url: 'eventForm.php',
        method: 'post',
        baseParams:{
            date: date,
            userType: userType
        }
    });
};

/**
 * Submits the form! This will make an ajax call to eventForm, which will in turn talk to the
 * database. Depending on the action, it will add/modify/delete.
 */
utc.calendar.event.submitForm = function(newAction) {
    var successHandler = function (response) {
        // After the ajax call successfully finishes, reload the calendar.
        utc.calendar.reloadCalendar();
        if (newAction != 'delete') {
            utc.calendar.displayResponse(userType, date, day, response.responseText, newAction);
        }
    }

    var con = new Ext.data.Connection();

    con.request({url:'eventForm.php?'
                     + 'date=' + date + '&userType=' + userType + '&action=' + newAction,
                params: form.getValues(),
                method: 'POST',
                success: successHandler});
}

/**
 * Delete an event from the calendar.
 */
utc.calendar.event.deleteEvent = function(aUser, aDate) {
    var con = new Ext.data.Connection();

    con.request({url:'eventForm.php',
                method: 'POST',
                success: function() {
                    utc.calendar.reloadCalendar()
                },
                params: {date: aDate,
                         userType: aUser,
                         action: 'delete'}
                });
}
