function checkSpecialEvents (resArrive, resDepart, action) {
	arriveYear = resArrive.getFullYear(); // refererence point for events database
	for (i=0; i < num_events; i++) {
		// convert to timestamps
		eventStart = new Date(arriveYear, specialEvents[i][1] - 1, specialEvents[i][2], 0,0,0,0);
		// if the event end month is less than the start month, put it in the following year
		if (specialEvents[i][3] < specialEvents[i][1]) endYear = arriveYear + 1;
		else endYear = arriveYear;
		eventEnd = new Date(endYear,  specialEvents[i][3] - 1, specialEvents[i][4], 0,0,0,0);
		// check for intersection
		if (eventStart < resDepart && eventEnd > resArrive) {
			if (action == "min_stay") return specialEvents[i][5];
			else if (action == "min_stay2") return specialEvents[i][6];
			else if (action == "min_stay3") return specialEvents[i][7];
			else if (action == "pricing") return specialEvents[i][8];
			else if (action == "event") return specialEvents[i][0];
		}
	}
	return 0;
}

function checkMinStay (resArrive, resDepart) {
	startMo = resArrive.getMonth();
	startYear = resArrive.getYear()
	endMo = resDepart.getMonth();
	endYear = resDepart.getYear()
	
	// check for year cross
	endMo = endMo + (12 * (endYear - startYear));
	//alert(startMo + " start/end " + endMo);
	
	minStay = houseMinStay;
	
	for (i=startMo; i<=endMo; i++) {
		index = (i % 12);
		if (houseMinStayMo[index] > minStay) minStay = houseMinStayMo[index]
		//alert("Mo: " + index + ", " + houseMinStayMo[index] + ", " + minStay);
	}
	return minStay;
}


function resTotal(withDeposit) {
	var subtotal = 	parseFloat(document.reservation.up_rent.value) +
					parseFloat(document.reservation.up_fee_arr_early.value) +
					parseFloat(document.reservation.up_fee_dep_late.value) +
					parseFloat(document.reservation.up_fee_pool.value) +
					parseFloat(document.reservation.up_city_tax.value) +
					parseFloat(document.reservation.up_fee_cleaning.value) +
					parseFloat(document.reservation.up_fee_pet.value) +
					parseFloat(document.reservation.up_fee_misc.value);
	var total = subtotal + parseFloat(document.reservation.up_fee_sec_dep.value) + parseFloat(document.reservation.up_fee_peace_waiver.value);
	if (withDeposit) return total;
	else return subtotal;
}
					
function updateTotals() {
	// calculate processing fee - only if res is longer than 7 days, or res is 7 days or more in advance
	// and, the processing fee checkbox is checked CHANGED TO ALWAYS APPLY THE PROCESSING FEE - 2009-06-19
	if (!document.reservation.up_custom_pricing.checked) {
		if (document.reservation.up_proc_fee_pricing_ch.value == "yes" && (lengthOfStay >= 0 || lengthOfAdvance >= -1)) {
			up_fee_processing = Math.round((resTotal(true) * processing_fee_rate) * 100)/100;
		} else up_fee_processing = 0;
		document.reservation.up_fee_processing.value = formatAsMoney(up_fee_processing);
	}

	// display the totals
	theRent = "$" + formatAsMoney(resTotal(false));
	document.getElementById('subtotal').innerHTML = theRent;
	theRent = "$" + formatAsMoney(resTotal(true));
	document.getElementById('total').innerHTML = theRent;
	
	totalRent = Math.round((resTotal(true) + parseFloat(document.reservation.up_fee_processing.value)) * 100)/100;
	theRent = "$" + formatAsMoney(totalRent);
	document.getElementById('total_w_proc_fee').innerHTML = theRent;
	document.reservation.credit_total.value = totalRent;
}

var nightsChangeID = 0;


function nightsChange(nights) {
	clearTimeout(nightsChangeID);
	if (nights != "") nightsChangeID = setTimeout('nightsChangeExecute()',800);
}

function nightsChangeExecute() {

	nights = document.reservation.up_stay_nights.value
	nowYear = document.reservation.up_arrive_year.value
	nowMonth = document.reservation.up_arrive_month.value
	nowDay = document.reservation.up_arrive_day.value
	
	arriveDate = new Date(nowYear, nowMonth - 1, nowDay, 0, 0, 0, 0);
	theNights = nights * (1000 * 60 * 60 * 24);
	departDate.setTime(arriveDate.getTime() + theNights);
	
	thenYear = departDate.getFullYear();
	thenMonth = departDate.getMonth() + 1;
	thenDay = departDate.getDate();
		
	document.reservation.up_depart_year.selectedIndex = thenYear - currentDate.getFullYear() + 1;
	document.reservation.up_depart_month.selectedIndex = thenMonth - 1; // months 0-11
	document.reservation.up_depart_day.selectedIndex = thenDay - 1;
	updateCalc();
		
}

function updateCalc() {
	nowYear = document.reservation.up_arrive_year.value
	nowMonth = document.reservation.up_arrive_month.value
	nowDay = document.reservation.up_arrive_day.value
	thenYear = document.reservation.up_depart_year.value
	thenMonth = document.reservation.up_depart_month.value
	thenDay = document.reservation.up_depart_day.value
	//alert(nowYear + "-" + nowMonth + "-" + nowDay);
	//alert(thenYear + "-" + thenMonth + "-" + thenDay);	
	currentDate = new Date();
	currentDate = new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate(),0,0,0,0) ;
	arriveDate = new Date(nowYear, nowMonth - 1, nowDay, 0, 0, 0, 0);
	//alert(arriveDate.toLocaleString());
	// don't allow dates prior to today - only for customer entry
	//if (arriveDate < currentDate) arriveDate = new Date(currentDate.getFullYear() + 1, nowMonth - 1, nowDay, 0, 0, 0, 0);
	departDate = new Date(thenYear, thenMonth - 1, thenDay, 0, 0, 0, 0);
	//alert(departDate.toLocaleString());
	
	lengthOfStay = Math.round((departDate.getTime() - arriveDate.getTime())/(1000 * 60 * 60 * 24));
	//alert(lengthOfStay);
	
	lengthOfAdvance = Math.round((arriveDate.getTime() - currentDate.getTime())/(1000 * 60 * 60 * 24));

	
	if (lengthOfStay < 2) {
		twoDays = 2 * (1000 * 60 * 60 * 24);
		departDate.setTime(arriveDate.getTime() + twoDays);
		
		// alert(departDate.toLocaleString());		
		// alert(thenYear + "-" + thenMonth + "-" + thenDay);
		
		lengthOfStay = 2;
	}

	//alert("checkminstay");
	// get the house minimumStay for this reservation (houses have minimum stays per month)
	resMinStay = checkMinStay(arriveDate, departDate);

	// check for intersecting special event minimum stay
	if (numBeds <= 3) specialEventMinStay = checkSpecialEvents(arriveDate, departDate, "min_stay");
	else if (numBeds == 4) specialEventMinStay = checkSpecialEvents(arriveDate, departDate, "min_stay2");
	else if (numBeds > 4) specialEventMinStay = checkSpecialEvents(arriveDate, departDate, "min_stay3");
	if (specialEventMinStay > resMinStay) resMinStay = specialEventMinStay;

	if (lengthOfStay < resMinStay) {
		minDays = resMinStay * (1000 * 60 * 60 * 24);
		departDate.setTime(arriveDate.getTime() + minDays);
		
		// alert(departDate.toLocaleString());		
		// alert(thenYear + "-" + thenMonth + "-" + thenDay);
		
		lengthOfStay = resMinStay;
	}

	document.reservation.up_stay_nights.value = lengthOfStay;

	// convert back from date format to correct for invalid dates such as Feb 31
	nowYear = arriveDate.getFullYear();
	nowMonth = arriveDate.getMonth() + 1;
	nowDay = arriveDate.getDate();
	
	thenYear = departDate.getFullYear();
	thenMonth = departDate.getMonth() + 1;
	thenDay = departDate.getDate();
	
	document.reservation.up_arrive_year.selectedIndex = nowYear - currentDate.getFullYear() + 1;
	document.reservation.up_arrive_month.selectedIndex = nowMonth - 1; // months 0-11
	document.reservation.up_arrive_day.selectedIndex = nowDay - 1;
	
	document.reservation.up_depart_year.selectedIndex = thenYear - currentDate.getFullYear() + 1;
	document.reservation.up_depart_month.selectedIndex = thenMonth - 1; // months 0-11
	document.reservation.up_depart_day.selectedIndex = thenDay - 1;

	// NEW determine the summer/winter rate period
	arriveYear = arriveDate.getFullYear(); // refererence point start year
	summerStart = new Date(arriveYear, summerMo - 1, summerDy, 0,0,0,0);
	
	if (winterMo < summerMo) endYear = arriveYear + 1;
	else endYear = arriveYear;
	
	summerEnd = new Date(endYear, winterMo - 1, winterDy - 1, 0,0,0,0);
	
	// check for only the arrive date of the res to determine if in summer or winter
	if (summerStart <= arriveDate && summerEnd >= arriveDate) ratePeriod = "summer";
	else ratePeriod = "winter";	
	
	// calculate the rent
	if (ratePeriod == "summer") {
		// staying less than a week
		if (lengthOfStay < 7) up_rent = lengthOfStay * summer_nt;
		// staying more than a week, but less than a month
		else if (lengthOfStay >= 7 && lengthOfStay < 30) up_rent = lengthOfStay * (summer_wk / 7.0);
		// staying longer than a month
		else up_rent = lengthOfStay * (summer_mo / 30.0);
	} else { // winter
		// staying less than a week
		if (lengthOfStay < 7) up_rent = lengthOfStay * winter_nt;
		// staying more than a week, but less than a month
		else if (lengthOfStay >= 7 && lengthOfStay < 30) up_rent = lengthOfStay * (winter_wk / 7.0);
		// staying longer than a month
		else up_rent = lengthOfStay * (winter_mo / 30.0);
	}


		
	// check for special event pricing
	priceAdjustment = 100; // 100 percent
	earlyLateFee = fee_early_late;
	if (lengthOfStay < 30) {
		specialEventPricing = checkSpecialEvents(arriveDate, departDate, "pricing");
		if (specialEventPricing != 0) {
			priceAdjustment = specialEventPricing;
			earlyLateFee = fee_early_late_special;
		}
	}
	specialEventName = checkSpecialEvents(arriveDate, departDate, "event");
	if (specialEventName == 0) 	specialEventName = "None";

	eventString = "Min Stay: " + resMinStay + ", Event: " + specialEventName + ", Event Min: " + specialEventMinStay + ", Price: " + priceAdjustment + "%";
	document.getElementById('special_event').firstChild.nodeValue = eventString;
	
	// indicate early - late fees
	document.getElementById('show_early').innerHTML = earlyLateFee;
	document.getElementById('show_late').innerHTML = earlyLateFee;

	if (document.reservation.up_event_pricing_ch.value == "yes") up_rent = up_rent * priceAdjustment/100;
	
	if (document.reservation.up_guest_type.value == "owner" || document.reservation.up_guest_type.value == "maintenance") {
		document.reservation.up_custom_pricing.checked = true;
		document.reservation.up_rent.value = formatAsMoney(0);
		document.reservation.up_city_tax.value = formatAsMoney(0);
		document.reservation.up_fee_cleaning.value = formatAsMoney(0);
		document.reservation.up_fee_sec_dep.value = formatAsMoney(0);
		document.reservation.up_fee_peace_waiver.value = formatAsMoney(0);
		document.reservation.up_fee_pet.value = formatAsMoney(0);
		document.reservation.up_fee_arr_early.value = formatAsMoney(0);
		document.reservation.up_fee_dep_late.value = formatAsMoney(0);
		document.reservation.up_fee_pool.value = formatAsMoney(0);
		document.reservation.up_fee_processing.value = formatAsMoney(0);
	} else if (!document.reservation.up_custom_pricing.checked) {
		up_rent = Math.round(up_rent * 100)/100;
		document.reservation.up_rent.value = formatAsMoney(up_rent);
	
		// calculate lodging tax	
		if (lengthOfStay < 30) {
			total_rent = up_rent;
			if (document.reservation.up_arr_early.checked) total_rent += earlyLateFee;
			if (document.reservation.up_dep_late.checked) total_rent += earlyLateFee;
			up_city_tax = city_tax_rate * total_rent;
			up_city_tax = Math.round(up_city_tax * 100)/100;  // round to 2 decimal places
		} else up_city_tax = 0;
		document.reservation.up_city_tax.value = formatAsMoney(up_city_tax);
				
		// calculate cleaning fee
		document.reservation.up_fee_cleaning.value = formatAsMoney(fee_cleaning);
	
		// calculate security deposit & peace of mind damage waiver fee
		if (document.reservation.up_peace_waiver_ch.value == "yes") {
			document.reservation.up_fee_sec_dep.value = formatAsMoney(0);
			document.reservation.up_fee_peace_waiver.value = formatAsMoney(fee_peace_waiver);
		} else {
			document.reservation.up_fee_sec_dep.value = formatAsMoney(fee_sec_dep);
			document.reservation.up_fee_peace_waiver.value = formatAsMoney(0);
		}
			
	
		// calculate pet fee
		if (document.reservation.up_num_pets.selectedIndex > 0) document.reservation.up_fee_pet.value = formatAsMoney(75*document.reservation.up_num_pets.selectedIndex);
		else document.reservation.up_fee_pet.value = formatAsMoney(0);
		
		// calculate early/late arrival/departure
		if (document.reservation.up_arr_early.checked) document.reservation.up_fee_arr_early.value = formatAsMoney(earlyLateFee);
		else document.reservation.up_fee_arr_early.value = formatAsMoney(0);
	
		if (document.reservation.up_dep_late.checked) document.reservation.up_fee_dep_late.value = formatAsMoney(earlyLateFee);
		else document.reservation.up_fee_dep_late.value = formatAsMoney(0);
		
		// calculate pool charges	
		if (document.reservation.up_pool_heat.checked) {
			if (lengthOfStay < 7) { // staying less than a week
				up_fee_pool = lengthOfStay * fee_pool_day;
			} else if (lengthOfStay >= 7 && lengthOfStay < 30) { // staying more than a week, but less than a month
				up_fee_pool = lengthOfStay * (fee_pool_week / 7.0);
			} else up_fee_pool = 0; // staying longer than a month, no pool feel, will be charged for utilties
			
			up_fee_pool = Math.round(up_fee_pool * 100)/100; // round to 2 decimal places
		} else up_fee_pool = 0.00;
		document.reservation.up_fee_pool.value = formatAsMoney(up_fee_pool);
		
		// calculate t-shirt
		//if (document.reservation.up_tshirt.checked) document.reservation.up_fee_tshirt.value = formatAsMoney(15);
		//else document.reservation.up_fee_tshirt.value = formatAsMoney(0);			

 	}
	
	updateTotals();
}
		
function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}

