function deleteCookie(name) {
document.cookie = name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 

function findCookie(name) {
	if (document.cookie == '') { 
	return false;
	} else {
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name);
		if(firstChar != -1)  {
		firstChar += name.length + 1;
		lastChar = theBigCookie.indexOf(';', firstChar);
		if(lastChar == -1) lastChar = theBigCookie.length;
			return true;
		} else {
			return false;
		}
    }
}

function FindTrafficSource(TheSource)
{
	if (document.cookie == '')
	{
		return false;
	}
	else
	var firstChar, lastChar;
	var WholeCookie = document.cookie;
	firstChar = WholeCookie.indexOf("trafficsource");
	if ( firstChar == -1 )
	{
		return false;
	}
	else
	{
		lastChar = WholeCookie.indexOf(";",firstChar+14);
		if ( lastChar == -1 ) lastChar=WholeCookie.length;
		var tsInfo = WholeCookie.substr(firstChar+14,lastChar-14);
		var TSValues = unescape(tsInfo);
		var TSPairs = TSValues.split("&");
		for (var i=0;i<TSPairs.length;++i)
		{
			var pair = TSPairs[i].split("|");
			if ( pair[0] == "source")
			{
				if (pair[1] == TheSource)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
	}
	return false;
}

function buildTrafficSource(source)
{
	if (!(findCookie ("trafficsource"))) {
		var expires = 730;
		var formatdate = getFormatDate();
		var expr = new Date();
		var path = '/';
		var expr = new Date(expr.getTime() + (expires * 86400000));
		var data =  "source|" + source + "&createdate|" + formatdate; 
		var cookie = "trafficsource=" + escape(data) + ( (expires) ? ";expires=" + expr.toGMTString() : "") + ( (path) ? ";path=" + path : "");;
		document.cookie = cookie;
	}
}

function buildTrafficSourceCS(source, csdata)
{
	if (!(findCookie ("trafficsource"))) {
		var expires = 730;
		var formatdate = getFormatDate();
		var expr = new Date();
		var path = '/';
		var expr = new Date(expr.getTime() + (expires * 86400000));
		var data =  "source|" + source + "&createdate|" + formatdate + "&csdata|" + csdata;
		var cookie = "trafficsource=" + escape(data) + ( (expires) ? ";expires=" + expr.toGMTString() : "") + ( (path) ? ";path=" + path : "");
		document.cookie = cookie;
	}
}

function buildWebsiteOptimizer(id)
{
	if (!(findCookie ("websiteOptimizer"))) {
		var expires = 730;
		var formatdate = getFormatDate();
		var expr = new Date();
		var path = '/';
		var expr = new Date(expr.getTime() + (expires * 86400000));
		var data =  "id|" + id + "&createdate|" + formatdate;
		var cookie = "websiteOptimizer=" + escape(data) + ( (expires) ? ";expires=" + expr.toGMTString() : "") + ( (path) ? ";path=" + path : "");
		document.cookie = cookie;
	}
}

function buildAffiliateInfo(source)
{
	if ((findCookie ("AffiliateInfo"))) {
		deleteCookie("AffiliateInfo")
	}
	if (window.location.search.substring(1) && window.location.search.substring(1) != "") {
		var expires = 730;
		var formatdate = getFormatDate();
		var expr = new Date();
		var path = '/';
		var expr = new Date(expr.getTime() + (expires * 86400000));
		var data =  "AffiliateID|" + getQueryVariable('aid') + "&OfferID|" + getQueryVariable('oid') + "&createdate|" + formatdate; 
		var cookie = "AffiliateInfo=" + escape(data) + ( (expires) ? ";expires=" + expr.toGMTString() : "") + ( (path) ? ";path=" + path : "");
		document.cookie = cookie;
	}
}

function getFormatDate()
{
	var today = new Date();
	var timezoneoffset = today.getTimezoneOffset();
	if ((navigator.appVersion.indexOf('MSIE 3') != -1)) timezoneoffset = timezoneoffset * (-1);
	today.setTime(today.getTime() + timezoneoffset*60*1000);
	today.setTime(today.getTime() - 8*60*60*1000);
	var month = today.getMonth() + 1;
	var date = today.getDate();
	var year = today.getYear();
	if (year < 1900) year += 1900;
	var hour = today.getHours();
	if (hour < 10) hour = "0" + hour;
	var minute = today.getMinutes();
	if (minute < 10) minute = "0" + minute;
	var second = today.getSeconds();
	if (second < 10) second = "0" + second;
	var formatdate = month + "/" + date + "/" + year + " " + hour + ":" + minute + ":" + second;
	return formatdate;
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return "";
}
