// Form object
// For prefilling the insert/edit form
var form = {
	mode : 'insert',
			
	// Populate
	populate : function(data) {
				
		// Clear out the form
		form.clear();
				
		if(this.mode == 'insert') {
			var day		= data.day;
			var month 	= $('#monthChange').val();
			var year  	= $('#yearChange').val();
			var date	= eventObj.filterDate(year,month,day);
			$("#fromDate").val(date);
			$("#toDate").val(date);
			$("#day").val(day);			// Hidden day field for passing day data forward
			$("#mode").val(this.mode);	// Set the form mode
		}
		else {	
			// Take the data part
			var dataH = data.dataH;
					
			// Set the form mode
			$("#mode").val(this.mode);
					
			// Set the dates
			$("#fromDate").val(dataH.fromDate);
			$("#toDate").val(dataH.toDate);
					
			// Set the hours
			// From time
			var fromTime = dataH.fromTime.split(':');
			$("#fromHours option[text=" + fromTime[0] +"]").attr("selected","selected");
			$("#fromMinutes option[text=" + fromTime[1] +"]").attr("selected","selected");
					
			// To time
			var toTime = dataH.toTime.split(':');
			$("#toHours option[text=" + toTime[0] +"]").attr("selected","selected");
			$("#toMinutes option[text=" + toTime[1] +"]").attr("selected","selected");
					
			// Repeat
			$("#repeatEvent option[value=" + dataH.repeat +"]").attr("selected","selected");
					
			// Set the day
			$("#day").val(dataH.day);
					
			// Set the index in the array to edit
			$("#index").val(data.i);
		}
	},
			
	// Clear
	clear : function() {
		$(':input','#eventForm').not(':button, :submit, :reset').val('').removeAttr('checked').removeAttr('selected');
	},
			
	// Set calendar
	getUiCalendar : function() {
		// Remove current
		$("#toDate").datepicker("destroy").datepicker({
			yearRange: "-80:+80",
			dateFormat: 'yy MM d',
			altFormat: 'yy-mm-dd',
			showOn: 'button',
			buttonImage: '/lib/jquery/jqueryui/themes/calendar.png',
		    buttonImageOnly: true,
			changeMonth: true,
			changeYear: true
		});
		$("#toDate").datepicker('option', 'minDate', $("#fromDate").val()); // We need to set this on load time for the dialog too.
				
		$("#fromDate").datepicker("destroy").datepicker({
			yearRange: "-80:+80",
			dateFormat: 'yy MM d',
			altFormat: 'yy-mm-dd',
			showOn: 'button',
			buttonImage: '/lib/jquery/jqueryui/themes/calendar.png',
		    buttonImageOnly: true,
			changeMonth: true,
			changeYear: true,
			onSelect: function(dateText, inst) {
				$('#toDate').datepicker('option', "defaultDate", dateText);
				$('#toDate').datepicker('option', "minDate", dateText);
			}
		});
	},
			
	// Add the events array of objects to the form so that php can process it
	addEventsToForm : function() {
		var events = eventObj.returnAllEvents();
		$("#eventArray").val($.toJSON(events));
	}
};
