
addEventToObject = function(obj,evt,func) {
	var oldhandler = obj[evt];
	obj[evt] = (typeof obj[evt] != 'function') ? func : function(ev){oldhandler(ev);func(ev);};
}

setFocus = function(selectorID)
{
	$(selectorID).select();
}

changeStyle = function(selectorID,selectorClass)
{
	if ($(selectorID) != null)
	{
		$(selectorID).className = selectorClass;
	}
}

showKeywordSearch = function()
{
	$('search-container').style.display = 'block';
}

// Handle table zebra striping
// ----------------------------------------------
stripetableBody = function()
{
	if (!document.getElementsByTagName) return false;
	var tableBody = document.getElementsByTagName("tbody");
	for (var i=0; i<tableBody.length; i++)
	{
		if (tableBody[i].getAttribute("class") == "alternateTableRows" || tableBody[i].getAttribute("className") == "alternateTableRows")
		{
		
			var odd = false;
			var rows = tableBody[i].getElementsByTagName("tr");
			for (var j=0; j<rows.length; j++)
			{
				if (odd == true)
				{
					addClass(rows[j],"alternate");
					odd = false;
      			}
				else
				{
					odd = true;
		  		}
			}
	  	}
	}
}

// Modify presentation of page if loaded
// ----------------------------------------------
function stampBodyAsLoaded()
{
	if (!document.getElementsByTagName) return false;
	var selectors = document.getElementsByTagName("body");
	var selector = selectors[0];
	
	addClass(selector,'is-loaded');
}

// Add classes to selectors
// ----------------------------------------------
addClass = function(selector,value)
{
	if (!selector.className)
	{
    	selector.className = value;
	}
	else
	{
    	newClassName = selector.className;
	    newClassName+= " ";
	    newClassName+= value;
	    selector.className = newClassName;
  	}
}

findLabels = function()
{
	var el = document.getElementsByTagName("label");
	for (i=0;i<el.length;i++)
	{
		var thisId = el[i].getAttribute("for");
		if ((thisId)==null)
		{
			thisId = el[i].htmlFor;
		}
		if(thisId!="")
		{
			el[i].onmouseover = highlightRelationship;
		}
	}
}

highlightRelationship = function()
{
	var thisId = this.getAttribute("for");
	if ((thisId)==null)
	{
		thisId = this.aspxFor;
	}	
	if ($(thisId).type=="text") $(thisId).select();
	if ($(thisId).type=="password") $(thisId).select();
	if ($(thisId).tagName=="textarea") $(thisId).select();
}


// Handle the launching of new browser windows for offsite links as well as non-web files.
// ----------------------------------------------
scanForExternalLinks = function()
{
	if (!document.getElementsByTagName) return false;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		{
			anchor.href = "javascript:getOffsiteLink('"+anchor.getAttribute("href")+"');";
		}
		else if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "nonWebDocument")
		{
			anchor.href = "javascript:getFile('"+anchor.getAttribute("href")+"');";
		}
		else if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "popup")
		{
			anchor.href = "javascript:getFile('"+anchor.getAttribute("href")+"');";
		}
	}
}

getOffsiteLink = function(href)
{
	attributes = 'scrollbars=no,toolbar=no,menubar=0,status=no,width=960,height=590,directories=no,location=no,resizable=0';
	newWindow = window.open("/timeline/index.htm",'myOpenWindow',attributes);
	newWindow.focus();
}
getOffsiteLink2 = function(href)
{
	attributes = 'scrollbars=no,toolbar=no,menubar=0,status=no,width=790,height=472,directories=no,location=no,resizable=0';
	newWindow = window.open(href,'myOpenWindow2',attributes);
	newWindow.focus();
}

getFile = function(href)
{
	attributes = 'scrollbars=auto,toolbar=no,menubar=no,status=no,directories=no,location=no,resizable=yes';
	newWindow = window.open(href,'',attributes);
	newWindow.focus();
}

// Sets the value for the keyword search
var smartKeyword = {
	init : function()
	{
		var keywordInput = $('txtKeyword');
		if (keywordInput)
		{
			addEventToObject(keywordInput,'onclick',smartKeyword.click);
			addEventToObject(keywordInput,'onblur',smartKeyword.blur);
		}	
	},
	click : function()
	{
		var keywordInput = $('txtKeyword');
		if (keywordInput.value == "Search")
		{
			keywordInput.value = "";
		}
	},
	blur : function()
	{
		var keywordInput = $('txtKeyword');
		if (keywordInput.value == "" || keywordInput.value == " ") {keywordInput.value = "Search";}
	}
};
	
// Browser detection
// ----------------------------------------------
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

// Attach functions to window onload
// ----------------------------------------------
Event.observe(window,'load',smartKeyword.init);
Event.observe(window,'load',showKeywordSearch);
Event.observe(window,'load',stripetableBody);

// Assist the behaviour and presentation of form elements
// ----------------------------------------------
scanForFormElements = function()
{
	if (!document.getElementsByTagName) return false;
	var anchors = document.getElementsByTagName("input");
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		if ((anchor.getAttribute("type") == "text") || (anchor.getAttribute("type") == "password"))
		{
			if (anchor.className!="noCSS")
			{
				addClass(anchor,'textfield');	
			}
		}
		if ((anchor.getAttribute("type") == "button") || (anchor.getAttribute("type") == "submit") || (anchor.getAttribute("type") == "reset") )
		{
			if (anchor.className!="noCSS")
			{
				addClass(anchor,'button');
			}
		}
	}
}
scanForPdfLinks = function() {
    $$('a:([href$=".pdf"])').each(function(a) {
        Event.observe(a,
                                                              'click',
                                                              function() { makePdfTrackingCode(a.href); }
                                                              )
    }
                                  );
}

makePdfTrackingCode = function(href) {
    var start = -1;
    for (i = 0; i < 3; i++) {
        start = href.indexOf("/", start + 1);
    }
    var url = href.slice(start);
    return "javascript: pageTracker._trackPageview('" + url + "');";

}

Event.observe(window, 'load', scanForFormElements);
Event.observe(window, 'load', scanForPdfLinks);


/*
areleavingSite = true;

notLeavingSite = function() {
    areleavingSite = false;
}


scanForInternalLinks = function() {
    $$('a:not([href^="http"])').each(function(a) { Event.observe(a, 'click', notLeavingSite)});
}
var feb2009Survey = "questionnaire20090223";


clearCookie = function(cookie) {
        document.cookie = cookie + ";path=\"/\";max-age=0";
    }

launchQuestionnaire = function() {
        attributes = 'scrollbars=no,toolbar=no,menubar=0,status=no,width=1190,height=830,directories=no,location=no,resizable=0';
        newWindow = window.open("http://www.keysurvey.com/survey/242523/3f0a/", 'questionnaireWindow', attributes);
        if (newWindow != null) {
            newWindow.focus();
        }
        else {
            writeCookieDays(feb2009Survey, 'Next_Visit', 150);           
        }
        return newWindow;
}

getQuestionnaireCookie = function(cookie) {
    var ret = "";
    var cookieName = cookie + "=";
    var allCookies = document.cookie;
    var pos = allCookies.indexOf(cookieName);
    if (pos != -1) {
        var start = pos + cookieName.length;
        end = document.cookie.indexOf(";", start);
        if (end == -1) {
            end = allCookies.length;
        }
        ret = allCookies.substring(start, end);
    }
    return ret;
}
checkForLeavingSite = function() {
    if (areleavingSite) {
        var setCookie = getQuestionnaireCookie(feb2009Survey);
        if ((setCookie == null) || (setCookie == 'Never')) {
            // NOP - the case for "unless" as a keyword
        }
        else if (setCookie == 'Yes') {
            writeCookieDays(feb2009Survey, 'Never', 150);
            launchQuestionnaire();
        }
        else if (setCookie == "Later") {
            writeCookieDays(feb2009Survey, 'Next_Visit', 150);
        }
        return true;
    }
}
function doClickQuestionnaire(arg) {
    if (arg == 'yes') {
        writeCookieDays(feb2009Survey, 'Yes', 150);
        showSmallContent();
    }
    else if (arg == 'never') {
        writeCookieDays(feb2009Survey, 'Never', 150);
        hideContent();
    }
    else if (arg == 'later') {
        writeCookieDays(feb2009Survey, 'Later', 150);
        hideContent();
    }
}

writeCookieDays = function(name, value, days) {
    var date = new Date();
    date.setTime(date.getTime() + (24 * 60 * 60 * 1000 * days));
    var expires = "; expires=" + date.toGMTString();
    document.cookie = name + "=" + value + ";" + expires + "; path=/";
}

function showSmallContent() {
    new Ajax.Updater('questionnaireContent', '/contents/questionnaire/questionnaireShortFormFebruary2009.html', { method: 'get', evalScripts: true });
}

function hideContent() {
    $('questionnaireContent').hide();
}

doQuestionnaire = function() {
    var w = launchQuestionnaire();
    if (w != null) {
        writeCookieDays(feb2009Survey, 'Never', 150);
    }
    $('shortForm').hide();
}

Event.observe(window,'load', scanForInternalLinks);
Event.observe(window,'unload', checkForLeavingSite);
*/