//************************//
//*** MODIFYABLE PROPERTIES ***//
//**********************//
//Set qf_rightenabled and qf_lowerenabled to true if right and/or lower qf_sections are being used, 
//false otherwise
var qf_rightenabled = false; 
var qf_lowerenabled = false;
var qf_Club = "996"; 

//************************//
//*** 	UI SCRIPTS    ***//
//**********************//

//where tab images are kept
var qf_imgpath = "qfimages/"; 
//qf_sections definition, contains keywords for qf_sections that are being used in all id's for
//criteria, dynamic lower and right qf_sections, tab img ids and tab anchor ids
var qf_sections = ["flights", "cars", "hotels", "cruises","vacations", "directions" ];
//variable that holds current section, used for referring to current section in quick finder
var qf_cursect = qf_sections[0];

//loadQfTabImgs loads images for dormant states of all tabs
function loadQfTabImgs()
{
	var tabimgs = new Array(qf_sections.length);
	for (var i=0;i<tabimgs.length;i++)
	{
		tabimgs[i] = new Image();
		tabimgs[i].src = i == 0 ? qf_imgpath + qf_sections[i] + "_off.gif" : qf_imgpath + qf_sections[i] + "_on.gif";
	}
}

//Sets tabs for new section
function SetQfTabs(newtabname)
{	
	ResetQfTab(qf_cursect, true);
	HighlightQfTab(newtabname); 
}

//Hides old criteria form, displays new one and sets qf_cursect var for new section
function SetQfCriteriaAndSection(tabname)
{
	try
	{
		getElem(qf_cursect+"Criteria").style.display = "none"; 
		getElem(tabname+"Criteria").style.display = "inline"; 
		changeCriteriaDisplay(tabname);
	}
	catch(e){}
	
	qf_cursect = tabname;
}

//Displays content beneath line at bottom of quickfinder
//also sets optional lower and right qf_sections in page content
function SetQfDynamicContent(tabname)
{
	try
	{
		getElem(qf_cursect+"LC").style.display = "none";
		getElem(tabname+"LC").style.display = "inline";
	}
	catch(e){}

		
	if(qf_rightenabled)
	{
		getElem(qf_cursect+"Right").style.display = "none"; 
		getElem(tabname+"Right").style.display = "inline"; 
	}
	if(qf_lowerenabled)
	{	
		getElem(qf_cursect+"Lower").style.display = "none"; 
		getElem(tabname+"Lower").style.display = "inline"; 
	}
	
	appendQfSubForms(tabname); 
}

function appendQfSubForms(tabname)
{
	if(tabname == "flights" || tabname == "cars" || tabname == "hotels" || tabname == "vacations" )
	{
		var parentEl = getElem(tabname + "Criteria"); 
		var subForm1 = getElem("FHC_FHCriteria"); 
		var subForm2 = getElem("HCCriteria"); 
		parentEl.appendChild(subForm1); 
		parentEl.appendChild(subForm2); 
	}
}

//Called when tab is selected, performs necessary action for tab switching
function SwitchQfTab(tabname)
{ 
	hideDynamicCriteriaForms(); 
	SetQfTabs(tabname);
	SetQfDynamicContent(tabname); 
	SetQfCriteriaAndSection(tabname);
}

//Highlights a tab, on mouse over or when switching tabs
function HighlightQfTab(tabname)
{
	var imgtag = getElem("img" + tabname);
	imgtag.src = qf_imgpath + tabname + "_on.gif"; 
}

//Resets a tab, on mouse out or when switching tabs
function ResetQfTab(tabname, bIgnoreSect)
{
	if(tabname != qf_cursect || bIgnoreSect == true)
	{
		var imgtag = getElem("img" + tabname);
		imgtag.src = qf_imgpath + tabname + "_off.gif"; 
	}
}

//HighlightQfButtonImg and ResetQfButtonImg for highlighting buttons on focus
function HighlightQfButtonImg(tag)
 {
 	try
	{
	if(tag.childNodes[0].tagName) tag.childNodes[0].onmouseover();
	else tag.childNodes[1].onmouseover();
	}
	catch(e){}
 }
 
function ResetQfButtonImg(tag)
 {
 	try
	{
	if(tag.childNodes[0].tagName) tag.childNodes[0].onmouseout();
	else tag.childNodes[1].onmouseout();
	}
	catch(e){}
 }
 
function initQfPageContent()
{
	try
	{
		getElem("qfDivLower").style.display = qf_lowerenabled ? "inline" : "none"; 
		getElem("qfDivRight").style.display = qf_rightenabled ? "inline" : "none"; 
		if(!qf_rightenabled)
		{
			getElem("qfDivRight").style.width = "0"; 
			getElem("qfTdRight").width = "0"; 
		}
		if(!qf_lowerenabled)
		{
			getElem("qfDivLower").style.height = "0";
		}
	}
	catch(e){ if(dbg) window.status = "initQfPageContent : lower or right section not found"; }
}
//******************************************//
//*** 	PAGE LOAD AND COMMON SCRIPTS    ***//
//****************************************//
var dbg = false;

//getElem is shorthand for "document.getElementByID"
function getElem(ID)
 {
 	var el = document.getElementById(ID);
	if(!el && dbg) window.status = "getElem : Element " + ID + " was not found"; 
	return el;
 }

//call load page for anything that needs to be done on page load. 
function LoadQfPage()
{
	qf_imgpath = bLoc ? "qfimages/" : "/aaa/"+qf_Club+"/quickfinder/qfimages/"; 
	loadQfTabImgs();
	initQfPageContent();
	initializeTravelDates();
	SwitchQfTab("hotels");
}

//replaces the body onload event since we can't call it in environment
function qf_Load()
{
	if(document.all)
	{
		if(document.readyState == "complete")
		{
			LoadQfPage();
			//alert("ie loaded");
		}
		else
			window.setTimeout(qf_Load, 100); 
	}
	else
	{
		LoadQfPage();
		//alert("Moz loaded");
	}	
}

//load eventlistener for NN and FF Moz browsers, to call above Load function
try { document.addEventListener("DOMContentLoaded", qf_Load, null); } catch(e){}

//*************************//
//*** 	FORM SCRIPTS    ***//
//*************************//

//for testing, set bLoc = true for testing forms locally, referenced by DoQfSubmit function
//false when file is online
var bLoc = false;

function DoQfSubmit(form, bSubmit)
{
	var prefix = "http://stager.aaa.com" ; 
	if(bLoc && dbg && form.action.indexOf(prefix)==-1)form.action = prefix + form.action;
	if(bSubmit != false) form.submit();
	return true;
}

function advanceSearch(criteriaForm)
{
  // Perform an "Advance" search
  performSearch(criteriaForm, true);
}

function advancePackageSearch(criteriaForm)
{
	if(!criteriaForm)
	{
	var theform = getElem(qf_cursect + "PackageSearch"); 
	
	if(!theform) return; 
	
	var searchtype = getCheckedVal(theform.searchType);
	
	if (searchtype == "TOURFH" || searchtype == "TOURFHC")
		criteriaForm = 'Tour_FHC_FH_Search'
	else
		criteriaForm = 'Tour_HC_Search'
	}
	// Perform an "Advance" package search
	performPackageSearch(criteriaForm, true);
}

function hideDynamicCriteriaForms()
{
	getElem('HCCriteria').style.display="none";
	getElem('FHC_FHCriteria').style.display="none";
	getElem('vacationsSubCriteria').style.display="none"; 
	getElem('carsCriteriaForm').style.display="none";
	getElem('hotelsCriteriaForm').style.display="none";
	getElem('flightsCriteriaForm').style.display="none";
}

function getCheckedVal(radiobuttons)
{
for (var i=0;i<radiobuttons.length;i++)
  {
	if(radiobuttons[i].checked==true)
	{
		return radiobuttons[i].value;
	}
  }
  return null; 
}

function changeCriteriaDisplay(sectname)
{
  // Update the tour criteria section based on the package search type
  if(sectname == "hotels" || sectname == "cars" || sectname == "flights" || sectname == "vacations")
		var packagesearchform = getElem(sectname + "PackageSearch"); 
  else
  	return; 
 
  var theselection = getCheckedVal(packagesearchform.searchType); 
  
  switch(theselection)
  { 
  	case "flights"  :
	case "cars" 	: 
	case "hotels"	: 
		hideDynamicCriteriaForms(); 
		//show the default criteria page
		getElem(theselection + "CriteriaForm").style.display = "inline"; 
	break;

	case "TOURFHC" : 
		// Hide the  other criteria qf_sections
		hideDynamicCriteriaForms(); 
		// Show the Flight/Hotel/Car criteria section
		getElem('vacationsSubCriteria').style.display="inline";
		getElem('FHC_FHCriteria').style.display="inline";
	
		// Update the child selection display
		updateChildSelectList('Tour_FHC_FH_Search');
	
		// Set the tour search type to Flight/Hotel/Car
		document.Tour_FHC_FH_Search.searchType.value="TOURFHC";
	
	break; 
	case "TOURFH" : 
		hideDynamicCriteriaForms(); 
		// Show the Flight/Hotel criteria section
		getElem('vacationsSubCriteria').style.display="inline";
		getElem('FHC_FHCriteria').style.display="inline";
		
		// Update the child selection display
		updateChildSelectList('Tour_FHC_FH_Search');
		
		// Set the tour search type to Flight/Hotel
		document.Tour_FHC_FH_Search.searchType.value="TOURFH";
	
	break; 
	case "TOURHC": 
		hideDynamicCriteriaForms(); 
		// Show the Hotel/Car criteria section
		getElem('vacationsSubCriteria').style.display="inline";
		getElem('HCCriteria').style.display="inline";
	
		// Update the child selection display
		updateChildSelectList('Tour_HC_Search');
	
		// Set the tour search type to Hotel/Car
		document.Tour_HC_Search.searchType.value="TOURHC";

	break;
  }

}

// Create an array for maintaining the 3-character month abbreviations
var monthNames = ["Jan","Feb","Mar","Apr","May","Jun",
				  "Jul","Aug","Sep","Oct","Nov","Dec"];

// Return the 3-character month abbreviation corresponding to the numeric month value						 
function getMonthName(month)
{
  return monthNames[month];
}

// Initialize the travel dates for each of the criteria qf_sections
function initializeTravelDates()
{
  // Get the current date
  var currentDate=new Date();

  // Initialize the travel dates on the air criteria display
  setAirTravelDates(currentDate, "AirSearch");

  // Initialize the travel dates on the package flight/hotel/car criteria display
  setAirTravelDates(currentDate, "Tour_FHC_FH_Search");

  // Initialize the travel dates on the car criteria display
  setCarTravelDates(currentDate, "CarSearch");

  // Initialize the travel dates on the hotel criteria display
  setHotelTravelDates(currentDate, "HotelSearch");

  // Initialize the travel dates on the package hotel/car criteria display
  setHotelTravelDates(currentDate, "Tour_HC_Search");
}

function performSearch(criteriaForm, advanceSearch)
{
  // Check if an "Advance" search is to be performed
  if (advanceSearch)
  {
	// Set the advance search form element to yes
	getElem(criteriaForm).advancedSearch.value='YES';
  }
  else
  {
	if (getElem(criteriaForm).advancedSearch != null)
	{
	  // Reset the advance search form element to no
	  getElem(criteriaForm).advancedSearch.value='NO';
	}
  }

  // Submit the form

  DoQfSubmit(getElem(criteriaForm)); 
}


function performPackageSearch(criteriaForm, advanceSearch)
{
  var winName;

  var winParams;

  var frm = getElem(criteriaForm); 
  
  // Check if an "Advance" search is to be performed
  if (advanceSearch)
  {
	// Set the advance search form element to yes
	frm.advancedSearch.value='YES';
  }
  else
  {
	// Reset the advance search form element to no
	frm.advancedSearch.value='NO';
  }

  // Set the attributes for the pop-up window
  winParams  = "menubar=0,";
  winParams += "toolbar=0,";
  winParams += "location=0,";
  winParams += "status=0,";
  winParams += "scrollbars=1,";
  winParams += "resizable=1,";
  winParams += "top=50,";
  winParams += "left=50,";
  winParams += "height=900,";
  winParams += "width=800";

  // Set the window name for the pop-up
  winName    = "packageWindow";

  // Open the pop-up window
  window.open('', winName, winParams);

  // Set the target form element to the name of the pop-up window
  frm.target=winName;

  // Submit the form
  DoQfSubmit(frm);
}

function setAirTravelDates(currentDate, criteriaForm)
{
  var cForm = getElem(criteriaForm); 
  if(!cForm) return; 
  
  // Set the depart date to 21 days from the current date
  var tempDate=new Date(currentDate);
  tempDate.setUTCDate(currentDate.getUTCDate() + 21);
  
  with (cForm)
  {
	  depMonth.value=getMonthName(tempDate.getMonth());
	  depDay.value=tempDate.getDate();
	
	  // Set the return date to 7 days from the depart date
	  tempDate.setUTCDate(tempDate.getUTCDate() + 7);
	
	  retMonth.value=getMonthName(tempDate.getMonth());
	  retDay.value=tempDate.getDate();
  }
}

function setCarTravelDates(currentDate, criteriaForm)
{
  var cForm = getElem(criteriaForm);
  if(!cForm) return; 
  // Set the pick-up date to 21 days from the current date
  var tempDate=new Date(currentDate);
  tempDate.setUTCDate(currentDate.getUTCDate() + 21);
  
  with (cForm)
  {
	  pickupMonth.value=getMonthName(tempDate.getMonth());
	  pickupDay.value=tempDate.getDate();
	
	  // Set the drop-off date to 7 days from the pick-up date
	  tempDate.setUTCDate(tempDate.getUTCDate() + 7);
	
	  dropoffMonth.value=getMonthName(tempDate.getMonth());
	  dropoffDay.value=tempDate.getDate();
  }

}

function setHotelTravelDates(currentDate, criteriaForm)
{
	var cForm = getElem(criteriaForm);
	if(!cForm) return; 
	// Set the check-in date to 7 days from the current date
	var tempDate=new Date(currentDate);
	tempDate.setUTCDate(currentDate.getUTCDate() + 7);

 	with (cForm)
 	{
	  checkinMonth.value=getMonthName(tempDate.getMonth());
	  checkinDay.value=tempDate.getDate();
	
	  // Set the check-out date to 2 days from the check-in date
	  tempDate.setUTCDate(tempDate.getUTCDate() + 2);
	
	  checkoutMonth.value=getMonthName(tempDate.getMonth());
	  checkoutDay.value=tempDate.getDate();
  	}
}

// Set the return month equal to the departure month
function updateAirReturnMonth(criteriaForm)
{
  getElem(criteriaForm).retMonth.value=getElem(criteriaForm).depMonth.value;
}

// Set the drop-off month equal to the pick-up month
function updateCarDropoffMonth(criteriaForm)
{
  getElem(criteriaForm).dropoffMonth.value=getElem(criteriaForm).pickupMonth.value;
}

// Appropriately enable/disable the child age dropdowns based on the number of children selected
function updateChildSelectList(criteriaForm)
{
  if (getElem(criteriaForm).children.options[getElem(criteriaForm).children.selectedIndex].text=='0')
  {
	getElem(criteriaForm).childage1.disabled=true;
	getElem(criteriaForm).childage2.disabled=true;
	getElem(criteriaForm).childage3.disabled=true;
  }
  else if (getElem(criteriaForm).children.options[getElem(criteriaForm).children.selectedIndex].text=='1')
  {
	getElem(criteriaForm).childage1.disabled=false;
	getElem(criteriaForm).childage2.disabled=true;
	getElem(criteriaForm).childage3.disabled=true;
  }
  else if (getElem(criteriaForm).children.options[getElem(criteriaForm).children.selectedIndex].text=='2')
  {
	getElem(criteriaForm).childage1.disabled=false;
	getElem(criteriaForm).childage2.disabled=false;
	getElem(criteriaForm).childage3.disabled=true;
  }
  else if (getElem(criteriaForm).children.options[getElem(criteriaForm).children.selectedIndex].text=='3')
  {
	getElem(criteriaForm).childage1.disabled=false;
	getElem(criteriaForm).childage2.disabled=false;
	getElem(criteriaForm).childage3.disabled=false;
  }
}

// Set the check-out month equal to the check-in month
function updateHotelCheckoutMonth(criteriaForm)
{
  getElem(criteriaForm).checkoutMonth.value=getElem(criteriaForm).checkinMonth.value;
}
