//Date - 03/09/2004
//Author - Jose Conteras
//Comments - Consolidated JS file for common Find And Reserve functionality

function addLoadEvent(func) {
  		var oldonload = window.onload;
  		if (typeof window.onload != 'function') {
    		window.onload = func;
  		} else {
    		window.onload = function() {
      		oldonload();
      		func();
    	}
  	}  	
}

function WinOpen(sURL, sName, intTop, intLeft, intWidth, intHeight)
{
	var windowprops;
	if (null == sURL) {
		alert("ERROR: WinOpen() requires a URL");
		return;
	}
	if (null == sName)
		sName = '';
	if (null == intTop)
		intTop = 200;
	if (null == intLeft)
		intLeft = 200;
	if (null == intWidth)
		intWidth = 200;
	if (null == intHeight)
		intHeight = 200;
	windowprops = "location=no,scrollbars=yes,menubar=no,toolbar=no,locationbar=no,resizable=yes,alwaysRaised=yes,height="+intHeight+",width="+intWidth+",left="+intLeft+",top="+intTop;
	return window.open(sURL,sName,windowprops);
}


function clusterCode_onclick()
{
  var clusterCodeGroup = document.forms["FindAndReserveSearchActionForm"].elements["clusterCode"];
  
  if ( clusterCodeGroup[clusterCodeGroup.length-1].checked )
  {
    document.forms["FindAndReserveSearchActionForm"].elements["corporateCode"].focus();
  }
  
  else
  {
    document.forms["FindAndReserveSearchActionForm"].elements["corporateCode"].value = "";
  }
}


function corporateCode_onchange( )
{
  var clusterCodeGroup = document.forms["FindAndReserveSearchActionForm"].elements["clusterCode"];
  var corporateCodeField = document.forms["FindAndReserveSearchActionForm"].elements["corporateCode"];
  var value = corporateCodeField.value;
  
  if ( value == null || value.length == 0 )
  {
    clusterCodeGroup[0].checked = true;
  }
  
  else
  {
    clusterCodeGroup[clusterCodeGroup.length - 1].checked = true;
  }
}


function recordsPerPage_onchange( recordsPerPageSelectBox )
{
  var selectedValue = recordsPerPageSelectBox.value;
  var displayResultsMapCheckbox = document.forms["FindAndReserveSearchActionForm"].elements["displayResultsMap"];
  if ( selectedValue == "all" )
  {
    displayResultsMapCheckbox.checked = false;
    displayResultsMapCheckbox.disabled = true;
  }
  else
  {
    displayResultsMapCheckbox.disabled = false;
  }
}


function stateProvince_onchange( addressSource )
{
  var countryField = addressSource + "Address.country";
  var stateProvinceField = addressSource + "Address.stateProvince";
  var countrySelectBox = document.forms["FindAndReserveSearchActionForm"].elements[countryField];
  var stateSelectBox = document.forms["FindAndReserveSearchActionForm"].elements[stateProvinceField];
  if ( stateSelectBox.selectedIndex == 0 )
  {
    countrySelectBox.selectedIndex = 0;
  }
  else
  {
    countrySelectBox.value = "US";
  }
}


function country_onchange( addressSource )
{
  var countryField = addressSource + "Address.country";
  var stateProvinceField = addressSource + "Address.stateProvince";
  var countrySelectBox = document.forms["FindAndReserveSearchActionForm"].elements[countryField];
  var stateSelectBox = document.forms["FindAndReserveSearchActionForm"].elements[stateProvinceField];
  if ( countrySelectBox.value != "US" )
  {
    stateSelectBox.selectedIndex = 0;
  }
}


function preferences_onclick( toggledIndex )
{
  var checkboxGroup = document.forms["FindAndReserveSearchActionForm"].elements["preferences"];
  var allPreferencesCheckboxIndex = checkboxGroup.length - 1;
  validateCheckboxSelection( checkboxGroup, toggledIndex, allPreferencesCheckboxIndex );
}


function marriottFeatures_onclick( toggledIndex )
{
  var checkboxGroup = document.forms["FindAndReserveSearchActionForm"].elements["marriottFeatures"];
  var noneCheckboxIndex = checkboxGroup.length - 1;
  validateCheckboxSelection( checkboxGroup, toggledIndex, noneCheckboxIndex );
}


function marriottBrands_onclick( toggledIndex )
{
  var checkboxGroup = document.forms["FindAndReserveSearchActionForm"].elements["marriottBrands"];
  var allHotelsCheckboxIndex = checkboxGroup.length - 1;
  validateCheckboxSelection( checkboxGroup, toggledIndex, allHotelsCheckboxIndex );
}


function validateCheckboxSelection( checkboxGroup, toggledIndex, allCheckboxIndex )
{
  var checkboxGroupSize = checkboxGroup.length;
  var allCheckbox = checkboxGroup[allCheckboxIndex];
  if ( toggledIndex == allCheckboxIndex )
  {
    if ( allCheckbox.checked )
    {
      for ( counter = 0; counter < checkboxGroupSize; counter++ )
      {
        if ( counter != allCheckboxIndex )
        {
          checkboxGroup[counter].checked = false;
        }          
      }
    }
  }
  else
  {
    if ( checkboxGroup[toggledIndex].checked )
    {
      allCheckbox.checked = false;
    }
    else
    {
      var isAnySelected = false;
      for ( counter = 0; counter < checkboxGroupSize; counter++ )
      {
        if ( counter != allCheckboxIndex &&
             checkboxGroup[counter].checked )
        {
          isAnySelected = true;
          allCheckbox.checked = false;
          break;
        }
      }
      if ( !isAnySelected )
      {
        allCheckbox.checked = true;
      }
    }
  }  
}

//new function for RRE for the left nav search form
function validateLeftNavClusters()
{
	if(document.forms["FindAndReserveSearchActionForm"].elements["useRewardsPoints"].value == "true")
	{
		if((document.forms["FindAndReserveSearchActionForm"].elements["maximumRate"].value != "") || (document.forms["FindAndReserveSearchActionForm"].elements["clusterCode"].value != "none"))
		{
			var choice = confirm("You have selected to search by both Marriott Rewards points and a special rate or price range.  We're sorry, we can check only one at a time.\n" + 
									 "Click 'OK' to search by points\n" +
									 "or 'Cancel' to go back and change your search options.");
				if(choice == true)
				{
					return true;
				}
				else
				{
					return false;
				}
		}
	}
}

//sends current form data to actionPath
function moreOptions(actionPath)
{
	var form = document.forms["FindAndReserveSearchActionForm"];

	form.action=actionPath;
	form.submit();
}

//handles cluster code UI for checkboxes interface
function CheckRateType(objField) {
	var objClusterCode = objField.form.clusterCode;
	var objCorporateCode = objField.form.corporateCode;

	if ((objField.type == "checkbox") && (objField.name == "clusterCode")){
	
		objCorporateCode.value = "";
		
		for (i = 0; i < objClusterCode.length; i++) {
			if (objClusterCode[i].value != objField.value) {
				objClusterCode[i].checked = false;
			}
		}
	}
	
	if ((objField.type == "text") && (objField.name == "corporateCode")){
		if (objClusterCode != null) {
			for (i = 0; i < objClusterCode.length; i++) {
				objClusterCode[i].checked = false;
			}
			objClusterCode[eval(objClusterCode.length)-1].checked = true;
		}					
	}
}
 

function setSearchCriteria()
{
	document.forms["FindAndReserveSearchActionForm"].elements["searchCriteria"].value = "Location";
	if ((document.forms["FindAndReserveSearchActionForm"].elements["fromDate"] != null)
		&& (document.forms["FindAndReserveSearchActionForm"].elements["fromDate"].value.length > 0))
	{
		document.forms["FindAndReserveSearchActionForm"].elements["searchCriteria"].value = "Availability";
	}
}

function setSearchNVDataCookie(formObj, vsObjectMapObj)
{
	setSearchCriteria();
	setVDataCookieFromFormNoSubmit(formObj, vsObjectMapObj);
}

function checkAndSendto(formID,elementName,country,brand,URL,type) {		
	document.getElementById(formID).elements[elementName].checked = true;
	sendto(country,brand,URL,type);	
}

// Function to register submit of Search Promotions LeftNav Form
function initSearchPromotions()
{
	var searchPromotionsLeftNavForm = document.getElementById("searchPromotionsLeftNavForm");
	if(searchPromotionsLeftNavForm)
	{
		searchPromotionsLeftNavForm.onsubmit = submitSearchPromotionsLeftNav;	
	}	
}

// Function to handle corporate, cluster codes on Search Promotions LeftNav Form
function submitSearchPromotionsLeftNav()
{
	var corpCode = $("#searchPromotionsLeftNavForm").find(":input#corporateCode");
	var clusterCode = $("#searchPromotionsLeftNavForm").find(":input#clusterCode");
	$(clusterCode).val("");
	if($(corpCode).length > 0 && $(corpCode).val() != "")
	{
		$(clusterCode).val("other");
	}
	return true;
}

addLoadEvent(initSearchPromotions); 