var isIE = navigator.appVersion.indexOf("MSIE") != -1;
var isSafari = navigator.appVersion.indexOf("WebKit") != -1;
var isIE6 = (document.documentElement && typeof document.documentElement.style.maxHeight!="undefined") ? false : true;
var isIE7 = (document.documentElement && typeof document.documentElement.style.maxHeight!="undefined") ? true : false;
var isMac = navigator.userAgent.indexOf("Macintosh") != -1;
var isWin = navigator.userAgent.indexOf("Windows") != -1;

var AgencyTaxesFeesBlurb = "You will be charged directly by the hotel for the amount shown. Additional taxes and fees if applicable may be charged at the time of check-out, including, without limitation, sales and use tax, occupancy tax, room tax, excise tax, value added tax and/or other similar taxes.";

function hideElements(scopeElement) {
	if (!scopeElement) {
		scopeElement = document.body;
	}
	$(scopeElement).select("div.show").each(function(element) {
		Element.removeClassName(element, 'show');
		Element.addClassName(element, 'hide');
	});
}

function showElements(showElement, scopeElement) {
	if (!scopeElement) {
		scopeElement = document;
	}
	hideElements(scopeElement);
	Element.removeClassName(showElement, 'hide');
	Element.addClassName(showElement, 'show');
}

function updateTabs(onTab, scopeId) {
	$(scopeId).select('li').each(function(tab) {
		Element.removeClassName(tab, 'current_tab');
	}); 
	Element.addClassName(onTab, 'current_tab');
}

function validateSearchCity(searchFieldId) {
	searchValue = $F(searchFieldId);
	if (searchValue.length == 0) {
		return true;
	}
  if (searchValue.length == 3 || searchValue.match(/\(\w{3}\)/)) {
    return true;
  } else {
    alert('Please select a city name from the auto complete drop down menu or enter a 3 letter airport code.');
    return false;
  }
}

function validateSearchDate(searchFieldId) {
	searchValue = $F(searchFieldId);
  if (!searchValue.match(/\d{1,2}\/\d{1,2}\/\d{4}/)) {
    alert('Please enter dates in the format mm/dd/yyyy.');
    return false;
  }
	return true;
}

function showHidePolicy(event, element) {
	var node = element.parentNode;
	Event.stop(event);
	$(node).select('div').each(function(div) {
		var currentClassName = div.className
		Element.removeClassName(div, (currentClassName == 'hide' ? 'hide' : 'show'));
		Element.addClassName(div, (currentClassName == 'hide' ? 'show' : 'hide'));
		if (currentClassName == 'hide') {
			node.style.listStyleImage = 'url(/images/delta/arrow-down_ltblue.gif)'
		} else {
			node.style.listStyleImage = 'url(/images/delta/arrow-right_ltblue.gif)'
		}
	}); 
}

function showHideCarTypes(event, element) {	
	Event.stop(event);
	if (element.innerHTML == 'Less Car Classes') {
		$('car_results_table_container').style.overflow = 'hidden';	
		element.innerHTML = 'More Car Classes';
		$('car_results_table_container').select('th.more_type_header').each(function(e) {
			Element.hide(e);
		});
		$('car_results_table_container').select('td.hideable_car').each(function(e) {
			Element.hide(e);
		});
		
	} else {
		$('car_results_table_container').scrollLeft = 0;
		$('car_results_table_container').style.overflow = 'auto';	
		element.innerHTML = 'Less Car Classes';
		$('car_results_table_container').select('th.more_type_header').each(function(e) {
			Element.show(e);
		});
		$('car_results_table_container').select('td.hideable_car').each(function(e) {
			Element.show(e);
		});
	}
}

function showHideCarVendors(event, element) {
	var node = element.parentNode;
	var currentClassName = node.className;
	Event.stop(event);
	if (currentClassName == 'more_link') {
		Element.show('more_car_companies');
		$('car_results_table_container').select('tr.hidden_supplier_cars').each(function(td) {
			Element.show(td);
		});
	} else {
		Element.hide('more_car_companies');
		$('car_results_table_container').select('tr.hidden_supplier_cars').each(function(td) {
			Element.hide(td);
		});
	}
	Element.removeClassName(node, (currentClassName == 'more_link' ? 'more_link' : 'less_link'));
	Element.addClassName(node, (currentClassName == 'more_link' ? 'less_link' : 'more_link'));
	element.innerHTML = (currentClassName == 'more_link' ? 'Less Car Companies' : 'More Car Companies');
}

function showLightbox(event, elementId, options) {	
	$(elementId).style.zIndex = 1002;
	var scrollTop = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
	$(elementId).style.top = scrollTop + 50 + "px";
	if (isIE) { // only necessary in IE
		var selectNodes = document.getElementsByTagName("select");
		for (var i=0; i < selectNodes.length; i++) {
			selectNodes[i].style.visibility = 'hidden';
		}
	}
	
	if (elementId == 'general_lightbox') {
		if (options && options.set_spinner) {
		  $('lightbox_h2').innerHTML = '';
		  $('lightbox_p').innerHTML = '<p class="center"><img src="/images/delta/spinner.gif" /></p>';
		} else {
		  $('lightbox_h2').innerHTML = '';
		  $('lightbox_p').innerHTML = '';
		}
	}
	
	Effect.Appear('lb_overlay', {duration: 0.5});
	Effect.Appear(elementId, {duration: 0.5}); 
	Event.stop(event); 
}

function hideLightbox(event, elementId, elementsToRemove) {
	if (isIE) { // only necessary in IE
		var selectNodes = document.getElementsByTagName("select");
		for (var i=0; i < selectNodes.length; i++) {
			selectNodes[i].style.visibility = 'visible';
		}
	}	
	if (elementsToRemove) {
    for (var i = 0; i < elementsToRemove.length; i++) {
       var ele = $(elementsToRemove[i]);
        if (ele.hasChildNodes()) {
          removeChildNodes(ele);
        }
     }
	} else {
    removeChildNodes($(elementId));
	}
	Effect.Fade(elementId, {duration: 0.2}); 
	Effect.Fade('lb_overlay', {duration: 0.25});
	Event.stop(event); 
}

function updatePhoto(event, callerObj, options) {  
  Event.stop(event);

  var whichImage = window.selectedImageIdx ? window.selectedImageIdx : 2;
  $('photo_count').innerHTML = whichImage;
  
  Element.show('next_link');

  if (whichImage.toString() == "1") {
	Element.hide('prev_link');
  } else if (whichImage >= window.imageCount) {
	Element.hide('next_link');
	Element.show('prev_link');	
  } else {
	Element.show('prev_link');	
  }
	
  if (whichImage > window.imageCount) {
    window.selectedImageIdx = 1;
    whichImage = 1;
  } else {
	if (options.nextLink) {
    	window.selectedImageIdx = whichImage + 1;
	} else {
    	window.selectedImageIdx = whichImage - 1;
	}
  }
  if (options.browseLink) {
    $('photo_detail').src = $(options.thumbnailId).src;
  } else if (options.nextLink || !options.nextLink) {
    $('photo_detail').src = $('tn' + whichImage).src;
  } else if (options.thumbLink) {
    $('photo_detail').src = Element.childElements(callerObj)[0].src;
  }
}

/* DOM Helpers */

function removeChildNodes(ele) {
  while (ele.childNodes.length >= 1 ) {
    ele.removeChild( ele.firstChild );   
  }
}

function isAllWhitespace(node)
{
  // Use ECMA-262 Edition 3 String and RegExp features
  return !(/[^\t\n\r ]/.test(node.data));
}

function isIgnorable(node)
{
  return ( node.nodeType == 8) || // A comment node
         ( (node.nodeType == 3) && isAllWhitespace(node) ); // a text node, all ws
}

function lastChild(node)
{
  var child = node.lastChild;
  while (child) {
    if (!isIgnorable(child)) return child;
    child = child.previousSibling;
  }
  return null;
}

function validateAdd(dspItemId) {
	
	bookingOptionsDiv = $('booking_options_'+dspItemId);
	hintEle = bookingOptionsDiv.select('p.hint').first();
	
	checkbox = bookingOptionsDiv.select('input.checkbox').first();
	
	if (hintEle.innerHTML == '' && !checkbox.checked) return true;
	
	check_the_box = false;
	if (hintEle.innerHTML != '') {
		check_the_box = true;
	}
	
	passedDate = true;
	$(bookingOptionsDiv).select('select.date').each(function(dateSelect) {
		if (dateSelect.selectedIndex == 0) {
			errorEle = dateSelect;
			passedDate = false;
		}
	});
	
	passedQuantity = false;
	$(bookingOptionsDiv).select('select.quantity').reverse().each(function(quantitySelect) {
		if (quantitySelect.selectedIndex > 0 || ( quantitySelect.selectedIndex == 0 && quantitySelect.length == 1 ) ) {
			passedQuantity = true;
		} else {
			errorEle = quantitySelect;
		}
	});
	
	if (passedDate && passedQuantity) {
		hintEle.innerHTML = '';
		hintEle.hide();
		if (check_the_box) checkbox.checked = true;
	} else {
		checkbox.checked = false;
		hint = 'Please select ';
		hintArray = new Array;
		if (!passedQuantity) {
			hintArray.push('a quantity');
		}
		if (!passedDate) {
			hintArray.push('a date');
		}
		hint += (hintArray.length == 1 ? hintArray[0] : hintArray[0]+' and '+hintArray[1]);
		hintEle.innerHTML = hint;
		hintEle.show();
		errorEle.focus();
	}	
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function showCarDetails(event, carTypeCode, isLeftJustified) {
	// isLeftJustified lets you know how much less space to use for the style.left property
  if (Element.visible(carTypeCode)) {
    return;
  }
  var anchor = Event.element(event);
  var xPos = findPosX(anchor);
  var yPos = findPosY(anchor);
  $(carTypeCode).style.top = yPos + "px";
  $(carTypeCode).style.left = Event.pointerX(event) - (isLeftJustified ? 100 : 200) + "px";
  window.carDetailPopupId = carTypeCode;
  Element.show(carTypeCode);
  Event.stop(event); 
  return false;
}

function showPolicies(event, shortDescription, cancellationPolicy, guaranteePolicy) {
	$('lb_rate_description').innerHTML = '<strong>Rate Description:</strong> ' + shortDescription;
	
	if (cancellationPolicy != '') {
		$('lb_cancellation_policy').innerHTML = '<strong>Cancellation Policy:</strong> ' + cancellationPolicy;
		$('lb_cancellation_policy').show();
	} else {
		$('lb_cancellation_policy').hide();
	}
	
	if (guaranteePolicy != '') {
		$('lb_guarantee_policy').innerHTML = guaranteePolicy;
		$('lb_guarantee_policy').show();
	} else {
		$('lb_guarantee_policy').hide();
	}
	
	showLightbox(event, 'policies_lightbox', true);
}

function chooseRadioNice(radioButtonsDivId, skipFocus) {
	
	$(radioButtonsDivId).select('input.radio').each(function(radioEle) {

		labelEle = radioEle.adjacent('label').first();
		
		textfieldArray = radioEle.adjacent('input.textfield');
		if (textfieldArray.length > 0) {
			textfieldEle = textfieldArray.first();
		} else {
			textfieldEle = null;
		}

		if (radioEle.checked) {
			labelEle.removeClassName('inactive');
			if (textfieldEle)	{
				if (!skipFocus) textfieldEle.focus();
			}
		} else {
			labelEle.addClassName('inactive');
			if (textfieldEle) {
				//textfieldEle.value = '';
			}
		}
	});

}

function showSpinner(obj) {
	obj.innerHTML = '<div style="margin-left:40%; margin-right:40%; padding:25px"><img src="/images/delta/spinner.gif" /></div>';	
}