var months = new Array(12);
months[0] = new monthObj(31,"January","Jan");
months[1] = new monthObj(28,"February","Feb");
months[2] = new monthObj(31,"March","Mar");
months[3] = new monthObj(30,"April","Apr");
months[4] = new monthObj(31,"May","May");
months[5] = new monthObj(30,"June","Jun");
months[6] = new monthObj(31,"July","Jul");
months[7] = new monthObj(31,"August","Aug");
months[8] = new monthObj(30,"September","Sep");
months[9] = new monthObj(31,"October","Oct");
months[10] = new monthObj(30,"November","Nov");
months[11] = new monthObj(31,"December","Dec");

var days = new Array(7);
days[0] = new dayObj("Sunday","Sun");
days[1] = new dayObj("Monday","Mon");
days[2] = new dayObj("Tuesday","Tue");
days[3] = new dayObj("Wednesday","Wed");
days[4] = new dayObj("Thursday","Thu");
days[5] = new dayObj("Friday","Fri");
days[6] = new dayObj("Saturday","Sat");

function getSelectedOptionIdx(theField, theVal) {
	var idx = 0;
	for(var i=0;i<theField.length;i++) {
		if (theField.options[i].value == theVal) {
			idx = i;
			break;
		}
	}
	return idx;
}

function rewriteDays(theYearMonField, theDayField, minDate, maxDate) {
	var year = theYearMonField.value.substr(0,4);
	var mon = theYearMonField.value.substr(4,2); mon--;
	var loopDate = new Date(year, mon, 1);
	var defVal = theDayField.value;
	if (minDate > loopDate) {
		loopDate.setDate(minDate.getDate());
	}
	theDayField.length = 0;
	while (loopDate <= maxDate && loopDate.getFullYear() == year && loopDate.getMonth() == mon) {
		theDayField.options[theDayField.length] = new Option(getDayName(loopDate.getDay()).shortName + " " + loopDate.getDate(), loopDate.getDate());
		loopDate.setDate(loopDate.getDate()+1);
	}
	for(var i=0;i<theDayField.length;i++) {
		theDayField.selectedIndex = i;
		if (theDayField.options[i].value == defVal) {
			break;
		}
	}
}

function rewriteMonths(theMonthField, minDate, maxDate) {
	var loopDate = new Date(minDate.getFullYear(), minDate.getMonth(), 1);
	var defVal = theMonthField.value;
	theMonthField.length = 0;
	while (loopDate < maxDate || (loopDate.getYear() == maxDate.getYear() && loopDate.getMonth() == maxDate.getMonth())) {
		theMonthField.options[theMonthField.length] = new Option(months[loopDate.getMonth()].shortName + " " + loopDate.getFullYear(), loopDate.getFullYear()+""+(fillZeros(loopDate.getMonth()+1)));
		loopDate.setMonth(loopDate.getMonth()+1);
	}
	for(var i=0;i<theMonthField.length;i++) {
		if (theMonthField.options[i].value == defVal) {
			theMonthField.selectedIndex = i;
			break;
		}
	}
}

function monthObj(days, name, shortName) {
	this.days = days;
	this.name = name;
	this.shortName = shortName;
}

function dayObj(name, shortName) {
	this.name = name;
	this.shortName = shortName;
}

function getMonthDays(theMonth, theYear) {
	theMonth = parseInt(theMonth, 10);
	if (theYear % 4 == 0 && theMonth == 1) { return 29; }
	else { return months[theMonth].days; }
}

function getDayName(dayIdx) {
	return days[dayIdx];
}

function getMonthName(monthIdx) {
	return months[monthIdx];
}

function isDateAvail(theDate, firstAvailDate, lastAvailDate, monthFlag) {
	var isAvail = true;
	if (!monthFlag &&
		((theDate.getFullYear() == firstAvailDate.getFullYear() && theDate.getMonth() == firstAvailDate.getMonth() && theDate.getDate() < firstAvailDate.getDate())
		||
		(theDate.getFullYear() == lastAvailDate.getFullYear() && theDate.getMonth() == lastAvailDate.getMonth() && theDate.getDate() > lastAvailDate.getDate()))
	) {
			isAvail = false;
	}
	if (
		(theDate.getFullYear() < firstAvailDate.getFullYear())
		||
		(theDate.getFullYear() == firstAvailDate.getFullYear() && theDate.getMonth() < firstAvailDate.getMonth())
		||
		(theDate.getFullYear() > lastAvailDate.getFullYear())
		||
		(theDate.getFullYear() == lastAvailDate.getFullYear() && theDate.getMonth() > lastAvailDate.getMonth())
	) {
		isAvail = false;
	}
	return isAvail;
}

function fillZeros(theVal) {
	var theStr = theVal;
	if (theVal < 10) {
		theStr = "0"+theVal;
	}
	return theStr;
}

function leftPad(s, len)
{
	s = "000000" + s;
	return s.substring(s.length - len);
}
function formatDateYYYYsMMsDD(d)
{
		return d.getFullYear() + "/" + leftPad(d.getMonth() + 1, 2) + "/" + leftPad(d.getDate(), 2);
	}
function formatDateYYYYMMDD(d)
{
		return d.getFullYear() + leftPad(d.getMonth() + 1, 2) + leftPad(d.getDate(), 2);
	}
function formatDateMYYYY(d)
{
		return d.getMonth() + "-" + d.getFullYear();	// 0-2009 where 0 = index
	}
function formatDateMMMYY(d)
{
		return getMonthName(d.getMonth()).shortName + " " + leftPad(d.getYear(),2);	// Jul 09
}


