var bolBookNow = false;

var	date_range_string = "";
var valid_dates = new Array();
var from_dates = new Array();
var thru_dates = new Array();
var limit_to_package_dates = 0;

var objToday = new Date();
var objMinArrival = new Date(objToday.getTime());// + 2*24*60*60*1000);
var min_arrival = (objMinArrival.getMonth() + 1) + '/' + objMinArrival.getDate() + '/' + objMinArrival.getFullYear();
var objMaxArrival = new Date(objMinArrival.getTime() + 364*24*60*60*1000);
var max_arrival = (objMaxArrival.getMonth() + 1) + '/' + objMaxArrival.getDate() + '/' + objMaxArrival.getFullYear();
var objMinDeparture = new Date(objToday.getTime() + 3*24*60*60*1000);
var min_departure = (objMinDeparture.getMonth() + 1) + '/' + objMinDeparture.getDate() + '/' + objMinDeparture.getFullYear();
var objMaxDeparture = new Date(objMinDeparture.getTime() + 364*24*60*60*1000);
var max_departure = (objMaxDeparture.getMonth() + 1) + '/' + objMaxDeparture.getDate() + '/' + objMaxDeparture.getFullYear();

var	one_day = 1000 * 60 * 60 * 24;
var	one_month = 30 * one_day;
var	one_year = 364 * one_day;
var	one_year_less_two_days = 362 * one_day;
var	one_year_less_one_day = 363 * one_day;

function fix_year( tm )
{
	at = new Date( tm );
	year = at.getFullYear();
	if( year < 100 )
	{	year = year + 2000;	}
	else if( year < 1900 )
	{	year = year + 1900;	}
	else if( year < 2000 )
	{	year = year + 2000;	}
	at.setFullYear( year );
	tm = at.getTime();
	return tm;
}

function fix_dates( frm )	// kgt
{
	alrt = 0;

	today = new Date();
	today_str = (today.getMonth() + 1) + "/" + today.getDate() + "/" + today.getFullYear();

	todays_time = Date.parse( today_str );

	arrival_time = Date.parse( frm.arrival_date.value );
	departure_time = Date.parse( frm.departure_date.value );
	original_arrival_time = arrival_time;
	original_departure_time = departure_time;

	min_departure_time = Date.parse( min_departure );
	max_departure_time = Date.parse( max_departure );

	min_arrival_time = Date.parse( min_arrival );
	max_arrival_time = Date.parse( max_arrival );

	if( isNaN(arrival_time) || isNaN(departure_time) )
	{
		arrival_time = Date.parse( min_arrival );
		departure_time = Date.parse( min_departure );
		alrt = 1;
	}

	arrival_time = fix_year( arrival_time );
	departure_time = fix_year( departure_time );

	done = 0;
	limit = 10;
	while( (done == 0) && (limit-- > 0) )
	{
		done = 1;

		at = new Date( arrival_time );
		arrival_month = at.getMonth();
		arrival_day = at.getDate();
		arrival_year = at.getFullYear();

		dt = new Date( arrival_time );
		departure_month = dt.getMonth();
		departure_day = dt.getDate();
		departure_year = dt.getFullYear();

		if( limit_to_package_dates )
		{
			i = 0;
			while( !isNaN( Date.parse( from_dates[i] ) ) )
			{
				fd_time = Date.parse( from_dates[i] );
				td_time = Date.parse( thru_dates[i] );
				if(		( arrival_time >= fd_time )
					&&	( arrival_time <= td_time ) )
				{
					min_arrival_time = fd_time;
					min_departure_time = fd_time + one_day;
					max_departure_time = td_time + one_day;
					max_arrival_time = max_departure_time - one_day;
					done = 0;
					break;
				}
				i++;
			}
		}
		else
		{
			max_departure_time = arrival_time + one_month;
		}


		if(    (arrival_time   < Date.parse( min_arrival ))
			|| (departure_time < Date.parse( min_departure )) )
		{
			arrival_time = Date.parse( min_arrival );
			departure_time = Date.parse( min_departure );
			done = 0;
			alrt = 7;
		}
		/*else */if( departure_time > max_departure_time )
		{
			departure_time = max_departure_time;
			done = 0;
			alrt = 6;
		}
		/*else */if( arrival_time >= departure_time )
		{
			departure_time = arrival_time  + one_day;
			done = 0;
			alrt = 2;
		}
		/*else */if( departure_time > (arrival_time + one_month) )
		{
			departure_time = arrival_time  + one_month;
			done = 0;
			alrt = 3;
		}
		/*else */if(   (departure_time > (Date.parse( max_departure )))
				|| (arrival_time   > (Date.parse( max_arrival ))) )
		{
			departure_time = Date.parse( max_departure );
			arrival_time = Date.parse( max_arrival );
			done = 0;
			alrt = 4;
		}
		/*else */if(	( limit_to_package_dates )
				&&	(	(min_arrival_time > arrival_time)
					||	(max_departure_time < departure_time) ) )
		{
			departure_time = max_departure_time;
			arrival_time = min_arrival_time;
			done = 0;
			alrt = 5;
		}
		/*else */if(	( limit_to_package_dates )
				&&	( isNaN( valid_dates[ ( ( arrival_year ) * 100 + arrival_month ) * 100 + arrival_day ] ) ) )
		{
			departure_time = max_departure_time;
			arrival_time = min_arrival_time;
			done = 0;
			alrt = 6;
		}

		if( max_departure_time > arrival_time + one_month )
		{
			max_departure_time = arrival_time + one_month; 
			done = 0;
		}

		if( max_departure_time > todays_time + one_year )
		{
			max_departure_time = todays_time + one_year; 
			done = 0;
		}

	}

	nights = Math.ceil( ( departure_time - arrival_time ) / one_day );

	at = new Date( arrival_time );
	dt = new Date( departure_time );

	mt = new Date( max_departure_time );
	mit = new Date( arrival_time + one_day );

	arrival_month = at.getMonth();
	arrival_day = at.getDate();
	arrival_year = at.getFullYear();

	departure_month = dt.getMonth();
	departure_day = dt.getDate();
	departure_year = dt.getFullYear();

	max_month = mt.getMonth();
	max_day = mt.getDate();
	max_year = mt.getFullYear();

	min_month = mit.getMonth();
	min_day = mit.getDate();
	min_year = mit.getFullYear();

	frm.arrival_date.value = (arrival_month + 1) + "/" + arrival_day + "/" + arrival_year;
	frm.departure_date.value = (departure_month + 1) + "/" + departure_day + "/" + departure_year;

	max_departure = (max_month + 1) + "/" + max_day + "/" + max_year;
	min_departure = (min_month + 1) + "/" + min_day + "/" + min_year;

	//alert( 'XX From: '+min_departure+'\nTo: '+max_departure );

	frm.nights.value = nights;

	// 20100108, colin added in alert sequence
	if (alrt) {
		switch (alrt) {
			case 1 :
				alert('Please enter valid dates for both your departure and arrival dates.');
			break;
			case 2 :
			break;
			case 3 :
			break;
			case 4 :
				alert('Please enter a departure date before '+max_departure+' and an arrival date before '+max_arrival+'.');
			break;
			case 5 :
				alert('Dates for this package are limited to travel between '+min_arrival+' and '+max_departure+'.');
			break;
			case 6 :
			break;
			case 7 :
				if (original_arrival_time<min_arrival_time) alert("Arrival date must be on or after today's date ("+min_arrival+').');
				else if (original_departure_time<original_arrival_time) alert('Departure date must be after arrival date.');
				else if (original_departure_time<min_departure_time) alert("Departure date must be on or after "+min_departure+'.');
			break;
		}
	}
	return alrt;
}

function arrival_plus_nights( frm )
{
	nights = frm.nights.value;

	if( nights > 30 ){	nights = 30;	}

	arrival_time = Date.parse( frm.arrival_date.value );
	departure_time = arrival_time + Math.ceil( nights * one_day );

	dt = new Date( departure_time );

	departure_month = dt.getMonth();
	departure_day = dt.getDate();
	departure_year = dt.getFullYear();

	frm.departure_date.value = (departure_month + 1) + "/" + departure_day + "/" + departure_year;

	fix_dates( frm, true );
}

function validate_number(objField, intMin, intMax) {

	varValue = objField.value;
	bolError = false;
	
	if (varValue.length <= 0) {
		bolError = true;
	} else if (parseInt(varValue)!=varValue-0) {
		bolError = true;
	} else if (varValue < intMin) {
		bolError = true;
	} else if (varValue > intMax) {
		bolError = true;
	}
	
	if (bolError) {
		objField.value = intMin;
	}
	
}

function findObj(n, d)
{ //v4.01
	var p,i,x;
	if( !d )	d=document; 
	if( ( p = n.indexOf("?") ) > 0 && parent.frames.length )
	{
		d = parent.frames[ n.substring(p+1) ].document;
		n = n.substring(0,p);
	}
	if( !(x=d[n])&&d.all) x=d.all[n]; 
	for( i=0; !x && i<d.forms.length; i++ )	x=d.forms[i][n];
	for( i=0; !x && d.layers && i > d.layers.length; i++ )	x=findObj( n, d.layers[i].document );
	if( !x && d.getElementById )	x=d.getElementById(n);
	return x;
}

function showHideLayers()
{
	var i, p, v, b, obj, args = showHideLayers.arguments;

	for( i = 0; i < (args.length - 2); i += 3 )
	{
		if( ( obj = findObj( args[i] ) ) != null )
		{
			v=args[i+2];
			b=args[i+1];
			if( obj.style )
			{
				obj=obj.style;
				b=(v=='show')?((b=='')?'block':b):'none'; 
				v=(v=='show')?'visible':(v=='hide')?'hidden':v; 
			}
			obj.visibility=v;
			obj.display=b;
		}
	}
}

function hide_ages()
{
	showHideLayers(
		'ages','','hide',
		'age1','','hide',
		'age2','','hide',
		'age3','','hide',
		'age4','','hide',
		'age5','','hide',
		'age6','','hide',
		'age7','','hide',
		'age8','','hide'
	);
}

function show_ages(frm)
{

	hide_ages();

	var intChildren = frm.number_of_children.value;
	
	if (intChildren > 0) {
		showHideLayers('ages','','show');

	}
	
	for (i=0;i<=intChildren;i++) {
		showHideLayers('age' + i,'inline','show');
	}
}

function frm_Validator(theForm)
{

	if (theForm.arrival_date.value == "")
	{
		alert("Please enter a value for the \"Arrival Date\" field.");
		theForm.arrival_date.focus();
		return (false);
	}

	if (theForm.arrival_date.value.length < 8)
	{
		alert("Please enter at least 8 characters in the \"Arrival Date\" field.");
		theForm.arrival_date.focus();
		return (false);
	}

	if (theForm.arrival_date.value.length > 10)
	{
		alert("Please enter at most 10 characters in the \"Arrival Date\" field.");
		theForm.arrival_date.focus();
		return (false);
	}

	var checkOK = "0123456789-/";
	var checkStr = theForm.arrival_date.value;
	var allValid = true;
	var validGroups = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		alert("Please enter only digit and \"/\" characters in the \"Arrival Date\" field.");
		theForm.arrival_date.focus();
		return (false);
	}

	if (theForm.departure_date.value == "")
	{
		alert("Please enter a value for the \"Arrival Date\" field.");
		theForm.departure_date.focus();
		return (false);
	}

	if (theForm.departure_date.value.length < 8)
	{
		alert("Please enter at least 8 characters in the \"Departure Date\" field.");
		theForm.departure_date.focus();
		return (false);
	}

	if (theForm.departure_date.value.length > 10)
	{
		alert("Please enter at most 10 characters in the \"Departure Date\" field.");
		theForm.departure_date.focus();
		return (false);
	}
	
	if(theForm.departure_date.value<theForm.arrival_date.value){alert('Choose a departure date that is after your arrival date.');theForm.departure_date.focus();return (false);}
	
	var checkOK = "0123456789-/";
	var checkStr = theForm.departure_date.value;
	var allValid = true;
	var validGroups = true;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
	}
	if (!allValid)
	{
		alert("Please enter only digit and \"/\" characters in the \"Departure Date\" field.");
		theForm.departure_date.focus();
		return (false);
	}

	if( theForm.number_of_children.value > 0 )
	{
		if( ( theForm.number_of_children.value > 0 ) && ( document.getElementById( 'a1' ).value == 0 ) )
		{
			alert("Please select an age for the 1st child.");
			document.getElementById( 'a1' ).focus();
			return (false);
		}
		if( ( theForm.number_of_children.value > 1 ) && ( document.getElementById( 'a2' ).value == 0 ) )
		{
			alert("Please select an age for the 2nd child.");
			document.getElementById( 'a2' ).focus();
			return (false);
		}
		if( ( theForm.number_of_children.value > 2 ) && ( document.getElementById( 'a3' ).value == 0 ) )
		{
			alert("Please select an age for the 3rd child.");
			document.getElementById( 'a3' ).focus();
			return (false);
		}
		if( ( theForm.number_of_children.value > 3 ) && ( document.getElementById( 'a4' ).value == 0 ) )
		{
			alert("Please select an age for the 4th child.");
			document.getElementById( 'a4' ).focus();
			return (false);
		}
		if( ( theForm.number_of_children.value > 4 ) && ( document.getElementById( 'a5' ).value == 0 ) )
		{
			alert("Please select an age for the 5th child.");
			document.getElementById( 'a5' ).focus();
			return (false);
		}
		if( ( theForm.number_of_children.value > 5 ) && ( document.getElementById( 'a6' ).value == 0 ) )
		{
			alert("Please select an age for the 6th child.");
			document.getElementById( 'a6' ).focus();
			return (false);
		}
		if( ( theForm.number_of_children.value > 6 ) && ( document.getElementById( 'a7' ).value == 0 ) )
		{
			alert("Please select an age for the 7th child.");
			document.getElementById( 'a7' ).focus();
			return (false);
		}
		if( ( theForm.number_of_children.value > 7 ) && ( document.getElementById( 'a8' ).value == 0 ) )
		{
			alert("Please select an age for the 8th child.");
			document.getElementById( 'a8' ).focus();
			return (false);
		}
	}

	return (true);
}

function updateDateField(dateFieldName, dateString)
{
  var targetDateField = document.getElementsByName (dateFieldName).item(0);
  if (dateString)
    targetDateField.value = dateString;
 
  var pickerDiv = document.getElementById(datePickerDivID);
  pickerDiv.style.visibility = "hidden";
  pickerDiv.style.display = "none";
 
  adjustiFrame();
  targetDateField.focus();
 
  // after the datepicker has closed, optionally run a user-defined function called
  // datePickerClosed, passing the field that was just updated as a parameter
  // (note that this will only run if the user actually selected a date from the datepicker)
  if ((dateString) && (typeof(datePickerClosed) == "function"))
    datePickerClosed(targetDateField);
}

function do_reset(frm)
{
	frm.arrival_date.value = min_arrival;
	frm.departure_date.value = min_departure;
	frm.nights.value = 1;
	frm.number_of_adults.value = 1;
	frm.number_of_children.value = 0;
	show_ages(frm);
}