var __SOURCECODES_JS__ = 1;

// For the cookie stuff
var zsPreprocCookieLifespan = 1000*60*60*24; // One day
var zsCookieLifespan = 1000*60*60*24; // One day
var zsLongtermCookieLifespan = 1000*60*60*24*365*10; // Ten years
var today = new Date();

// Maxes
var MAX_BROWSER_ZOOMIES = 3;
var MAX_EMAIL_ZOOMIES = 3;

function MyQueryString(key)
{
	var value = null;
	var toPrint = "";
	var len = 0;
	var i = 0;
	
	for (i=0, len = MyQueryString.keys.length; i < len; i++)
	{
		if (MyQueryString.keys[i] == key)
		{
			value = MyQueryString.values[i];
			break;
			//toPrint += "Found!\n";
		}
		//toPrint += MyQueryString.keys[i]+"="+MyQueryString.values[i]+"\n";
	}
	
	//alert (toPrint);
	
	return value;
}
MyQueryString.keys = new Array();
MyQueryString.values = new Array();

function MyQueryString_Parse()
{
	//alert ("MyQueryString_Parse()");
	var query = window.location.search.substring(1);
	var pairs = query.split("&");
	
	for (var i=0;i<pairs.length;i++)
	{
		var pos = pairs[i].indexOf('=');
		if (pos >= 0)
		{
			var argname = pairs[i].substring(0,pos);
			var value = pairs[i].substring(pos+1);
			MyQueryString.keys[MyQueryString.keys.length] = argname;
			MyQueryString.values[MyQueryString.values.length] = value;		
		}
	}

}
MyQueryString_Parse();

//calculate cookie expiration - pass it a numbe of days
function getexpirydate(nodays)
{
	var UTCstring;

	Today = new Date();
	nomilli=Date.parse(Today);
	Today.setTime(nomilli+nodays*24*60*60*1000);
	UTCstring = Today.toUTCString();

	return UTCstring;
}

//set the longterm cookie - currently only the referral source
function setLongCookie(doc)
{
	if (MyQueryString ("source") && MyQueryString ("source") != "null")
	{
		setCookie (doc, "source", unescape(MyQueryString("source")), getexpirydate(60));
	}
}

//get the longterm cookie - currently only the referral source
function getLongCookie()
{
	var the_cookie = null;
	var broken_cookie = null;
	var retval = null;

	if (!document.cookie || document.cookie == '')
	{
		// there's no cookie, so go no further
		return 'none';
	}

	// there is a cookie
	the_cookie = document.cookie;
	the_cookie = unescape(the_cookie);
	broken_cookie = the_cookie.split(":");
	retval = broken_cookie[1];

	return retval;
}

setLongCookie(document);

// When processing starts
function zsProcStart (thedoc, email)
{
	var expires = new Date ();

	expires.setTime (today.getTime () + zsPreprocCookieLifespan);

	setCookie (
		thedoc,
		"zsProcHold",
		email,
		expires
	);

	return true;
}

// When processing ends
function zsProcDone (thedoc)
{
	var email = getCookie (thedoc, "zsProcHold");
	var expires = new Date ();
	var killit = new Date ();
	var longex = new Date ();
	var countCheck = 0;

	// We can't do anything if there's no holding cookie.
	if (email == null)
	{
		return true;
	}

	expires.setTime (today.getTime () + zsCookieLifespan);
	longex.setTime (today.getTime () + zsLongtermCookieLifespan);
	killit.setTime (today.getTime () - (zsLongtermCookieLifespan*2));

	// Now we work with the global cookie
	countCheck = getCookie (thedoc, "zsBrowser");
	//alert ("zsBrowser=='"+countCheck+"'");
	if (countCheck == null)
	{
		setCookie (
			thedoc,
			"zsBrowser",
			1,
			expires
		);
	}
	else
	{
		setCookie (
			thedoc,
			"zsBrowser",
			countCheck,
			killit
		);
		setCookie (
			thedoc,
			"zsBrowser",
			parseInt (countCheck) + 1,
			expires
		);
	}
	countCheck = getCookie (thedoc, "zsBrowser");
	//alert ("zsBrowser=='"+countCheck+"'");

	// Let's move the holding cookie to a processed cookie
	setCookie (
		thedoc,
		"zoomshare." + email,
		"hello",
		expires
	);
	// Erase the holding cookie
	setCookie (
		thedoc,
		"zsProcHold",
		email,
		killit
	);

	// Work with long-term email-specific cookie
	countCheck = getCookie (thedoc, "zsLong." + email);
	if (countCheck == null)
	{
		setCookie (
			thedoc,
			"zsLong." + email,
			1,
			longex
		);
	}
	else
	{
		setCookie (
			thedoc,
			"zsLong." + email,
			countCheck,
			killit
		);
		setCookie (
			thedoc,
			"zsLong." + email,
			parseInt (countCheck) + 1,
			longex
		);
	}

	return true;
}

// Reset the timer for the persistent ones
function zsExtend (doc, email)
{
	setCookie (
		doc,
		"zoomshare." + email,
		"hello",
		today.getTime () + zsCookieLifespan
	);

	return true;
}

// Check if we already have a cookie for this user or not.
function zsDenyCheck (doc, email)
{
	var check = null;

	// Work with email-specific one-day cookie
	check = getCookie (doc, "zoomshare."+email);
	//alert ("check == '" + check + "'");
	if (check != null)
	{
		return true;
	}
	
	// Work with browser-specific one-day cookie
	check = getCookie (doc, "zsBrowser");
	//alert ("check == '" + check + "'");
	if (check != null && parseInt (check) >= MAX_BROWSER_ZOOMIES)
	{
		return true;
	}

	// Work with email-specific long-term cookie
	check = getCookie (doc, "zsLong." + email);
	//alert ("check == '" + check + "'");
	if (check != null && parseInt (check) >= MAX_EMAIL_ZOOMIES)
	{
		return true;
	}
	
	return false;
}
