
/*
 *	Digg friendly URLEncode function -- taken from:
 *	http://cass-hacks.com/articles/code/js_url_encode_decode/
 *	[CB] Modified to use %20 instead of + for spaces
 */
function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '%20';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  
  return output;
}

/*
 * Cookie functions -- taken from:
 * http://www.quirksmode.org/js/cookies.html
 */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/*
 * Returns the value of the named cookie, or null if no such cookie
 * exists.
 */
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/*
 * Deletes the specified cookie.
 */
function eraseCookie(name) {
	createCookie(name,"",-1);
}

/*
 * Utility function to test if another object is actually a function.
 */
function isFunction(object) {
	return (object && typeof(object) == 'function');
}

/*
 * The afterLoad function, which is responsible for triggering the
 * dynamic JavaScript pull.
 */
function afterLoad() {
	//	We call all the existing onload functions _first_,
	//	in the hope that if they modify the document we
	//	will catch the changes.
	if (isFunction(chain_onload)) {
		chain_onload();
	}
	
	//	Check and see if we have a BLVD session cookie,
	//	if so, maintain that session value. If not, a session
	//	value will be generated by the PHP script.
	var blvdSessionId = readCookie("blvdSessionId");
	if (blvdSessionId == null) blvdSessionId = '';
	
	//	Generate the dynamic javascript tag
	var scriptTag = document.createElement("script");
	scriptTag.src =
		"http://69.12.4.242/js/dynamicJS.php" +
		"?blvdSessionId=" + blvdSessionId + 
		"&referrer=" + URLEncode(document.referrer) +
		"&title=" + URLEncode(document.title) +
		"&screenWidth=" + screen.width +
		"&screenHeight=" + screen.height +
		"&screenDepth=" + screen.colorDepth
	;
	
	var bodyNode = document.getElementsByTagName("body")[0];
	bodyNode.appendChild(scriptTag);
}

/*
 * Chain the BLVD afterLoad function into the OnLoad event handler.
 * We store the current OnLoad handler first so we can call it
 * from our own function.
 */
var chain_onload = window.onload;
window.onload = afterLoad;