// *******************************************************************
// Global Forrester.com Javascript: Footer Edition
// ------------------------------------------------------------------
// Updated: 18-AUG-2010 (JAM)
//
// *******************************************************************


// Updates the footer's copyright year to the correct year
function updateDate() {
	if (document.getElementById("currentCopyrightYear")) {
		var d = new Date();
		var currentYear = d.getFullYear();
		document.getElementById("currentCopyrightYear").innerHTML = currentYear;
	}
}
updateDate();

// *******************************************************************

// Makes the (normally unlinked) header logo a link to the home page
if (currentURL.indexOf("loginreg.jsp") != -1 || +
	currentURL.indexOf("cblogin.jsp") != -1 || +
	currentURL.indexOf("cart.jsp") != -1 || +
	currentURL.indexOf("campaignlogin.jsp") != -1 || +
	currentURL.indexOf("/ecomm/confirmation.jsp") != -1) {
	document.getElementById("regHeaderLink").href = "/";
}

// *******************************************************************

// Slides #userName menu to the left if no persona is displayed (h1 class="role_-1") 
function slideuserName() {
	if (document.getElementById("userName")) {
		if (document.getElementsByTagName("h1")[0].className == "role_-1") {
			document.getElementById("userName").style.marginLeft = "0";
		}
	}
}
slideuserName();

// *******************************************************************

// Prevents IE6 background image flicker in links 
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

// *******************************************************************

// Trims a string's white space. Does not inherently exist in Javascript.
// Example: var trimmedString = originalString.trim();
String.prototype.trim = function () {
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

// *******************************************************************

// Completely clear a form (radio buttons included)
function clearForm(oForm) {
	
	var elements = oForm.elements; 
	oForm.reset();
	
	for (i=0; i<elements.length; i++) {
	
		field_type = elements[i].type.toLowerCase();
		
		switch(field_type) {
		
			case "text": 
			case "password": 
			case "textarea":
			case "hidden":	
			
			elements[i].value = ""; 
			break;
			
			case "radio":
			case "checkbox":
			if (elements[i].checked) {
				elements[i].checked = false; 
			}
			break;
			
			case "select-one":
			case "select-multi":
				elements[i].selectedIndex = -1;
			break;
			
			default: 
			break;
		}
	}
}

// *******************************************************************

// Tabs (show/hide tab content)
//  * Requires <ul id="allTabs">
//  * Each tab's representative content <div> ID, prepended by "content_",
//    must match the ID of each tab <li>, so:
//       <li id="tab3">
//       <div id="content_tab3">
 
if (document.getElementById("allTabs") && document.getElementById("tabContent")) {
	// Declaring tabs variables
	var tabsContent = document.getElementById("tabContent");
	var tabContentDivs = tabsContent.getElementsByTagName("div");
	var tabList = document.getElementById("allTabs");
	var tabs = tabList.getElementsByTagName("li");
	
	// Hides tab content DIV
	tabsContent.style.display = "none";
	
	// On page load, displays a tab's content if its <li> has class="current"
	for (z=0;z<tabs.length;z++) {
		var currentTabContent = "content_" + (tabs[z].id);
		if (tabs[z].className.indexOf("current") != -1) {
			tabsContent.style.display = "block";
			document.getElementById(currentTabContent).style.display = "block";
		} else {
			if (document.getElementById(currentTabContent)) {
				document.getElementById(currentTabContent).style.display = "none";
			}
		}
	}
	
	function currentTab() {
	// Highlights the current tab & shows the tab's content
	
		// Highlight current tab	
		for (i=0; i<tabs.length; i++) {
			tabs[i].className = "";
		}
		this.className = "current";
	
		// Unhides tab content DIV
		tabsContent.style.display = "block";
		
		// Displays only the currently-selected tab's content
		var tabContentDivs = tabsContent.getElementsByTagName("div");
		for (i=0; i<tabContentDivs.length; i++) {
			if (tabContentDivs[i].id.indexOf("content_") != -1) {
				
				if (tabContentDivs[i].id.indexOf(this.id) != -1) {
					tabContentDivs[i].style.display = "block";
				} else {
					tabContentDivs[i].style.display = "none";
				}
			}
		}
		
	}
	
	// Applies currentTab() as an onclick event for all tabs
	for (i=0; i<tabs.length; i++) {
		tabs[i].onclick = currentTab;
	}
}

// *******************************************************************

// Activates a thickbox when a checkbox is clicked
function checkboxThickbox(cbox,url,TBheight,TBwidth) {
	if (cbox.checked == true) {
		tb_show(null, url + "?height=" + TBheight + "&amp;width=" + TBwidth, false);
	}
}

// *******************************************************************
// FOR ALERTS OVERHAUL 7/2010

// Apply onclick to all "Delete (all) Alert" links
function loadAlertRSSonclicks () {
	if (document.getElementById("delAllAlerts")) {	
		// Apply to "Delete All" link
		document.getElementById("delAllAlerts").onclick = function(){deleteAlerts("all of your Research Alerts");};
		// Apply to all "Delete" links
		var allLinks = document.getElementsByTagName("a");
		for (i=0;i<allLinks.length;i++) {
			if (allLinks[i].className == "delAlert") {
				allLinks[i].onclick = function(){deleteAlerts("this Research Alert");};
			}
		}
	}
}
loadAlertRSSonclicks();

function deleteAlerts(confirmText) {
	var deleteConfirm = confirm("Are you sure you want to delete " + confirmText + "?");
	if (deleteConfirm == true) {
		// [PERFORM DELETE ACTIONS]
	}
}

// *******************************************************************

// ---------- IE6 BUG FIX (Badger 23652) ------------ //
// ------- Displays all tab content at once --------- //

function IEblowout() {
	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == 6 && document.getElementById("advancedSearch")) {

		// Hides list of tabs to choose from
		document.getElementById("allTabs").style.display = "none";
		
		// Shows all tabs' content at once
		tabsContent.style.display = "block";
		tabsContent.style.marginTop = ".2em";
		if (tabContentDivs == null) {
			var tabContentDivs = tabsContent.getElementsByTagName("div");
		}
		for (i=0; i<tabContentDivs.length; i++) {
			if (tabContentDivs[i].id.indexOf("content_") != -1) {
				tabContentDivs[i].id = "IEblowout" + i;
				tabContentDivs[i].style.display = "block";
			}
		}


		// hide the 'close' button
		document.getElementById("closeTabs").style.display = "none";
		
		// Make <H2> visible as headers
		var refineByHeaders = document.getElementById("refineBy").getElementsByTagName("h2");
		for (j=0;j<refineByHeaders.length;j++) {
			if (refineByHeaders[j].className == "accessibility") {
				refineByHeaders[j].className = "subtitle_green";
				refineByHeaders[j].style.marginTop = ".7em";
			}
		}	
		// Adjust spacing for some headers
		refineByHeaders[0].style.marginTop = "0";
		refineByHeaders[1].style.marginTop = "1.8em";
		

		// Clears checkboxes on page (re)load
		// This was done because in IE6, on refresh the checkboxes remain checked,
		// but the tally list is cleared. There was no (easy) way to repopulate the
		// tally list, because IE6 doesn't recognize the checkboxes as being "checked":
		// (e.g. checked = false) even when they were, in fact, checked. Got that?
		var allInputs = document.getElementsByTagName("input");
		for (k=0;k<allInputs.length;k++) {
			if (allInputs[k].type == "checkbox" && allInputs[k].type == true) {
	document.write(allInputs[i].checked + "\n");
				allInputs[k].checked = false;
			}
		}
	}
}
IEblowout();

// *******************************************************************

// Gets an element's next sibling, ignoring whitespace
function nextSibling(elem) {
	do {
		elem = elem.nextSibling;
	} 
	while (elem && elem.nodeType != 1);
	return elem;                
}

