function getMonthName(monthNum) {
	switch (monthNum) {
		case 1:
			return "1";
		case 2:
			return "2";
		case 3:
			return "3";
		case 4:
			return "4";
		case 5:
			return "5";
		case 6:
			return "6";
		case 7:
			return "7";
		case 8:
			return "8";
		case 9:
			return "9";
		case 10:
			return "10";
		case 11:
			return "11";
		case 12:
			return "12";
	}
}

function validateSearch(searchFieldId) {

	if (searchFieldId == null)
		return(null);

	var searchField = document.getElementById(searchFieldId);

	if (searchField == null)
		return(null);


	var errorMsg = '';

	if (searchFieldId == 'searchproducts') {
		if (searchField.value == '') {
			errorMsg = 'Please enter one or more words to search';
		}
	} else if (searchField.options[searchField.selectedIndex].value == '') {
		if (searchFieldId == 'searchcategories')
			errorMsg = 'Please select a category to search';
		else if (searchFieldId == 'searchprices')
			errorMsg = 'Please select a price range to search';
	}

	if (errorMsg != '') {
		alert(errorMsg);
		return(false);
	} else {
		return(true);
	}

}

function updateSelectionToday(fieldPostfix, idPostFix)
{
	var byInput = document.getElementById("sendprinttoday" + fieldPostfix);
	byInput.checked = true;
	var cardTypeInput = document.getElementById(idPostFix);
	cardTypeInput.checked = true;
}

function updateSelectionDays(fieldPostfix, idPostFix, minLeadDays)
{

	var minDate = new Date;
	minDate.setDate(minDate.getDate() + minLeadDays);

	var byInput = document.getElementById("sendprintby" + fieldPostfix);
	byInput.checked = true;
	var cardTypeInput = document.getElementById(idPostFix);
	cardTypeInput.checked = true;

/*
	for(i = 0; i < cardTypeInput.options.length; i++)
	{
		if (dayInput.options[i].value == fieldPostfix) {
			byInput.selectedIndex = i
			break;
		}
	}
*/

	var dayInput = document.getElementById("sendprintbyday" + fieldPostfix);
	var monthInput = document.getElementById("sendprintbymonth" + fieldPostfix);
	var yearInput = document.getElementById("sendprintbyyear" + fieldPostfix);

/*	dayInput.selectedIndex = (dayInput.selectedIndex == 0) ? 0 : dayInput.selectedIndex - 1;
	monthInput.selectedIndex = (monthInput.selectedIndex == 0) ? 0 : monthInput.selectedIndex - 1;
	yearInput.selectedIndex = (yearInput.selectedIndex == 0) ? 0 : yearInput.selectedIndex - 1;*/

	if (monthInput.options.length > 12)
	{
		rebuildYearList(yearInput, minDate.getFullYear());
	}

	var monthCount = 12 - minDate.getMonth();

	var selectedYear = yearInput.options[yearInput.selectedIndex].value;
	var minYear = minDate.getFullYear();

	if ( selectedYear == minYear && monthInput.options.length != monthCount) {
		//this year selected but month count wrong
		rebuildMonthList(monthInput, monthCount, minDate.getMonth());
	} else if ( selectedYear != minYear && monthInput != 12) {
		//not min year selected, but not all months showing
		rebuildMonthList(monthInput, 12, 0);
	}

	year = parseInt(yearInput.options[yearInput.selectedIndex].value);
	month = parseInt(monthInput.options[monthInput.selectedIndex].value) - 1;
	day = parseInt(dayInput.options[dayInput.selectedIndex].value);

	newSelectedDay = -1;

	minDay = (year == minDate.getFullYear() && month == (minDate.getMonth())) ? minDate.getDate() - 1 : 0;

	dayInput.options.length = 0;

	var days = new Array(31, ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	for(i = 0; i < days[month]; i++)
	{
		if (i >= (minDay)) {
			dayInput.options.length = dayInput.options.length + 1;
			dayInput.options[dayInput.options.length - 1].value = i + 1;
			dayInput.options[dayInput.options.length - 1].text = i + 1;
			if (day == i + 1) {
				newSelectedDay = dayInput.options.length - 1;
			}
		}
	}

	if ( newSelectedDay != -1 ) {
		dayInput.selectedIndex = newSelectedDay;
	} else if ( day > dayInput.options[dayInput.options.length - 1].value ) {
		dayInput.selectedIndex = dayInput.options.length - 1;
	} else {
		dayInput.selectedIndex = 0;
	}
}

function rebuildMonthList(monthInput, monthCount, minMonth) {
	oldMonthCount = monthInput.options.length;

	var month = parseInt(monthInput.options[monthInput.selectedIndex].value);

	var oldSelIndex = monthInput.selectedIndex;

	monthInput.options.length = monthCount;

	for(i = 0; i < monthCount; i++)
	{
		monthInput.options[i].value = 1 + minMonth + i;
		monthInput.options[i].text = getMonthName(1 + minMonth + i);
	}

	//oldMonthCount will be 13 if initial refresh
	if (oldMonthCount == 13) {
		monthInput.selectedIndex = (monthInput.selectedIndex == 0) ? 0 : monthInput.selectedIndex - 1;
	} else if (oldMonthCount != monthCount) {
		if (month < 1 + minMonth) {
			monthInput.selectedIndex = 0;
		} else {
			monthInput.selectedIndex = oldSelIndex + (monthCount - oldMonthCount);
		}
	}
}

function rebuildYearList(yearInput, minYear) {
	for(i = 0; i < yearInput.options.length - 1; i++)
	{
		yearInput.options[i].value = minYear + i;
		yearInput.options[i].text = minYear + i;
	}

	yearInput.selectedIndex = (yearInput.selectedIndex == 0) ? 0 : yearInput.selectedIndex - 1;
	yearInput.options.length = yearInput.options.length - 1;
}