/**
 * This file will create a calendar that can allow the user to select a date
 **/
 


function renderCalendar(name, date) {
	// prase the form-date
	var date_array = date.split("-");
	
	// get the days are in the month
	var days_in_month;
	days_in_month = getDaysInMonth(date_array[0], date_array[1]);	
	
	// render the element
	var output = "";
	
	// begin the element
	output += "<div id=\"calendar_" + name + "\">";
	
	// month
	output += selectMonth(name, date_array[1], "onchange=refreshSelectDay(\""+ name +"\")");
	
	// day (has own div to reflect correct day count by month
	output += "<div id=\"calendar_" + name + "_day\">";
	output += selectDay(name, days_in_month, date_array[2]);
	output += "</div>";
	
	// year
	output += selectYear(name, 2008, 2012, date_array[0], "onchange=refreshSelectDay(\""+ name +"\")");
	
	// close the element
	output += "</div>";
	
	// wrtie the content to the screen
	document.write(output);
}

function setCalendar(name, date) {
	// prase the form-date
	var date_array = date.split("-");
	
	// get the days are in the month
	var days_in_month;
	days_in_month = getDaysInMonth(date_array[0], date_array[1]);
	
	// change the value of the date element
	var this_year = document.getElementById(name + "_year");
	selectItem(this_year, stripLeadingZero(date_array[0]));
	
	var this_month = document.getElementById(name + "_month");
	selectItem(this_month, stripLeadingZero(date_array[1]));
	
	var this_day = document.getElementById(name + "_day");
	selectItem(this_day, stripLeadingZero(date_array[2]));
}

function selectItem(object, value) {	
	var i;
	for (i=0; i<object.options.length; i++) {
		if (object.options[i].value == value) {
			object.selectedIndex = i;
		}
	}
}


/**
 * removes the leading zero from the passed value
 * example: 05 would equal 5
 **/
function stripLeadingZero(value) {
	return value.replace(/^[0]+/g,"");
}

function refreshSelectDay(name) {
	// get the current date
	var current_year = document.getElementById(name +"_year").value;
	var current_month = document.getElementById(name +"_month").value;
	var current_day = document.getElementById(name +"_day").value;
	
	// get the days are in the month
	var days_in_month;
	days_in_month = getDaysInMonth(current_year, current_month);
	
	// render the updated list of days
	document.getElementById("calendar_"+ name + "_day").innerHTML = selectDay(name, days_in_month, current_day);
}

function getDaysInMonth(year, month) {
	/**
	 * The getDate() function returns the day of the month, starting from the beginning 
	 * of the month that the date is in. So, day 32 of March is considered to be day 
	 * 1 of April. Subtracting 1 from 32 gives the correct number of days in March!
	 **/
	return 32 - new Date(year, month - 1, 32).getDate();
}

function selectMonth(name, selected, aux) {
	// local variables
	var output = "";
	
	// set the name for the elements
	var element_name = name + "_month";
	
	// build the array of months
	var month = new Array(13);
	month[0] = "(none)";
	month[1] = "Jan";
	month[2] = "Feb";
	month[3] = "Mar";
	month[4] = "Apr";
	month[5] = "May";
	month[6] = "Jun";
	month[7] = "Jul";
	month[8] = "Aug";
	month[9] = "Sept";
	month[10] = "Oct";
	month[11] = "Nov";
	month[12] = "Dec";
	
	// create the list of months
	output += "<select name=\"" + element_name + "\" id=\"" + element_name + "\" style=\"float: left;\" "+ aux +">";
	
	var m=0;
	for (m=1; m<13; m++)
	{
		// check if this month is selected
		if (m == selected) {
			output += "<option value=\"" + m +"\" selected>" + month[m] + "</option>";
		}
		else {
			output += "<option value=\"" + m +"\">" + month[m] + "</option>";
		}
	}
	
	output += "</select>";
	
	return output;
}

function selectDay(name, days_in_month, selected, aux) {
	// local variables
	var output = "";
	
	// set the name for the elements
	var element_name = name + "_day";
	
	// create the list of months
	output += "<select name=\"" + element_name + "\" id=\"" + element_name + "\" style=\"float: left;\" "+ aux +">";
	
	var m=0;
	for (m=1; m<= days_in_month; m++)
	{
		// check if this month is selected
		if (m == selected) {
			output += "<option value=\"" + m +"\" selected>" + m + "</option>";
		}
		else {
			output += "<option value=\"" + m +"\">" + m + "</option>";
		}
	}
	
	output += "</select>";
	
	return output;
}

function selectYear(name, first_year, last_year, selected, aux) {
	// local variables
	var output = "";
	
	// set the name for the elements
	var element_name = name + "_year";
	
	// create the list of months
	output += "<select name=\"" + element_name + "\" id=\"" + element_name + "\" style=\"float: left;\" "+ aux +">";
	
	var y=0;
	for (y=last_year; y>=first_year; y--)
	{
		// check if this month is selected
		if (y == selected) {
			output += "<option value=\"" + y +"\" selected>" + y + "</option>";
		}
		else {
			output += "<option value=\"" + y +"\">" + y + "</option>";
		}
	}
	
	output += "</select>";
	
	return output;
}

function selectTime(name, min_time, max_time, increment, selected, aux)
{
	// set the name of the element
	var element_name = name + "_time";
	
	// create the list of times
	var output;
	output += "<select name=\""+ element_name +"\" id=\""+ element_name  +"\" style=\"float: left;\" "+ aux +">";
	
	var t=0;
	for (t=min_time; t<=max_time; t + increment)
	{
		// check if this month is selected
		if (t == selected) {
			output += "<option value=\"" + t +"\" selected>" + t + "</option>";
		}
		else {
			output += "<option value=\""+ t +"\">"+ t +"</option>";
		}
	}
	
	output += "</select>";
	
	return output; 
}