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 CTMBR and ZTM cookies . %}
    	document.cookie = "CTMBR=;path=/";
    	document.cookie = "ZTM=;path=/";
    }
}
//%{ --------------------------------------------------------------- %}

allcookies = document.cookie;					//%{ Read again the cookies. %}
var ctbalance = "";
var ctfirstname = "";
var ctlastname = "";


var wl = "$(MerchantRefNum)";
//%{ ---------- Look for the start of the cookie named "CTMBR" ---------- %}
var pos = allcookies.indexOf("CTMBR=");

var cookyvalue = allcookies;

//%{ If we find a cookie by that name, extract and use its value %}
if (pos != -1)
{
    var start = pos + 6;      						//%{ 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(",",3)				//%{ Split it in maximum 12 individual values %}
    
   	if(valArray[0] != undefined)
   		ctbalance = valArray[0];
   	if(valArray[1] != undefined)
		ctfirstname = valArray[1];
	if(valArray[2] != undefined)
		ctlastname = valArray[2];
	
	x = ctbalance.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');
	}
	ctbalance = x1 + x2;
	/*
	if(ctbalance == "")
	{
		ctbalance = "0";
	}
	*/
}