var b_stopBit = 0; // Setting to enable Date Increments
var b_overide = 0; //Overide for Survey Popup

s_userAgent = window.navigator.userAgent
KON = (s_userAgent.indexOf("KHTML")>-1) ? 1 : 0;
isMac = (navigator.userAgent.indexOf("Mac")>-1) ? 1 : 0;
isIE = (navigator.userAgent.indexOf("MSIE")>-1) ? 1 : 0;
var isNN6 = (o_navigator.indexOf("Netscape6") > -1) ? 1 : 0;
isMacIE = (isMac&&isIE) ? 1 : 0;
var isOpera =(s_navigator.indexOf("opera")> -1) ? 1: 0;//opera
navTimer = null;
currentNav = 0;
var b_loaded = 0;
//global functions
//MACIE5.2 doesn't support push
if (!Array.prototype.push) Array.prototype.push = function (e) {this[this.length] = e}
//boosted from prototype.js
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')element = document.getElementById(element);
    if (arguments.length == 1) return element;
    elements.push(element);
  }
  return elements;
}
function cDisplay(){
	a_args = cDisplay.arguments;
	for(var i=0;i<a_args.length;i++){a_items=a_args[i].split(",");
		if($(a_items[0])){
			$(a_items[0]).style.display = a_items[1];
		}
	}
}
function cVis(){
	a_args = cVis.arguments;
	for(var i=0;i<a_args.length;i++){a_items=a_args[i].split(",");document.getElementById(a_items[0]).style.visibility = a_items[1];}
}
//setup
var oairType;
function getRadioSet(sSet){
	var oSet = $(sSet);
	var aInput = oSet.getElementsByTagName("input");
	var aRadio = new Array();
	for(i=aInput.length-1;i>-1;i--)if(aInput[i].type=="radio")aRadio.push(aInput[i]);
	return aRadio;
}
function getCheckedRadio(aGroup){
	var oRadio;
	for(var i=0;i<aGroup.length;i++)if(aGroup[i].checked)oRadio = aGroup[i];
return oRadio;
}
function cbSetUp(){
	var aCB = $("cb_formFH","cb_formCO","cb_formHO","cb_formair");
	var oFormNav = $("cbNavForm");
	var oMultiRoom,oNearByAir,oAirType;
	var oSubNav = 0;
	var oCB;
	getRadioSet("cbNavForm");
	var aInput = oFormNav.getElementsByTagName("input");
	var aNav = new Array();
	var aSubNav = new Array();
	for(var i=0;i<aInput.length;i++) if(aInput[i].name=="nav")aNav.push(aInput[i]);
	for(var i=0;i<aNav.length;i++)if(aNav[i].checked)oNav = aNav[i];
	for(var i=0;i<aInput.length;i++) if(aInput[i].name==oNav.value+"nav")aSubNav.push(aInput[i]);
	for(var i=aSubNav.length-1;i>-1;i--){
		if(aSubNav[i].checked){
			oSubNav = aSubNav[i];
			aSubNav[i].parentNode.className = "cbSubNavOn";
		}else aSubNav[i].parentNode.className = "";
	}
	if(!oSubNav){
		aSubNav[0].checked=true;
		oSubNav = aSubNav[0];
	}
	switch (oSubNav.value){
		case "p_h_f":
		case "h_f":
		case "f_h":
			showFHCarMsg("none");	
			oCB = $("cb_formFH");
			oMultiRoom = $("FH_totalrooms");		
		break;		
		case "p_h_f_c":
		case "h_f_c":
		case "c_f_h":
		case "f_h_c":
			showFHCarMsg("block");		
			oCB = $("cb_formFH");
			oMultiRoom = $("FH_totalrooms");
		break;
		case "h":
			oCB = $("cb_formHO");
			oMultiRoom = $("HO_totalrooms");
		break;
		case "c":
			oCB = $("cb_formCO");
			$("FCO_pickupoption").selectedIndex = 0;
		break;
		break;
		case "c_r":
			oCB = $("cb_formCO");
		break;
		case "f":
			oAirType = getCheckedRadio(getRadioSet("cb_typeselect"));//cb_typeselect$("radioexactDates");
			oNearByAir = $("cb_nba_input");
			oCB = $("cb_formair");
			childAges($("air_children"),'cbairchildages');
		break;
		default:
	}
	cbChangeNavTab(oNav);
	cbDisplayCB(aCB,oCB);
	if(oAirType)cb_airDateType(oAirType);
	if(oNearByAir)cb_nearByAirports(oNearByAir);
	if(oMultiRoom)multirooms(oMultiRoom);
	b_loaded = 1;
}
function cbDisplayCB(aCB,oCB){
	for(var i=aCB.length-1;i>-1;i--){
		switch(aCB[i]!=oCB){
		case true:
			if(aCB[i]){aCB[i].style.position = "absolute";}
			if(aCB[i]){aCB[i].style.visibility = "hidden";}
			if(aCB[i]){aCB[i].style.display = "none";}
			if(isMacIE)aCB[i].className="cbform";
		break;
		case false:
			if(aCB[i]){aCB[i].style.position = "relative";}
			if(aCB[i]){aCB[i].style.visibility = "visible";}
			if(aCB[i]){aCB[i].style.display = "block";}
			if(isMacIE)aCB[i].className="cbform cbformon";
		break;
		}
	}
}
function cbChangeNavTab(oNav){
	oNavList = $("cbNavList");
	aNavList = oNavList.childNodes
	for(var i=aNavList.length-1;i>-1;i--)if(aNavList[i].nodeName=="LI")aNavList[i].className = (aNavList[i].id=="cb"+oNav.value+"tab")?"cbNavon":"";
}
function cbChangeNav(oNavLink){
	oNavLink.previousSibling.checked = true;
	cbSetUp();
	return false;
}
//get form bit returns field prefix
function formbit(s_form){
	a_formbit= s_form.split("form");
	s_formbit = a_formbit[1];
	return s_formbit;
}
//maintain the state of like fields 
function maintainState(o_input){
	if (!b_loaded) return false;
	var a_bit = o_input.id.split("_");
	var s_bit = a_bit[1];
	var aCBsupported = ["air","FH","HO","CO","act"];
	for(var i=aCBsupported.length-1;i>-1;i--){
		s_formbit = aCBsupported[i];
		o_formelement = document.getElementById(s_formbit+"_"+s_bit);	
		if(eval("document.form"+s_formbit)&&o_formelement!=o_input){
			if(o_formelement&&o_formelement!=o_input && o_input.type!="radio"){
				if(o_formelement.type=="select-one"&&o_formelement.type==o_input.type){
					for(var j=o_formelement.length-1;j>-1;j--){
						if(o_formelement.options[j].text==o_input.options[o_input.selectedIndex].text){
							o_formelement.options[j].selected = true;
							break;
						}
					}
				}else if(o_formelement.type==o_input.type)o_formelement.value=o_input.value;
			}else{
				a_input = eval("document.form"+aCBsupported[i]+"."+o_input.name);
				if(a_input) for(j=0;j<a_input.length;j++)if(a_input[j]!=o_input&&a_input[j].value==o_input.value)a_input[j].checked="checked";
			}
		}
	}
}

//childages for multiroom
function multiroomchild(i_rooms,i_maxrooms,s_type,o_childrooms){
	o_childrooms.style.display = "none";
	i_childrows = 0;
	for(var i=0;i<i_maxrooms;i++){
		o_children = document.getElementById(s_type+"_child"+(i+1))
		if(!o_children)break;
		if(isNN6)o_children.focus();
		i_ages = o_children.selectedIndex;
		o_childrow = document.getElementById(s_type+"_childroom"+(i+1))
		if(i_ages>0&&i<i_rooms){
			i_childrows++;
			o_childrooms.style.display = "block";
			o_childrow = document.getElementById(s_type+"_childroom"+(i+1))
			a_select = o_childrow.getElementsByTagName("select");
			for(var j=0;j<o_children.options.length-1;j++){
				if(a_select[j]){
				a_select[j].parentNode.style.display = (j<i_ages)?"block":"none";
				}
			}
			o_childrow.style.display = "block";
		}else o_childrow.style.display = "none";
	}
	return i_childrows;
}
//multiroom function
function multirooms(o_rooms){
	if(typeof o_rooms=="string")o_rooms=document.getElementById(o_rooms);
	var o_caller = o_rooms;
	s_type = formbit(o_rooms.form.name);
	o_rooms = document.getElementById(s_type+"_totalrooms");
	
	if(isNN6)o_rooms.focus();
	i_rooms = parseInt(o_rooms.options[o_rooms.selectedIndex].text);
	o_multiroomchild = document.getElementById(s_type+"_multichildroom");
	switch (o_rooms.options[o_rooms.selectedIndex].text.indexOf("+")){
	case -1:
		if(document.getElementById("HO_grpmsg"))document.getElementById("HO_grpmsg").style.display = "none";
		i_maxrooms = parseInt(o_rooms.options[o_rooms.options.length-1].text);
		for(var i=0;i<i_maxrooms;i++)if(document.getElementById(s_type+"_room"+(i+1)))document.getElementById(s_type+"_room"+(i+1)).style.display="none";
		for(var i=0;i<i_rooms;i++)if(document.getElementById(s_type+"_room"+(i+1)))document.getElementById(s_type+"_room"+(i+1)).style.display="block";
		i_childrows = (o_multiroomchild)?multiroomchild(i_rooms,i_maxrooms,s_type,o_multiroomchild):0;
	break;
	default:
		if(o_multiroomchild)o_multiroomchild.style.display = "none";
		if(document.getElementById("HO_grpmsg"))document.getElementById("HO_grpmsg").style.display = "block";
		for(var i=0;i<i_rooms;i++)if(document.getElementById(s_type+"_room"+(i+1)))document.getElementById(s_type+"_room"+(i+1)).style.display="none";
	}
	if(isNN6)o_caller.focus();
	if(isOpera)cVis(s_type+"_moreoptions,hidden",s_type+"_moreoptions,visible");//screwy refresh issue on position:absolute;bottom:0px elements for op9.1
}
//flights
//Nearby airport functionality
function cb_nearByAirports(o_check){
	o_check.checked ? cDisplay("exactDatesCSA,inline","exactDatesnoCSA,none","nearbymsg,inline","cb_air_pmd,none","cb_air_fd,none") : cDisplay("exactDatesCSA,none","exactDatesnoCSA,inline","nearbymsg,none","cb_air_pmd,inline","cb_air_fd,inline");
	o_check.checked ? cVis("airFHSubmit,hidden"):cVis("airFHSubmit,visible");
}

function cb_airDateType(o_radio){
	if(o_radio == oairType)return;
	o_ul = o_radio.parentNode.parentNode;
	a_li = o_ul.getElementsByTagName("li");
	for(var i=0;i<a_li.length;i++)a_li[i].className="";
	o_radio.parentNode.className="typeselected";
	a_msgspan = document.getElementById("cb_typeselectmsgtxt").getElementsByTagName("span");
	for(var i=0;i<a_msgspan.length;i++)a_msgspan[i].style.display = "none";
	cDisplay(o_radio.value+",inline");
	switch(o_radio.value){
		case "exactDates":
			cDisplay("datedf,none","daterf,none","air_fromrowfd,none","air_torowfd,none");			
			cDisplay("datelt,inline","datert,inline","air_fromrowed,block","air_torowed,block","exactDatesnoCSA,inline");
			cVis("cb_air_nba,visible","airFHSubmit,visible");			
		break;
		case "plusMinusDates":
			cDisplay("datedf,inline","daterf,inline","air_fromrowed,block","air_torowed,block");
			cDisplay("datelt,none","datert,none","air_fromrowfd,none","air_torowfd,none");
			cVis("cb_air_nba,hidden","airFHSubmit,hidden");
		break;
		case "flexibleDates":
			cDisplay("air_fromrowfd,block","air_torowfd,block","air_fromrowed,none","air_torowed,none");
			cVis("cb_air_nba,hidden","airFHSubmit,hidden");
		break;
		default:
	}
	oairType = o_radio;
}
function childAges(o_select,s_div){
	o_div = document.getElementById(s_div)
	if(o_select.selectedIndex!=0){
		a_children = o_div.getElementsByTagName("div")
		o_div.style.display = "block";
		for(i=0;i<a_children.length;i++) a_children[i].style.visibility = (i<o_select.selectedIndex) ? "visible" : "hidden";
	}else{
		o_div.style.display = "none";
	}
}	
//cars
function carPUDO(o_select){
	switch(o_select.options[o_select.selectedIndex].value){
	case "pu_air":
		document.getElementById("fco_airport").style.display = "block";
		document.getElementById("fco_nearaddress").style.display = "none";
		document.getElementById("fco_nearplace").style.display = "none";
	break;
	case "pu_addr":
		document.getElementById("fco_airport").style.display = "none";
		document.getElementById("fco_nearaddress").style.display = "block";
		document.getElementById("fco_nearplace").style.display = "none";	
	break;
	case "pu_poi":
		document.getElementById("fco_airport").style.display = "none";
		document.getElementById("fco_nearaddress").style.display = "none";
		document.getElementById("fco_nearplace").style.display = "block";	
	break;
	default:
		window.location=o_select.options[o_select.selectedIndex].value;
	}
}
//show hide state/providence list
function cb_hoscope(o_select,s_state){
	switch(o_select.value.toLowerCase()){
		case "us":
		case "ca":
			document.getElementById(s_state).style.visibility = "visible";
		break;
		default:
			document.getElementById(s_state).style.visibility = "hidden";
	}
}
//hotels -- add form values to url for smarter advanced search needs rewrite
function addvars(url,o_form){
s_url=new String(url);
s_url+="&expr_path="+o_form.expr_path.value;
s_url+= (o_form.name=="formHOgbl") ? "&airport="+o_form.airport.value : "&city="+o_form.city.value;
s_url+="&searchMode="+o_form.searchMode.value;
o_hototalrooms = document.getElementById("HO_totalrooms");
s_url+="&numRooms="+o_hototalrooms.options[o_hototalrooms.selectedIndex].text;
s_url+="&leavingDate="+document.getElementById("HO_fromdate").value;
s_url+="&returningDate="+document.getElementById("HO_todate").value;
if(o_hototalrooms.options[o_hototalrooms.selectedIndex].text.indexOf("+")==-1){
	for(var i=1;i<o_hototalrooms.selectedIndex+2;i++){
		o_adult = document.getElementById("HO_adult"+i);
		s_url+="&adult"+(i)+"="+o_adult.options[o_adult.selectedIndex].text;
		o_child = document.getElementById("HO_child"+i);
		s_url+="&child"+(i)+"="+o_child.options[o_child.selectedIndex].text;
	}
}
s_url+="&state="+o_form.state.value;
if(o_form.cityCountryCode)s_url+="&cityCountryCode="+o_form.cityCountryCode.value;
parent.window.location=s_url;
}
//IEmac fix select --hidden select lists appear to be on the first option but are actually on other options
function fixMacIEselect(s_formdivid,o_radio){
o_formdiv = document.getElementById(s_formdivid)
a_modOptions = o_formdiv.getElementsByTagName("select");
if(a_modOptions)for(i=0;i< a_modOptions.length;i++){
	a_modOptions[i].focus();
	a_modOptions[i].blur();
}
o_radio.focus();
}

//forms and popunder control
function formCtrl(o_form){
	s_formName = o_form.name;
	var b_submit = false;
	var s_popFlag= arguments[1]||'0';
	switch (s_formName){  
		case 'formCO': //Car Only
		o_select = document.getElementById("FCO_pickupoption") 
		switch(o_select.options[o_select.selectedIndex].value){
			case "pu_poi": //Car by Point of Interest
				b_submit = checkform(o_form,2,3,6);
			break;
			case "pu_addr": //Car by Address
				b_submit = checkform(o_form,6)
			break;
			default:
				b_submit = checkform(o_form,1,2,3,6);
		}
		break;
		case 'formair': //Flight Only
			b_submit = checkform(document.formair,1,2,3,4,5,8);
		break;
		case 'formFH': //Flight & Hotel
			b_submit = checkform(o_form,1,2,3,4,8,9);
		break;
		case 'formFOW': //Flight One Way
			b_submit = checkform(o_form,1,2,3,4,5);
		break;
		case 'formHO': //Hotel Only
			b_submit = cb_hosubmit(o_form);
		break;
		case 'formact': //Activities
			b_submit = checkform(o_form,8);
		break;    
	}
	return b_submit; 
}
//checks for groople
function cb_hosubmit(o_form){
	b_form = false;

	var __fromDate = document.getElementById("HO_fromdate");
	var __fromMM = document.getElementById("HO_doa_mm");
	var __fromDD = document.getElementById("HO_doa_dd");
	var __fromYY = document.getElementById("HO_doa_yy");
	var __toDate = document.getElementById("HO_todate");
	var __toMM = document.getElementById("HO_dod_mm");
	var __toDD = document.getElementById("HO_dod_dd");
	var __toYY = document.getElementById("HO_dod_yy");

	returnSelectedDate(__toDate,__toYY,__toMM,__toDD);
	returnSelectedDate(__fromDate,__fromYY,__fromMM,__fromDD);

	b_form = ((__toDate.value=="mm/dd/yyyy"||__toDate.value=="")&&(__fromDate.value=="mm/dd/yyyy"||__fromDate.value==""))?checkform(o_form,1,2,3,9):checkform(o_form,1,2,3,8,9);

	return b_form;
}

function returnSelectedDate(dateField,year,month,day){

	if(dateField.value.toLowerCase() != "mm/dd/yyyy" && (dateField.value.toLowerCase() != "dd.mm.yyyy" && dateField.value.toLowerCase() != "dd/mm/yyyy")) {	
		var splitstring = '/';
		var inputDateArray = dateField.value.split(splitstring);
		if(inputDateArray.length == 3) {
			if (isNaN(parseInt(inputDateArray[0])) || isNaN(parseInt(inputDateArray[1])) || isNaN(parseInt(inputDateArray[2]))) {
				dateField.value="Invalid Date";
				dateField.style.background = "#fff url(/images-general/warningBG.gif) 100% 50% no-repeat";
				return false;		
			} else {
				//this.dateInput.style.background = "";
				dateField.style.background = "";
				(year) ? year.value = inputDateArray[2]: "";
				month.value = inputDateArray[0];
				day.value = inputDateArray[1];
				return true;
			}
		}
	} else {
		dateField.value="Enter Date";
		dateField.style.background = "#fff url(/images-general/warningBG.gif) 100% 50% no-repeat";
		return false;					
	}

	return false;
}

function getElementByValue(ElementCollection, Value)
{
    var i, Element;
     for (i = 0 ; i < ElementCollection.length ; i++)
     {
          if (ElementCollection[i].value == Value)
          {
               return ElementCollection[i];
          }
     }
     return null;
}

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 24*60*60*1000*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function GetCookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

//flight+hotel+car
function showFHCarMsg(s_display){
	$('cb_FHCarMsg').style.display = s_display;
}
function adjDate(o_form) {
 if (b_stopBit == 0) {   
  // Set Variables
  var a_selectedForm;
  var o_todays_date=new Date;  // Create new Date Object
  var a_currentYear=o_todays_date.getFullYear(); // Get the current Year
  var o_days = new Array(31, ((a_currentYear % 4 == 0 && a_currentYear % 100 != 0) || a_currentYear % 400 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // Set the days for each month
  var a_dateDiff = 3; // Number of days to Increment
  if(o_form){
    a_selectedForm = o_form.name;
  } else {
    a_selectedForm = getCBform(); // Get what form is in focus
  }	 
  var a_formPrefix = a_selectedForm.substring(4); // Obtain Prefix to use for element IDs
  var a_fromVal = document.getElementById(a_formPrefix+"_fromdate").value; // Form Value : From Date
  var a_fromMonth = document.getElementById(a_formPrefix+"_frommonth").selectedIndex; // Form Value : From Month 
 	
  // Check if they selected a date outside the Max Date
  if(a_fromVal > o_days[a_fromMonth]) {
   document.getElementById(a_formPrefix+"_fromdate").options[eval(o_days[a_fromMonth] - 1)].selected=true; // Correct Date
   var a_fromVal = document.getElementById(a_formPrefix+"_fromdate").value; // Reset the from Date variable
  } 
  
  var o_selectedDate=new Date(a_currentYear,a_fromMonth,a_fromVal); // Create a Date Obj from dates selected
  var o_toDate = o_selectedDate.setDate(o_selectedDate.getDate() + a_dateDiff); // Increment the Date
 
  // Set the form fields
  document.getElementById(a_formPrefix+"_tomonth").options[o_selectedDate.getMonth()].selected=true;
  document.getElementById(a_formPrefix+"_todate").options[(o_selectedDate.getDate() - 1)].selected=true;
  
  // Make sure all the Cannonball forms keep dates
  maintainState(document.getElementById(a_formPrefix+"_tomonth"));
  maintainState(document.getElementById(a_formPrefix+"_todate"));
  maintainState(document.getElementById(a_formPrefix+"_fromdate"));
  maintainState(document.getElementById(a_formPrefix+"_frommonth"));
  
  //Unset Variables
  var a_fromMonth;
  var a_fromVal;
  var a_currentYear;
  var selectedDate;
 }
}
function FHO_dates(oForm){
   var oPrefix = oForm.name.replace("form","");
   var dodVal = oForm.leavingDate.value;
   var doaVal = oForm.returningDate.value;
   var dodValues = dodVal.split("/");
   var dodMonth = dodValues[0];
   var dodDay = dodValues[1];
   var dodYear = dodValues[2];
   var doaValues = doaVal.split("/");
   var doaMonth = doaValues[0];
   var doaDay = doaValues[1];
   var doaYear = doaValues[2];
   switch (oPrefix) {
	   case "HO" :
		   $('dod_mm').value = dodMonth;
		   $('dod_dd').value = dodDay;
		   $('dod_yy').value = dodYear;
		   $('doa_mm').value = doaMonth;
		   $('doa_dd').value = doaDay;
		   $('doa_yy').value = doaYear;
	   break;
	   case "FH":
		   $('dateLeavingMonth').value = dodMonth;
		   $('dateLeavingDay').value = dodDay;
		   $('dateReturningMonth').value = doaMonth;
		   $('dateReturningDay').value = doaDay;
		   /*$('stops').value = oForm.numOfRooms.value;
		   $('adults').value = oForm.adult1.value;
		   $('children').value = oForm.child1.value;
		   $('seniors').value = oForm.senior1.value;*/
	   break;
   }
}
