// JScript source code

// set up the URL of the locations listing page
var URL = new String(document.location);
var startPos = URL.indexOf("//") + 2;
var endPos = URL.indexOf("/", startPos);
var _root = URL.substring(startPos, endPos);
var _bFound = false;

var g_LOCATION_PICKER_URL;


var _locationPickerIFrame = null;    // IFRAME element if in IFrame
var _locationPickerWindow = null;
  
// bExplicit indicates if the search button was pressed, true or
// if the textbox lost focus (onblur), false  to allow the client to leave the text box without validation
function doLocationSearch(bExplicit)
{
   var txtbox = document.getElementById('Locationsearch_txtLocation');
	var htxtboxLast = document.getElementById('Locationsearch_lastLocation');
	if(htxtboxLast == null)
	    htxtboxLast = document.getElementById('lastLocation');
	    
	var searchCriteria = txtbox.value; 
	
	// user entered nothing.
	if ( searchCriteria.length < 1 )
	{
	   if( bExplicit )
	   {
	      alert(g_strpleaseEnterCityName);
		   txtbox.focus();
		}
		return;
	}

	// less than 3 chars.
	if(  searchCriteria.length<3)
	{
		if( bExplicit )
	   {
	      alert(g_strpleaseEnterThreeLetters);
	      txtbox.focus();
	   }
	   return;
	}
	
	// nothing has changed since last search.
	if ( (htxtboxLast != null) && ( searchCriteria == htxtboxLast.value ))
	{ 
		return; 
	}
	else if ( htxtboxLast != null)
	   htxtboxLast.value == searchCriteria;
	   

	var locationCode = searchCriteria.toUpperCase();
	
	txtbox.style.cursor = 'wait';
   for( var i=0; i < g_LocationCodeArray.length; i++)
	{ 
		if( g_LocationCodeArray[i] == locationCode )
		{
			getPlaceNameFromLocationCode(searchCriteria.toUpperCase());
			_bFound = true;
			txtbox.style.cursor = 'Auto';
			return;
		}  
	}
	txtbox.style.cursor = 'Auto';
   if (_isHttps())
   {
      g_LOCATION_PICKER_URL = "https://" + _root + "/fleet/locations.aspx?Code=" + searchCriteria + "&Country=" + g_Country + "&Culture=" + g_Culture;
   }
   else
   {  
      g_LOCATION_PICKER_URL = "http://"  + _root + "/fleet/locations.aspx?Code=" + searchCriteria + "&Country=" + g_Country + "&Culture=" + g_Culture;
   }

	// Display the IFrame and perform the query
	_locationPickerIFrame = document.getElementById('LocFrame');
	if(_locationPickerIFrame != null) 
	{
	   ShowIframe();
	}
	// firefox stopped returning the IFrame
	mozfirefox = document.getElementsByName("LocFrame"); 
   if(mozfirefox.item(0) != null)
   {  
      _locationPickerIFrame =  mozfirefox.item(0)
      ShowIframe();
   }

	else // display it as a popup window
	{
		var url = g_LOCATION_PICKER_URL;
      _locationPickerWindow = window.open(url, "locationWin","status=no,toolbar=no,location=no,menu=no,scrollbars=1,width=360,height=313");
      window.setTimeout("_locationPickerWindow.focus()", 200);
	} 
}
function ShowIframe()
{
	if( g_Culture == null) 
      g_Culture = "en-US";
   top.frames["LocFrame"].location = g_LOCATION_PICKER_URL;

   _locationPickerIFrame.style.height = 104;

	showElement ( _locationPickerIFrame);
	var txtbox = document.getElementById('Locationsearch_txtLocation');
	  
	hideElement ( txtbox);
}
//Called from the IFrame child script 	
//Hide or close the iframe window
function closeLocFrame() 
{
   if (_locationPickerWindow==null ) 
   {
      if(_locationPickerIFrame != null) 
	   {
	      _locationPickerIFrame.style.height = 4; // make it small to not push the footer down.
	      hideElement(_locationPickerIFrame);
	   }
	   var txtbox = document.getElementById('Locationsearch_txtLocation');
	   txtbox.value = ""; // clear it out. 
	   showElement(txtbox);
   }
   else
   {  
      _locationPickerWindow.close();
   }
}

function DecideWhetherOrNotToRefresh()
{
   // na
}

// java was jumping directly to the overload with two arguements, passing 'undefined' as the second.
function getPlaceNameFromLocationCode(Code)
{
   //DoLookup to get the city name for this code.
   var ajaxResponse = _location_search.LookupLocation(Code);
   chooseLocation(Code, ajaxResponse.value);
}
function chooseLocation(Code, City)
{	
   var txtbox = document.getElementById('Locationsearch_txtLocation');
		
  	closeLocFrame(); // NOTE: this sets the search text to empty string.
  	// if the user has CLICKED the search button, ONBLUR is called FIRST !!@
  	// onclick then finds an empty text box and puts up an alert message.
  	txtbox.value = Code;     
	
	document.location = "/fleet/fleet_location_detail.aspx?CD=" + Code + "&LD=" + City + "&CC=" + g_Country; 
}

function handleEnter(field, event, id)
{	
   var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(keyCode != 13)  // Enter
	   return true;
	
	doLocationSearch(true);  
	if(_bFound != null && _bFound)
	{
	   field.style.cursor = 'wait';
	   field.disabled = true;
	   
	   field.blur(); 
	   return false;
	}
}

function setLocationTextBoxSelect() // set the focus to pickup or return textbox
{ 
   var txtbox = document.getElementById('Locationsearch_txtLocation');
   txtbox.select();
}

// cancel click if IFRAME still open
//TODO:TCTRAS
function checkLocationPickerOpen(event,tag)
{ 	
   if (  (_locationPickerWindow!=null) || ( elementVisible(_locationPickerIFrame)) ) 
	{
	   event.returnValue = false; 
	   if(event.preventDefault) 
	      event.preventDefault(); 
	      return false;
	}	
	try{sendDcsTag(tag);}catch(e){}
}

