/**
 **
 **  Cookie Functions
 **
 **/ 
function SetCookie(name, value, hours) 
{
	if (typeof(hours) != 'undefined') 
	{
		var now = new Date();
		now.setTime(now.getTime() + (hours*60*60*1000));		
		var expires = '; expires=' + now.toGMTString();
	}
	else 
	{
		var expires = '';
	}

	document.cookie = name + '=' + value + expires + '; path=/';
}
 
function GetCookie(name)
{
	name = name + '=';
	var carray = document.cookie.split(';');

	for (var i=0; i<carray.length; i++) 
	{
		var c = carray[i];
		while (c.charAt(0)==' ') 
			c = c.substring(1,c.length);
		if (c.indexOf(name) == 0) 
			return(c.substring(name.length,c.length));
	}

	return(null);
}
 
function DeleteCookie(name) 
{
	SetCookie(name, '', -1);
}




/**
 **
 **  Recording and Control Functions
 **
 **/ 
function RecordLanding(defpage)
{
	SetCookie('LandingPage', defpage, 240)
}

function RecordHistory2(page)
{
	var history = GetCookie('History');
	
	if (history == null)
		history = page;
	else
		history = history + ',' + page;

	SetCookie('History', history, 24);	
}

function RecordHistory(page)
{
	var history = GetCookie('History');
	var lasttime = GetCookie('LastTime');	
	var last = new Date();	
	var now = new Date();
	var s = 0;

	if (lasttime != null)
	{
		last.setTime(Date.parse(lasttime));		
		s = Math.ceil((now.getTime() - last.getTime()) / 1000);	
		//alert('RecordHistory: now=' + now.getTime() + ' last=' + last.getTime() + ' diff=' + s);	
	}
	
	if (history == null)
		history = page;
	else
		history = history + '(' + s + ')' + ',' + page;
	//alert('RecordHistory: ' + history);

	SetCookie('History', history, 24);	
	SetCookie('LastTime', now.toGMTString(), 1);
}


function RecordReferrer()
{
	var referrer = document.referrer;

	if (referrer == '')
		return;	
	if ((referrer.indexOf('dataplow.com') > 0) &&
		(referrer.indexOf('dataplow.com') < 15))
		return;
		
	SetCookie('Referrer', referrer , 24);
}


function PageJump(pre, defaultpage)
{
	var landing = GetCookie('LandingPage');	

	if (landing == null)
	{
		//alert('PageJump: defaultpage=' + pre + defaultpage)
		window.location = pre + defaultpage;
	}
	else    
	{
		//alert('PageJump: landing=' + pre + landing)
		window.location = pre + landing; 
	}
}
