var LinksLimit = 4;
LinksLimit = parseInt(LinksLimit);

// Specify cookie name.
//var CookieName = "ABCRegion";
var CookieName = "ABCGuestID";

var DaysToLive = 0;
DaysToLive = parseInt(DaysToLive);

// No other customizations required.
var HistoryLink = new Array();
var HistoryTitle = new Array();
var HistoryContent = new String();

//if a cookie for this site exists, read its values
function GetCookie() 
{
var cookiecontent = '';
if(document.cookie.length > 0) 
{
   var cookiename = CookieName + '=';
   var cookiebegin = document.cookie.indexOf(cookiename);
   if(cookiebegin > -1) 
      {
      cookiebegin += cookiename.length;
      var cookieend = document.cookie.indexOf(";",cookiebegin);
      if(cookieend < cookiebegin) { cookieend = document.cookie.length; }
      //cookiecontent = document.cookie.substr(cookiebegin,cookieend);
	  //cookiecontent = document.cookie.substr(cookiebegin,document.cookie.length);
	  if (document.cookie.indexOf("smtp.abc.net.au")>-1)
	  	{
	  	cookiecontent = document.cookie.substr(44,document.cookie.length);
		}
	 else {
	  	cookiecontent = document.cookie.substr(cookiebegin,document.cookie.length);	
		}		  
	  //alert("cookiecontent: " + cookiecontent);
      }
   }
//alert("cookiecontent: " + cookiecontent); 
if( cookiecontent.length < 3 ) { return; } 	//see if cookie contains any values
cookiecontent = unescape(cookiecontent); 	//decode the extracted value
var historyList = cookiecontent.split('&'); 	//add the cookiecontent into historylist and split it at & char occurance
for( var i = 0; i < historyList.length; i++ ) 	//loop through the array to extract its values
   {
   var link = historyList[i].split('=',2);
   HistoryLink.push(link[0]);
   HistoryTitle.push(link[1]);
   var temparray = link[0].split('amp;');
   link[0] = temparray.join('&');
   temparray = link[1].split('amp;');
   link[1] = temparray.join('&');   
   }
}

function PutCookie() 
{
if( HistoryLink.length < 1 ) { return; }
var len = HistoryLink.length;
var pairs = new Array();
for( var i = 0; i < HistoryLink.length; i++ ) 
	{ 
	pairs.push(HistoryLink[i]+'='+HistoryTitle[i]); 
	} //end for
var value = pairs.join('&');
var exp = new String();

if(DaysToLive > 0) {
   var now = new Date();
   now.setTime( now.getTime() + (DaysToLive * 24 * 60 * 60 * 1000) );
   exp = '; expires=' + now.toGMTString();
   }
   document.cookie = CookieName + "=" + escape(value) + '; path=/' + exp;
}

Array.prototype.in_array = function(p_val) 
{
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
};

function SaveCurrentPage() 
{
var city_found_index;	
//extract the page link/url
var link = document.URL;
var title = document.title.length > 1 ? document.title : 'Irrelevant page';
var temparray = link.split('&');
link = temparray.join('amp;');
var temparray = title.split('&');
title = temparray.join('amp;');
//HistoryLink.push(link); 		//add the link to the array - comment for overloading fix
//HistoryTitle.push(title); 	//add page title to the array - comment for overloading fix

//VB START: added fix to the overloadng issue.
//to eliminate duplicates, make sure the link isn't in the array already
if (! HistoryLink.in_array(link))
	{
	//alert("the city isn't in the array");
	HistoryLink.push(link); //add the link to the array
	HistoryTitle.push(title); //add page title to the array
	}
else {
	city_found_index = HistoryLink.indexOf(link);	//if the item exists, get the index of it

	HistoryLink.splice(city_found_index,1);
	HistoryTitle.splice(city_found_index,1);
	
	HistoryLink.push(link); //add the link to the array
	HistoryTitle.push(title); //add page title to the array
	//alert("the city is IN the array,the index of it IS: "+city_found_index);
	//insert it right at the end
	}
if (HistoryLink.length>4)
	{
	HistoryLink.shift(); //use shift() to get rid of the element at the end
	HistoryTitle.shift();	
	}

//VB END
}

GetCookie();
SaveCurrentPage();
PutCookie();

