// these values are set by the persona cookie
var personaName, personaId;
var blazeFullHost;
var omnitureId = 'No ID';
    
/*====Cookies===========================*/
function getCookie(name) 
{
    var nameEQ = name + "=";
    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) 
        {
        	return c.substring(nameEQ.length,c.length);
        }
    }
    return null;
}

function setCookie(name, value, expDateStr) 
{
    var cookieString = name + "=" + value + ";path=/;";
    if (expDateStr) 
    {
        document.cookie = cookieString + "expires=" + expDateStr;
    } 
    else 
    {
        document.cookie = cookieString;
    }
}

function deleteCookie(name) 
{
	document.cookie = name + "=;path=/;" + new Date().toGMTString();
}

//Loads the cookie used to set persona info
function loadPersonaCookie(){
    var fullPersona = decode64(urldecode(getCookie('TWOCookie[PERSONA]')));
		
    if(fullPersona)
    {
        var arrCookie = fullPersona.split("&");
        for(var i=0; i<arrCookie.length; i++)
        {
            if (arrCookie[i] != undefined)
            {
                if(arrCookie[i].toLowerCase().indexOf("personaid") == 0)
                {
                    personaId = arrCookie[i].substr(arrCookie[i].indexOf("=")+1);

                    // choose one of the slaves
                    var blazeHostIdx = personaId % blazeHosts.length; 
                    blazeFullHost = blazeHosts[blazeHostIdx];
                }
                else if(arrCookie[i].toLowerCase().indexOf("personaname") == 0)
                {
                    personaName = arrCookie[i].substr(arrCookie[i].indexOf("=")+1);
                }
                else if(arrCookie[i].toLowerCase().indexOf("omnitureid") == 0)
                {
                    omnitureId = arrCookie[i].substr(arrCookie[i].indexOf("=")+1);
                }
            }
        }
    }
    else
    {
    	omnitureId = 'No ID';

    	// choose one of the demo slaves - THIS SHOULD BE DEPRECATED, BUT THERE IS NOT ENOUGH TIME TO TEST BEFORE
    	//   DEMO TOURNAMENT RELEASE...  should be no need to set blazeFullHost here
    	var blazeHostIdx = new Date().getTime() % blazeDemoHosts.length; 
    	blazeFullHost = blazeDemoHosts[blazeHostIdx];
    }
}

/*===================Subscriptions==========================*/
function isSubscriber(cookieString)
{
    if(cookieString == "" || cookieString == null || cookieString == undefined  || cookieString == "none")
        return false;
    
    if(cookieString == "tigerUnlimited" || 
       cookieString == "subscription" )
    {
        return true;
    }
    return false;
}

function updateSubscriptionCookie(successCallback, errorCallback){
	if(ignoreSubscriptions != "1"){
		DAL_Request(true, "/nucleus/entitlements/",
				WSRequestTimeout,
				null,
				function(data,TextStatus){
					var subscriptionInfo = "";
					var teeTimeInfo = "";
					var teeTimeCount = 0;
					var eaInfo = "";
					var cookieString = "freetoplay|0";
					
					$("entitlement", data).each(function(i){
							if($("groupName", this).text() == "TIGERONLINE" && $("status", this).text() == "ACTIVE")
							{
								if($("entitlementTag", this).text() == "TIGERSUBSCRIPTION")
								{
									subscriptionInfo = "subscription";
								}
								else if($("entitlementTag", this).text() == "TIGERTEETIMES")
								{
									var useEntitlement = false;
									
									if($("terminationDate", this).text() == "")
									{
										useEntitlement = true;
									}
									else
									{
										var termArray = $("terminationDate", this).text().split("T");																		
										var termDateArray = termArray[0].split("-");
										var termYear = parseInt(termDateArray[0]);
										var termMonth = parseInt(termDateArray[1]);
										var termDay = parseInt(termDateArray[2]);										
										var nowDate = new Date();
										
										if(termYear > nowDate.getFullYear())
										{
											useEntitlement = true;
										}
										else if(termYear == nowDate.getFullYear())
										{
											if(termMonth > nowDate.getMonth() + 1)
											{
												useEntitlement = true;
											}
											else if(termMonth == nowDate.getMonth() + 1)
											{
												if(termDay >= nowDate.getDate())
												{
													useEntitlement = true;
												}
											}
										}
									}
																		
									if( useEntitlement )
									{
										teeTimeInfo = "freetoplay|";
										teeTimeCount += parseInt( $("useCount",this).text() );
									}
								}
								else if($("entitlementTag", this).text() == "TIGERUNLIMITED")
								{
									eaInfo = "tigerUnlimited";
								}
							}							
					});

					if(eaInfo != "")
						cookieString = eaInfo;					
					else if(subscriptionInfo != "")
						cookieString = subscriptionInfo;
					else if(teeTimeInfo != "")
						cookieString = teeTimeInfo + teeTimeCount;
					
					setCookie("TWOCookie[SUBSCRIPTION]", cookieString, null);
					if(successCallback != null)
						successCallback(cookieString);
                    try
                    {
                        setupTopNav();
                    }
                    catch(Err){}
				}, function(){
					try
					{
						errorCallback("");
					}catch(Err){}
				}, null);
	}
	else
	{
		var cookieString = "tigerUnlimited";

		setCookie("TWOCookie[SUBSCRIPTION]", cookieString, null);
		if(successCallback != null)
			successCallback(cookieString);
		setupTopNav();
	}
}
