// set days in month for selected month/year
function DaysInMonth(WhichMonth, WhichYear) {
     var DaysInMonth = 31;

     if (WhichMonth == "April" || WhichMonth == "June" || WhichMonth == "September" || WhichMonth == "November") DaysInMonth = 30;

     if (WhichMonth == "February" && (WhichYear/4) != Math.floor(WhichYear/4)) DaysInMonth = 28;

     if (WhichMonth == "February" && (WhichYear/4) == Math.floor(WhichYear/4)) DaysInMonth = 29;

     return DaysInMonth;
}

// set option list for selected month/year
function ChangeOptionDays(Which) {

	DaysObject = eval("document.eventform." + Which + "day");
	MonthObject = eval("document.eventform." + Which + "month");
	YearObject = eval("document.eventform." + Which + "year");

	Month = MonthObject[MonthObject.selectedIndex].text;
	Year = YearObject[YearObject.selectedIndex].text;

	DaysForThisSelection = DaysInMonth(Month, Year);

	CurrentDaysInSelection = DaysObject.length;

	if (CurrentDaysInSelection > DaysForThisSelection) {
		for (i = 0; i < (CurrentDaysInSelection-DaysForThisSelection); i++) {
			DaysObject.options[DaysObject.options.length - 1] = null;
		}
	}

	if (DaysForThisSelection > CurrentDaysInSelection) {
		for (i = 0; i < (DaysForThisSelection-CurrentDaysInSelection); i++) {
			NewOption = new Option(DaysObject.options.length + 1);
			DaysObject.add(NewOption);
		}
	}

	if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
}

// pass start/end date values to the event form from calendar.cgi
function PassDate(which,month,day,year){
	if (opener){

		// set which select list
		var MonthSelect = eval("opener.document.eventform." + which + "month");
		var DaySelect = eval("opener.document.eventform." + which + "day");
		var YearSelect = eval("opener.document.eventform." + which + "year");

		// set which option within select list
		var MonthOption = eval("MonthSelect.options[" + (month - 1) + "]");
		var DayOption = eval("DaySelect.options[" + (day - 1) + "]");
		var YearOption = "";

		// set which option within year select list
		for (var i = 0; i < YearSelect.length; i++) {
			if (YearSelect.options[i].text == year) {
				YearOption = YearSelect.options[i];
			}
		}

		//alert(MonthOption.text);
		//alert(DayOption.text);
		//alert(YearOption.text);

		MonthOption.selected = true;
		DayOption.selected = true;
		YearOption.selected = true;

		// close this window
		self.close();
	}
	else alert("An error occured while attempting to pass the date information to the form (error: opener window not found)");
}

// check times entered
// start/end times required if any time selected
function checkTimes(eventform) {
	var msg = '';

	if (! document.eventform.cancel.value) {
		if (document.eventform.shour.selectedIndex != 0) {
			if (document.eventform.sminute.selectedIndex == 0) {
				msg = msg + 'You must enter a starting minute with the starting hour.\n';
			}
			if (document.eventform.stime.selectedIndex == 0) {
				msg = msg + 'You must enter a starting AM/PM with the starting hour.\n';
			}
		}
	
		if (document.eventform.sminute.selectedIndex != 0) {
			if (document.eventform.shour.selectedIndex == 0) {
				msg = msg + 'You must enter a starting hour with the starting minute.\n';
			}
			if (document.eventform.stime.selectedIndex == 0) {
				msg = msg + 'You must enter a starting AM/PM with the starting minute.\n';
			}
		}
	
		if (document.eventform.stime.selectedIndex != 0) {
			if (document.eventform.sminute.selectedIndex == 0) {
				msg = msg + 'You must enter a starting minute with the starting AM/PM.\n';
			}
			if (document.eventform.shour.selectedIndex == 0) {
				msg = msg + 'You must enter a starting hour with the starting AM/PM.\n';
			}
		}
	
		if (document.eventform.ehour.selectedIndex != 0) {
			if (document.eventform.eminute.selectedIndex == 0) {
				msg = msg + 'You must enter a ending minute with the ending hour.\n';
			}
			if (document.eventform.etime.selectedIndex == 0) {
				msg = msg + 'You must enter a ending AM/PM with the ending hour.\n';
			}
		}
	
		if (document.eventform.eminute.selectedIndex != 0) {
			if (document.eventform.ehour.selectedIndex == 0) {
				msg = msg + 'You must enter a ending hour with the ending minute.\n';
			}
			if (document.eventform.etime.selectedIndex == 0) {
				msg = msg + 'You must enter a ending AM/PM with the ending minute.\n';
			}
		}
	
		if (document.eventform.etime.selectedIndex != 0) {
			if (document.eventform.eminute.selectedIndex == 0) {
				msg = msg + 'You must enter a ending minute with the ending AM/PM.\n';
			}
			if (document.eventform.ehour.selectedIndex == 0) {
				msg = msg + 'You must enter a ending hour with the ending AM/PM.\n';
			}
		}
	
		if (msg != "") {
			msg = 'The following problems were found:\n\n' + msg;
			alert(msg);
			return false;
		}
	}
}

// creates a new popup window
function PopUp(URL,Name,w,h,a,t) {

	// collect attributes
	var winN = Name;	// set the value of the window's name
	var winW = w;		// set window's width to passed width (if any)
	var winH = h;		// set window's height to passed height (if any)
	var aVal = a;		// set the value of the window's attributes (if any)
	var tVal = t;		// set the value of the window's toolbar

	// set attributes if undefined

	// set window name to a random number if undefined
	if (!winN) winN = Math.round(Math.random() * 1000000000);

	// set window width if undefined
	if (!winW) winW = 400;

	// set window height if undefined
	if (!winH) winH = 400;

	// set window default attribute value
	if (!aVal) aVal = "yes";

	// set window default toolbar value
	if (!tVal) tVal = "yes";

	// open popup window
	var newWin= window.open(URL,winN,'width=' + winW + ',height=' + winH + ',resizable=' + aVal + ',scrollbars=' + aVal + ',toolbar=' + tVal);

	// set focus on the new window
	newWin.focus();
}

// view maint item in a separate window
function ViewMaint(page,id_name,id,Name,w,h,a,t) {
	// collect attributes
	var page = page;
	var id = id;
	var winN = Name;	// set the value of the window's name
	var winW = w;		// set window's width to passed width (if any)
	var winH = h;		// set window's height to passed height (if any)
	var aVal = a;		// set the value of the window's attributes (if any)
	var tVal = t;		// set the value of the window's toolbar

	// set attributes if undefined

	// set window name to a random number if undefined
	if (!winN) winN = Math.round(Math.random() * 1000000000);

	// set window width if undefined
	if (!winW) winW = 400;

	// set window height if undefined
	if (!winH) winH = 400;

	// set window default attribute value
	if (!aVal) aVal = "yes";

	// set window default toolbar value
	if (!tVal) tVal = "no";

	// open popup window
	var newWin = window.open(page + '.cgi/' + id + '/0/1',winN,'width=' + winW + ',height=' + winH + ',resizable=' + aVal + ',scrollbars=' + aVal + ',toolbar=' + tVal);
	//var newWin= window.open(URL,                                                                 winN,'width=' + winW + ',height=' + winH + ',resizable=' + aVal + ',scrollbars=' + aVal + ',toolbar=' + tVal);
	newWin.focus();				// set focus on the new window
}
