function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function Search(sSearch,sView,sSubmitPage,bsubmit) {

	var eform = document.frmSearch;
	var sHighlight = 0;

	// F I R S T   C H E C K   S E C T I O N S
	var sSections = "";
	for (i=0; i<document.frmSearch.elements.length; i++)
	{
		// Set Flag if any checkboxes are found
		if (document.frmSearch.elements[i].type == "checkbox" && document.frmSearch.elements[i].name != "chkHighlight") {
			var sCheckBoxesPresent = true;
		}

		// T H I S   I S   T H E   O N L Y   C O M P E N D I U M 
		// S P E C I F I C   B I T   T H A T   N E E D S   C H A N G I N G
		
		if (document.frmSearch.elements[i].checked == true && document.frmSearch.elements[i].name == "chkTextSections") {
			sSections = sSections + "/NAM2/TXT3";
		}
		if (document.frmSearch.elements[i].checked == true && document.frmSearch.elements[i].name == "chkBibliography") {
			sSections = sSections + "/BIB2";
		}
		if (document.frmSearch.elements[i].checked == true && document.frmSearch.elements[i].name == "chkLibrary") {
			sSections = sSections + "/LIB2";
		}
		if (document.frmSearch.elements[i].checked == true && document.frmSearch.elements[i].name == "chkGlossary") {
			sSections = sSections + "/GLOS/GLOSDEF";
		}
		if (document.frmSearch.elements[i].checked == true && document.frmSearch.elements[i].name == "chkDrugSearch") {
			sSections = sSections + "/DR1/DR2";
		}
		if (document.frmSearch.elements[i].checked == true && document.frmSearch.elements[i].name == "chkHighlight") {
			sHighlight = 1;
		}
	}

	
	// T E S T   I S   A N Y   S E C T I O N S   S E L E C T E D
	if (sSections == "" && sCheckBoxesPresent == true) {
		alert('Please select at least one section to search on');
		return;
	}

	// R E M O V E   T H E   F I R S T   S L A S H
	sSections = sSections.substring(1,sSections.length);


	// N E X T   D E T E R M I N E   S E A R C H   T Y P E
	if (sSearch == "advanced") {										// A D V A N C E D   S E A R C H

		// A S S I G N   V A L U E S
		var sWords_All = eform.txWords_All.value;
		var sWords_Exact = eform.txWords_Exact.value;
		var sWords_AtLeast = eform.txWords_AtLease.value;
		var sWords_Without = eform.txWords_Without.value;
		
		// T E S T   I F   A N Y   V A L U E S   P R E S E N T
		var sTestString = sWords_All + sWords_Exact + sWords_AtLeast + sWords_Without;
		var sStringTest = TestBox(sTestString);
		if (sStringTest == false) return;
		
		// R E P L A C E   S P A C E S
		sWords_All = replaceSubstring(sWords_All, " ", "+")
		sWords_Exact = replaceSubstring(sWords_Exact, " ", "+")
		sWords_AtLeast = replaceSubstring(sWords_AtLeast, " ", "+")
		sWords_Without = replaceSubstring(sWords_Without, " ", "+")
	
		// A S S I G N   T O   S E A R C H   S T R I N G   A N D   S U B M I T
		sSearch = "asp1=" + sWords_All + "&asp2=" + sWords_Exact + "&asp3=" + sWords_AtLeast + "&asp4=" + sWords_Without;
	}
	else																// S I M P L E   S E A R C H
	{
		// A S S I G N   V A L U E S
		var sSearchString = eform.txtCriteria.value;
		
		// T E S T   I F   A N Y   V A L U E S   P R E S E N T
		var sTestString = sSearchString;
		var sStringTest = TestBox(sTestString);
		if (sStringTest == false) return;
		
		// R E P L A C E   S P A C E S
		sSearchString = replaceSubstring(sSearchString, " ", "+")

		// A S S I G N   T O   S E A R C H   S T R I N G   A N D   S U B M I T
		sSearch = "as_search=" + sSearchString;
	}

	
	// S E T   P A G E   F O R   S U B M I S S I O N
	switch (sSubmitPage) {
	
		case "home.asp":	// If submitted from home page then run "Overview" search
		{
			document.frmSearch.action = "search_overview_results.asp?" + sSearch + "&as_sections=" + sSections + "&highlight=" + sHighlight + "&SearchBoxView=" + sView;
			break;
		}
		case "search_overview_results.asp":	// If submitted from home page then run "Overview" search
		{
			document.frmSearch.action = "search_overview_results.asp?" + sSearch + "&as_sections=" + sSections + "&highlight=" + sHighlight + "&SearchBoxView=" + sView;
			break;
		}

		default:			// If page is anything else then submit to itself.
		{
			document.frmSearch.action = "?" + sSearch + "&banner=1" + "&highlight=" + sHighlight + "&SearchBoxView=" + sView;
			break;
		}

	}


	// S U B M I T   T O   R E L E V A N T   P A G E
	// bsubmit is used to identify if the calling page called this function via a form submit or querystring click. If,
	// it was submitted by a form then we return true to the calling to prevent the form being submitted twice, i.e. when a search
	// takes place. If called from a querystring, i.e. the Advanced Search link, then we need to submit it as a form.

//alert(document.frmSearch.action);
//alert(bsubmit);

	if (bsubmit == 'false')
		{
			return true;
		}
	else
		{
			document.frmSearch.submit()
		}

}


function TestBox(sTestString) {
	if (sTestString == "")
	{
		alert('You must enter a value in the search box first');
		return false;
	}
}
