var substAttempt = 0;
var waitMessageHtml = '<SPAN class="ajaxRedirectWaitStyle">Your Availability Search has found : no matches. <br/><br/>Searching for availability in an alternative resort ({0} of {1}), please wait.</SPAN>';

function AvSrch_ChangeArrivalDate(dStr) {
	var f = document.getElementById("#CfmlId#");
	f = f.form;
	f.AvSrch.value = 1;
	f.ArrivalDate.value = dStr;
	ShowResultsPage(1);
}

function ShowNextPeriod(d){
	$('#AvSrch_ArrivalDate').val(d);
	ShowResultsPage(1);
}

function ShowPreviousPeriod(d){
	$('#AvSrch_ArrivalDate').val(d);
	ShowResultsPage(1);
}

function AvSrch_ChangeArrivalDate(dStr) {
	f = document.frmSearchCriteria;
	f.AvSrch.value = 1;
	f.AVSRCH_ARRIVALDATE.value = dStr;
	f.submit();
}

function GoBackToSearch() {

	if( $('#AvSrch_FromAccomPage').val() == '1' ){ // back to accom page search
		document.frmSearchCriteria.action = "default.cfm/AccomCode." + $('#AvSrch_AccommodationCode').val();
	}
	else if( $('#AvSrch_FromResortPage').val() == '1' ){ // Back to resort page search
		document.frmSearchCriteria.action = "default.cfm/ResortCode." + $('#AvSrch_OriginalResortCode').val();
	}
	else{ // back to home page search
		document.frmSearchCriteria.action = "default.cfm/loadfile.AvailabilitySearch.cfm";
	}

	document.frmSearchCriteria.submit();
}

function ChangeResultsOrder(pageNum){
	$('#AvSrch_ResultsOrder').val($('#AvSrch_Order').val());
	ShowResultsPage(pageNum);
}

function ShowResultsPage(pageNum)
{
	var url = basehref + 'AvailabilitySearchResultsPaging.cfm?ajax=true';
	var duration = $('#AvSrch_Duration').val();
	
	$('#AvSrch_ResultsPageNum').val(pageNum);
	
	$('div#divSearchResults').css('opacity', 0.3);
	$('div#divLoading').show();

	$.post(url, $('#frmSearchCriteria').serialize(), function(data) {

		if( data.indexOf('[cf_error]') > -1 ){
			$('#debugTextArea').val(data);
		
			alert('An error occurred, please try again!');
		}
		else if( data.indexOf('[results_found]') > -1 )
		{
			$('div#divSearchResults').html(data);
		}
		else if( pageNum == 1 ) // Check for substitutes if on first page
		{
			if( duration == 6 || substAttempt < arrSubstResorts.length )
			{
				checkForAlternatives();
			}
		}

		$('div#divLoading').hide(1);
		$('div#divCheckForSubs').hide(1);
		$('div#divSearchResults').css('opacity',1);
		
		// Make sure the duration change message remains visible when the page changes
		if( $('#AvSrch_Duration').val() != $('#AvSrch_OriginalDuration').val() ){
			$('#divDurationChange').show();
		}
		
		// Make sure resort code substitution message remains visible when page changes
		if( $('#AvSrch_ResortCode').val() != $('#AvSrch_OriginalResortCode').val() ){
			$('#divSubst').show();
		}
	});
}

function checkForAlternatives(){
	var duration = $('#AvSrch_Duration').val();

	if( duration == 6 ){
		//$('#divDurationChange').show();
	
		$('#AvSrch_Duration').val(7);
		ShowResultsPage(1);
	}
	else if( arrSubstResorts.length > 0 ){ // if resort search, check substitute resorts
	
		$('#divSubst').show();
		$('#divOriginalHeader').show();
		$('#divCheckForSubs').show();
		
		$('#AvSrch_ResortCode').val(arrSubstResorts[substAttempt]);
		substAttempt++;
		ShowResultsPage(1);
	}
}

function AvSrchPanel_OnSubmit() {
	var f = document.getElementById("AvSrch").form;
	var msg = '';
	
	if( $('#AvSrch_Duration').val() == -1 ){
		msg += 'Please select a duration';
	}
	
	if( msg != '' ){
		alert(msg);
		return;
	}
	
	f.AvSrch.value = "1";
	f.action = "default.cfm/loadfile.AvailabilitySearchResultsAjax.cfm";
	f.submit();
}

function AvSrch_OnSubmit() {
	var f = document.getElementById("AvSrch").form;
	var msg = '';
	
	if( $('#AvSrch_Duration').val() == -1 ){
		msg += 'Please select a duration';
	}
	
	if( msg != '' ){
		alert(msg);
		return;
	}

	f.AvSrch.value = "1";
	f.action = "default.cfm/loadfile.AvailabilitySearchResultsAjax.cfm";
	f.submit();
}
