// -------------------------------------------------------------------------- //
// referrerCookie.js
//
// Sets a cookie on the client recording the referrer
//
// -------------------------------------------------------------------------- //


/////////////////////////////////////////////////////////////
//
// setCookie()
//
// DESC:	Creates/overwrites a cookie
//
// USAGE:	setCookie(name, value, expires, path, domain, secure);
//
// INPUT:	name		- Name of the cookie
//			value		- Value of the cookie
//			[expires]	- Expiration date of the cookie (defaults to end of the current session)
//			[path]		- Path to which the cookie applies (defaults to entire domain)
//			[domain]	- Domain to which the cookies applies (defaults to current domain)
//			[secure]	- Boolean specifying if a secure connection is required (defaults to false)
//
// OUTPUT:	None
//
// CHANGED:	document.cookie
//
/////////////////////////////////////////////////////////////

function setCookie(name, value, expires, path, domain, secure)
{
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		"; path=/" +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}


/////////////////////////////////////////////////////////////
//
// getCookie()
//
// DESC:	Returns the value of the specified cookie or NULL
//			if the specified cookie is undefined.
//
// USAGE:	value = getCookie(name);
//
// INPUT:	name		- Name of the cookie to be retrieved
//
// OUTPUT:	Value of the specified cookie or NULL
//
// CHANGED:	None
//
/////////////////////////////////////////////////////////////

function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}


/////////////////////////////////////////////////////////////
//
// deleteCookie()
//
// DESC:	Deletes the specified cookie
//
// USAGE:	deleteCookie(name, path, domain);
//
// INPUT:	name		- Name of the cookie to be deleted
//			[path]		- Path of the cookie to be deleted
//			[domain]	- Domain of the cookie to be deleted
//
// OUTPUT:	None
//
// CHANGED:	document.cookie
//
/////////////////////////////////////////////////////////////

function deleteCookie(name, path, domain)
{
	if ( getCookie(name) )
	{
		document.cookie = name + "=" + 
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function getParameter(paramName) {
	var currentUrl = window.location.search
	var strBegin = currentUrl.indexOf(paramName) 
	if (strBegin == -1)
	{
		return "";
	}
	
	//alert('begin='+strBegin);
	//+ (paramName.length+1)
	var strBegin = currentUrl.indexOf(paramName) + (paramName.length+1)
	var strEnd = currentUrl.indexOf("&",strBegin)

	if (strEnd==-1)
		strEnd = currentUrl.length

	return currentUrl.substring(strBegin,strEnd)
}


//
// Set referrer cookie if it doesn't already exist
//
if ( ! getCookie("jsRef") )
{
	// Set expiration date of 3 days
	var currDate = new Date();
	currDate.setTime(currDate.getTime() + 3 * 24 * 60 * 60 * 1000);

	// Set cookie
	setCookie("jsRef", document.referrer, currDate);
}


//
// Set referrer cookie if it doesn't already exist
//
if ( ! getCookie("PPCCode") )
{
	// Set expiration date of 3 days
	var currDate = new Date();
	currDate.setTime(currDate.getTime() + 3 * 24 * 60 * 60 * 1000);


}

//alert('gothere');
//alert ('***' + getParameter("PPCCode") + '***');
if (getParameter("PPCCode") != "")
{
	//alert ("Not Null");
	// Set cookie
	setCookie("PPCCode", getParameter("PPCCode"), currDate);
}
else
{
	//alert ("Null - did nothing");
}

//alert ('cookie=' + getCookie("PPCCode"));
