var isNetscape = false; if(navigator.appName.indexOf("Netscape") >= 0) {isNetscape = true;}else {isNetscape = false;}

// Reference to the location picker IFRAME element. 
var _locationPickerIFrame = null; var _locationPickerWindow = null;

// Reference to the location text boxes the user enters text into.
var _pickupLocationTextBox = null; var _returnLocationTextBox = null; 
var _pickupLocationHiddenValue = null; var _returnLocationHiddenValue = null;		

// Reference to the selector hidden form elements to indicate if code or description was selected
var _pickupSelector = null; var _returnSelector = null;

// Save the last known search text for these pickup and return locations. We need
// this to identify if anything has changed.
var _lastPickupText = ""; var _lastReturnText = "";

// Indicates if the object references have been initialized.
var _referencesInitialized = false; var _locationCodeFoundInArray = false;

// Which location is the user picking for?
var _searchLocation = ""; var _PICKUP = "pickup";  var _RETURN = "return";

// URL of the location picker page.
var LOCATION_PICKER_URL; var _locFrameLeft; var _locFrameTop; var _locFrameWidth; var _locFrameHeight;

var theForm = (isNetscape)? document.forms["resForm"]:document.resForm;

var _selectorCulture = "";					// DJJ CA MERGE 2004203

if (_isHttps()){LOCATION_PICKER_URL = "https://" + _getUrlRoot() + "/includes/locationsHttps.aspx?clastic=t&Location=";}
		   else{LOCATION_PICKER_URL = "http://"  + _getUrlRoot() + "/includes/locations.aspx?clastic=t&Location=";}

// Set object references to HTML objects we will need to interact with.
function setLocationObjectReferences()
{	_returnLocationTextBox = theForm.res1_rloc;	
	_returnLocationHiddenValue = theForm.res1_rcode;
	_pickupLocationTextBox = theForm.res1_ploc;	
	_pickupLocationHiddenValue = theForm.res1_pcode;
	_pickupSelector = theForm.res1_pselector; 
	_returnSelector = theForm.res1_rselector;
	
	_selectorCulture = theForm.res1_cultureForLocationSelector;		// DJJ CA MERGE 2004203, use new selectorCulture var
	
	if(isNetscape){_locationPickerIFrame = document.getElementById('LocFrame');}else{ _locationPickerIFrame = document.getElementById('LocFrame')};
	getLocFrameDimensions();_referencesInitialized = true;
}

//Hide or close the iframe window
function closeLocFrame() {
if (_locationPickerWindow==null) {
	hideElement(_locationPickerIFrame);
}else{_locationPickerWindow.close();}
// Reset the initial values so we don't get triggered when the user moves off the field.
initLocationText();
}

//Gets the dimension for the iframe
function getLocFrameDimensions() {
	var container = document.getElementById('res1_pnlRentalInformation');
	if (container) {
		_locFrameLeft = 0; _locFrameTop = 0;
		for (var obj = container; obj && obj.tagName != 'BODY'; obj = obj.offsetParent) {
			_locFrameLeft += obj.offsetLeft;
			_locFrameTop += obj.offsetTop;
		}
		//_locFrameWidth = container.offsetWidth;
		//_locFrameHeight = container.offsetHeight;								
	}	
	return;
}

function refreshLocationsIFrame(locationSearchText)
// DJJ CA MERGE 2004203, commented out dollar.com line of code below, replaced with CA code which
// added the culture to the location picker URL
{ var cultureValue = ""; if(_selectorCulture != null) cultureValue = _selectorCulture.value; else cultureValue = "en-US";
top.frames["LocFrame"].location = LOCATION_PICKER_URL + locationSearchText + "&Culture=" + cultureValue;}
//{top.frames["LocFrame"].location = LOCATION_PICKER_URL + locationSearchText;}

//Positions the location iframe and displays it
function showIFrameLocPicker(searchText)
{ refreshLocationsIFrame(searchText);
	// to position the IFRAME.
	var locationTextBox = getLocationTextBox(_searchLocation);
	var offsetLeft = 0;	var offsetTop = 0;		
	for (var o=locationTextBox; (o) && (o.tagName!="BODY"); o=o.offsetParent)
	{offsetLeft += o.offsetLeft;offsetTop += o.offsetTop;}
	_locationPickerIFrame.style.left = offsetLeft;
	_locationPickerIFrame.style.top = offsetTop + locationTextBox.offsetHeight;
	showElement(_locationPickerIFrame);
}

function showPopupLocationPicker(searchText)
{
	// DJJ CA MERGE 2004203, commented out dollar.com line of code below, replaced with CA code which uses the culture
	var url = LOCATION_PICKER_URL + searchText + "&Culture=" + _selectorCulture.value;
	//var url = LOCATION_PICKER_URL + searchText;
	_locationPickerWindow = window.open(url, "locationWin","status=no,toolbar=no,location=no,menu=no,scrollbars=1,width=360,height=313");
} 

// singleLocation - means either pickup or return only.
function showLocationPicker(locationType, isExplicit, singleLocation)
{
	if ((isExplicit) && (!_referencesInitialized)) 
	{ 
		setLocationObjectReferences(); 
	}

	//Clear out Previous Pickup Text if direct link
	if (theForm.res1_directLink!= null && theForm.res1_directLink.value == 'DIRECT' )
	{
		_lastPickupText = "";
		theForm.res1_directLink.value = 'DONE';
	}

	var searchText = getCurrentLocationText(locationType);
	var selector = getSelector(locationType);
	_searchLocation = locationType; selector.value = false;

	// user entered nothing.
	if (searchText.length==0)
	{
		clearHiddenLocationValue(locationType);
		if (isExplicit)
		{
			// DJJ CA MERGE 2004203, commented out hard-coded english and used translated
			// variable that will be spit out in code-behinds
			alert(pleaseEnterCityName);
			//alert("Please enter a city name to find a location.");
			setLocationTextBoxFocus(locationType);
			return;
		}
		else 
		{
			if(!singleLocation)
			{ 
				if(_pickupLocationTextBox != null)
				{
					_returnLocationHiddenValue.value = _pickupLocationHiddenValue.value;
					_returnLocationTextBox.value = _pickupLocationTextBox.value;	
				}
				else
				{
					_returnLocationHiddenValue.value = getLocationHiddenInput(_PICKUP).value;
					_returnLocationTextBox.value = getLocationHiddenInput(_PICKUP).value
				}
				return;
			}
			else return;
		}
	}

	if(!singleLocation)
	{
		// DJJ CA MERGE 2004203, the hard-coded english string "same" needed to be changed to
		// use the sameText variable instead
		if((!isExplicit) && (searchText.toLowerCase() == sameText.toLowerCase()))
		//if((!isExplicit) && (searchText.toLowerCase() == "same"))
		{
			_returnLocationHiddenValue.value = _pickupLocationHiddenValue.value;
			return;
		}
		// DJJ CA MERGE 2004203, the hard-coded english string "same" needed to be changed to
		// use the sameText variable instead
		if ((isExplicit) && (searchText.toLowerCase()== sameText.toLowerCase()))
		//if ((isExplicit) && (searchText.toLowerCase()=="same"))
		{
			// DJJ CA MERGE 2004203, replaced hard-coded message with translated variable
			alert(returnCitySetToSame);
			//alert("The Return city is currently set to the same as the Pick Up city. Please replace 'Same' with the name of the Return city to find a different Return location.");
			return;
		}
	}
	
	// less than 3 chars.
	if(searchText.length<3)
	{
		clearHiddenLocationValue(locationType);
		if(isExplicit)
		{
			// DJJ CA MERGE 2004203, replaced hard-coded english message with translated variable
			alert(pleaseEnterThreeLetters);
			//alert("Please Enter atleast 3 letters to find a location.");
			setLocationTextBoxFocus(locationType);
			return;
		}
		else{return;}
	}
	
	// nothing has changed since last search.
	if ((!isExplicit) && (searchText==getLastLocationText(locationType))) 
	{ 
		return; 
	}

	findLocationCode(searchText.toUpperCase());

	if(_locationCodeFoundInArray) 
	{
		chooseLocation(searchText.toUpperCase(),searchText.toUpperCase());
	}
	else
	{
		if(_locationPickerIFrame != null) 
		{
			showIFrameLocPicker(searchText); 
		}
		else 
		{
			showPopupLocationPicker(searchText); 
			window.setTimeout("_locationPickerWindow.focus()", 200);
		} 
	}
}

function chooseLocation(locationCode, locationName)
{	var locationTextBox = getLocationTextBox(_searchLocation);
	var locationHiddenInput = getLocationHiddenInput(_searchLocation);
	locationTextBox.value = locationName;
	locationHiddenInput.value = locationCode;
	//if the locationCode and locationName are different remember that description needs to be displayed
	if (locationCode != locationName) {var plocSelector = getSelector(_searchLocation);plocSelector.value = 'true';}
	
	// set return location hidden field value
	if (_searchLocation==_PICKUP)
	{	if(_returnLocationTextBox != null)
		{var returnHiddenInput = getLocationHiddenInput(_RETURN);
			// DJJ CA MERGE 2004203, replace hard-code "same" with translated variable
			if ((_returnLocationTextBox.value=="") || (_returnLocationTextBox.value.toLowerCase() == sameText.toLowerCase()))
			{ returnHiddenInput.value = locationCode;
				if (locationCode != locationName) {var rlocSelector = getSelector(_RETURN);rlocSelector.value = 'true';	}
				// DJJ CA MERGE 2004203, replaced hard-coded "same" with translated sameText variable
				if (_returnLocationTextBox.value == '')_returnLocationTextBox.value = sameText; } }									   
	}
	closeLocFrame();
	if (_searchLocation==_PICKUP)
	{
		OnPickupLocationChange(locationCode);
	}
}

// Called from the onfocus() events of the location text boxes..
function initLocationText()
{ if (!_referencesInitialized)setLocationObjectReferences();
  if(_returnLocationTextBox != null){_lastReturnText = _returnLocationTextBox.value;}
  if(_pickupLocationTextBox != null){_lastPickupText = _pickupLocationTextBox.value;}
}

// Find location code in array
function findLocationCode(locationCode)
{ 
   _locationCodeFoundInArray = false;
	for(var i=0; i<LocationCodeArray.length; i++)
	{
		if(locationCode.toUpperCase() == LocationCodeArray[i])
		{
			_locationCodeFoundInArray = true;
			return;
		}
	}
	
	return false;
}

function getLocationHiddenInput(locationType) // return hidden value inputs
{if (locationType==_PICKUP)return _pickupLocationHiddenValue;else return _returnLocationHiddenValue;}

function clearHiddenLocationValue(locationType)	// clears the hidden values
{ if (locationType==_PICKUP){_pickupLocationHiddenValue.value = "";}else{_returnLocationHiddenValue.value = "";} }

//2005041 -- DO NOT SET PICKUP LOCATION'S FOCUS IF PICKUP LOCATION ALREADY PROVIDED
//what happens if user provides a location string, which requires an iframe search???
function setLocationTextBoxFocus(locationType) // set the focus to pickup or return textbox
{if (!_referencesInitialized){ setLocationObjectReferences(); } 

//following line commented out; prevented pickup location from being "focused" when linking in via 3rd party
//if (locationType==_PICKUP){  if((_pickupLocationTextBox != null) && (_pickupLocationTextBox.value == ""))_pickupLocationTextBox.focus();}
if (locationType==_PICKUP){  if(_pickupLocationTextBox != null)_pickupLocationTextBox.focus();}
						else{if(_returnLocationTextBox != null)_returnLocationTextBox.focus();} }


function setLocationTextBoxSelect() // set the focus to pickup or return textbox
{ if (!_referencesInitialized){ setLocationObjectReferences(); }
  if (_searchLocation==_PICKUP){  if(_pickupLocationTextBox != null)_pickupLocationTextBox.select();}
							 else{if(_returnLocationTextBox != null)_returnLocationTextBox.select();} }

function getLocationTextBox(locationType) // returns either pickup or return textbox object
{if (locationType==_PICKUP)return _pickupLocationTextBox;else return _returnLocationTextBox;}

function getLastLocationText(locationType) // return variables containing the users last search
{if (locationType==_PICKUP)	return _lastPickupText; else return _lastReturnText; }

function getCurrentLocationText(locationType) // get the user's entered text.
{if (locationType==_PICKUP) return _pickupLocationTextBox.value; else return _returnLocationTextBox.value;}

function getSelector(locationType) { // return  the selector values.
if (locationType == _PICKUP)return _pickupSelector;else	return _returnSelector;}

// Handle Enter
function handleEnter(field, event,locationType)
{	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(keyCode != 13) return true;
	var resButton = document.getElementById("res1_btnRateShop"); 
	if (!_referencesInitialized){ setLocationObjectReferences(); }
	var searchText = getCurrentLocationText(locationType); _searchLocation = locationType; 
	if (searchText==getLastLocationText(locationType)) { return true; }
	if ( (searchText.length == 0) || (searchText.length < 3) ) 
		{clearHiddenLocationValue(locationType);if(resButton != null)resButton.click(); 
		setLocationTextBoxFocus(locationType); return false;}
	findLocationCode(searchText.toUpperCase());
	if(_locationCodeFoundInArray){
		chooseLocation(searchText.toUpperCase(),searchText.toUpperCase()); 
		if(resButton != null) {resButton.click();} setLocationTextBoxFocus(locationType); return false;
	} else{field.blur(); return false;}
}

// cancel click if IFRAME still open
function checkLocationPickerOpen(event,tag)
{ 	if (  (_locationPickerWindow!=null) || ( elementVisible(_locationPickerIFrame)) ) 
	{event.returnValue = false; if(event.preventDefault) vent.preventDefault(); return false;}	
	try{sendDcsTag(tag);}catch(e){}
}
