var selectedChannel='';
var selectedSection='';
var cookieName ='';
var paramValues = new Array();
var tempparamValues = {};
var domainname=null;
var qryString='';
var adserver='www.revresda.com';
function swapWidgetForm (product) {
	productArray = new Array ("flightsAwayBot", "hotelsAwayBot", "carsAwayBot" ,"dealsAwayBot" );
	imageArray = new Array ("flights", "hotels", "cars" ,"deals" );
	var imghostUrl = "http://media.away.com/plan-a-trip/"; 
	for (i = 0; i < productArray.length; i++) {
		if (productArray[i] == product) {
			document.getElementById(product + "Form").style.display = "block";
			document.getElementById("compareNav_" + product).src = imghostUrl + imageArray[i] + "_on.gif";
		} else { 
			document.getElementById(productArray[i] + "Form").style.display = "none";
			document.getElementById("compareNav_" + productArray[i]).src =  imghostUrl + imageArray[i] + "_off.gif";
		}
	}
}



function okHandler(type,args,passedObj) {
   var myCal = passedObj.myCal;
   var myDialog = passedObj.myDialog;
   var myElement = passedObj.myElement; 

   if (myCal.getSelectedDates().length > 0) {
       var selDate = myCal.getSelectedDates()[0];
       
       var dStr =selDate.getDate();
       var mStr = selDate.getMonth()+1;
       
       var yStr = selDate.getFullYear();
       YAHOO.util.Dom.get(myElement).value =  addLeadingZero(mStr) + "/" + addLeadingZero(dStr) + "/"  + yStr;
   } else {
       YAHOO.util.Dom.get(myElement).value = "";
   }
  myDialog.hide();
   
}

function addLeadingZero(dateString) {
    if(dateString <10) {
        dateString = "0"+dateString;
    }
    return dateString;
}

function chooseTabCompareRates() {
		var isAllchecked=false;
		getAllQueryVariable();
		if(typeof(getQueryVariable("all")) != undefined && getQueryVariable("all")!= null 
			&& getQueryVariable("all")=='true' && isBot){
				isAllchecked=true;
				populateData(paramValues,"flights");
				populateData(paramValues,"hotels");
				populateData(paramValues,"cars");
				populateData(paramValues,"package");
		}
		var overrideproducttype=getQueryVariable("type");
		if (!isBot &&( typeof (getQueryVariable('type')) == undefined
     		|| getQueryVariable('type') ==null || getQueryVariable('type') =='')){
				overrideproducttype=getOverrideProductType(window.location.href);
		}
        switch (overrideproducttype) {
          case "hotel" :			 			
			selectedChannel='hotel';
			if(!isAllchecked){
				populateData(paramValues,"hotels");
			}
			if(isBot){
				swapProductForm("hotels");
			}else{
				swapBotNav("hotels");
			}
          	break;
          case "car" :
			   selectedChannel='car';
			if(!isAllchecked){
				populateData(paramValues,"cars");
			}
			if(isBot){
				swapProductForm("cars");      
			}else{
				swapBotNav("cars");
			}
         	break;
          case "vacation" :
			if(isBot){
				populateData(paramValues,"vacations");
				swapProductForm("vacations");      
			}     
          	break;
          case "bnb" :
			if(isBot){
				populateData(paramValues,"bnb");
				swapProductForm("b&bs");      
			}     
          	break;
          case "package":
          		if(!isAllchecked){
          			populateData(paramValues,"package");
          		}
          		if(isBot){
          			swapProductForm("packages");
          		}else{
          			swapBotNav("packages");
				}
          		break;
          case "cruise":
          		populateData(paramValues,"cruise");
          		if(isBot){
          			swapProductForm("cruises");
          		}else{
          			swapBotNav("cruises");
				}
				break;
		  case "vacationrentals":
          		populateData(paramValues,"rentals");
          		if(isBot){
          			swapProductForm("rentals");
          		}else{
          			swapBotNav("rentals");
				}
				break;
          default: 
			selectedChannel='air';
			if(!isAllchecked){
				populateData(paramValues,"flights");
			}
			if(isBot){
				swapProductForm("flights");
			}else{
				swapBotNav("flights");
			}
    	}
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0].toLowerCase() == variable.toLowerCase()) {
      return pair[1];
    }
  } 
 
}


function getAllQueryVariable() {
  window.location.search.substring(1).replace(/([^&=]+)([=]([^&]*))?(&|$)/g,function(queryparams,paramname,param,paramvalue){
        if (undefined == tempparamValues[paramname]) {
            tempparamValues[paramname] = [];
        }
        tempparamValues[paramname].push(decodeURIComponent(paramvalue));
    });
    for (var paramname in tempparamValues) {
		var tempkey = paramname.toLowerCase();
        var values = tempparamValues[paramname];
        qryString = qryString+"&"+tempkey+"="+encodeURIComponent(((1 == values.length) ? values[0] : values));
		paramValues[tempkey]=((1 == values.length) ? values[0] : values);
    }
	
}


function getDomain()
{
	var domainname= document.domain ;
	if(domainname=='away.com') {
		domainname='away.com';
		cookieName='awyprd';
	}else if(domainname=='vacation-staging.away.com'){
		domainname='away.com';
		cookieName='awystg';
	}
	else if(domainname == 'www.trip.com'){
		domainname='trip.com';
		cookieName='awyprd';
	}else if(domainname == 'uk.trip.com'){
		cookieName='awyprd';
	}else{
		domainname='trip.com';
		cookieName='awystg';
		adserver='www.revresdatest.com';
	}
	
	return domainname;

}





function populateData(elm,producttype)
{
		var fieldsDisable=false;
		if(typeof(getQueryVariable("fieldsdisable")) != undefined && getQueryVariable("fieldsdisable")!= null 
			&& getQueryVariable("fieldsdisable")=='true' && isBot){
			fieldsDisable=true;
		}
		var populateDataDateFormat='MM/dd/yyyy';
		if(calendarUSDateFormat=='dd/mm/yy'){
			populateDataDateFormat='dd/MM/yyyy'
		}
		if(producttype =='flights')
		{
				if(typeof(elm['airfrom']) != undefined && elm['airfrom']!=null && elm['airfrom']!='' && document.airFormCompareRates.airFrom!=null && document.airFormCompareRates.airFrom !=undefined){
					document.airFormCompareRates.airFrom.value=decodeURI(elm['airfrom']);
					
				}
				if(typeof(elm['airto']) != undefined && elm['airto']!=null && elm['airto']!='' && document.airFormCompareRates.airTo!=null && document.airFormCompareRates.airTo!=undefined){
					document.airFormCompareRates.airTo.value=decodeURI(elm['airto']);
					if(fieldsDisable){
						document.airFormCompareRates.airTo.disabled=true;
					}
				}
				if(typeof(elm['deptdate']) != undefined && elm['deptdate']!=null && elm['deptdate']!='' && document.airFormCompareRates.dateFlOut!=null && document.airFormCompareRates.dateFlOut !=undefined){
				
					document.airFormCompareRates.dateFlOut.value=formatDate(parseDate(elm['deptdate']),populateDataDateFormat);
				}
				if(typeof(elm['rtndate']) != undefined && elm['rtndate']!=null && elm['rtndate']!='' && document.airFormCompareRates.dateFlIn!=null && document.airFormCompareRates.dateFlIn!=undefined){
					document.airFormCompareRates.dateFlIn.value=formatDate(parseDate(elm['rtndate']),populateDataDateFormat);
				}
				if(typeof(elm['travelers']) != undefined && elm['travelers']!=null && elm['travelers']!='' && document.airFormCompareRates.travelers!=null && document.airFormCompareRates.travelers!=undefined){
					document.airFormCompareRates.travelers.value=decodeURI(elm['travelers']);
				}
				if(typeof(elm['childtravelers']) != undefined && elm['childtravelers']!=null && elm['childtravelers']!='' && document.airFormCompareRates.childtravelers!=null && document.airFormCompareRates.childtravelers!=undefined){
					document.airFormCompareRates.childtravelers.value=decodeURI(elm['childtravelers']);
				}
		}
		else if(producttype =='hotels')
		{
				if(typeof(elm['howhere']) != undefined && elm['howhere']!=null && elm['howhere']!='' && document.hotelForm.hoWherebox!=null && document.hotelForm.hoWherebox!=undefined){
					document.hotelForm.hoWherebox.value=decodeURI(elm['howhere']);
					if(fieldsDisable){
						document.hotelForm.hoWherebox.disabled=true;
					}
				}
				if(typeof(elm['checkin']) != undefined && elm['checkin']!=null && elm['checkin']!='' && document.hotelForm.dateHoIn!=null && document.hotelForm.dateHoIn!=undefined){
					document.hotelForm.dateHoIn.value=formatDate(parseDate(elm['checkin']),populateDataDateFormat);
				}
				if(typeof(elm['checkout']) != undefined && elm['checkout']!=null && elm['checkout']!='' && document.hotelForm.dateHoOut!=null && document.hotelForm.dateHoOut!=undefined){
					document.hotelForm.dateHoOut.value=formatDate(parseDate(elm['checkout']),populateDataDateFormat);
				}
				if(typeof(elm['guests']) != undefined && elm['guests']!=null && elm['guests']!='' && document.hotelForm.hoGuests!=null && document.hotelForm.hoGuests!=undefined){
					document.hotelForm.hoGuests.value=decodeURI(elm['guests']);
				}
				if(typeof(elm['rooms']) != undefined && elm['rooms']!=null && elm['rooms']!='' && document.hotelForm.hoRooms!=null && document.hotelForm.hoRooms!=undefined){
					document.hotelForm.hoRooms.value=decodeURI(elm['rooms']);
				}
		}
		else if(producttype =='cars')
		{
			if(typeof(elm['pickup']) != undefined && elm['pickup']!=null && elm['pickup']!='' && document.carForm.carPickCityBox!=null && document.carForm.carPickCityBox!=undefined){
					document.carForm.carPickCityBox.value=decodeURI(elm['pickup']);
					if(fieldsDisable){
						document.carForm.carPickCityBox.disabled=true;
					}
				}
				if(typeof(elm['pickupdate']) != undefined && elm['pickupdate']!=null && elm['pickupdate']!='' && document.carForm.dateCarIn!=null && document.carForm.dateCarIn!=undefined){
					document.carForm.dateCarIn.value=formatDate(parseDate(elm['pickupdate']),populateDataDateFormat);
				}
				if(typeof(elm['dropoffdate']) != undefined && elm['dropoffdate']!=null && elm['dropoffdate']!='' && document.carForm.dateCarOut!=null && document.carForm.dateCarOut!=undefined){
					document.carForm.dateCarOut.value=formatDate(parseDate(elm['dropoffdate']),populateDataDateFormat);
				}
				if(typeof(elm['pickuptime']) != undefined && elm['pickuptime']!=null && elm['pickuptime']!='' && document.carForm.pickupTime!=null && document.carForm.pickupTime!=undefined){
					document.carForm.pickupTime.value=decodeURI(elm['pickuptime']);
				}
				if(typeof(elm['dropofftime']) != undefined && elm['dropofftime']!=null && elm['dropofftime']!='' && document.carForm.returnTime!=null && document.carForm.returnTime!=undefined){
					document.carForm.returnTime.value=decodeURI(elm['dropofftime']);
				}

		}

		else if(producttype =='package')
		{
				if(typeof(elm['packagefrom']) != undefined && elm['packagefrom']!=null && elm['packagefrom']!='' && document.packagesFormCompareRates.packageFrom!=null && document.packagesFormCompareRates.packageFrom!=undefined){
					document.packagesFormCompareRates.packageFrom.value=decodeURI(elm['packagefrom']);
				}
				if(typeof(elm['packageto']) != undefined && elm['packageto']!=null && elm['packageto']!='' && document.packagesFormCompareRates.packageTo!=null && document.packagesFormCompareRates.packageTo!=undefined){
					document.packagesFormCompareRates.packageTo.value=decodeURI(elm['packageto']);
					if(fieldsDisable){
						document.packagesFormCompareRates.packageTo.disabled=true
					}
				}
				if(typeof(elm['datepkout']) != undefined && elm['datepkout']!=null && elm['datepkout']!='' && document.packagesFormCompareRates.datePkOut!=null && document.packagesFormCompareRates.datePkOut!=undefined){
					document.packagesFormCompareRates.datePkOut.value=formatDate(parseDate(elm['datepkout']),populateDataDateFormat);
				}
				if(typeof(elm['datepkin']) != undefined && elm['datepkin']!=null && elm['datepkin']!='' && document.packagesFormCompareRates.datePkIn!=null && document.packagesFormCompareRates.datePkIn!=undefined){
					document.packagesFormCompareRates.datePkIn.value=formatDate(parseDate(elm['datepkin']),populateDataDateFormat);
				}

				if(typeof(elm['packageadults']) != undefined && elm['packageadults']!=null && elm['packageadults']!='' && document.packagesFormCompareRates.packageAdults!=null && document.packagesFormCompareRates.packageAdults!=undefined){
					document.packagesFormCompareRates.packageAdults.value=decodeURI(elm['packageadults']);
				}
				if(typeof(elm['packagechildren']) != undefined && elm['packagechildren']!=null && elm['packagechildren']!='' && document.packagesFormCompareRates.packageChildren!=null && document.packagesFormCompareRates.packageChildren!=undefined){
					document.packagesFormCompareRates.packageChildren.value=decodeURI(elm['packagechildren']);
				}
				
		}
		else if(producttype =='cruise')
		{
				
				if(typeof(elm['cruisedestination']) != undefined && elm['cruisedestination']!=null && elm['cruisedestination']!='' && document.cruisesFormCompareRates.cruiseDestination!=null && document.cruisesFormCompareRates.cruiseDestination!=undefined){
				var cruiseDestinationlength= document.cruisesFormCompareRates.cruiseDestination.options.length;		
					for (var i = 0; i < cruiseDestinationlength; i++) {					
						var value= document.cruisesFormCompareRates.cruiseDestination.options[i].value;
						if(value.toLowerCase() == elm['cruisedestination'].toLowerCase())
							{
								document.cruisesFormCompareRates.cruiseDestination.options[i].selected=true;
								break;
							}
					}
				}
				
				if(typeof(elm['datecruout']) != undefined && elm['datecruout']!=null && elm['datecruout']!='' && document.cruisesFormCompareRates.dateCruOut!=null && document.cruisesFormCompareRates.dateCruOut!=undefined){
					datesForCruises();	
					var cruiseDate=new Date(elm['datecruout']);
					var cruiseDateFormated=formatDate(cruiseDate,'M/1/yyyy');
					var datecruoutlength= document.cruisesFormCompareRates.dateCruOut.options.length;		
					for (var i = 0; i < datecruoutlength; i++) {
						var value= document.cruisesFormCompareRates.dateCruOut.options[i].value;
							if(value ==cruiseDateFormated)
							{
								document.cruisesFormCompareRates.dateCruOut.options[i].selected=true;
								break;
							}
							
					}
					//document.cruisesFormCompareRates.dateCruOut.value=formatDate(parseDate(elm['datecruout']),populateDataDateFormat);
				}
				if(typeof(elm['cruiselength']) != undefined && elm['cruiselength']!=null && elm['cruiselength']!='' && document.cruisesFormCompareRates.cruiseLength!=null && document.cruisesFormCompareRates.cruiseLength!=undefined){
					var cruiselength= document.cruisesFormCompareRates.cruiseLength.options.length;		
					for (var i = 0; i < cruiselength; i++) {
					
						var value= document.cruisesFormCompareRates.cruiseLength.options[i].value;
						
							if(value ==elm['cruiselength'])
							{
								document.cruisesFormCompareRates.cruiseLength.options[i].selected=true;
								break;
							}
							
					}
				}
				if(typeof(elm['overfiftyfive']) != undefined && elm['overfiftyfive']!=null && elm['overfiftyfive']!='' && document.cruisesFormCompareRates.overFiftyFive!=null && document.cruisesFormCompareRates.overFiftyFive!=undefined){
					if(elm['overfiftyfive'].toLowerCase()  =='t')
						{
							document.cruisesFormCompareRates.overFiftyFive.checked=true;
						}else{
							document.cruisesFormCompareRates.overFiftyFive.checked=false;
						}
				}

		}else if(producttype =='vacations' ){
			if(typeof(elm['vacfrom']) != undefined && elm['vacfrom']!=null && elm['vacfrom']!='' && document.vacationForm.locationBox!=null && document.vacationForm.locationBox!=undefined){
				document.vacationForm.locationBox.value=decodeURI(elm['vacfrom']);
			}
			if(typeof(elm['vaccheckin']) != undefined && elm['vaccheckin']!=null && elm['vaccheckin']!='' && document.vacationForm.dateVacIn!=null && document.vacationForm.dateVacIn!=undefined){
				document.vacationForm.dateVacIn.value=formatDate(parseDate(elm['vaccheckin']),populateDataDateFormat);
			}
			if(typeof(elm['vaccheckout']) != undefined && elm['vaccheckout']!=null && elm['vaccheckout']!='' && document.vacationForm.dateVacOut!=null && document.vacationForm.dateVacOut!=undefined){
				document.vacationForm.dateVacOut.value=formatDate(parseDate(elm['vaccheckout']),populateDataDateFormat);
			}
		}else if(producttype =='bnb'){
			if(typeof(elm['bnbfrom']) != undefined && elm['bnbfrom']!=null && elm['bnbfrom']!='' && document.bnbsForm.cityBox!=null && document.bnbsForm.dateBnbIn!=undefined){
				document.bnbsForm.cityBox.value=decodeURI(elm['bnbfrom']) ;
			}
			if(typeof(elm['bnbcheckin']) != undefined && elm['bnbcheckin']!=null && elm['bnbcheckin']!='' && document.bnbsForm.dateBnbIn!=null && document.bnbsForm.dateBnbIn!=undefined){
				document.bnbsForm.dateBnbIn.value=formatDate(parseDate(elm['bnbcheckin']),populateDataDateFormat);
			}
			if(typeof(elm['bnbcheckout']) != undefined && elm['bnbcheckout']!=null && elm['bnbcheckout']!='' && document.bnbsForm.dateBnbOut !=null && document.bnbsForm.dateBnbOut !=undefined){
				document.bnbsForm.dateBnbOut.value=formatDate(parseDate(elm['bnbcheckout']),populateDataDateFormat);
			}
		}
		
		else if(producttype =='rentals')
		{
				if(typeof(elm['rentalWherebox']) != undefined && elm['rentalWherebox']!=null && elm['rentalWherebox']!='' && document.vacationRentalForm.rentalWherebox!=null && document.vacationRentalForm.rentalWherebox!=undefined){
					document.vacationRentalForm.rentalWherebox.value=elm['rentalWherebox'];
				}
				if(typeof(elm['rentalIn']) != undefined && elm['rentalIn']!=null && elm['rentalIn']!='' && document.vacationRentalForm.dateRentalIn!=null && document.vacationRentalForm.dateRentalIn!=undefined){
					document.vacationRentalForm.dateRentalIn.value=formatDate(parseDate(elm['rentalIn']),'MM/dd/yyyy');
				}
				if(typeof(elm['rentalOut']) != undefined && elm['rentalOut']!=null && elm['rentalOut']!='' && document.vacationRentalForm.dateRentalOut!=null && document.vacationRentalForm.dateRentalOut!=undefined){
					document.vacationRentalForm.dateRentalOut.value=formatDate(parseDate(elm['rentalOut']),'MM/dd/yyyy');
				}
				if(typeof(elm['guests']) != undefined && elm['guests']!=null && elm['guests']!='' && document.vacationRentalForm.rentalGuests!=null && document.vacationRentalForm.rentalGuests!=undefined){
					document.vacationRentalForm.rentalGuests.value=elm['guests'];
				}
				if(typeof(elm['rooms']) != undefined && elm['rooms']!=null && elm['rooms']!='' && document.vacationRentalForm.hoRooms!=null && document.vacationRentalForm.hoRooms!=undefined){
					document.vacationRentalForm.rentalRooms.value=elm['rooms'];
				}
		}
}

function getOverrideProductType(requestUrl){
	var rtnprodtype;
	if(requestUrl.indexOf("hotels.html") > 0){
		rtnprodtype="hotel";
	}
	if(requestUrl.indexOf("car-rentals.html") > 0){
		rtnprodtype="car";
	}
	if(requestUrl.indexOf("cruises.html") > 0){
		rtnprodtype="cruise";
	}
	if(requestUrl.indexOf("vacation.html") > 0){
		rtnprodtype="package";
	}
	if(requestUrl.indexOf("vacation-rentals.html") > 0){
		rtnprodtype="vacationrentals";
	}
	if(requestUrl.indexOf("flights.html") > 0){
		rtnprodtype="air";
	}
	if(requestUrl.indexOf("index.html") > 0){
		rtnprodtype="air";
	}
	return rtnprodtype;
}
								

