// Method to submit Login Form in the Global Navigation
function submitGlobalLoginForm() {
	if (document.getElementById('accountId').value.length < 11) { 
		alert('The Total Rewards number is not valid. Please check it and enter a valid Total Rewards number.'); 
		return false; 
	}
	else {
		// Write a cookie. This cookie will be read in the LoginHttpServlet. If cookies are disabled or browser does not support cookies,
		// read operation on cookie will fail and user will be redirected to cookie error page.html    
		document.cookie = 'detectCookie' + "=" + escape ('true') + "; path=/";
		return true;
	}
}
/*
 * this function calls the GGWU point lookup 
 * interface to get the patron's point balance.
 */
function getPointBalance() {
	var winetId = readCookie("accountId");
	
	if (GetCookie("accountId") == "true" && GetCookie("userSessionCookie") == "true") {
		ajaxWindowRequestObject = createRequestObject();
		ajaxWindowRequestObject.open('get', 'https://www.totalrewards.com/total_rewards/ggwu/balanceCheck.jsp?accountId=' + 
			winetId + '&promotionId=' + ajaxPromotionId); 
		ajaxWindowRequestObject.onreadystatechange = GGWU_handleResponse;
		ajaxWindowRequestObject.send(null); 
	
		if (document.getElementById('ajaxLoading') != null)
			document.getElementById('ajaxLoading').style.display = "inline";
	}
	else {
		// the patron is not logged in so display the login section
		if (document.getElementById('ajaxLoginForm') != null)
			document.getElementById('ajaxLoginForm').style.display = "inline";
	}
}

/*
 * this function gets called when the AJAX call 
 * completes and will render the point balance.
 */
function GGWU_handleResponse() { 
	 if (ajaxWindowRequestObject.readyState == 4 && ajaxWindowRequestObject.status == 200) {
		var response = ajaxWindowRequestObject.responseText; 
		
		if (response) {
			var points = "0";
			var lastDate = new Date();
			var html = "";
			
			// split the response into the parts.
			var parts = response.split('|');
			try {
				points = parts[0];
				lastDate = parts[1];
			} catch (e) { }
			
			html += '<div id="ajaxDisplayResults">';
			html += '<span class="pointCount">You currently have ';
			html += points;
			html += ' Gift Points in your account!*</span><br>';
			html += 'This estimated Gift Point balance reflects all play through ';
			html += lastDate;
			html += '.</div>';
			
			// display the HTML for the point balance
			if (document.getElementById('ajaxPointBalance') != null)
				document.getElementById('ajaxPointBalance').innerHTML = html;
	
			// hide the "loading" section as we're done loading now
			if (document.getElementById('ajaxLoading') != null)
				document.getElementById('ajaxLoading').style.display = "none";
				
			// now show the point balance section and the disclaimer if any.
			if (document.getElementById('ajaxPointBalance') != null)
				document.getElementById('ajaxPointBalance').style.display = "inline";
			if (document.getElementById('ajaxDisclaimer') != null)
				document.getElementById('ajaxDisclaimer').style.display = "inline";
		} 
	}   
} 

