﻿/************/
/* On Ready */
/************/

$(document).ready(function() {
	setChildAgeVisibility('false');	
	hidePlannerButton();
});

function setChildAgeVisibility(moveSubmit) {
	var selectedChildren = $('[ID*=ddlChildCount]').val();

	if (!selectedChildren)
		return;

	if (selectedChildren > 0)
		$('#planVacationChildAgeHeader').show();
	else
		$('#planVacationChildAgeHeader').hide();

	var $rptChildren = $('[ID*=rptChildren]');

	//	$rptChildren.slice(selectedChildren + 1).hide();
	//	$rptChildren.slice(0, selectedChildren).show();
	
	for (var pos = 7; pos >= 0; pos--) {
		if (pos + 1 > selectedChildren)
			$rptChildren.eq(pos).hide();
		else
			$rptChildren.eq(pos).show();
	}

	if (moveSubmit == 'true')
		setSubmitButtonPosition();
}

function setSubmitButtonPosition() {
    var row1and2 = document.getElementById("planVacationRow1and2");
    var row3 = document.getElementById("planVacationRow3");
    var row4 = document.getElementById("planVacationRow4");
    var container = document.getElementById("planVacationContent");
    var row5 = document.getElementById("planVacationRow5");

    var height = container.offsetHeight - row1and2.offsetHeight - row3.offsetHeight - row4.offsetHeight;
    var imgHeight = 42;

    var totalSpacing = (height / 2) - (imgHeight / 2);

    row5.style.marginTop = totalSpacing + "px";
   }

function resyncPlanner() {
    calendarUpdateCheckoutDate();
}

function showAdvancedOptions() {
	var destinations = $('[ID*=ddlDestination]');
	var selectedDestination = null;

	destinations.children().each(function() {
		if ($(this).val() == destinations.val()) {
			selectedDestination = $(this);
		}
	});

	var show = selectedDestination[0].attributes["Bookable"].value == "True";

	hidePlannerRow($('[ID*=planVacationRow2]'), show);
	hidePlannerRow($('[ID*=planVacationRow3]'), show);
	hidePlannerRow($('[ID*=planVacationRow4]'), show && $('[ID*=ddlChildCount]').val() > 0);	
}

function hidePlannerRow(row, show) {

	if (row == null)
		return;
	
	if (show) {
		row.children().each(function() {
			$(this).fadeIn();
		});	
	}
	else {
		row.children().each(function() {
			$(this).fadeOut();
		});	
	}
}

function displayErrorBox(title, error) {
	$('#errorDialogMessage').append(error);
	$(function() {
		$('#errorDialog').dialog({
			resizable: false,
			modal: true
		});
		$('#errorDialog').dialog('option', 'title', title);
	});
}

function displayTravelerCountDialog() {
	$(function() {
		var error = $('[ID*=hdnError]').val();
		var title = $('#spanTravelerCount').html();	
	
		if ($('#travelerCountDialog').dialog('isOpen') == false)
			$('#travelerCountDialog').dialog('open');
		else
			$('#travelerCountDialog').dialog({
				resizable: false,
				modal: true,
				buttons: {
					'Update': function() {
						travelerCountUpdate(error);
					},
					Cancel: function() {
						$('[ID*=lblError]').html('');
						$(this).dialog('close');
					}
				}
			});
		
		$('#travelerCountDialog').dialog('option', 'title', title);
		$('#travelerCountDialogMessage').show();
	});
}

function travelerCountUpdate(error) {
	$('[ID*=lblError]').html('');
	
	if (validateChildAges()) {
		loadHotelListing(1, 'false');
		$('#travelerCountDialog').dialog('close');
	}
	else {
		$('[ID*=lblError]').html(error);
	}
}

function validateChildAges() {
	var valid = true;
	var ddlChildAge = $('#planVacationChildAgeItem option:selected');
	var ddlChildCount = $('[ID*=ddlChildCount]');
	var pos = 0;
	
	ddlChildAge.each(function() {
		if (pos++ >= ddlChildCount.val())
			return;
		if ($(this).val() < 0)
			valid = false;
	});
	return valid;
}

function hidePlannerButton() {
	var btnPlanVacation = $('[ID*=btnPlanVacation]');
	if (btnPlanVacation && btnPlanVacation[0] && btnPlanVacation[0].onclick) {
		btnPlanVacation.hide();
	}
}

function showPlannerButton() {
	var btnPlanVacation = $('[ID*=btnPlanVacation]');
	if (btnPlanVacation && btnPlanVacation[0].onclick) {
		btnPlanVacation.fadeIn('slow');
	}
}

function getAdultCount() {
	var adultCount = $('[ID*=ddlAdultCount]');
	if (!adultCount)
		return null;
	else 
		return adultCount.val();
	
}

function getChildAges() {
	var ddlChildAge = $('#planVacationChildAgeItem option:selected');
	var ddlChildCount = $('[ID*=ddlChildCount]');
	var childAges = new Array();
	var pos = 0;

	ddlChildAge.each(function() {
		if (pos >= ddlChildCount.val())
			return false;
		childAges[pos++] = $(this).val();
	});

	return childAges;
}
