/* 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, oDate) {

	if (eval("form.month_"+sOVN) != null) {

		SetSelectValue (eval("form.month_"+sOVN), 1+oDate.getMonth());

		UpdateDays (form, sOVN); //since the month may have changed; make sure the number of days stays in sync

		SetSelectValue (eval("form.day_"+sOVN), oDate.getDate());

		SetSelectValue (eval("form.year_"+sOVN), oDate.getFullYear());

	}

	if (eval("form.hour_"+sOVN) != null) {

		SetSelectValue (eval("form.hour_"+sOVN), oDate.getHours() % 12  == 0 ? '12' : oDate.getHours() % 12);

		SetSelectValue (eval("form.minute_"+sOVN), Math.floor(oDate.getMinutes()/5)*5);	//this converts minutes into intervals of 5 (rounding down)

		SetSelectValue (eval("form.ampm_"+sOVN), oDate.getHours() < 12 ? 'AM' : 'PM');

	}

	UpdateDateTime (form, sOVN);

}

function SetSelectValue (oSelect, sOptionVal) {

	for (var i=0; i < oSelect.length; i++)

		if (oSelect.options[i].value == sOptionVal) oSelect.options[i].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 be after 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 endDate < startDate, set endDate = startDate

		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 most recently selected month

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;

}

function AlertTest (s) {

	alert (s);

}
