<!--
function highlightThis(id, status, action)
{
	if ((action == "update") || ((action == "reserve") && (status == 0) ))
	{
		id.style.backgroundColor = "#CCCCCC";
		document.body.style.cursor = "hand";
	}
}

function unhighlightThis(id, status, action)
{
	if ((action == "update") || ((action == "reserve") && (status == 0) ))
	{
		if (status == 0) { id.style.backgroundColor = "#FFFFFF"; } //vacant
		else if (status == 1) { id.style.backgroundColor = "#E0B0FF"; } //occupied
		else if (status == 2) { id.style.backgroundColor = "#B7FFFF"; } //reserved
	}
	document.body.style.cursor = "default";
}

function clickThis(unitNumber,unitSize,unitStatus,action)
{
	//alert("unit:" + unitNumber + " - size:" + unitSize + " - status:" + unitStatus);
	if (action == "reserve") { reserveUnit(unitNumber, unitSize); }
	else if (action == "update") { updateUnitListing(unitNumber, unitSize, unitStatus); }
	//'$unit','$size','status','$action'
}

function reserveUnit(unitNumber, unitSize)
{
	var f = document.reservation;
	var cost = 0.00;
	f.unitSize.value = unitSize;
	f.unitNumber.value = unitNumber;
	
	if (unitSize == '10x5' ) cost = 35.00;
	else if (unitSize == '10x10' ) cost = 45.00;
	else if (unitSize == '10x15' ) cost = 55.00;
	else if (unitSize == '10x20' ) cost = 65.00;
	
	f.performance.value = MakeCents(cost * 100);
	getRates(unitSize, cost);
	var total = (parseFloat(f.performance.value) + parseFloat(f.firstMonthRent.value) + parseFloat(f.prorate.value));
	f.total.value = MakeCents(Math.round(total * 100))
}

function updateUnitListing(unitNumber, unitSize, unitStatus )
{
	var f = document.updateform;
	f.unitStatus.disabled = false;
	f.unitSize.value = unitSize;
	f.unitNumber.value = unitNumber;
	f.unitStatus.value = unitStatus;
}

function getRates(unitSize, firstMonthRent)
{
	var f = document.reservation;
	var today = new Date();
	var date = today.getDate();
	var month = today.getMonth();
	var year = today.getFullYear();
	var daysInMonth = 32 - new Date(year,month,32).getDate();
	var daysLeftInMonth = daysInMonth - date;
	var prorateFee = 0.00;
	var ratio = 0.00;
	
	var rent = MakeCents(firstMonthRent * 100);
	var ratio = MakeCents(daysLeftInMonth * 100 / daysInMonth * 1000)/1000;	
	
	prorateFee = MakeCents(Math.round(rent * ratio * 100));

	if (date <= 10) {
		f.firstMonthRent.value = prorateFee;
		f.prorate.value = MakeCents(0.00);
	} else {
		f.firstMonthRent.value = rent;
		f.prorate.value = prorateFee;
	}
}

function MakeCents(value) {
	var valueWithCorrectDecimalPosition = value / 100;
	return FormatFractionalNumber(valueWithCorrectDecimalPosition, 2);
}   

function FormatFractionalNumber(num, decimalPlaces) {
	var negativeSign = "";
	if (num < 0) {
		num = -num; // To keep the code simple, we will work with a positive number, and add a negative sign back in when returning a value
		negativeSign = "-";
	}
	var stringNum = num.toString();
	if (stringNum.indexOf(".") >= 0) { // If the string version of the number contains a period
		var digitsBeforeDot = stringNum.indexOf(".");
		var digitsAfterDot = stringNum.length - digitsBeforeDot - 1;
		if (digitsAfterDot == decimalPlaces) {
			return negativeSign + stringNum;
		} else if (digitsAfterDot < decimalPlaces) {
			for (var i = digitsAfterDot; i < decimalPlaces; i++) {
				stringNum += "0";
			}
			return negativeSign + stringNum;
		} else if (digitsAfterDot > decimalPlaces) {
			var shiftValue = 1;
			for (var i = 0; i < decimalPlaces; i++) {
				shiftValue = shiftValue * 10;
			}
			var correctLengthNumWithNoDecimal = Math.round(num * shiftValue); 
			// The actual digits of the final number, without the decimal in place
			var noDecimalStringNum = correctLengthNumWithNoDecimal.toString(); 
			// String version of correctLengthNumWithNoDecimal
			var leadingZero = "";
			var dotPos = digitsBeforeDot;
			if (num < 1) {
				leadingZero = "0" 
				// Leading zeros are removed from correctLengthNumWithNoDecimal. When the number 
				//is less than 1, a leading zero has to be added back.
				dotPos = 0; 
				// digitsBeforeDot (which dotPos is based on) counts a single leading zero before 
				//the decimal point as 1, but since leading zeros are removed, we have to offset the dot position.
			}
			if (decimalPlaces == 0) {
				return negativeSign + leadingZero + noDecimalStringNum.substring(0, dotPos);
			} else {
				return negativeSign + leadingZero + noDecimalStringNum.substring(0, dotPos) + "." + noDecimalStringNum.substring(dotPos, noDecimalStringNum.length);
			}
		}
	} else { // If there is no period in the number
		if (decimalPlaces > 0) {
			stringNum += ".";
			for (var i = 1; i <= decimalPlaces; i++) {
				stringNum += "0";
			}
			return negativeSign + stringNum;
		} else {
			return negativeSign + stringNum;
		}
	}
}

function isToday() {
	
	var today = new Date();
	var f = document.reservation;
	var ftoday = padout(today.getDate()) + '/' + padout((today.getMonth() + 1)) + '/' + today.getYear();
	f.moveindate.value = ftoday;
}

function padout(number) { return (number < 10) ? '0' + number : number; }


// -->
