// travelAgents.js

var already_open = false;

//checks date range on spccommsearchform
function checkFormDateRange(formname)
{
	var returnValue = 0;
	var arrivalDateBegin = document.forms[formname].elements["arrivalDateBegin"].value;
	var arrivalDateEnd = document.forms[formname].elements["arrivalDateEnd"].value;
	var dateFormatPattern = $(':hidden[name=dateFormatPattern]').val().toLowerCase();
	if((arrivalDateBegin != null || arrivalDateBegin != '') && isDateValid(arrivalDateBegin,dateFormatPattern) &&
		(arrivalDateEnd != null || arrivalDateEnd != '') && isDateValid(arrivalDateEnd,dateFormatPattern)){
		var dateDiff = new Date(arrivalDateEnd) - new Date(arrivalDateBegin);
		returnValue = Math.round(dateDiff/1000/60/60/24);
	}
	return returnValue;
}

//checks that the dates are not sooner than 30 days prior to today
function checkEachDateValue(formname, formItem)
{
	var returnValue;
	var dateToTest = new Date(document.forms[formname].elements[formItem].value);
	var todaysDate = new Date();
	returnValue = todaysDate.getTime() - dateToTest.getTime();
	returnValue = Math.round(returnValue/1000/60/60/24)
	return returnValue;
}

//submits the unpaid commission form
function submitUnpaidComm(formname)
{
	//check that the dates are not sooner than 30 days from now
		/*var datesSooner;
		datesSooner = checkEachDateValue(formname, "arrivalDate");
		if (datesSooner < 30)
		{
			alert("We're sorry.  The arrival date must be at least 30 days prior to today.");
			return false;
		}
		else
		{*/
			document.forms[formname].submit();
		//}
}

//submits the specific commission search form
function submitSpcCommForm(formname)
{
	//check the dates on the form to make sure they are no more than 30 days apart
	var returnValue;
	returnValue = checkFormDateRange(formname);
	if (returnValue > 30)
	{
		alert("We're sorry.  The date range cannot exceed 30 days.");
		return false;
	}
	else
	{
		//check that the dates are not sooner than 30 days from now
		/*var datesSooner;
		datesSooner = checkEachDateValue(formname, "arrivalDateEnd");
		if (datesSooner < 30)
		{
			alert("We're sorry.  The arrival dates must be at least 30 days prior to today.");
			return false;
		}
		else
		{*/
			//get the booking iata that was entered on the screen
			var bookingIata = document.forms['SpcCommSearchByGuestHotelForm'].bookingIataNumber.value;
			document.forms['SpcCommSearchByGuestHotelForm'].bookingIataNumber.value = bookingIata;
			document.forms['SpcCommSearchByConfirmationForm'].bookingIataNumber.value = bookingIata;
			document.forms['SpcCommSearchByConfirmationForm'].userIataNumber.value = document.forms['SpcCommSearchByGuestHotelForm'].userIataNumber.value;
			document.forms[formname].submit();
		//}
	}
}

//all functions below are used in determining when to set the arrival end date based on the inputs in the begin date
function arrivalDateBegin_onchange(formname)
{
	var arrivalBeginDate = getArrivalBeginDate(formname);
  if ( arrivalBeginDate != null )
  {
    setArrivalBeginDate(arrivalBeginDate, formname);
    var arrivalEndDate = getArrivalEndDate(formname);
    if ( arrivalEndDate == null || arrivalBeginDate >= arrivalEndDate )
    {
      setArrivalEndDate( new Date( arrivalBeginDate.getTime() + FIVE_DAYS_IN_MILLSECONDS ), formname);
    }
  }

  if(document.forms[formname].elements["arrivalDateBeginDay"].selectedIndex==0
  		&& document.forms[formname].elements["arrivalDateBeginMonth"].selectedIndex==0)
  {
  	document.forms[formname].elements["arrivalDateEndDay"].selectedIndex=0;
  	document.forms[formname].elements["arrivalDateEndMonth"].selectedIndex=0;
  }
}

//needed for unpaid commission inquiry which only has one set of dropdowns
function arrivalDate_onchange(formname)
{
	var daySelectBox = document.forms[formname].elements["arrivalDateDay"];
  	var monthAndYearSelectBox = document.forms[formname].elements["arrivalDateMonth"];
  	var arrivalDate = getDate( daySelectBox, monthAndYearSelectBox );
  	if(arrivalDate != null)
  	{
  		var arrivalDateBeginField = document.forms[formname].elements["arrivalDate"];
  		var daySelectBox = document.forms[formname].elements["arrivalDateDay"];
  		var monthAndYearSelectBox = document.forms[formname].elements["arrivalDateMonth"];
  		setDate( daySelectBox, monthAndYearSelectBox, arrivalDateBeginField, arrivalDate);
  	}
}

function arrivalDateEnd_onchange(formname)
{
	var arrivalEndDate = getArrivalEndDate(formname);
  if ( arrivalEndDate != null )
  {
    setArrivalEndDate(arrivalEndDate, formname );
  }
  if(document.forms[formname].elements["arrivalDateEndDay"].selectedIndex==0
  		&& document.forms[formname].elements["arrivalDateEndMonth"].selectedIndex==0)
  {
  	document.forms[formname].elements["arrivalDateBeginDay"].selectedIndex=0;
  	document.forms[formname].elements["arrivalDateBeginMonth"].selectedIndex=0;
  }
}

function getArrivalBeginDate(formname)
{
  var daySelectBox = document.forms[formname].elements["arrivalDateBeginDay"];
  var monthAndYearSelectBox = document.forms[formname].elements["arrivalDateBeginMonth"];
  return getDate( daySelectBox, monthAndYearSelectBox );
}

function getArrivalEndDate(formname)
{
  var daySelectBox = document.forms[formname].elements["arrivalDateEndDay"];
  var monthAndYearSelectBox = document.forms[formname].elements["arrivalDateEndMonth"];
  return getDate( daySelectBox, monthAndYearSelectBox );
}

function setArrivalBeginDate(date,formname)
{
  var arrivalDateBeginField = document.forms[formname].elements["arrivalDateBegin"];
  var daySelectBox = document.forms[formname].elements["arrivalDateBeginDay"];
  var monthAndYearSelectBox = document.forms[formname].elements["arrivalDateBeginMonth"];
  setDate( daySelectBox, monthAndYearSelectBox, arrivalDateBeginField, date );
}

function setArrivalEndDate(date,formname)
{
  var arrivalDateEndField = document.forms[formname].elements["arrivalDateEnd"];
  var daySelectBox = document.forms[formname].elements["arrivalDateEndDay"];
  var monthAndYearSelectBox = document.forms[formname].elements["arrivalDateEndMonth"];
  setDate( daySelectBox, monthAndYearSelectBox, arrivalDateEndField, date );
}

function getDate( daySelectBox, monthAndYearSelectBox )
{
  var date = null;
  if ( daySelectBox.selectedIndex != 0 && monthAndYearSelectBox.selectedIndex != 0 )
  {
    var dateArray = monthAndYearSelectBox.value.split( "/" );
    var day = Math.min( daySelectBox.value, daysInMonth( dateArray[0] - 1, dateArray[1] ) );
    date = new Date( dateArray[1], dateArray[0] - 1, day );
  }
  return date;
}

function setDate( daySelectBox, monthAndYearSelectBox, dateField, date )
{
  monthAndYearSelectBox.value = ( date.getMonth() + 1 ) + "/" + date.getFullYear();
  //need to check if the month value is in the drop-down, if not, leave all fields blank

  if (monthAndYearSelectBox.value != "")
  {
	  daySelectBox.value = date.getDate();
	  dateField.value = ( date.getMonth() + 1 ) + "/" + daySelectBox.value + "/" + date.getFullYear();
  }
  else
  {
    dateField.value = "";
    daySelectBox.value = "";
  }
}

function daysInMonth( month, year )
{
  return 32 - new Date( year, month, 32 ).getDate();
}

function TravelAgentsPopup(strURL, strMenu, strResizable, poupstyle)
{
	page = strURL;

	var lcl_width = document.body.clientWidth;
	var lcl_height = document.body.clientHeight;
	if( typeof( window.innerWidth ) == 'number' )
	{
    	//Non-IE
    	myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
  	}
  	else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    {
    	//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
  	}
  	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  	{
    	//IE 4 compatible
	    myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
  	}

	lcl_width = myWidth ;
	lcl_height = myHeight ;

	if (strMenu == 'Y') {
		windowprops = "location=yes,scrollbars=yes,menubar=yes,toolbar=yes,status=yes";
	}
	else {
		windowprops = "location=no,scrollbars=yes,menubar=no,toolbar=no,status=no";
	}
	if (strResizable == 'Y') {
		windowprops = windowprops + ",resizable=yes";
	}
	else {
		windowprops = windowprops + ",resizable=no";
	}

	switch(poupstyle)
	{
		case "1":
			lcl_width = 255;
			lcl_height = 206;
			break;
		case "2":
			lcl_width = 418;
			lcl_height = 583;
			break;
		case "3":
			lcl_width = 370;
			lcl_height = 475;
			break;
		case "4":
			lcl_width = 372;
			lcl_height = 306;
			break;
		case "5":
			lcl_width = 605;
			lcl_height = 385;
			break;
		case "6":
			lcl_width = 605;
			lcl_height = 535;
			break;
		case "7":
			lcl_width = ((lcl_width*90)/100);
			lcl_height = ((lcl_height*90)/100);
			break;

	}
	windowprops = windowprops + ",height=" + lcl_height;
	windowprops = windowprops + ",width=" + lcl_width;

	window.open(page, "TravelAgentsPopUp", windowprops);
	
	
}
function TravelAgentsPopupWithDimension(strURL, strMenu, strResizable, strWidth, strHeight)
{
	page = strURL;

	var lcl_width = document.body.clientWidth;
	var lcl_height = document.body.clientHeight;

	if( typeof( window.innerWidth ) == 'number' )
	{
    	//Non-IE
    	myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
  	}
  	else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    {
    	//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
  	}
  	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  	{
    	//IE 4 compatible
	    myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
  	}

	lcl_width = myWidth ;
	lcl_height = myHeight ;


	if (strMenu == 'Y') {
		windowprops = "location=yes,scrollbars=yes,menubar=yes,toolbar=yes,status=yes";
	}
	else {
		windowprops = "location=no,scrollbars=yes,menubar=no,toolbar=no,status=no";
	}
	if (strResizable == 'Y') {
		windowprops = windowprops + ",resizable=yes";
	}
	else {
		windowprops = windowprops + ",resizable=no";
	}
	if (lcl_height != "") {
		windowprops = windowprops + ",height=" + lcl_height;
	}
	else
	{
		lcl_height = ((lcl_height*90)/100);
		windowprops = windowprops + ",height="+ lcl_height;
	}
	if (lcl_width != "") {
		windowprops = windowprops + ",width=" + lcl_width;
	}
	else
	{
		lcl_width = ((lcl_width*90)/100);
		windowprops = windowprops + ",width=" + lcl_width;
	}
	window.open(page, "TravelAgentsPopUp", windowprops);
	
}


function OpenBrandPage(brandcode)
{
 	var url = new String("");
	var local_width = document.body.clientWidth;
	var local_height = document.body.clientHeight;

	if( typeof( window.innerWidth ) == 'number' )
	{
    	//Non-IE
    	myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
  	}
  	else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
    {
    	//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
    	myHeight = document.documentElement.clientHeight;
  	}
  	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
  	{
    	//IE 4 compatible
	    myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
  	}

	local_width = myWidth ;
	local_height = myHeight ;

	brandcode = brandcode.toUpperCase();
	switch(brandcode)
	{
		case "MC":
			url = url+ "/marriott-hotels-resorts/travel.mi?bc=MC";
			break;
		case "JW":
			url = url+ "/jw-marriott/travel.mi?bc=JW";
			break;
		case "BR":
			url = url+ "/renaissance-hotel/travel.mi?bc=BR";
			break;
		case "CY":
			url = url+ "/courtyard/travel.mi?bc=CY";
			break;
		case "RI":
			url = url+ "/residence-inn/travel.mi?bc=RI";
			break;
		//CTAC brand code
		case "RC":
			url = url+ "/residence-inn/travel.mi?bc=RI";
			break;
		case "FN":
			url = url+ "/fairfield-inn/travel.mi?bc=FN";
			break;
		case "ET":
			url = url+ "/conference-centers/travel.mi?bc=ET";
			break;
		case "TP":
			url = url+ "/towneplace-suites/travel.mi?bc=TP";
			break;
		//CTAC brand code
		case "TO":
			url = url+ "/towneplace-suites/travel.mi?bc=TP";
			break;
		case "SH":
			url = url+ "/springhill-suites/travel.mi?bc=SH";
			break;
		//CTAC brand code
		case "XV":
			url = url+ "/springhill-suites/travel.mi?bc=SH";
			break;
		case "MB":
			url = url+ "/marriott-vacation-club/travel.mi?bc=MB";
			break;
		case "ER":
			url = url+ "/executive-apartments/travel.mi?bc=ER";
			break;
		default:
			url = url;
			break;

	}

		windowprops = "location=yes,scrollbars=yes,menubar=yes,status=yes,toolbar=yes,resizable=yes";

		local_height = ((local_height*90)/100);
		windowprops = windowprops + ",height=" + local_height;
		local_width = ((local_width*90)/100);
		windowprops = windowprops + ",width=" + local_width;

		if(url != "")
		{
			//window.open(url,"TravelAgentsPropSearchPopup",windowprops);
			sendToMarriott(url, windowprops);
		}
		else
		{
			sendto('us','no_brand','http://www.execustay.com',2);
		}

}

function openTravelAgentWindow(loc)
{
	window.open(loc,"TravelAgentsPropSearchPopup");
}


function setPage(objElement, param)
{
	var curValue = objElement.options[objElement.selectedIndex].value;
	location.href = location.pathname+"?"+param+"="+curValue;
}

function setStmntDetailPage(objElement)
{
	var curValue = objElement.options[objElement.selectedIndex].value;
	location.href = "/travelagents/displaystatement.mi"+curValue;
}

function setPage(objElement)
{
	var curValue = objElement.options[objElement.selectedIndex].value;
	location.href = location.pathname+curValue;
}

function GetAirportList( choice, form )
{
	var page = "/search/getAirportList.mi?airportListType=" + choice + "&formName=" + form;
	windowprops = "height=425,width=475,location=no,"
	+ "scrollbars=yes,menubar=no,toolbars=no,resizable=yes";
	window.open(page, "AirportListPopUp", windowprops);
}
//needed for search by preference link
function submitOptions(actionPath, formname)
{
	var form = document.forms[formname];

	form.action = actionPath;
	form.submit();
}

function submitOptionsVData(actionPath, formname, vsObjectMap)
{
	var form = document.forms[formname];

	setVDataCookieFromFormNoSubmit(form, vsObjectMap);

	form.action = actionPath;
	form.submit();
}

/* Top Nav DHTML Functions */
function montre(id)
{
  if (document.getElementById)
  {
	  document.getElementById(id).style.display="block";
  }
  else if (document.all)
  {
	  document.all[id].style.display="block";
  }
  else if (document.layers)
  {
	  document.layers[id].display="block";
  }
	already_open = true;
}

function callopen1(id)
{

  if(already_open)
  {	if (document.getElementById)
		{
		  document.getElementById(id).style.display="block";
		}
		else if (document.all)
		{
		  document.all[id].style.display="block";
		}
		else if (document.layers)
		{
		  document.layers[id].display="block";
		}
	}

}

 function cache(id)
 {
	  if (document.getElementById)
	  {
		  document.getElementById(id).style.display="none";
	  }
	  else if (document.all)
	  {
		  document.all[id].style.display="none";
	  }
	  else if (document.layers)
	  {
		  document.layers[id].display="none";
	  }
	  already_open = false;

}

function cacheparent(id)
{
  if (document.getElementById)
  {
	  document.getElementById(id).style.display="none";
  }
  else if (document.all)
  {
	  document.all[id].style.display="none";
  }
  else if (document.layers)
  {
	  document.layers[id].display="none";
  }

}

// Opens up popup in new window for Travel Agent site
function sendToMarriott(path, winprop)
{	
	
	var protocol = window.location.protocol;
	var hostLocation = window.location.host;
	var hostArray = hostLocation.split('.');
	
	var isGpSite= "false";
	var isTaSite= "false";
	
	for ( var i= 0 ; i< hostArray.length; i++)
	{
		if(hostArray[i] == 'grouppartners')
		{
			isGpSite = "true"
			
			
		}
		else if (hostArray[i] == 'travelagents')
		{
			isTaSite = "true";
			
		}
		
	}
	
	var fullPopupPath = protocol+"//";
	if((isGpSite == "true") || (isTaSite == "true"))
	{
		if(hostArray[0] == "www")
		{
			//fullPopupPath += "www.";
			for( var i= 2; i< hostArray.length; i++)
			{
				if(i != hostArray.length-1)
				{
					fullPopupPath += hostArray[i]+".";
				}
				else
				{
					fullPopupPath += hostArray[i]
				}
			}
			fullPopupPath += path;
		}
		else
		{
			for(var i= 1; i<hostArray.length; i++)
			{
				if(i != hostArray.length-1)
				{
					fullPopupPath += hostArray[i]+".";
				}
				else
				{
					fullPopupPath += hostArray[i];
				}
			}
			fullPopupPath += path;
		}
		
	}
	else
	{
		fullPopupPath += hostLocation+path;
	}
	if(winprop != 'null')
	{
		window.open(fullPopupPath, "_blank", winprop);
	}
	else{
		window.open(fullPopupPath, "_blank")
	}

}


//FROM OLD TA SITE, KEEPING IN HERE TO MAKE SURE NOONE IS STILL USING - COMMENTING OUT
/*function airportChanged(searchForm)
{
	if (searchForm.Airport.value.length > 0)
	{
		searchForm.city.value = '';
		searchForm.searchType.value = 'NearAirport';
		searchForm.elements['destinationStateProvince'].selectedIndex = 0;
		searchForm.elements['destinationCountry'].selectedIndex = 0;
	}
}

function cityChanged(city)
{
	if (city != null && city.value.length > 0)
	{
		var searchForm = city.form;
		searchForm.searchType.value = 'InCity';
		searchForm.elements['Airport'].selectedIndex = 0;
	}
}

function stateChanged(searchForm)
{
	if (searchForm.destinationStateProvince.value.length > 0)
	{
		searchForm.destinationCountry.value = 'US';
		searchForm.searchType.value = 'InCity';
		searchForm.elements['Airport'].selectedIndex = 0;
	}
}

function countryChanged(searchForm)
{
	if (searchForm.destinationCountry.value != 'US')
	{
		searchForm.destinationStateProvince.value = '';
		searchForm.searchType.value = 'InCity';
		searchForm.elements['Airport'].selectedIndex = 0;
	}
}

function resetForm(searchForm)
{
	searchForm.searchType.value = 'InCity';
	searchForm.city.value = '';
	searchForm.brand.value = 'all';
	searchForm.elements['destinationStateProvince'].selectedIndex = 0;
	searchForm.elements['destinationCountry'].selectedIndex = 0;
	searchForm.elements['Airport'].selectedIndex = 0;
	searchForm.elements['fromDate'].value= '';
	searchForm.elements['toDate'].value= '';
	return false;
}

function submitFamTasticForm(searchForm)
{
	if(searchForm.clusterCode[0].checked == false && searchForm.clusterCode[1].checked == false)
	{
		alert("Please specify a Fam-Tastic or Travel Industry Rate.");
		return false;
	}
	else if ((searchForm.city.value.length == 0) && (searchForm.Airport.value.length == 0))
	{
		if(searchForm.destinationStateProvince.value.length == 0)
		{
			if(searchForm.destinationCountry.value.length == 0)
			{
				alert('Please enter a city/state/country or an airport to search');
				return false;
			}
		}
	}
	else if ((searchForm.destinationStateProvince.value.length == 0) && (searchForm.destinationCountry.value == 'US'))
	{
		alert('Please enter a state when searching by USA');
		return false;
	}
	else
	{
		if (searchForm.Airport.value.length > 0)
		{
			searchForm.searchType.value = 'NearAirport';
			searchForm.action = '/search/newNearAirportSearch.mi';
		}
		return true;
	}
}*/

