

//book_widget_functions.js

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.getYear();
	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( top.document.frm.ad.value );
	departure_time = Date.parse( top.document.frm.dd.value );

	if( isNaN(arrival_time) || isNaN(departure_time) )
	{
		arrival_time = todays_time;
		departure_time = todays_time + one_day;
		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;

		if(    (arrival_time   < todays_time)
			|| (departure_time < todays_time) )
		{
			arrival_time = todays_time;
			departure_time = todays_time + one_day;
			done = 0;
			alrt = 1;
		}
		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 > (todays_time + one_year))
			|| (arrival_time   > (todays_time + one_year_less_one_day)) )
		{
			departure_time = todays_time + one_year;
			arrival_time = todays_time + one_year_less_one_day;
			done = 0;
			alrt = 4;
		}

	}

	nights = Math.ceil( ( departure_time - arrival_time ) / one_day );

	at = new Date( arrival_time );
	dt = new Date( departure_time );

	max_departure_time = arrival_time + one_month;
	if( max_departure_time > todays_time + one_year )
	{	max_departure_time = todays_time + one_year; }

	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();

	top.document.frm.ad.value = (arrival_month + 1) + "/" + arrival_day + "/" + arrival_year;
	top.document.frm.dd.value = (departure_month + 1) + "/" + departure_day + "/" + departure_year;
	top.document.frm.max_departure.value = (max_month + 1) + "/" + max_day + "/" + max_year;
	top.document.frm.min_departure.value = (min_month + 1) + "/" + min_day + "/" + min_year;

	top.document.frm.nd.value = nights;

	return alrt;
}

function arrival_plus_nights( frm )
{
	nights = top.document.frm.nd.value;

	if( nights > 30 ){	nights = 30;	}

	arrival_time = Date.parse( top.document.frm.ad.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();

	top.document.frm.dd.value = (departure_month + 1) + "/" + departure_day + "/" + departure_year;

	fix_dates( frm );
}

function adjust_dates( frm )
{
	res = fix_dates( frm );
	switch( res )
	{
		case 1:
			/*msg = "The arrival date cannot be before today.\n" +
				"The departure date must be after the arrival date.\n" +
				"The maximum stay is 30 nights starting on the day of arrival.";
			alert( msg );*/
			break;
		case 2:
			//msg = "The departure date must be after the arrival date.\n" +
			//	"The maximum stay is 30 nights starting on the day of arrival.";
			//alert( msg );
			break;
		case 3:
			msg = "The maximum stay is 30 nights starting on the day of arrival.";
			alert( msg );
			break;
		case 4:
			msg = "The departure date can be no further than one year out.\n" +
				"The arrival date must be before the departure date.";
			alert( msg );
			break;
	}
}

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()
{
	hide_ages();

	switch( top.document.frm.ch.selectedIndex )
	{
		case 8:
			showHideLayers( 'age8','inline','show' );
		case 7:
			showHideLayers( 'age7','inline','show' );
		case 6:
			showHideLayers( 'age6','inline','show' );
		case 5:
			showHideLayers( 'age5','inline','show' );
		case 4:
			showHideLayers( 'age4','inline','show' );
		case 3:
			showHideLayers( 'age3','inline','show' );
		case 2:
			showHideLayers( 'age2','inline','show' );
		case 1:
			showHideLayers(
			 'age1','inline','show',
			  'ages','','show' );
			break;
	}
}

function frm_Validator(theForm)
{

	if (theForm.ad.value == "")
	{
		alert("Please enter a value for the \"Arrival Date\" field.");
		theForm.ad.focus();
		return (false);
	}

	if (theForm.ad.value.length < 8)
	{
		alert("Please enter at least 8 characters in the \"Arrival Date\" field.");
		theForm.ad.focus();
		return (false);
	}

	if (theForm.ad.value.length > 10)
	{
		alert("Please enter at most 10 characters in the \"Arrival Date\" field.");
		theForm.ad.focus();
		return (false);
	}

	var checkOK = "0123456789-/";
	var checkStr = theForm.ad.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.ad.focus();
		return (false);
	}

	if (theForm.dd.value == "")
	{
		alert("Please enter a value for the \"Arrival Date\" field.");
		theForm.dd.focus();
		return (false);
	}

	if (theForm.dd.value.length < 8)
	{
		alert("Please enter at least 8 characters in the \"Departure Date\" field.");
		theForm.dd.focus();
		return (false);
	}

	if (theForm.dd.value.length > 10)
	{
		alert("Please enter at most 10 characters in the \"Departure Date\" field.");
		theForm.dd.focus();
		return (false);
	}

	var checkOK = "0123456789-/";
	var checkStr = theForm.dd.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.dd.focus();
		return (false);
	}

	if( theForm.ch.selectedIndex > 0 )
	{
		for(i = 1; i <= theForm.ch.selectedIndex; i++) {
			if(document.getElementById( 'a' + i ).selectedIndex == 0) {
				alert("Please specify ages for all children.");
				document.getElementById( 'a' + i ).focus();
				return (false);
			}
		}
	}

	return (true);
}

function do_reset( frm )
{
	show_ages();
	//return false
}
