// ------------ 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
if (document.getElementById("advDateRange")) {
	var dateRangeSelects = document.getElementById("advDateRange").getElementsByTagName("select");
	for (e=0;e<dateRangeSelects.length;e++) {
		dateRangeSelects[e].onclick = 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 ------------ //
for (i=0;i<document.getElementsByTagName("a").length;i++) {
	if (document.getElementsByTagName("a")[i].className == "clickToShowDiv" || document.getElementsByTagName("a")[i].className == "clickToHideDiv") {
		document.getElementsByTagName("a")[i].onclick = function() {toggleDiv(this,this.parentNode.parentNode); return false;};	
	}
}

// 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 ******* //

//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?
//	var allInputs = document.getElementsByTagName("input");
//	for (i=0;i<allInputs.length;i++) {
//		if (allInputs[i].type == "checkbox") {
//document.write(allInputs[i].checked + "\n");
//			allInputs[i].checked = false;
//		}
//	}
//}
//// As this file is used for Alerts as well as Advance Search, checking 
//// for Advanced Search to see if we should clear checkboxes or not
////if (currentURL.indexOf("advanced.jsp") != -1) {
////	window.onload = clearChecks();
////}

			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 tabsContent = document.getElementById("tabContent")		// Creates a var for the "#tabContent" div
			var allInputs = tabsContent.getElementsByTagName("input");	// Grabs all the checkboxes (inputs)
			var tabContentDivs = tabsContent.getElementsByTagName("div");
	
// 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 = clearForm;
}

if (document.getElementById("butFormSubmit")) {
	var submitButton = document.getElementById("butFormSubmit");
	submitButton.onclick = function() {submitForm("advancedSearch");}
} 

function clearForm() {
	var mainForm = this.parentNode;
	mainForm.reset();
	theList.innerHTML = ""; // Clears the 'Refine by' tally list
	colonShow.style.display = "none"; // Hides the colon after the "Refine by" label
	if (! (BrowserDetect.browser == "Explorer" && BrowserDetect.version == 6)) { // if the browser is anything other than IE6
		resetTabs();
	}
}

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 == "")) {
		
		var noneWordsError = document.createElement("p");
		noneWordsError.className = "text_red";
		noneWordsError.style.margin = "3px 0 0 5px";
		noneWordsError.style.width = document.getElementById("noneWords").offsetWidth + "px";
		noneWordsError.innerHTML = "Sorry, in order to use \"None of these words\" to search, you must also enter a word or phrase in one of the fields above.";
		
		var cell = document.getElementById("noneWords").parentNode;
		cell.appendChild(noneWordsError);
		
		return false;
	}
	document.forms[buttonName].submit();
}


//// ---------- IE6 BUG FIX (Badger 23652) ------------ //
//// ------- Displays all tab content at once --------- //
//
//function IEblowout() {
//	if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == 6) {
//
//		// 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 (i=0;i<refineByHeaders.length;i++) {
//			if (refineByHeaders[i].className == "accessibility") {
//				refineByHeaders[i].className = "subtitle_green";
//				refineByHeaders[i].style.marginTop = ".7em";
//			}
//		}	
//		// Adjust spacing for some headers
//		refineByHeaders[0].style.marginTop = "0";
//		refineByHeaders[1].style.marginTop = "1.8em";
//	}
//}
//IEblowout();
