var firstTime = 0;

if(document.location.href.indexOf("127.0.0.1") > 0){
	var baseURL	= "http://127.0.0.1:8500/SeaDev/";
}else if(document.location.href.indexOf("qa.cf7.zimmerman.com") > 0){
	var baseURL	= "http://qa.cf7.zimmerman.com/Seabourn/Websites/SeabournDotCom/2008/";
}else if(document.location.href.indexOf("www.seabourn.com") > 0){
	var baseURL	= "http://www.seabourn.com/";
}else{
	var baseURL	= "http://seabourn.com/";
}

function updateSelectOptions(lastSel) {
	if((firstTime >= 1 && isFrontPage == 1) || isFrontPage == 0){
 	 $("#total_results").text('Searching...');
	}
  $.ajax({
    url: baseURL + 'Resources/Services/getDropdownData.cfc',
    type: 'GET',
    dataType: 'html',
    timeout: 10000,
    data: {
      method:'getData',
      CurrencyCode:'USD',
      REgionID:$("select[name='RegionID']").val(),
      date:$("select[name='date']").val(),
      duration:$("select[name='duration']").val(),
      shipID:$("select[name='shipID']").val(),
	  isStackNavigation:isStackNavigation,
      lastSelected:lastSel
    },
    error: function(){
      $("#total_results").text('An error occured.');
    },
    success: function(response){
      // Replace the elements returned from the service completely
      // This couldn't be done during the replace
	  if((firstTime >= 1 && isFrontPage == 1) || isFrontPage == 0){
	      $("#total_results").replaceWith($(response)[0]);
	      $("#total_results").text($("#total_results").text() + ' results found');
	  }
      // The select menus
      $("select[name='RegionID']").replaceWith($(response)[1]);
      $("select[name='date']").replaceWith($(response)[2]);
      $("select[name='shipID']").replaceWith($(response)[3]);
      $("select[name='duration']").replaceWith($(response)[4]);   
      // We lose the event binding when we replace the select so re-apply it
      applyChangeHandlers();
	  firstTime = 1;
    }
   });
}

function applyChangeHandlers(){
  $("select[name='RegionID']").change(function (){
    updateSelectOptions('RegionID');
  });
  $("select[name='date']").change(function (){
    updateSelectOptions('date');
  });
  $("select[name='shipID']").change(function (){
    updateSelectOptions('shipID');
  });
  $("select[name='duration']").change(function (){
    updateSelectOptions('duration');
  });
}

$(document).ready(function(){
  var lastSelected = '';
  updateSelectOptions(lastSelected);
});