/*
		This function determines if the Check Rates & Availability module should be displayed
		author: cstat481
	*/
	function checkToDisplayForm()
	{
		var bookable = 'true';
		
		if(bookable == 'true')
		{
			return 'true';
		}
		else 
		{
			return 'false';
		}
	}
	/*
		This function determines if values were passed in the cookie
		and then reads the present values for corporateCode and groupCode
		author: cstat481
	*/
	function checkCookieValues()
	{
		
		var nameEQ = "FormInfoCookie";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			{
				if (c.indexOf(nameEQ) == 0)
				{
							
					var cookieValue = c.substring(0,c.length);
					var values = cookieValue.split("|");
					cookieValues = true;
									
					var clusterCodeCookie = values[5];
					if(clusterCodeCookie != 'null' && clusterCodeCookie != '')
					{
						corporateCode = clusterCodeCookie;
					}				
					
					var groupCodeCookie = values[6];
					if(groupCodeCookie != 'null' && groupCodeCookie != '')
					{
						groupCode = groupCodeCookie;
					}
	
				}
			}
	
		}
		return null;
	
	}

	/*
		This function sets cookie values when the find button is pressed
		author: 
	*/
	function saveDataInCookieAndValidate(myForm, vsObjectMap)
	{
		setVDataCookieFromFormNoSubmit(myForm, vsObjectMap);
		var submit = validate();
		if(submit)
		{
		return true;
		}
		else
		{
		return false;
		}
	}
	
	/*
		This function validates form submittal
		author: 
	*/
	function validate()
	{
		var pageId = 'HWHOM';
		var pageIdSpecials = 'HWSAP';
		
		var objForm = document.fullRatesCalloutForm;
		var corporateCode = objForm.corporateCode.value;
		var groupCode = objForm.groupCode.value;
		var clusterCode = objForm.clusterCode;
		var submit = false;
		
		var objClusterSell = objForm.clusterSell;
		objClusterSell.value = false;
	
		if(document.fullRatesCalloutForm.corporateCode.value != null)
		{
			document.fullRatesCalloutForm.corporateCode.value 
				= document.fullRatesCalloutForm.corporateCode.value.toUpperCase();
		}
	
		if (clusterCode != null && clusterCode.checked != null && clusterCode.checked == true)
		{
			objClusterSell.value = true;
		}
		if(corporateCode.value != null) 
		{
			objClusterSell.value = true;
		}
	
		submit = validateMRNumForCorpCode();
	
		if(submit)
		{
	
			if(clusterCode != null &&
				clusterCode.checked == true)
			{
				corporateCode = "";
				groupCode = "";
			}
			
			if (pageId != pageIdSpecials)
			{
				submit = validateForm();
			}
			else
			{
				var offerStartDate = '';
				var offerEndDate = '';
				var rewardsOffers = '1000, 1010, 1020, 1030, 1040';
				var subCat = '';
	
				if(validateForm() == true)
				{
					submit =  ValidateOfferDates(offerStartDate, offerEndDate, rewardsOffers, subCat);
				}
			}
		}
		return submit;
	}

	/*
		This function validates the entered Rewards Number for the entered corporateCode
		author: 
	*/
	function validateMRNumForCorpCode()
	{
		var submit = true;	
	
		if(document.fullRatesCalloutForm.corporateCode != null &&
			(document.fullRatesCalloutForm.corporateCode.value == 'M11' || document.fullRatesCalloutForm.corporateCode.value == 'm11'))
		{
			if(document.fullRatesCalloutForm.marriottRewardsNumber == null || document.fullRatesCalloutForm.marriottRewardsNumber.value == '')
			{
				alert("Please enter a Marriott Rewards Number");
				document.fullRatesCalloutForm.marriottRewardsNumber.focus();
				var submit = false;
			}
		}
		return submit;
	}
			
	/*
		This function validates the check in and check out dates before submittal
		author: 
	*/
	function ValidateStayDates()
	{
	
		if (!IsValidDateString(document.fullRatesCalloutForm.elements['checkInDate'].value))
		{
			alert("Check In Date is not valid.");
			return false;
		}
	
		if (!IsValidDateString(document.fullRatesCalloutForm.elements['checkOutDate'].value))
		{
			alert("Check Out Date is not valid.");
			return false;
		}
	
		if (!IsDateAfter(document.fullRatesCalloutForm.elements['checkInDate'].value, document.fullRatesCalloutForm.elements['checkOutDate'].value))
		{
			alert("Check Out Date must be a date after the Check In Date.  Please verify your selection criteria.");
			return false;
		}
	
		var curDate = new Date()
		var curMonth = curDate.getMonth() + 1;
		var curDay = curDate.getDate() - 1;
	
		var formattedDate = curMonth + "/" + curDay + "/" + curDate.getFullYear()
	
		if (!IsDateAfter(formattedDate, document.fullRatesCalloutForm.elements['checkInDate'].value)) 
		{
			alert("Check In Date occurs in the past.");
			return false;
		}
	
		var kDate = new Date();
		// add 357 days to current day and substarct current week day from it.
		kDate.setTime(curDate.getTime() + (357 * 86400000) - ((curDate.getDay()) * 86400000));
	
		var ciDate = new Date(document.fullRatesCalloutForm.elements['checkInDate'].value);
		var coDate = new Date(document.fullRatesCalloutForm.elements['checkOutDate'].value);
	
		if (ciDate.getTime() >=  kDate.getTime() ||coDate.getTime() > kDate.getTime() ) 
		{
			alert("Dates occur beyond inventory period.");
			return false;
		}
			
		return true;
	}

	/*
		This function determines the query string values for Enrich Group
		author: cstat481
	*/
	function getQueryStringValues()
	{
		var qString = document.location.search;
		var corporateString = "corporateCode=";
		var groupString = "groupCode=";
		var clusterString = "clusterCode=";
		
		
		var corporateLocale = qString.indexOf(corporateString);

		if (corporateLocale > 0)
		{
			corporateCode = qString.substring(corporateLocale + corporateString.length,corporateLocale + corporateString.length + 3);
			qsValues = true;
		}
		
		// IPMI00024270
		// If both corporate code and cluster code are present in the query string, corporate code takes precedence.  So check if 
		// corporatecode is not empty and then map the querystring value of clustercode to corporatecode.
		if (corporateCode == null || corporateCode =='' || corporateCode.length <= 0)
		{
			var clusterLocale = qString.indexOf(clusterString);
			if (clusterLocale > 0)
			{
				corporateCode = qString.substring(clusterLocale + clusterString.length,clusterLocale + clusterString.length + 3);
				qsValues = true;
			}
		}

		var groupLocale = qString.indexOf(groupString);

		if (groupLocale > 0)
		{
			groupCode = qString.substring(groupLocale + groupString.length, groupLocale + groupString.length + 7);
			qsValues = true;
		}
	}

	/*
		This function determines if the link is coming from ResLink in group tools
		author: cstat481 - not used, maybe later
	*/
	function checkEnrichQueryString()
	{
		var qString = document.location.search;
		
		var resLink = qString.indexOf("app=resvlink");

		if (resLink > 0)
		{
			return true;		
		}

	}

	/*
		This function creates the EnrichCookie
		author: cstat481
	*/
	function writeEnrichCookie(corporateCode, groupCode)
	{

		var name = "EnrichCookie";
		var fullRatesObject = document.fullRatesCalloutForm;
		var value = "|" + corporateCode + "|" + groupCode + "|";	
		document.cookie = name + '=' + value + '; path=/';
	}

	/*
		This function determines if values were passed in the cookie
		and then reads the present values for corporateCode and groupCode
		author: cstat481
	*/
	function checkEnrichCookieValues()
	{
		
		var nameEQ = "EnrichCookie";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++)
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			{
				if (c.indexOf(nameEQ) == 0)
				{
							
					var cookieValue = c.substring(0,c.length);
					var values = cookieValue.split("|");
					cookieValues = true;
									
					var clusterCodeCookie = values[1];
					if(clusterCodeCookie != 'null' && clusterCodeCookie != '')
					{
						corporateCode = clusterCodeCookie;
					}				
					
					var groupCodeCookie = values[2];
					if(groupCodeCookie != 'null' && groupCodeCookie != '')
					{
						groupCode = groupCodeCookie;
					}
	
				}
			}
	
		}
		return null;
	}