/* Date/Time Picker for the Urban Planet Content Management System 
 * (c) 2005 Urban Planet
*/

function SetDefault (form, sOVN) {
	var oDate = new Date( eval("form."+sOVN+".value") );
	//if the "date" is time only, it'll throw NaN; this catches it and creates a valid date object
	if (oDate.toString() == 'NaN') oDate = new Date("1/1/00 "+ eval("form."+sOVN+".value"));
	if (eval("form."+sOVN+".value") == '') oDate = new Date();
	SetDateFields (form, sOVN, oDate);
}
function SetToNow (form, sOVN) {
	SetDateFields (form, sOVN, new Date());
}
function SetToOther (form, sOVN, sOVNsrc) {
	SetDateFields (form, sOVN, new Date(form[sOVNsrc].value));
}
function SetDateFields (form, sOVN, date) {
	if (eval("form.month_"+sOVN) != null) {
		SetYears(eval("form.year_"+sOVN), date.getFullYear());
		SetSelectValue (eval("form.month_"+sOVN), 1+date.getMonth());
		UpdateDays (form, sOVN); //since the month may have changed; make sure the number of days stays in sync
		SetSelectValue (eval("form.day_"+sOVN), date.getDate());
	}
	if (eval("form.hour_"+sOVN) != null) {
		SetSelectValue (eval("form.hour_"+sOVN), date.getHours() % 12  == 0 ? '12' : date.getHours() % 12);
		//SetMinutes (eval("form.minute_"+sOVN), Math.floor(date.getMinutes()/5)*5);	//this converts minutes into intervals of 5 (rounding down)
		SetMinutes (eval("form.minute_"+sOVN), date.getMinutes());
		SetSelectValue (eval("form.ampm_"+sOVN), date.getHours() < 12 ? 'AM' : 'PM');
	}
	UpdateDateTime (form, sOVN);
}
function SetMinutes (minutesObj, optionVal) {
	minutesObj.options.length = 0; // clear all minutes
	for (var i=0; i <= 55; i+=5) {
		var iVal = ('0'+ i); iVal = iVal.substring(iVal.length-2);
		minutesObj.options[minutesObj.length] = new Option(iVal, iVal);
		//if requested minute is not a multiple of 5 then add that extra option
		if (optionVal > i && optionVal < i+5) {
			iVal = ('0'+ optionVal); iVal = iVal.substring(iVal.length-2);
			minutesObj.options[minutesObj.length] = new Option(iVal, iVal);
		}
	}
	SetSelectValue (minutesObj, optionVal);
}
function SetYears (yearObj, optionVal) {
	yearObj.options.length = 0; //clear all years
	var startYear = 1999, endYear = 2010;
	if (optionVal < startYear) startYear = optionVal;
	if (optionVal > endYear) endYear = optionVal;

	for (var i=startYear; i <= endYear; i++) yearObj.options[yearObj.length] = new Option(i, i);
	SetSelectValue (yearObj, optionVal);
}
function SetSelectValue (selectObj, optionVal) {
	var foundMatch = false;
	for (var i=0; i < selectObj.length; i++)
		if (selectObj.options[i].value == optionVal) {
			selectObj.options[i].selected = true;
			foundMatch = true;
		}
	if (!foundMatch) {
		selectObj.options[selectObj.length] = new Option(optionVal, optionVal);
		selectObj.options[selectObj.length-1].selected = true;
	}
}
// every time a select box changes, this method is called to keep the hidden input in sync
function UpdateDateTime (form, sOVN) {
	if (eval ("form.hour_"+sOVN) == null) 
		eval ("form."+sOVN+".value = GetValue(form.month_"+sOVN+") +\"/\"+ GetValue(form.day_"+sOVN+") +\"/\"+ GetValue(form.year_"+sOVN+");");
	else if (eval ("form.month_"+sOVN) == null) //  \"1/1/00 \"+ 
		eval ("form."+sOVN+".value = GetValue(form.hour_"+sOVN+") +\":\"+ GetValue(form.minute_"+sOVN+") +\" \"+ GetValue(form.ampm_"+sOVN+");");		
	else
		eval ("form."+sOVN+".value = GetValue(form.month_"+sOVN+") +\"/\"+ GetValue(form.day_"+sOVN+") +\"/\"+ GetValue(form.year_"+sOVN+") +\" \"+ GetValue(form.hour_"+sOVN+") +\":\"+ GetValue(form.minute_"+sOVN+") +\" \"+ GetValue(form.ampm_"+sOVN+");");
		
	//if this is startDate and endDate exists we can force endDate to follow startDate
	if (sOVN == "startDate" && document.getElementById("endDate") != null) {
		var start = new Date(document.getElementById("startDate").value);
		var end = new Date(document.getElementById("endDate").value);
		if (end.getTime() < start.getTime()) SetDateFields (form, "endDate", start);
	}
}
//returns the value of element, be it a select, text or hidden
function GetValue (oElement) {
	if (oElement.type.substr(0,6) == "select") return oElement.options[oElement.selectedIndex].value;
	return oElement.value;
}
/// resets the list of days to match the current month & year
function UpdateDays (form, sOVN) {
	var numDays, nSelectedDay;
	if (eval ("form.day_"+sOVN+".type.substring(0,6)") == 'select') {
		eval ("nSelectedDay = form.day_"+sOVN+".options[form.day_"+sOVN+".selectedIndex].value;");
		//remove all options in select
		eval ("form.day_"+sOVN+".length = 0;");
		//add new options; one for each day
		eval ("numDays = NumDaysInMonth (form.month_"+sOVN+".options[form.month_"+sOVN+".selectedIndex].value, form.year_"+sOVN+".options[form.year_"+sOVN+".selectedIndex].value);");
		for (var i=1; i <= numDays; i++)
			eval ("form.day_"+sOVN+".options[i-1] = new Option(i.toString(), i.toString());");
		//default select
		eval ("form.day_"+sOVN+".options[(nSelectedDay-1 < numDays ? nSelectedDay-1 : numDays-1)].selected=true;");
	}
}
function NumDaysInMonth(nMonth, nYear) {    //note: nMonth will be 1 higher than it should be
	var nDate = new Date (nYear, nMonth-1, 1);
	for (var i=29; i < 33; i++) {
	    nDate.setDate (i);
	    if (nDate.getMonth() != nMonth-1) return i-1;
	}
	alert ("Javascript error: can't determine number of days for: "+ nMonth +"/"+ nYear +"!");
	return 0;
}