/*
 * date.js
 *
 * Contains functions for manipulating
 * the displayed form dates.
 *
 */

var currDate;
var prevDate;

var currMonth;
var prevMonth;

var currDay;
var prevDay;

var currYear;
var prevYear;

var currHour;
var prevHour;

var selectedMonth;
var selectedDay;
var selectedYear;
var selectedHour;

// init_date()
//
// Initializes the date of the forms to
// be today.

function init_date() {

	// Calculate the current date
	// and the date 30 days ago
	//currDate = new Date(2007, 8, 7);
	//prevDate = new Date(2007, 8, 7);
	currDate = new Date();
	prevDate = new Date();
	prevDate.setDate(currDate.getDate() - 30);
	
	// Month
	currMonth = currDate.getMonth() + 1;
	
	if (currMonth < 10) {
		currMonth = '0' + currMonth;
	}
	
	selectedMonth = currMonth;
	
	prevMonth = prevDate.getMonth() + 1;
	
	if (prevMonth < 10) {
		prevMonth = '0' + prevMonth;
	}
	
	// Day
	currDay = currDate.getDate();
	
	if (currDay < 10) {
		currDay = '0' + currDay;
	}
	
	selectedDay = currDay;
	
	prevDay = prevDate.getDate();
	
	if (prevDay < 10) {
		prevDay = '0' + prevDay;
	}
	
	// Year
	currYear = currDate.getFullYear();
	prevYear = prevDate.getFullYear();
	selectedYear = currYear;
	
	// Hour
	currHour = currDate.getHours();
	
	if (currHour < 10) {
		currHour = '0' + currHour;
	}
	
	prevHour = prevDate.getHours();
	
	if (prevHour < 10) {
		prevHour = '0' + prevHour;
	}
	
	selectedHour = currHour;
	
	
	// Show the current time on the page's form
	update_form('s');
	update_form('e');
	
}

// get_year_form()
//
// Populates the year portion of a date
// form.

function get_year_form() {
	var yr = "";
	
	if(currYear != prevYear) {
		yr += "<option value=\"" + prevYear + "\"";
		
		if (prevYear == selectedYear) {
			yr += " SELECTED";
		}
		
		yr += ">" + prevYear + "</option>\n";
	}
	
	yr += "<option value=\"" + currYear + "\"";
	
	if (currYear == selectedYear) {
		yr += " SELECTED";
	}
	
	yr += ">" + currYear + "</option>\n";
	
	return yr;
}


//	militaryTo12Time(hour)
//
//	Converts from 24-hour time to 12-hour
//	time.  Takes the 24-hour time as input.

function militaryTo12Time(hour) {
	var str = "";

	// Hack to cast hour as a number
	hour++;
	hour--;

	if (hour == 0) {
		str = "12am";
	}
	else if (hour < 12) {
		str = hour + "am";
	}
	else if (hour == 12) {
		str = "12pm";
	}
	else {
		str = (hour-12) + "pm";
	}

	return str;
}

//	monthToString(num)
//
//	num - a number (1-12) representing the month
//
//	Converts a numbered month into its string
//	representation.

function monthToString(num) {

	if (num == "01") {return "January";} else
	if (num == "02") {return "February";} else
	if (num == "03") {return "March";} else
	if (num == "04") {return "April";} else
	if (num == "05") {return "May";} else
	if (num == "06") {return "June";} else
	if (num == "07") {return "July";} else
	if (num == "08") {return "August";} else
	if (num == "09") {return "September";} else
	if (num == "10") {return "October";} else
	if (num == "11") {return "November";} else
	if (num == "12") {return "December";}

}

// get_month_form()
//
// Populates the month portion of a date
// form.

function get_month_form() {
	var mon = "";
	
	if (currMonth != prevMonth) {
		mon += "<option value=\"" + prevMonth + "\"";
		
		if (prevMonth == selectedMonth) {
			mon += " SELECTED";
			
			if (prevYear != currYear) {
				selectedYear = prevYear;
			}
		}
		
		mon += ">" + monthToString(prevMonth) + "</option>\n";
	}
	
	mon += "<option value=\"" + currMonth + "\"";

	if (currMonth == selectedMonth) {
		mon += " SELECTED";

		if (prevYear != currYear) {
			selectedYear = currYear;
		}
	}
	
	mon += ">" + monthToString(currMonth) + "</option>\n";
	
	return mon;
}

// last_day_of_month(mon)
//
// mon - the number representation of a
// 	month
//
// calculates the last day of a month...
// "Thirty days have september, april, june
// and november!"  Accounts for leap years as
// well.

function last_day_of_month(mon) {

	if (mon == "01") {return 31;} else
	if (mon == "02") {
		if (selectedYear % 4 == 0) {
			return 29;
		} else {
			return 28;
		}
	} else
	if (mon == "03") {return 31;} else
	if (mon == "04") {return 30;} else
	if (mon == "05") {return 31;} else
	if (mon == "06") {return 30;} else
	if (mon == "07") {return 31;} else
	if (mon == "08") {return 31;} else
	if (mon == "09") {return 30;} else
	if (mon == "10") {return 31;} else
	if (mon == "11") {return 30;} else
	if (mon == "12") {return 31;}

}

// get_day_form()
//
// Sets the day portion of a form to include
// just the right number of days, keeping with
// only showing the past 30 days from today.

function get_day_form() {

	var dy = "";
	var first_day = 1;
	var last_day = 31;
	
	if (currMonth == prevMonth) {
		last_day = currDay;
	} else {
	
		if (selectedMonth == currMonth) {
			last_day = currDay;
		} else {
			last_day = last_day_of_month(prevMonth);
			first_day = prevDay;
		}
		
	}
	
	for (i = first_day; i <= last_day; i++) {
	
		// Hack: convert i to an integer but don't change its value
		i++;
		i--;
		// End Hack
		
		if (i < 10) {
			i = '0' + i;
		}
		if (i == selectedDay) {
			dy += "<option value=\"" + i + "\" SELECTED>" + i + "</option>";
		} else {
			dy += "<option value=\"" + i + "\">" + i + "</option>";
		}
	}
	
	return dy;

}


//	get_hour_form()
//
//	Populate the hour portion of the form on index.php
//	This selects the current hour if no other choice
//	is made.

function get_hour_form() {
	var hr = "";
	
	for (i=0; i < 24; i++) {
		var h = i;
		var h_orig = h;
		
		if (h < 10) {
			h = '0' + h;
		}
		
		hr += "<option value=\"" + h + "\"";
		
		if (h == selectedHour) {
			hr += " SELECTED";
		}
		
		hr += ">";
		hr += militaryTo12Time(h_orig);
		hr += "</option>\n";
	}
	
	return hr;
}

//	update_form(start_or_end)
//
//	start_or_end - either 's' or 'e'
//		depending on if we are updating the
//		start or end time
//
//	Updates the form on index.php
//	with a set of valid dates that
//	do not exceed the past 30 days.

function update_form(start_or_end) {

	// Month
	var months = get_month_form();
	//document.getElementById(start_or_end + 'month').innerHTML = months;
	
	// Day
	var days = get_day_form();
	//document.getElementById(start_or_end + 'day').innerHTML = days;
	
	// Year
	var years = get_year_form();
	//document.getElementById(start_or_end + 'year').innerHTML = years;
	
	// Hour
	var hours = get_hour_form();
	//document.getElementById(start_or_end + 'hour').innerHTML = hours;
	
	var html = "<select id=\"" + start_or_end + "month\" onChange=\"update_date()\">" + months + 
	"</select><select id=\"" + start_or_end + "day\">" + days + "</select>" +
	", <select id=\"" + start_or_end + "year\" onChange=\"update_date()\">" + years + "</select>" + 
	"<select id=\"" + start_or_end + "hour\">" + hours + "</select>";

	document.getElementById(start_or_end + 'date_form').innerHTML = html;
}


//	update_date()
//
//	Updates the selections on the form on index.php
//	such that only the previous 30 days remain selectable

function update_date() {

	// Grab form inputs
	selectedYear = document.getElementById('syear').value;
	selectedMonth = document.getElementById('smonth').value;
	selectedDay = document.getElementById('sday').value;
	selectedHour = document.getElementById('shour').value;
	
	update_form('s');
	
	selectedYear = document.getElementById('eyear').value;
	selectedMonth = document.getElementById('emonth').value;
	selectedDay = document.getElementById('eday').value;
	selectedHour = document.getElementById('ehour').value;

	update_form('e');
}
