// JavaScript Document
<!-- Hide from browsers

var originations   = new Object();
var destinations   = new Object();
var cities_by_orig = new Object();
var states_by_orig = new Object();
var states         = new Object(); 

function array_intersect(x, y) {
	var ret = new Array();
	for (var j = 0; j < x.length; j++) {
		for (var i = 0; i < y.length; i++) {
			if (x[j] == y[i])
				ret.push(x[j]);
		}
	}
	return ret;
}

function do_array_intersect(a, b) {
	if (a.length < b.length) {
		return array_intersect(a, b);
	} else {
		return array_intersect(b, a);
	}
}

function init_destinations(o_key) {
	var state       = document.dest.in_destform_state;
	var state_codes = states_by_orig[o_key];
	var i, j;

	state.options[0] = new Option('Choose a state or region', 'all');
	for (i = 1, j = 0; j < state_codes.length; i++, j++) {
		var s = state_codes[j]; 
		state.options[i] = new Option(states[s].name, s);
	}
}

function init_originations() {
	var orig = document.dest.in_origination_key;
	var i = 0;
	for (o in originations) {
		orig.options[i] = new Option(originations[o], o); 
		i++;
	}
	var idx = 0;
	i = 0;
	for (k in originations) {
		if (k == 371)
			break;
		i++;
	}
	orig.selectedIndex = i;
	init_destinations(371);
}

function change_state() {
	var orig = document.dest.in_origination_key;
	var state = document.dest.in_destform_state;
	var city = document.dest.in_location_key;

	var this_orig  = orig.options[orig.selectedIndex].value;
	var this_state = state.options[state.selectedIndex].value;
	var cities = do_array_intersect(cities_by_orig[this_orig],
			states[this_state].cities);
	var i, j;

	city.options.length = 0;
	city.options[0] = new Option('Choose a city', 'all');
	city.options[1] = new Option('Show all cities', 'all');
	for (i = 2, j = 0; j < cities.length; i++, j++) {
		k = cities[j];
		city.options[i] = new Option(destinations[k], k);
	}
}


/* 
 * takes document.dest.in_origination_key.selectedIndex as arg
 */
function change_origination(o_idx) {
	var state = document.dest.in_destform_state;
	var city = document.dest.in_location_key;
	var o_key = document.dest.in_origination_key.options[o_idx].value;
	var state_codes = states_by_orig[o_key];
	var i, j;

	state.options.length = 0;
	state.options[0] = new Option('Choose a state or region', 'all');
	for (i = 1, j = 0; j < state_codes.length; i++, j++) {
		var s = state_codes[j]; 
		state.options[i] = new Option(states[s].name, s);
	}
	city.options.length = 0;
	city.options[0] = new Option('Choose a city', 'all');
	city.options[1] = new Option('Show all cities', 'all');
}






// BEGIN WCT LMD JS
// $Id: JSProcessor.pm,v 1.15 2004/05/27 13:31:35 stephens Exp $

function do_array_intersect(a, b) {
        if ( ! (a && b) ) {
                return new Array();
        }

        if (a.length < b.length) {
                return array_intersect(a, b);
        } else {
                return array_intersect(b, a);
        }
}


function wct_lmd_Array_indexOf(a, x)
{
  for ( var i = 0; i < a.length; ++ i ) {
    if ( a[i] == x ) return i;
  }
  return -1;
}


var _wct_lmd_in_origination_key;
var _wct_lmd_preferred_dests;
var _wct_lmd_no_preferred_dests;


function wct_lmd_prefer_dests(preferred_dests)
{
  var n, nt, diff;

  _wct_lmd_preferred_dests = preferred_dests;

  // Filter destinations.
  var new_destinations = new Object();
  n = nt = diff = 0;
  for ( de in destinations ) {
    ++ nt;
    if ( wct_lmd_Array_indexOf(preferred_dests, de) >= 0 ) {
      new_destinations[de] = destinations[de];
      ++ n;
    }
  }
  // alert("Scanned " + nt + ": Kept " + n + " destinations");

  // Filter out cities_by_orig
  var new_cities_by_orig = new Object();
  n = nt = diff = 0;
  for ( orig in originations ) {
    ++ nt;
    var orig_c = cities_by_orig[orig];
    orig_c = do_array_intersect(orig_c, preferred_dests);

    // diff += orig_c.length - cities_by_orig[orig].length;

    // If the orig has no dests that match...
    if ( orig_c.length ) {
      new_cities_by_orig[orig] = orig_c;
      ++ n;
    }
  }
  // alert("Scanned " + nt + ": Kept " + n + " cities_by_orig");
 
  // Filter states_by_orig.
  var new_states_by_orig = new Object();
  n = nt = diff = 0;
  for ( orig in states_by_orig ) {
    ++ nt;
    // Get the filtered destinations for the orig,
    // Use this list to filter the states_by_orig.
    var orig_c = new_cities_by_orig[orig];

    var old_orig_state = states_by_orig[orig];
    var orig_s = new Array();

    // Loop through the orig's states.
    for ( var i = 0; i < old_orig_state.length; ++ i ) {
      var state_code = old_orig_state[i];
      var state_cities = states[state_code].cities;
      state_cities = do_array_intersect(state_cities, orig_c);

      // defaultStatus = ("Orig " + orig + ": state " + state_code + " before: " + states[state_code].cities.length + " after: " + state_cities.length);
      if ( state_cities.length > 0 ) {
        // The state has at least one dest we are interested in.
        orig_s.push(state_code);
      } else {
        // The state has no matching dest cities anymore.
	// defaultStatus = ("Orig " + orig + ": deleted state " + state_code);
      }
    }

    // diff += orig_s.length - states_by_orig[orig].length;
    if ( orig_s.length ) {
      new_states_by_orig[orig] = orig_s;
      ++ n;
    }
  }
  // alert("Scanned " + nt + ": Kept " + n + " states_by_orig");

  // Filter out originations that have no
  // destinations.
  n = nt = diff = 0;
  var new_originations = new Object();
  for ( orig in originations ) {
    ++ nt;
    var orig_c = new_cities_by_orig[orig];

    if ( orig_c && orig_c.length ) {
      new_originations[orig] = originations[orig];
      ++ n;
    }
  }
  // alert("Scanned " + nt + ": Kept " + n + " originations[]");

  
  // If there is at least one matching, orignation
  // use the filtered data.
  if ( (_wct_lmd_no_preferred_dests = n > 0) ) {
    originations   = new_originations;
    destinations   = new_destinations;
    cities_by_orig = new_cities_by_orig;
    states_by_orig = new_states_by_orig;
  }
}


function wct_lmd_update_state(state_codes)
{
  var state = document.dest.in_destform_state;
  var i, j;

  if ( state != null ) {
    state.options.length = i = 0;
    if ( state_codes.length > 1 || ! _wct_lmd_preferred_dests ) {
      state.options[i ++] = new Option('Choose a state or region', 'all');
    }

    for ( j = 0; j < state_codes.length; j++ ) {
      var s = state_codes[j]; 
      state.options[i ++] = new Option(states[s].name, s);
    }

    // Default if only one state matches.
    if ( state_codes.length == 1 ) {
      state.selectedIndex = i - 1;
      wct_lmd_update_city(state_codes[0]);
    } else {
      wct_lmd_update_city();
    }
  }
}


function wct_lmd_update_city(this_state)
{
  var city = document.dest.in_location_key;
  var cities;
  var i, j;

  if ( this_state ) {
    var orig = document.dest.in_origination_key;
    var this_orig  = orig.options[orig.selectedIndex].value;
    cities = do_array_intersect(cities_by_orig[this_orig],
                                        states[this_state].cities);
  } else {
    cities = new Array();
  }

  city.options.length = i = 0;
  if ( (! _wct_lmd_preferred_dests) || cities.length > 1 ) {
    city.options[i ++] = new Option('Choose a city', 'all');
  }
  if ( (! _wct_lmd_preferred_dests) || cities.length == 0 ) {
    city.options[i ++] = new Option('Show all cities', 'all');
  }

  for (j = 0; j < cities.length; j++) {
    var k = cities[j];
    city.options[i ++] = new Option(destinations[k], k);
  }

  // Default if only one city matches.
  if ( cities.length == 1 ) {
    city.selectedIndex = i - 1;
  }
}


function init_destinations(o_key) {
  var state_codes = states_by_orig[o_key];
  wct_lmd_update_state(state_codes);
}



function change_state() {
  var orig = document.dest.in_origination_key;
  var state = document.dest.in_destform_state;

  var this_orig  = orig.options[orig.selectedIndex].value;
  var this_state = state.options[state.selectedIndex].value;

  wct_lmd_update_city(this_state);
}


/* 
 * takes document.dest.in_origination_key.selectedIndex as arg
 */
function change_origination(o_idx) {
  var state = document.dest.in_destform_state;
  var o_key = document.dest.in_origination_key.options[o_idx].value;
  var state_codes = states_by_orig[o_key];

  _wct_lmd_in_origination_key = o_key;

  wct_lmd_update_state(state_codes);
}


function init_originations() {
  var orig = document.dest.in_origination_key;
  var i = 0;
  var o_key = wct_lmd_in_origination_key();

  for ( o in originations ) {
    orig.options[i] = new Option(originations[o], o); 
    if ( o == o_key ) {
      orig.selectedIndex = i;
    }
    i++;
  }

  init_destinations(o_key);
}


function wct_lmd_in_origination_key() {
  if ( ! _wct_lmd_in_origination_key ) {
    if ( document.dest != null ) {
      // Try getting it from the Select widget?
      var orig = document.dest.in_origination_key;
      if ( orig.selectedIndex >= 0 ) {
        _wct_lmd_in_origination_key = orig.options[orig.selectIndex].value;
      }
    }
  }

  if ( ! _wct_lmd_in_origination_key ) {
    // Try getting it from the document URL?
    _wct_lmd_in_origination_key = document.location.href.match(/[?;&]in_origination_key=(\d+)/);
    _wct_lmd_in_origination_key = _wct_lmd_in_origination_key != null 
      ? _wct_lmd_in_origination_key[1] : 371; // "371" is magic for NYC.
  }

  return _wct_lmd_in_origination_key;
}


function wct_lmd_in_origination_key_changed()
{
  var p = document.location.href;
  var f = document.dest.in_origination_key;
  var v = f[f.selectedIndex].value;
  var r;

  if (p.indexOf('in_origination_key') >= 0) {
    r = p.replace(/in_origination_key=\d+/, 
		  'in_origination_key=' + v);
  }
  else if (p.indexOf('?') >= 0) {
    r = p + '&in_origination_key=' + v;
  }
  else {
    r = p + '?in_origination_key=' + v;
  }

  document.location = r;
}

function currencyWindow(popurl)
	{
	currWindow = window.open(popurl, 'CurrencyConverter', 'width=460,height=380');
	currWindow.focus;
	}

function changeForm(id)
{ 
	var f; 
	f=document.search;
	//alert(id);
	 if(id == 2)
	 {
		hotelbox.style.display='none';
		lastmindealsbox.style.display='none';
		carbox.style.display='none';
		hotelairbox.style.display='none';
		clickstay('id2');
	 }
	 else if(id == 3)
	 {
		hotelbox.style.display='none';
		lastmindealsbox.style.display='block';
		carbox.style.display='none';
		hotelairbox.style.display='none';
		clickstay('id5');

	 }
	 else if(id == 4)
	 {
		 hotelbox.style.display='none'; 
		 lastmindealsbox.style.display='none';
		 carbox.style.display='block';
		 hotelairbox.style.display='none';
		 clickstay('id4');
		 //alert(document.hotsrchbox.dod_dd.selectedIndex);
		 document.CarSearch.pudate_mo.options.selectedIndex = document.hotsrchbox.doa_mm.options.selectedIndex;
		 document.CarSearch.pudate_dy.options.selectedIndex = document.hotsrchbox.doa_dd.options.selectedIndex;
		 document.CarSearch.dodate_mo.options.selectedIndex = document.hotsrchbox.dod_mm.options.selectedIndex;
		 document.CarSearch.dodate_dy.options.selectedIndex = document.hotsrchbox.dod_dd.options.selectedIndex;
	 }
	 else if(id == 6)
	 {
		 hotelbox.style.display='none'; 
		 lastmindealsbox.style.display='none';
		 carbox.style.display='none';
		 hotelairbox.style.display='block';
		 clickstay('id3');
		 //alert(document.hotsrchbox.dod_dd.selectedIndex);
		 document.InitialSearchForm.dateReturningMonth.options.selectedIndex = document.hotsrchbox.dod_mm.options.selectedIndex;
		 document.InitialSearchForm.dateReturningDay.options.selectedIndex = document.hotsrchbox.dod_dd.options.selectedIndex;
		 document.InitialSearchForm.dateLeavingMonth.options.selectedIndex = document.hotsrchbox.doa_mm.options.selectedIndex;
		 document.InitialSearchForm.dateLeavingDay.options.selectedIndex = document.hotsrchbox.doa_dd.options.selectedIndex;
	 }
	 else 
	 {
		hotelbox.style.display='block';
		lastmindealsbox.style.display='none';
		carbox.style.display='none';
		hotelairbox.style.display='none';
		clickstay('id1');
	 }
}

function IsLeapYear(yrStr)
{
 var leapYear = false;
 var year = parseInt(yrStr, 10);
 if (year%4 == 0) 
  { 
	leapYear = true;
	if (year%100 == 0)
	 {
	  leapYear = false;
	  if (year%400 == 0)
	   {
	    leapYear = true;
		}
	  }
   }
  return leapYear;
 }


function getDaysInMonth(mthIdx, yrStr) 
{
 var maxDays = 31;
 if (mthIdx == 1)
	{
	 if (IsLeapYear(yrStr))
	   { 
	    maxDays = 29;
	   }
	 else

		{
		  maxDays = 28;
		}
     }
 if (mthIdx == 3 || mthIdx == 5 || mthIdx == 8 || mthIdx == 10)
	{
	 maxDays = 30;
	 }
 return maxDays;
}

function adjustDate(mthIdx, Dt, initDate)
{
var value = 0;

var today = new Date();
var theYear = parseInt(today.getYear(),10);

if (mthIdx < today.getMonth()) {
	theYear = (parseInt(today.getYear(),10) + 1);
}

if (theYear < 100) {
	theYear = "19" + theYear;
	}
else {
	if ((theYear - 100) < 10) {
		theYear = "0" + (theYear - 100);
		}
	else 
		{
		theYear = (theYear - 100) + "";
		}
	theYear = "20" + theYear
}

var numDays = getDaysInMonth(mthIdx, theYear);

 
// checks whether to initialize date or not.
if (initDate == 1) 
	{
	  if ((numDays - Dt)==7)
		{

		//checks if month is December
		if (mthIdx==11) {
			document.hotsrchbox.doa_mm.options.selectedIndex = mthIdx;
			document.hotsrchbox.dod_mm.options.selectedIndex = 0;
			document.hotsrchbox.dod_yy.options.selectedIndex =+1;

			document.CarSearch.pudate_mo.options.selectedIndex = mthIdx;
			document.CarSearch.dodate_mo.options.selectedIndex = 0;
		} else {
			document.hotsrchbox.doa_mm.options.selectedIndex = mthIdx;
			document.hotsrchbox.dod_mm.options.selectedIndex = mthIdx+1;

			document.CarSearch.pudate_mo.options.selectedIndex = mthIdx;
			document.CarSearch.dodate_mo.options.selectedIndex = mthIdx+1;
		}

		//hotel search box
	    document.hotsrchbox.doa_dd.options.selectedIndex = numDays - 1;
	    document.hotsrchbox.dod_dd.options.selectedIndex =  7 - (numDays - Dt);

		//car search box
		document.CarSearch.pudate_dy.options.selectedIndex = numDays - 1;
		document.CarSearch.dodate_dy.options.selectedIndex = 7 - (numDays - Dt);

		}
		else if ((numDays-Dt)<7)
				{

				if (mthIdx==11) {
					document.hotsrchbox.doa_mm.options.selectedIndex = 0;
					document.hotsrchbox.dod_mm.options.selectedIndex = 
					document.hotsrchbox.doa_mm.options.selectedIndex;
					document.hotsrchbox.doa_yy.options.selectedIndex =+1;
					document.hotsrchbox.dod_yy.options.selectedIndex =+1;

					document.CarSearch.pudate_mo.options.selectedIndex = 0;
					document.CarSearch.dodate_mo.options.selectedIndex = 
					document.CarSearch.pudate_mo.options.selectedIndex;
				} else {
					document.hotsrchbox.doa_mm.options.selectedIndex = mthIdx + 1;
					document.hotsrchbox.dod_mm.options.selectedIndex = document.hotsrchbox.doa_mm.options.selectedIndex;	

					document.CarSearch.pudate_mo.options.selectedIndex = mthIdx + 1;
					document.CarSearch.dodate_mo.options.selectedIndex = document.CarSearch.pudate_mo.options.selectedIndex;
				}					

				//hotel search box
				document.hotsrchbox.doa_dd.options.selectedIndex = 6 - (numDays - Dt);
				document.hotsrchbox.dod_dd.options.selectedIndex = 7 - (numDays - Dt);	

				//car search box
				document.CarSearch.pudate_dy.options.selectedIndex = 6 - (numDays - Dt);
				document.CarSearch.dodate_dy.options.selectedIndex = 7 - (numDays - Dt);

				}
		else
			{ 
			//hotel search box
			document.hotsrchbox.doa_mm.options.selectedIndex = mthIdx;
			document.hotsrchbox.doa_dd.options.selectedIndex = (Dt - 1) + 7;
			document.hotsrchbox.dod_mm.options.selectedIndex = document.hotsrchbox.doa_mm.options.selectedIndex;
			document.hotsrchbox.dod_dd.options.selectedIndex = (Dt - 1) + 8;

			//car search box
			document.CarSearch.pudate_mo.options.selectedIndex = mthIdx;
			document.CarSearch.pudate_dy.options.selectedIndex = (Dt - 1) + 7;
			document.CarSearch.dodate_mo.options.selectedIndex = document.CarSearch.pudate_mo.options.selectedIndex;
			document.CarSearch.dodate_dy.options.selectedIndex = (Dt - 1) + 8;
			}
		}
else
	{		
	if (mthIdx == 1)
		{
		if (Dt.options.selectedIndex + 1 < numDays) 
			{
			return 0;
			}
		else
			{
			Dt.options.selectedIndex=numDays - 1;
			if (numDays == 29)
				{
				return 99;
				}
			else
				{
				return 1;
				}
			}
		}
	 if (Dt.options.selectedIndex + 1 < numDays)
		{
		value = 0;
		}
	 else
		{
		if (Dt.options.selectedIndex + 1 > numDays)
			{
			Dt.options.selectedIndex--;
			value = 3;
			}
		else
			{
			value = 2;
			}
		}
	 return value;
	}
}


function amadChange(inM, inD, outM, outD)
{
 var res = adjustDate(inM.options.selectedIndex, inD, 0);
 if (res != 0 )
	{
		outD.options.selectedIndex = 0;
		if (outM.options.selectedIndex == 11) {
			outM.options.selectedIndex = 0;
			}
		else {
			outM.options.selectedIndex = inM.options.selectedIndex + 1;
			}
		}
 else
	{
	outM.options.selectedIndex = inM.options.selectedIndex;
	outD.options.selectedIndex = inD.options.selectedIndex + 1;
	}
return;
}

function initDate()
{
	var today = new Date();
	var currMth = today.getMonth();
	var currDate = today.getDate();

	var mth = document.hotsrchbox.doa_dd.options.selectedIndex = currMth;
	var Dt = document.hotsrchbox.doa_dd.options.selectedIndex = currDate;
	adjustDate(mth, Dt, 1);
}
	
function waitBox()
{
  var popup = window.open('http://reservations.perfectplacestravel.com/wait.html','wait','height=125 ,width=330,left=330,top=330');
}

function showCal(url)
	{
	calWindow = window.open(url, 'Calendar', 'scrollbars=YES, width=550,height=225');
	calWindow.focus;
	}

function disclaimerWin(tnc)
	{
	tncWindow = window.open(tnc, 'TermsAndConditions', 'width=325,height=500');
	tncWindow.focus;
	}

function airportCodes(url)
	{
	arptWindow = window.open(url, 'AirportCodes', 'width=450,height=350');
	arptWindow.focus;
	}

var anyopen = "id1";
function clickstay(tdid) {
	document.all[anyopen].className = "tabOff";
	document.all[tdid].className = "tabOn";
	anyopen=tdid;
	}
//-->
