function loadCountry(inForm) {

  // var region_selected = inForm.continent.options[inForm.continent.selectedIndex].value;
	var region_selected = inForm.continent.value;
	
	if (region_selected != '') {

		var thisRegions = oRegions[region_selected];
		if (thisRegions) {
			
			inForm.country.options.length = 0;
			
			inForm.country.options[0] = new Option('--country--', '');
			for (var i=0; i < thisRegions.length; i++) {
				if (thisRegions[i])
					inForm.country.options[i+1] = new Option(thisRegions[i][0], thisRegions[i][1]);
	
			}
			inForm.country.options[0].selected=true;
			inForm.txtCode.value = inForm.continent.value;
	
		}
			
	} else {	// no region selected - add them all!

		var arrRegions = new Array();	// to store countries - no duplicates
		
		inForm.country.options.length = 0;
		inForm.country.options[0] = new Option('--country--', '');

		for ( region_selected in oRegions) {
			
			var thisRegions = oRegions[region_selected];
			if (thisRegions) {
				
				for (var i=0; i < thisRegions.length; i++) {
					if (thisRegions[i] && !arrRegions[thisRegions[i][1]]) {
						inForm.country.options[inForm.country.options.length] = new Option(thisRegions[i][0], thisRegions[i][1]);
						arrRegions[thisRegions[i][1]] = thisRegions[i][0];
					}
				}
				
			}
			
		}
		sortSelect(inForm.country);
		inForm.country.options[0].selected=true;
		inForm.txtCode.value = '';
	}	// end if else

	loadTrip(inForm);
}

function loadTrip(inForm) {

	var country_selected = inForm.country.options[inForm.country.selectedIndex].value;
	var region_selected = inForm.continent.options[inForm.continent.selectedIndex].value;
	
	if (country_selected == '') {

		// add all the trips for this region...
    var tmpRegions = oRegions[region_selected];
		if (tmpRegions) {
			// create empty array
			var thisRegions = new Array();
			// fetch each country for this region
			for (var i=0; i < tmpRegions.length; i++) {
				// get the trips for this country
				sCountry = tmpRegions[i][1];
				thisRegions = thisRegions.concat(oTrips[sCountry]);
			}
		}

	} else {

		// set it based on country
    var thisRegions = oTrips[country_selected]; // select region here

	}

	// always clear the bludger first...
	inForm.trip.options.length = 0;
	inForm.trip.options[0] = new Option('--trip (optional)--', '');

	if (thisRegions) {
	
		// alert('Region = ' + region_selected + ' :: Length = ' + thisRegions.length);

		for (var i=0; i < thisRegions.length; i++) {
			if (thisRegions[i])
				inForm.trip.options[i+1] = new Option(thisRegions[i][0], thisRegions[i][1]);
		}
		inForm.trip.options[0].selected=true;
		
	} // else alert('No regions either!!!');

	if(inForm.country.selectedIndex > 0 && inForm.txtCode) {
		inForm.txtCode.value = inForm.country.options[inForm.country.selectedIndex].value;
	}
	if (inForm.txtCodeDisplay)
		inForm.txtCodeDisplay.value = '';
}

function updateCode(inForm) {

	var index = inForm.trip.selectedIndex;
	if(index > 0) {
		inForm.txtCode.value = inForm.trip.options[index].value;
    if (inForm.txtCodeDisplay)
			inForm.txtCodeDisplay.value = inForm.trip.options[index].value;
	}
}

function srchSubmit(objForm) {
	
	/* cross domina Suntrek trip search hack */
	if(/\.intrepidsuntrek\./.test(window.location)){
		if($F($('tripsrch_translation'))){
	    	objForm.action = "http://www.intrepidtravel.com/search/trips/translation/" + $F($('tripsrch_translation')); 
	    }else{
	    	objForm.action = "http://www.intrepidtravel.com/search/trips/translation/0";
		}
		
		/* query url bits
		if($F($('searchregion'))){
			objForm.action += "/" + $F($('searchregion'));
		}
		
		if($F($('searchdestination'))){
			objForm.action += "/" + $F($('searchdestination'));
		}*/
	}
	
	/* overwrite form action if trip code is defined*/
	if (objForm.txtCodeDisplay && objForm.txtCodeDisplay.value)
	{
	    objForm.txtCode.value = objForm.txtCodeDisplay.value;
	    if($F($('tripsrch_translation'))){
	    	objForm.action = "http://www.intrepidtravel.com/trips/" + objForm.txtCode.value + "/translation/" + $F($('tripsrch_translation'));
	    }else{
	    	objForm.action = "http://www.intrepidtravel.com/trips/" + objForm.txtCode.value;
		}
	}
  return objForm.txtCode.value != '';
}

function compareText (option1, option2) {
  return option1.text < option2.text ? -1 :
    option1.text > option2.text ? 1 : 0;
}

function sortSelect(select) {
  var options = new Array (select.options.length);
  for (var i = 0; i < options.length; i++)
    options[i] = 
      new Option (
        select.options[i].text,
        select.options[i].value,
        select.options[i].defaultSelected,
        select.options[i].selected
      );
  options.sort(compareText);
  select.options.length = 0;
  for (var i = 0; i < options.length; i++)
    select.options[i] = options[i];
}

function loadTrips()
{
  var inForm = $('floatingtripsearchform');
  // disable region select dropdown
  inForm.country.options[0] = null;
  inForm.country.options[0] = new Option('Loading..', '');
  inForm.country.options[0].selected=true;
  inForm.country.disabled = true;

  // load in the regions
  var url='/ajax/tripselect/regions.txt'
  var cacheRegions = new Ajax.Request(
    url, 
    {
      method: 'get', 
      onComplete: showResponseCacheRegions
    });
}

function showResponseCacheRegions(originalRequest)
{
  var inForm = $('floatingtripsearchform');
  eval(originalRequest.responseText);
  inForm.country.options[0] = null;
  inForm.country.options[0] = new Option('-- country --', '');
  inForm.country.options[0].selected=true;

  inForm.trip.options[0] = null;
  inForm.trip.options[0] = new Option('Loading..', '');
  inForm.trip.options[0].selected=true;
  inForm.trip.disabled = true;

  // load in the regions
  var url='/ajax/tripselect/trip.txt'
  var cacheRegions = new Ajax.Request(
    url, 
    {
      method: 'get', 
      onComplete: showResponseCacheTrips
    });
}

function showResponseCacheTrips(originalRequest)
{
  var inForm = $('floatingtripsearchform');
  eval(originalRequest.responseText);
  inForm.trip.options[0] = null;
  inForm.trip.options[0] = new Option('select trip (optional)', '');
  inForm.trip.options[0].selected=true;
  inForm.country.disabled = false;
  inForm.trip.disabled = false;

  // populate dropdowns
  loadCountry(inForm);
}

// ajax load in trips
var oTrips = new Object();
var oRegions = new Object();
loadTrips();