var allcookies = document.cookie;

//%{ ---------- Clearing the cookies when logged off: ---------- %}
var poz = allcookies.indexOf("SESSION_ID=");
var cookyval = allcookies;

//%{ If we find a cookie named SESSION_ID, extract and use its value, which is like:  -1,Ae6Htj4vHmggO0JN..... %}
if (poz != -1)
{
    var st = poz + 11;							//%{ Start of cookie value %}
    var j = allcookies.indexOf(",",st);			//%{ position of the intermediate comma %}
    cookyval = allcookies.substring(st, j);		//%{ Extract the value %}
    //cookyval = unescape(cookyval);       		%{ Decode it %}
    if(cookyval == "-1")
    {						//%{ In this case clear the old NWAMBR cookie . %}
    	document.cookie = "NWAMBR=;path=/";
    }
}
//%{ --------------------------------------------------------------- %}

allcookies = document.cookie;					//%{ Read again the cookies. %}
var nwaBalance = "";
var nwaEStatus = "";								// Tier, Elite Status
var nwaMembNbr = "";
var nwaFirstname = "";
var nwaLastname = "";


//%{ ---------- Look for the start of the cookie named "NWAMBR" ---------- %}
var pos = allcookies.indexOf("NWAMBR=");
var cookyvalue = allcookies;

//%{ If we find a cookie by that name, extract and use its value %}
if (pos != -1)
{
    var start = pos + 7;      						//%{ Start of cookie value %}
    var end = allcookies.indexOf(";", start);		//%{ End of cookie value %}
    if (end == -1) end = allcookies.length;
    cookyvalue = allcookies.substring(start, end);  //%{ Extract the value %}
    //cookyvalue = unescape(cookyvalue);            %{ Decode it %}

    valArray = cookyvalue.split(",",5)				//%{ Split it in maximum 5 individual values %}
    
   	if(valArray[0] != undefined)
   		nwaBalance = valArray[0];
   	if(valArray[1] != undefined)
		{		nwaEStatus = valArray[1];
				// Don't display "Base"
				if(nwaEStatus=="Base")	
				nwaEStatus = "";
		}

	if(valArray[2] != undefined)
		nwaMembNbr = valArray[2];
	if(valArray[3] != undefined)
		nwaFirstname = valArray[3];
	if(valArray[4] != undefined)
		nwaLastname = valArray[4];
		
	nwaBalance += '';
	x = nwaBalance.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	nwaBalance = x1 + x2;
	if(nwaBalance == "")
	{
		nwaBalance = "0";
	}
}