//var poop = document.getElementById("Teleconferences");
//alert(poop.offsetWidth);




// ------------ HELP TEXT ------------ //

// Declaring Help text variables
var srchForTable = document.getElementById("advSearchForTable");
var allCells = srchForTable.getElementsByTagName("td");
var helperText = document.getElementById("helpText");
var defaultText = helperText.innerHTML;
helperText.innerHTML = "";

// Apply showHelp()/clearHelp() as mouseover/out events to "i" images' parent TD
var infoImgs = srchForTable.getElementsByTagName("img");
for (i=0; i<infoImgs.length; i++) {
	infoImgs[i].parentNode.onmouseover = showHelp;
	infoImgs[i].parentNode.onmouseout = clearHelp;
}

function showHelp() {
	// Shows Help text on mouseover
	var cells = this.parentNode.getElementsByTagName("td");
	cells[1].className = "helpHighlight";
	cells[2].className = "helpHighlight";
	helperText.rowSpan = "4"
	helperText.className = "helpHighlight";
	this.id != "help00" ? helperText.innerHTML = cells[3].innerHTML : helperText.innerHTML = defaultText;
}

// Clears help context from all cells
function clearHelp() {
	for (i=0; i<allCells.length; i++) {
		if (allCells[i].className != "noDisplay") {
			allCells[i].className = "";
		}
	}
	helperText.innerHTML = "";
}




// ------------ DATE RANGE ------------ //

// Auto-selects appropriate radio button when <option> is clicked
var dateRangeSelects = document.getElementById("advDateRange").getElementsByTagName("select");
for (e=0;e<dateRangeSelects.length;e++) {
	dateRangeSelects[e].onchange = function() {drRadioSelect(this.parentNode);};
}
function drRadioSelect(theFieldset) {
	var allInputs = theFieldset.getElementsByTagName("input");
	for (i=0;i<allInputs.length;i++) {
		if (allInputs[i].type == "radio") {
			allInputs[i].checked = true;
		}
	}
}


// ------------ TABS ------------ //
var allLinks = document.getElementsByTagName("a");
for (i=0;i<allLinks.length;i++) {
	if (allLinks[i].className == "clickToShowDiv" || allLinks[i].className == "clickToHideDiv") {
		allLinks[i].onclick = function() {toggleDiv(this,this.parentNode.parentNode); return false;};	
	}
}


// Declaring tabs variables
var tabsContent = document.getElementById("tabContent");
var tabList = document.getElementById("allTabs");
var tabs = tabList.getElementsByTagName("li");

// Hides tab content DIV
tabsContent.style.display = "none";

// Applies currentTab() as an onclick event for all tabs
for (i=0; i<tabs.length; i++) {
	tabs[i].onclick = currentTab;
	// tabs[i].addEventListener("click", currentTab, false);
}

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";

	// Sets current tab's 'pointer' graphic to the center of the current tab
	// Needed to do this to support IE6 which wasn't being...helpful.
//	var pointerSet = 0;	
//	for (i=0;i<tabs.length;i++) {
//		if (tabs[i].className == "") {
//			pointerSet = (pointerSet + tabs[i].offsetWidth) -15;
//		} else if (tabs[i].className == "current") {
//			pointerSet = (pointerSet + (this.offsetWidth/2)) -15;  // the '-6' is because the first list item has no left padding
//			tabsContent.style.backgroundPosition = pointerSet + "px top";
//			break;
//		}	
//	}
	
	
	// 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) {
			tabContentDivs[i].id.indexOf(this.id) != -1 ? tabContentDivs[i].style.display = "block" : tabContentDivs[i].style.display = "none";
		}
		// Fix IE6 problem with 'Analyst' checkboxes bleeding through other tabs' content
		if (tabContentDivs[i].className == "showHideDiv") {
			tabContentDivs[i].className = tabContentDivs[i].className + " noDisplay";
		}
	}
}


// applies resetTabs() as onclick event for the "x" image
document.getElementById("closeTabs").onclick = resetTabs;

function resetTabs() {
// Hides tab content DIV and deselects all tabs
	document.getElementById("tabContent").style.display = "none";
	for (i=0; i<tabs.length; i++) {
		tabs[i].className = "";
	}
}




// ******* Adds/removes items from the checklists and selection tally ******* //
window.onload = clearChecks;

function clearChecks() {
// 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?
	for (i=0;i<allInputs.length;i++) {
		allInputs[i].checked = false;
	}
}

var colonShow = document.getElementById("colon");			// Colon that appears after "Refine by" when selections are made
var theList = document.getElementById("selectionList");		// Grabs the <UL> of "Refine By" terms tally...
	theList.style.display = "none";							// ...and hides it
var LIcount = theList.getElementsByTagName("li");			// Counts <LI>s in the "Refine by" terms tally <UL>
var allInputs = tabsContent.getElementsByTagName("input");	// Grabs all the checkboxes (inputs)
	
// Applies "addSelect()" as onclick to each checkbox
for (i=0;i<allInputs.length;i++) {
	allInputs[i].onclick = function() {addSelect(this);};
}

function addSelect(theBox) {
	colonShow.style.display = "inline";	// Unhides the colon after "Refine by"
	theList.style.display = "block";	// Unhides the <UL>

	if (theBox.checked == true) {
		var newLI = document.createElement("li");
		newLI.setAttribute("id",theBox.name);
		newLI.innerHTML = "<strong>" + theBox.className + "</strong> (<a href=\"#\" class=\"remove_term\">Remove</a>)"; 
		theList.appendChild(newLI); 
  		newLI.childNodes[2].onclick = function() {removeSelect(newLI.id); return false}; // Applies "removeSelect()" as onclick to each new <LI>
	} else if (LIcount.length > 0) {
		removeSelect(theBox.name);
	}
}

function removeSelect(refNum) {
	for (x=0;x<LIcount.length;x++) {
		if (LIcount[x].id == refNum) {
			var remover = LIcount[x];
			remover.parentNode.removeChild(remover);
			
			// unchecks checkbox if "remove" link is clicked
			for (i=0;i<allInputs.length;i++) {
				if (allInputs[i].name == refNum) {
					allInputs[i].checked = false;
				}
			}
		}
	}
	if (LIcount.length == 0) {
		colonShow.style.display = "none";
	}
}






// ------------ FORM HANDLING FUNCTIONS ------------ //

// Attach onclick events to the 'Submit' and 'Clear' form buttons
if (document.getElementById("butFormClear")) {
	var clearButton = document.getElementById("butFormClear");
	clearButton.onclick = function() {clearForm("advancedSearch");}
}

if(document.getElementById("butFormSubmit")) {
	var submitButton = document.getElementById("butFormSubmit");
	submitButton.onclick = function() {submitForm("advancedSearch");}
}

function clearForm(buttonName) {
	document.forms[buttonName].reset();
	theList.innerHTML = ""; // Clears the 'Refine by' tally list
	colonShow.style.display = "none";
}

function submitForm(buttonName) {
	var allWords = document.getElementById("allWords");
	var exactPhrase = document.getElementById("exactPhrase");
	var anyWords = document.getElementById("anyWords");
	var noneWords = document.getElementById("noneWords");
	if(noneWords.value != "" && (allWords.value == "" && exactPhrase.value == "" && anyWords.value == "")) {
		alert("You must enter at least two text fields when entering a value into the 'None of these words' field");
		return false;
	}
	document.forms[buttonName].submit();
}

