
// SAVE FOR DEBUGGING
//var jSys = java.lang.System;
//jSys.err.println( ": " +  );

//----------------------------------------------------------------------------------------
// "Internal" function to return the decoded value of a cookie
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//----------------------------------------------------------------------------------------
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//----------------------------------------------------------------------------------------
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}
//----------------------------------------------------------------------------------------
function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
//----------------------------------------------------------------------------------------
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
//----------------------------------------------------------------------------------------
//Global function read everytime page loads - sets cookie expiration date
var expdate = new Date ();
FixCookieDate (expdate); // Correct for Mac date bug - call only once for given Date object!
expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 365 * 1000)); // 1 year from now

//----------------------------------------------------------------------------------------
//records first name as a cookie when users presses submit
function recordName() {

	// first retrieve user input values
	fName = document.form.firstName.value;

	if (fName != document.form.firstName.defaultValue)
		SetCookie("aliiFirstName", fName, expdate, "/");

}


/*****************************************************************************************
PROGRAMMED BY DAVID REGIER

NOTE: to update calendar for new year, delete both HTML option tags for past year and change the constant "THIS_YEAR" to the new year.

The following capitilized variables are treated as constants in this program and all future price/tax/rate/etc. changes are done here & only here! This allows for program to easily adapt to future changes with very little effort. Naturally, program s/b throughly re-tested after any changes are made.

*****************************************************************************************/
//LEAP YEAR EXCEPTION CODE
// LEAP YEARS ARE EVERY 4 YEARS, NEXT ONE IN 2004
var leapYear = false;

var LO_DAILY_RATE = 430;
var LO_FIFTEEN_DAY_RATE = 390;

var HI_DAILY_RATE = 460;
var HI_FIFTEEN_DAY_RATE = 410;

var HOLIDAY_RATE = 600;

var TAX_RATE = 0.1225;

// This restriction imposed due to having a normal year fee schedule and a leap year fee schedule.
var MAX_VISIT = 58;

//compare user input dates against today's date & to set up select list to current date.
currentDate = new Date();
var theCurMonth = currentDate.getMonth();
theCurMonth = theCurMonth + 1;

// IMPORTANT: returns 98 for 1998, 99 for 1999 but in year 2000 it returns 2000 on 180 Mhz Gateway! On new 450 Mhz year 2000 it returns 100

var unForYear = "" + currentDate.getYear();
var theCurYear = currentDate.getFullYear() - 2000;

var theCurDay = currentDate.getDate();

//------------------------------------------------------------------------------------
//this is main function called when user presses "check cost"

function startCalc() {

	if( !cb ) {
		alert('We have detected that you are running older Browser Software that is not capable of running our automated rental calculator. You will still use this inquiry form to contact us, but you will need to do the math yourself.\n\nP.S. Our automated rental calculator runs on Netscape 3 and higher or Microsoft Internet Explorer 4 and higher.');
		return;
	}

	//read user arrive & depart dates
	arriveMonth = document.form.amonth.selectedIndex + 1;
	arriveDay = document.form.aday.selectedIndex + 1;
	arriveYear = document.form.ayear.selectedIndex;

	departureMonth = document.form.dmonth.selectedIndex + 1;
	departureDay = document.form.dday.selectedIndex + 1;
	departureYear = document.form.dyear.selectedIndex;

	var arriveYearValue = parseInt( document.form.ayear[ arriveYear ].text );
	var departureYearValue = parseInt( document.form.dyear[ departureYear ].text );

	// LEAP YEAR DETECTION CODE
	if( arriveYearValue % 4 != 0 && departureYearValue % 4 != 0 ) leapYear = false;
	else if( arriveYearValue % 400 == 0 || departureYearValue % 400 == 0 ) leapYear = true;
	else if ( arriveYearValue % 100 == 0 && departureYearValue % 100 == 0 ) leapYear = false;
	else leapYear = true;

//alert(leapYear);
	var z = accumDays(arriveMonth-1, arriveMonth);  // function call for accum days prior to arriveMonth
	var q = accumYears(arriveYear);  // function call for accum days prior to arriveYear
	//theArrivalDay is absolute number of days accumulated prior to & including arrival day
	var theArrivalDay = arriveDay + z + q;

	z = accumDays(departureMonth-1, departureMonth); //function call for accum days prior to departureMonth
	q = accumYears(departureYear);  // function call for accum days prior to departureYear
	//theDepartureDay is absolute number of days accumulated prior to & including departure day
	var theDepartureDay = departureDay + z + q

	m = accumDays(theCurMonth-1, theCurMonth); //function call for accum days prior to cur mon
	n = accumYears(theCurYear); //function call for accum days prior to cur year

	checkDates( theDepartureDay, theArrivalDay );
}
//------------------------------------------------------------------------------------

function checkDates( theDepartureDay, theArrivalDay ) {

	//nDays is total length of stay
	var nDays = theDepartureDay - theArrivalDay;
//alert( nDays );
	// Gives string names to arrive/depart months - allows for alerts using months by names.
	var monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

	for ( i = 0; i < 12; i++ ) {
		if( arriveMonth - 1 == i )	var arrivalMon = monthNames[i];
		if( departureMonth - 1 == i ) var departMon = monthNames[i];
	}


	// This following set of nested if-else statements roots out bogus dates, unacceptable short stays, etc

	// passes arrival Month to verifyDate function which returns max days of that month. User input arrive day is then compared with days allowed.
	if (arriveDay > verifyDate(arriveMonth)) {
		alert('You mistakenly indicated that you are arriving on ' + arrivalMon + ' ' + arriveDay + '.\n\n' + arrivalMon + ' has only ' + verifyDate(arriveMonth) + ' days!')
		clearFields();
	}

	// passes departure Month to verifyDate function which returns max days of that month. User input departure day is then compared with days allowed.
	else if (departureDay > verifyDate(departureMonth)) {
		alert('You mistakenly indicated that you are departing on ' + departMon + ' ' + departureDay + '.\n\n' + departMon + ' has only ' + verifyDate(departureMonth) + ' days!')
		clearFields();
	}

	else if (nDays < 0) {
		alert('You have mistakenly indicated that you are arriving on ' + arrivalMon + ' ' + arriveDay + ', ' + arriveYear + ' and that you are departing on ' + departMon + ' ' + departureDay + ', ' + eval(departureYear) + '.\n\n Sorry, that\'s not possible on planet earth!');
		clearFields();
	}

	else if (nDays == 0) {
		alert('You mistakenly indicated that you are arriving & departing on the same day.\n\nPlease make another request!');
		clearFields();
	}

	else if (nDays < 6) {
		alert('You requested only a ' + nDays + ' night visit.\n\nI\'m sorry, our minium stay is 6 nights');
		clearFields();
	}

	else if ( nDays > MAX_VISIT ) {
		alert('You requested a ' + nDays + ' night visit! Sorry, it is beyond the scope of this rental calculator to determine the cost for visits in excess of ' + MAX_VISIT + ' nights\n\nMake sure you contact us for your rate.');
		clearFields();
		displayNight(nDays, theArrivalDay, theDepartureDay);
	}

	else calcPrice( theArrivalDay, theDepartureDay );
}
//------------------------------------------------------------------------------------
//this function writes alternate display into text area with only nights tallied

function displayNight(stay, nArrivalDays, nTodayDays) {

	var Night = 'VISIT:    ' + stay + ' nights\n\nSUBTOTAL: Contact us\nTAX:      Contact us\nTOTAL:    Contact us';
	document.form.display.value = Night;

}
//------------------------------------------------------------------------------------

function calcPrice( theArrivalDay, theDepartureDay ) {

	document.form.display.value = 'Analyzing...';

	var nDays = theDepartureDay - theArrivalDay;

	// returns array containing prices for nights requested
	var dailyFee;

	var dailyFeeLength;

	if( !leapYear )
		dailyFee = getAnnualFeeSchedule( theDepartureDay, theArrivalDay, nDays );
	else
		dailyFee = getAnnualLeapYearFeeSchedule( theDepartureDay, theArrivalDay, nDays );

	var subTotal = 0;
	var feeSchedule = new Array( nDays );
	var skew = theArrivalDay - 1;

	for( i = theArrivalDay - 1; i < theDepartureDay - 1; i++ ) {
		var k = i;
		// while prevents reading out of bounds on feeSchedule array
		while( k >= dailyFee.length ) k -= dailyFee.length;
		feeSchedule[ i - skew ] = dailyFee[ k ];
		subTotal += dailyFee[ k ];
	}

	var nLowDays = 0;
	var lowRent = 0;
	var nHiDays = 0;
	var hiRent = 0;
	var nHolidays = 0;
	var holidayRent = 0;

	// Step thru array slice & extract nDays in each period & associated rent for that period
	for( i = 0; i < feeSchedule.length; i++ ) {

		if( feeSchedule[i] == LO_DAILY_RATE || feeSchedule[i] == LO_FIFTEEN_DAY_RATE ) {
			nLowDays++;
			lowRent = feeSchedule[i];
		}
		else if( feeSchedule[i] == HI_DAILY_RATE || feeSchedule[i] == HI_FIFTEEN_DAY_RATE ) {
			nHiDays++;
			hiRent = feeSchedule[i];
		}
		else if( feeSchedule[i] == HOLIDAY_RATE ) {
			nHolidays++;
			holidayRent = feeSchedule[i];
		}
	}

	var fSubTotal = formatMoney( subTotal );
	var fTax = formatMoney( subTotal * TAX_RATE );
	var fTotal = formatMoney( subTotal * TAX_RATE + subTotal );

	var lowDailyRent = "";
	var hiDailyRent = "";
	var holidayDailyRent = "";

	if( lowRent > 0 ) lowDailyRent = " @ $" + lowRent + "/night";
	if( hiRent > 0 ) hiDailyRent = " @ $" + hiRent + "/night";
	if( holidayRent > 0 ) holidayDailyRent = " @ $" + holidayRent + "/night";

	document.form.display.value =
		'VISIT:       ' + nDays + ' nights\n' +
		' Lo Season - ' + nLowDays + ' night(s)' + lowDailyRent + '\n' +
		' Hi Season - ' + nHiDays + ' night(s)' + hiDailyRent + '\n' +
		' Holiday   - ' + nHolidays + ' night(s)' + holidayDailyRent + '\n\n' +
		'SUBTOTAL:   $' + fSubTotal + '\n' +
		'TAX:        $' + fTax + '\n' +
		'TOTAL:      $' + fTotal;

}
//------------------------------------------------------------------------------------
// Dynamically generates, populates & then returns a 365 member array containing prices...
// This is a normal year array; not the leap year array (366 member array)

function getAnnualFeeSchedule( theDepartureDay, theArrivalDay, nDays ) {

	//  Normal Year
	//  1/31 => 31
	//  2/28 => 59
	//  3/31 => 90
	//  4/30 => 120
	//  5/31 => 151
	//  6/30 => 181
	//  7/31 => 212
	//  8/31 => 243
	//  9/30 => 273
	// 10/31 => 304
	// 11/30 => 334
	// 12/31 => 365

	var dailyFee = new Array( 365 );

	// dynamically populate "annual price" array
	for( i = 0; i < 365; i++ ) {

		// Jan 1 to and including Jan 4; holiday
		if( i < 4 ) {
			dailyFee[i] = HOLIDAY_RATE;
		}

		// Jan 5 to and including Apr 15; high
		else if( i >= 4 && i < 105 ) {
			if( nDays < 15 ) dailyFee[i] = HI_DAILY_RATE;
			else dailyFee[i] = HI_FIFTEEN_DAY_RATE;
		}

		// Apr 16 to and including Jun 1; low
		else if( i >= 105 && i < 151 ) {
			if( nDays < 15 ) dailyFee[i] = LO_DAILY_RATE;
			else dailyFee[i] = LO_FIFTEEN_DAY_RATE;
		}

		// Jun 2 to and including Aug 31; high
		else if( i >= 151 && i < 243 ) {
			if( nDays < 15 ) dailyFee[i] = HI_DAILY_RATE;
			else dailyFee[i] = HI_FIFTEEN_DAY_RATE;
		}

		// Sep 1 to and including Dec 19; low
		else if( i >= 243 && i < 353 ) {
			if( nDays < 15 ) dailyFee[i] = LO_DAILY_RATE;
			else dailyFee[i] = LO_FIFTEEN_DAY_RATE;
		}

		// Dec 20 to and including Dec 31; holiday
		else {
			dailyFee[i] = HOLIDAY_RATE;
		}
	}

	return dailyFee;
}
//------------------------------------------------------------------------------------
// Dynamically generates, populates & then returns a 366 member array containing prices...
// This is a leap year array; not the normal 365 member array

function getAnnualLeapYearFeeSchedule( theDepartureDay, theArrivalDay, nDays ) {

	//  Leap Year
	//  1/31 => 31
	//  2/29 => 60
	//  3/31 => 91
	//  4/30 => 121
	//  5/31 => 152
	//  6/30 => 182
	//  7/31 => 213
	//  8/31 => 244
	//  9/30 => 274
	// 10/31 => 305
	// 11/30 => 335
	// 12/31 => 366

	var dailyFee = new Array( 366 );

	// dynamically populate "annual price" array
	for( i = 0; i < 366; i++ ) {

		// Jan 1 to and including Jan 4; holiday
		if( i < 4 ) {
			dailyFee[i] = HOLIDAY_RATE;
		}

		// Jan 5 to and including Apr 15; high
		else if( i >= 4 && i < 106 ) {
			if( nDays < 15 ) dailyFee[i] = HI_DAILY_RATE;
			else dailyFee[i] = HI_FIFTEEN_DAY_RATE;
		}

		// Apr 16 to and including Jun 1; low
		else if( i >= 106 && i < 152 ) {
			if( nDays < 15 ) dailyFee[i] = LO_DAILY_RATE;
			else dailyFee[i] = LO_FIFTEEN_DAY_RATE;
		}

		// Jun 2 to and including Aug 31; high
		else if( i >= 152 && i < 244 ) {
			if( nDays < 15 ) dailyFee[i] = HI_DAILY_RATE;
			else dailyFee[i] = HI_FIFTEEN_DAY_RATE;
		}

		// Sep 1 to and including Dec 19; low
		else if( i >= 244 && i < 354 ) {
			if( nDays < 15 ) dailyFee[i] = LO_DAILY_RATE;
			else dailyFee[i] = LO_FIFTEEN_DAY_RATE;
		}

		// Dec 20 to and including Dec 31; holiday
		else {
			dailyFee[i] = HOLIDAY_RATE;
		}
	}

	return dailyFee;
}
//------------------------------------------------------------------------------------
// Determines accumulated day in prior months returns value

function accumDays(c, month) {

	var x = 0;
	var z = 0;

	// Accumulates total number of days in prior months
	for ( i = 1; i < month; i++ ) {
		if ( c == 4 || c == 6 || c == 9 || c == 11 ) x = 30;
		else if( c == 2) {
			if( leapYear ) x = 29;
			else x = 28;
		}
     else x = 31;
		 c -= 1;
		 z += x;
	 }
	return z;
}
//------------------------------------------------------------------------------------
// Accumulates total number of days in prior years & returns value

function accumYears(Year) {

	var p = 365;
	if( leapYear ) p = 366;
	var q = 0;

	// Accumulate total number of days in prior years
	for ( i = 1; i <= Year; i++ )
		q += p;

	return q;
}

//------------------------------------------------------------------------------------
// sets selected dates automatically to today's date

function setDates() {

	if( !cb ) return;

	document.form.aday.selectedIndex = theCurDay - 1;
	document.form.dday.selectedIndex = theCurDay - 1;

	document.form.amonth.selectedIndex = theCurMonth - 1;
	document.form.dmonth.selectedIndex = theCurMonth - 1;

	document.form.ayear.selectedIndex = theCurYear;
	document.form.dyear.selectedIndex = theCurYear;

}

//------------------------------------------------------------------------------------
function clearFields() {
		document.form.display.value = 'Press "Check Cost" and the cost\nof your trip will be displayed here\n\nPowered by "Java"';
		window.status='';
}
//------------------------------------------------------------------------------------
function verifyDate(c) {
	var x = 0;
	if ( c == 4 || c == 6 || c == 9 || c == 11 ) x = 30;
	else if( c == 2 ) {
		if( leapYear ) x = 29;
		else x = 28;
	}
	else x = 31;
	return x;
}
//------------------------------------------------------------------------------------
function formatMoney(theNumber) {
var newNum = "" + theNumber;
var theLength = newNum.length;
var theDecLoc = newNum.lastIndexOf(".");

	if (theDecLoc == -1) {
		newNum = newNum + ".00";
		return newNum;

}
	if (theDecLoc != -1) {
		var theDollars = newNum.substring(0,theDecLoc);
		var theCents = newNum.substring(theDecLoc,theLength);
		var theCents = "" + Math.round(theCents * 100);
		//if statment fixes bug when theCents = 100
		if (theCents == 100) {
			theDollars = parseFloat(theDollars); //theDollars from string into number
			theDollars += 1;
			newNum = theDollars + ".00";
			return newNum;
			}
		else {
		var newCents = (theCents.length == 1) ? "0" + theCents: theCents;
		newNum = theDollars + "." + newCents;
		return newNum;
		}
	}
}
//---------------------------------------------------------------------------------------
function disableField(field) {
		field.blur();
		self.status = "Programmed by David Regier";
}