 
    var loggedInStatus; // Logged In Status of the user    
    (function() {    	
		if (IsLoggedIn()) {
    		if (GetCookie('attemptedProductXML') == 'true') {
    			if (GetCookie('loggedInStatusCookie') == '2') {
    				loggedInStatus = 2;
    			} else if (GetCookie('loggedInStatusCookie') == '1') {
    				loggedInStatus = 1;
    			}
    		} else {
    			SetCookie('attemptedProductXML', 'true');	//	Set a cookie to stop the browser hitting the Product XML more than once per session    			
				checkProductXML(47); // Product Id
			}
				
    	} else {
    		setLoggedInStatus(0);
    	}}			
	());	
	
	function securePages() {
		// Set the lightbox to links 
		
		$('a[href^="/page/WatchLive"], a[href^="/page/WatchLiveStreaming"], a[href^="/page/sub/TennisTV/Video"], a[href^="/page/secure/loginOrCreateAccount"]').each(function(){
			$(this).bind("click", function(){
				// check if the user has access rights				
				if (loggedInStatus == 2) {
					return true;
				}
				else {
					if ($("#loginModal").css("display") == "block") {
						return true;
					}
					else {
						// change link on lightbox
						if(loggedInStatus == 1) {
							$(".buybuttonpop .buynow").attr("href","/page/secure/Upgrade");
						} else {
							$(".buybuttonpop .buynow").attr("href","/page/secure/loginOrCreateAccount");
						}
						// Show login box
						if (GLOBAL_SITE_ID == 13540 && (location.hash == "#china" || jQuery.inArray(userLocation, chinaloc) != -1)) {
							$("#loginModalCN").jqmShow();
						} else {
							$("#loginModal").jqmShow();							
						}						
						return false;
					}
				}
			});
		});
	}
		
	$(document).ready(function(){
		
		// Set the login form action		
		$(".loginForm").attr("action",GLOBAL_SECURE_SCHEME_AND_HOST+"/internalLogin/loginProcess/0,,"+GLOBAL_SITE_ID+",00.html")						
		securePages();
		 					
	});
	
	function setLoggedInStatus(status) {
		loggedInStatus = status;
    	SetCookie('loggedInStatusCookie', status);
	}
	
	function checkProductXML(id) {
		$.ajax({
			type: "GET", 
			url: '/page/siteuserservlet/productPartId_' + id + '/0,,' + GLOBAL_SITE_ID + ',00.xml', 
			dataType: "text", 
			cache: false, 
			async: false,
			error: function() {
				//	Ajax failed, set the logged in status to not logged in
				setLoggedInStatus(0);
				//Added on 18/02/2012 this is because when user logs in and if Ajax calls fails then status is set to "0" but the cookie attemptedProductXML is  set to true.
				SetCookie('attemptedProductXML', 'false');
					
			},
			success: function(msg){
				if (msg.indexOf("error") > -1) {
			    	//	Ajax response contained the error tag, user is not logged in
			    	setLoggedInStatus(0);
			    } else if (msg.indexOf("false") > -1) {
			    	//	Ajax response returned false, the user is logged in but has not subscribed to the product
					setLoggedInStatus(1);
			    } else if (msg.indexOf("true") > -1) {
			    	// Ajax response returned true, the usr is logged in and has a subscription to the product
					setLoggedInStatus(2);
				}
			}
    	});
	}
		    
    	$(".header").ready(function(){
			if (loggedInStatus != 0) {
				loginSuccess();
				$('.fname').replaceWith(GetCookie('ptvzon').substring(GetCookie('ptvzon').indexOf('fname=') + 6, GetCookie('ptvzon').indexOf('&')));
			} else {
				$("#header-loggedOut").show();
			}
			if (window.location.search == "?login=failed") {
				loginFailure()
			}
    	});
    
    function crossDomainLogout() {
        var nonSecureDomain = GetCookie("NonSecureSchemeAndHost");
        var logoutPageUrl;
          if (nonSecureDomain == undefined) {
            logoutPageUrl = GLOBAL_SECURE_SCHEME_AND_HOST + "/secureLogoutRedirect.html";
           } else {
              logoutPageUrl = GLOBAL_NON_SECURE_SCHEME_AND_HOST + "/secureLogoutRedirect.html";
           }
              window.location = logoutPageUrl;
          } 

    
    function logout() {
	    //Inside your logout function you must reset the values of the two cookies before you log the user out of the site
	    SetCookie('attemptedProductXML', 'false');
	    SetCookie('loggedInStatusCookie', '0');
	    LogOut();
	    crossDomainLogout();
    }
    
    loginSuccess = function() {
	    $("#header-loggedOut").hide();
	    $("#header-loggedIn").show();
    }; 
    
    loginFailure = function() {
		$("#signin_menu").show();
    	$(".header-loginError").slideDown("fast").animate({opacity: 1.0}, 5000).slideUp("fast");
    };
    
    function userHasNoAccess(){
    	$('#loginModal').jqmShow(); 
    }
    
    function userNotLoggedIn(){
    	$('#loginModal').jqmShow(); 
    }
    
    function passwordReminder() {
	    var userName = document.headerLogin.userName.value;
	    if (userName == "") {
		    alert ("Please enter your E-mail Address first.");
		    document.headerLogin.userName.focus();
	    } else {
	    	open("/internalLogin/passwordHintProcess/0,,13118,00.html?userName=" + urlEncode(userName),"Password","width=380,height=300");
	    }
	    return true;
    }
    
    function logFormCheck(form) {
    	
		SetCookie('attemptedSubscribeAjax', 'false');
	    var formSubmitted = false;
	    if (qs['target']) {
	    	form.target.value = qs['target'];
	    } else if (GetCookie('target')) {
	    	form.target.value = GetCookie('target');
	    }
	    
		var errorMsg = "";
	    var makeFocus = "";
	    if (form.userName.value == "") {
	    	errorMsg += "Please enter your userName first.\n";
	    	makeFocus = form.userName;
	    }
	    
		if (form.password.value == "") {
	    	errorMsg += "Please enter a Password\n";
	    	if (typeof makeFocus != "object" && makeFocus == "") {
	    		makeFocus = form.password;
	    	}
	    }
		
	    if (errorMsg != "") {
		    var errorMsgPre = "Sorry, there are some errors with the form!\n\n";
		    var errorMsgPost = "\nPlease review the errors before submitting the form.";
		    alert(errorMsgPre + errorMsg + errorMsgPost);
		    if (makeFocus != "") {
		    	makeFocus.focus();
		    }
	    } else {
	    	formSubmitted = true;
	    }
		
	    if(formSubmitted){	    	    
		    var redirectPath = window.location.href;		    
		    setTargetLoginTarget(redirectPath);
	    }
		
	    return formSubmitted;
		
    } 
    
    function usernameReminder() {
    open("/internalLogin/forgottenUsername","Username","width=380,height=300");return true;
    }
    
    function setTargetLoginTarget(url) {
    $("input[@name='target']", $("form[@name='loginForm']")).val(url)
    }
    
    /* Code from toolkit login form */
    function urlDecode(theString) {
    if (typeof theString != "undefined" && theString != "") {
    var returnString = new String();
    returnString = theString;
    return unescape(returnString.replace(/[+]/g,"%20"));
    }
    return "";
    }
    
    function urlEncode(theString) {
    var returnString = new String();
    returnString = escape(theString);
    return returnString.replace(/[+]/g,"%2b");
    } 
    
    var qs = new Array();
    var querystring = document.location.search;
    var varArray = querystring.substring(querystring.indexOf("?")+1 ,querystring.length).split("&");
    for (var ii = 0; ii < varArray.length; ii++) {
    var varValueArray = varArray[ii].split("=");
    if (typeof qs[varValueArray[0]] != "undefined") {
    if (typeof qs[varValueArray[0]] == "object") {
    qs[varValueArray[0]].push(urlDecode(varValueArray[1]));
    } else {
    var valueArray = new Array(qs[varValueArray[0]],urlDecode(varValueArray[1]));
    qs[varValueArray[0]] = valueArray;
    }
    } else {
    qs[varValueArray[0]] = urlDecode(varValueArray[1]);
    }
    } 
