var reservationFormWidth = 680;

// preload the on-states of images we'll use
var reservationImagePaths = new Array();
// buttons
reservationImagePaths.push('images/buttons/continue_on.gif');
reservationImagePaths.push('images/buttons/start_over_on.gif');
reservationImagePaths.push('images/buttons/get_quote_on.gif');
reservationImagePaths.push('images/buttons/back_on.gif');
reservationImagePaths.push('images/buttons/no_thanks_on.gif');
reservationImagePaths.push('images/buttons/yes_continue_on.gif');
reservationImagePaths.push('images/buttons/confirm_on.gif');

// tooltip slip images
reservationImagePaths.push('images/announcement/announcement_t.png');
reservationImagePaths.push('images/announcement/announcement_tl.png');
reservationImagePaths.push('images/announcement/announcement_tr.png');
reservationImagePaths.push('images/announcement/announcement_b.png');
reservationImagePaths.push('images/announcement/announcement_bl.png');
reservationImagePaths.push('images/announcement/announcement_br.png');
reservationImagePaths.push('images/announcement/announcement_l.png');
reservationImagePaths.push('images/announcement/announcement_r.png');

var reservationImages = new Array();
for (var i = 0; i < reservationImagePaths.length; i++) {
	var nextImage = new Image();
	nextImage.src = reservationImagePaths[i];
	reservationImages.push(nextImage);
}
// --------------- end preloading -------------------

function toggleReservationTooltip(element, text) {
	var reservationTooltip = document.getElementById('reservationTooltip');
	var reservationTooltipContents = document.getElementById('reservationTooltipContents');
	var iframeMask = document.getElementById('reservationTooltipIFrame');
	if (reservationTooltip && reservationTooltipContents) {
		// see if it's already shown
		if(reservationTooltip.style.display == 'block') {
			// hide the tooltip
			reservationTooltip.style.display = 'none';
			// and clear it's contents
			reservationTooltipContents.innerHTML = '';

			// for IE, just reset the iframe properties to a nice hidden state
			if (reservationTooltip.filters && iframeMask) {
				iframeMask.style.width = 0;
				iframeMask.style.height = 0;
			}
		} else {
			// set the tooltip contents
			reservationTooltipContents.innerHTML = text;
		
			// make it partially visible, so we can get a quick height reading
			reservationTooltip.style.visibility = 'hidden';
			reservationTooltip.style.display = 'block';
			// read the height
			var tooltipHeight = reservationTooltip.offsetHeight;
			var tooltipWidth = reservationTooltip.offsetWidth;
			// set it's display back to normal
			reservationTooltip.style.display = 'none';
			reservationTooltip.style.visibility = 'visible';
			
			// calculate where we'd naturally want to put it
			var positionTop = (element.offsetTop-(tooltipHeight/2));
			var positionLeft = (element.offsetLeft+element.offsetWidth);

			// make any adjustments needed, to keep it from running past the bounds of our form area
			var container = document.getElementById('reservationContainer');
			if (positionTop < 0) {
				positionTop = 0;
			} else if ((positionTop+tooltipHeight) > container.offsetHeight) {
				positionTop = (container.offsetHeight-tooltipHeight);
			}

			if (positionLeft+tooltipWidth > reservationFormWidth) {
				positionLeft = (element.offsetLeft-tooltipWidth);
			}

			// for IE, we'll need to position an iframe behind it, so drop-downs won't show through
			if(reservationTooltip.filters && iframeMask) {				
				iframeMask.style.top = positionTop;
				iframeMask.style.left = positionLeft;
				iframeMask.style.width = tooltipWidth+'px'; 
				iframeMask.style.height = tooltipHeight+'px'; 
				iframeMask.style.zIndex = reservationTooltip.style.zIndex - 1;
				iframeMask.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			}

			// position the tooltip
			reservationTooltip.style.top = positionTop+'px';
			reservationTooltip.style.left = positionLeft+'px';

			// and show it
			reservationTooltip.style.display = 'block';
		}
	}

}

/********* Used by the 'expirationDate' form field type *************/
function createExpirationDate(dateFieldID, monthFieldID, yearFieldID) {
	var dateField = document.getElementById(dateFieldID);
	var monthField = document.getElementById(monthFieldID);
	var yearField = document.getElementById(yearFieldID);

	if(dateField && monthField && yearField) {
		if(monthField.value != "" || yearField.value != "") {
			dateField.value = monthField.value + "/" + yearField.value;
		} else {
			dateField.value = "";
		}
	}
}	

/********* Used by the 'time' form field type *************/
function createTime(timeFieldID, hourFieldID, minuteFieldID, meridiemFieldID) {
	var timeField = document.getElementById(timeFieldID);
	var hourField = document.getElementById(hourFieldID);
	var minuteField = document.getElementById(minuteFieldID);
	var meridiemField = document.getElementById(meridiemFieldID);

	if(timeField && hourField && minuteField && meridiemField) {
		if(hourField.value != "" || minuteField.value != "" || meridiemField.value != "") {
			timeField.value = hourField.value + ":" + minuteField.value + " " + meridiemField.value;
		} else {
			timeField.value = "";
		}
		
	}
}

function iconBounceUp(elementID, bounceHeight) {
	var theElement = document.getElementById(elementID);
	if(theElement) {
		if(theElement.animation) {
			return;
		}

		theElement.animation = new Fx.Morph(theElement.id, {duration: 500, transition: Fx.Transitions.Quint.easeOut, onComplete: function(){ iconBounceDown(theElement, bounceHeight); } } );

		theElement.animation.start({'top': (theElement.offsetTop-bounceHeight)});
	}	
}

function iconBounceDown(theElement, bounceHeight) {
	if(theElement) {
		theElement.animation = new Fx.Morph(theElement.id, {duration: 500, transition: Fx.Transitions.Quint.easeIn, onComplete: function(){ theElement.animation = null; iconBounceUp(theElement.id, bounceHeight); } } );
		theElement.animation.start({'top': theElement.offsetTop+(bounceHeight)});
	}	
}

function stopIconBounce(elementID) {
	var theElement = document.getElementById(elementID);
	if(theElement && theElement.animation) {
		theElement.animation.cancel();
		theElement.animation = null;
		// hmm...to be safe, let's reset it back to it's normal, starting position
		theElement.style.top = '-5px'; 
		theElement.style.left = '-30px'; 
		//theElement.parentNode.removeChild(theElement);
	}
}

function scrubNonDigits(theElement, forceZero, allowDecimal) {
	if(allowDecimal) {
		theElement.value = theElement.value.replace(/[^\d.]/g, '');
	} else {
		theElement.value = theElement.value.replace(/[^\d]/g, '');
	}
	
	if(forceZero && (theElement.value == '' || theElement.value == null)) {
		theElement.value = '0';
	}
}

function popupReceipt() {
    // create the query string for the popup URL
	var receiptURL = "reservation_receipt.php";
	var newWindow = open(receiptURL, "ImagePopup", "width=680,height=620,scrollbars=yes,resizable=yes,toolbar=no,menubar=yes,titlebar=no,status=no,screenX=0,screenY=0");
}

function popupPolicies() {
    // create the query string for the popup URL
	var policiesURL = "policies.php";
	var newWindow = open(policiesURL, "PoliciesPopup", "width=680,height=500,scrollbars=yes,resizable=yes,toolbar=no,menubar=yes,titlebar=no,status=no,screenX=0,screenY=0");
}

