function	ValidateEmailForm(CheckEmail)
{
	return ValidateEmailFormJS(CheckEmail, PHP_Staging);
}

function	CheckEmailPopup(MapID,GameURL,PassForceGameCount)
{

	//	Force GameCount allows for testing.... without it, you need to press
	//	loads of buttons
	
	var	GamesPlayed			=	0;
	try {
		if(PassForceGameCount==null)	{
			PassForceGameCount = 0;
		}
		ForceGameCount	=	parseInt(PassForceGameCount);
		GamesPlayed		=	ForceGameCount;
	}
	catch (err) {
		ForceGameCount	= 	0;
	}
	
	//	Figure out how many times we have been here
		
	if (ForceGameCount == 0) {
		var	CookieValue	=	readCookie(GamesPlayedCookieName);
		if (IsNumeric(CookieValue) == false) {
			GamesPlayed	=	0;
		}
		else {
			GamesPlayed	=	parseInt(CookieValue);
		}
	}
	
	// GamesPlayed++;
	// setCookie(GamesPlayedCookieName,GamesPlayed);
	
	DisplayFooterInfo();

	//	Check to see what we need to display
	
	var Do_ForcePromo		=	CheckForcePromo(PHP_NumberOfDeposits, 
												PHP_GamesBeforePromo, 
												PHP_GamesPromoInterval
												);
	
	var Do_PopupEmail		=	CheckForceRegister(PHP_GamesBeforeRegister);
	var Do_ForceRegister	=	CheckForceDeposit(PHP_GamesBeforeDeposit);
	var Do_ForceDeposit		=	CheckPopupEmail(PHP_GamesBeforeEmailPopup);

		
	if (Do_ForceDeposit == true) {	//	Redirect to deposit page
		if ((PHP_NumberOfDeposits == 0)  && (PHP_ForceDepositURL.length > 0)) {
			window.location = PHP_ForceDepositURL;
			return;
		}
	}

	if (Do_ForceRegister == true) {	//	Redirect to register	
		if ((PHP_UserState == PHP_FreeUserState) && (PHP_ForceRegisterURL.length > 0)) {	
			window.location = PHP_ForceRegisterURL;
			return;
		}
	}
		
	if (Do_ForcePromo == true) {	//	Redirect to promo ads
		if (PHP_ForcePromoURL.length > 0) {	
			var EncodeURL=encodeURIComponent(GameURL);
			window.location = PHP_ForcePromoURL + "?OrgURL=" + EncodeURL;
			return;
		}
	}
		
	if (Do_PopupEmail == true) {	//	Show the Email popup
		if (PHP_EmailKnown == 0) {
			$('.fb_EmailFormTag').attr('action',GameURL);
			$('#fb_EmailFormTag').attr('action',GameURL);
			$('#fb_EmailForm_MapID').attr('value',MapID);
			$.facebox({ div: '#fb_request_email' },'lightbox');
			return;		
		}
	}
	
	document.location.href=GameURL;
	
}

function PlayGame(GameTypeID,PlayGameURL,ChooseGameURL,PassForceGameCount,MapIDCash,MapIDFree,DontReturnFalse)
{

	//	Figure out the state this user is in
	
	var	CookieValue		=	readCookie("UserState");
	var	UserState		=	"";
	var	Registered		=	-1;
	var	NeedLogin		=	0;
	var	ReturnStatus	=	false; // This is used to avoid HREFS being used instead of onclick

	/*

	This function is sometimes used as onclick="return PlayGame" in which case 
	it requires a return false. Sometimes it's used as a URL and if you return false, the screen
	will show "false" which is not the idea. So if you pass ,1 as the last parameter you avoide that.
	
	*/
	
	
	switch	(CookieValue.toUpperCase()) {
		
		case	'LOGIN':	UserState	=	"LOGIN";
							Registered	=	1;
							break;
							
		case	'LOGOUT':	UserState	=	"LOGOUT";
							NeedLogin	=	1;
							Registered	=	1;
							break;
							
		default:			UserState	=	"FREE";
							Registered	=	0;
							break;
	}

	var GamesPlayed	=	ReadGameCount();		

	//	Check to see what we need to display
	
	
	//	When we get here, we don't have to force them to do anything. So either they are registered and go one way
	//	OR they are not registered and they go another way
	
	if (GameTypeID == null) {
		GameTypeID 	=	0;
	}
	
	if (ChooseGameURL.indexOf("?") < 0) {
		ChooseGameURL	+=	"?GameTypeID=" + GameTypeID + "#FLASHGAME";
	}
	
	var	FreePlayURL	=	PlayGameURL	+ "?GameTypeID=" + GameTypeID + "&MapID=" + MapIDFree + "#FLASHGAME";
	var	CashPlayURL	=	PlayGameURL	+ "?GameTypeID=" + GameTypeID + "&MapID=" + MapIDCash + "#FLASHGAME";
	
	//	If there is no FreePlayURL, we use the cash one instead		
	
	if ((MapIDFree <= 0) && (MapIDCash > 0)) {
		FreePlayURL	= CashPlayURL;
	}

	if (PHP_NumberOfDeposits	== 0) {
		if (PHP_NumberOfDeposits == 0) {
			var Do_ForcePromo	=	false;
			if (PHP_GamesBeforePromo > 0) {
				Do_ForcePromo	=	true;
				if (GamesPlayed < PHP_GamesBeforePromo) {
					Do_ForcePromo	=	false;
				}
			}
			
			if (PHP_GamesPromoInterval > 0) {	//	Every xx games
				Do_ForcePromo	=	true;
				var GamesMod	=	GamesPlayed  % PHP_GamesPromoInterval;
				if (GamesMod != 1) {
					Do_ForcePromo	=	false;
				}
			}
		}
	}

	if (Do_ForcePromo == true) {	//	Redirect to promo ads
		if (PHP_ForcePromoURL.length > 0) {	
			var EncodeURL=encodeURIComponent(FreePlayURL);
			window.location = PHP_ForcePromoURL + "?OrgURL=" + EncodeURL;
			if (DontReturnFalse != 1) {
				return ReturnStatus;
			}
			else {
				return;
			}
		}
	}
		
	if (Registered == 1) {
	
		var Do_ForceDeposit	=	false;
		if (PHP_GamesBeforeDeposit > 0) {
			Do_ForceDeposit	=	true;
			if (GamesPlayed < PHP_GamesBeforeDeposit) {
				Do_ForceDeposit	=	false;
			}
		}
			
		if (Do_ForceDeposit == true) {	//	Redirect to deposit page
			if ((PHP_NumberOfDeposits == 0)  && (PHP_ForceDepositURL.length > 0)) {
				window.location = PHP_ForceDepositURL;
				if (DontReturnFalse != 1) {
					return ReturnStatus;
				}
				else {
					return;
				}
			}
		}
		
		try {
			if (NeedLogin == 1) {
				FBLoginPopup();
				// $.facebox({ div: '#fb_login_popup' },'lightbox');
				if (DontReturnFalse != 1) {
					return ReturnStatus;
				}
				else {
					return;
				}
			}
		}
		catch (err) {}
	
		document.location.href=ChooseGameURL;
	}
	else {
	
		var Do_PopupEmail	=	false;
		if (PHP_GamesBeforeEmailPopup > 0) {
			Do_PopupEmail	=	true;
			if (GamesPlayed < PHP_GamesBeforeEmailPopup) {
				Do_PopupEmail	=	false;
			}
		}
			
		var Do_ForceRegister	=	false;
		if (PHP_GamesBeforeRegister > 0) {
			Do_ForceRegister	=	true;
			if (GamesPlayed < PHP_GamesBeforeRegister) {
				Do_ForceRegister	=	false;
			}
		}
				
		if (Do_ForceRegister == true) {	//	Redirect to register	
			if ((PHP_UserState == PHP_FreeUserState) && (PHP_ForceRegisterURL.length > 0)) {	
				window.location = PHP_ForceRegisterURL;
				if (DontReturnFalse != 1) {
					return ReturnStatus;
				}
				else {
					return;
				}
			}
		}
			
		if (Do_PopupEmail == true) {	//	Show the Email popup
			if (PHP_EmailKnown == 0) {
				$('#fb_EmailFormTag').attr('action',FreePlayURL );
				$('#facebox #fb_EmailFormTag').attr('action',FreePlayURL );
				$.facebox({ div: '#fb_request_email' },'lightbox');
				if (DontReturnFalse != 1) {
					return ReturnStatus;
				}
				else {
					return;
				}
			}
		}
		
		document.location.href=FreePlayURL;

	}
	
	if (DontReturnFalse != 1) {
		return ReturnStatus;
	}
	
}
