// functions for day out / number of nights
// 1. ASSUME day in is correct
// array of months, 0 first so I don't have to futz the index.
//					   -  J  F  M  A  M  J  J  A  S  O  N  D
var mthArr = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);

function setOut() {
	m1 = document.frm_reservation.inDay;
	m2 = document.frm_reservation.inMth;
	m3 = document.frm_reservation.inYr;

	dIn = pickInt(m1.options[m1.selectedIndex].text); // not value!
	mIn = pickInt(m2.options[m2.selectedIndex].value);
	yIn = pickInt(m3.options[m3.selectedIndex].text); // not value!
	// adjust February
	if ((yIn%4 == 0 && yIn%100 > 0) || (yIn%100 == 0 && yIn%400 == 0)) {
		mthArr[2] = 29;
	} else {
		mthArr[2] = 28;
	}
	// illegal date for month?
	if (dIn > mthArr[mIn]) {
		dIn = mthArr[mIn];
		for(count = 0; count < m1.options.length; count++) {
			if (pickInt(m1.options[count].text) == dIn) {
				m1.selectedIndex = count;
			}
		}
	}
	
	dOut = pickInt(dIn + 1); // default two night stay
	mOut = mIn;
	yOut = yIn;
	// now check for month/year overflow
	checkMonth(mIn);
	updateSelects();
}

function checkMonth(monthToCheck) {
	rolled = 0;
	// month & year overflow
	if (dOut > mthArr[monthToCheck]) {
		rolled = 1;
		dOut = dOut - mthArr[monthToCheck];
		if (monthToCheck<12) {
			mOut = monthToCheck+1;
		} else {
			// year overflow
			mOut = 1;
			monthToCheck=0;
			yOut = yOut+1;
			// adjust February
			if ((yOut%4 == 0 && yOut%100 > 0) || (yOut%100 == 0 && yOut%400 == 0)) {
				mthArr[2] = 29;
			} else {
				mthArr[2] = 28;
			}
		}
	}
	// recurse if necessary
	if (rolled && dOut > mthArr[monthToCheck+1]) {
		checkMonth(monthToCheck+1);
	}
}

function updateSelects() {
	// dOut
	theSel = document.frm_reservation.outDay;
 	for(count = 0; count < theSel.options.length; count++) {
		if (pickInt(theSel.options[count].text) == dOut) {
			theSel.selectedIndex = count;
		}
	}
	// mOut
	theSel = document.frm_reservation.outMth;
 	for(count = 0; count < theSel.options.length; count++) {
 		//alert("m:"+count+":"+pickInt(theSel.options[count].value)+":"+mOut)
		if (pickInt(theSel.options[count].value) == mOut) {
			theSel.selectedIndex = count;
		}
	}
	// yOut
	theSel = document.frm_reservation.outYr;
 	for(count = 0; count < theSel.options.length; count++) {
		if (pickInt(theSel.options[count].text) == yOut) {
			theSel.selectedIndex = count;
		}
	}
}

function pickInt(textStr) { // fix for aberrant pickInt
	if (textStr == '08') { return 8; }
	else if (textStr == '09') { return 9; }
	else { return parseInt(textStr); }
}

function calcDays() {
	if (mIn == mOut && yIn == yOut) {
	// easy...
		nights = dOut - dIn;
	} else {
		// to the end of the month...
		nights = mthArr[mIn] - dIn;
		// now step through months
		themonth = mIn;
		theyear = yIn;
		// fix for broken while()
		notfound=1;
		while(notfound) {
			themonth ++;
			if (themonth > 12) {
				themonth = 1;
				theyear = theyear+1;
			}
			if (themonth == mOut && theyear == yOut) {
				nights = nights + dOut;
			} else {
				nights = nights + mthArr[themonth];
			}
			// fix for broken while()
			if (themonth==mOut && theyear==yOut) { notfound = 0; }
		}
	}
	document.frm_reservation.nights.value = nights;
}
