var inmenu=false;
var lastmenu=0;
function Menu(current) {
   if (!document.getElementById) return;
   inmenu=true;
   oldmenu=lastmenu;
   lastmenu=current;
   if (oldmenu) Erase(oldmenu);
   m=document.getElementById("menu-" + current);
   box=document.getElementById(current);
   //box.style.left= m.offsetLeft + m.offsetWidth + 2;
   box.style.left= document.body.clientWidth/2 - 265;
   box.style.top= m.offsetTop + 100;
   box.style.visibility="visible";
   m.style.backgroundColor="#ffffff";
   box.style.backgroundColor="#ffffff";
   box.style.width="135px";
}
function Erase(current) {
   if (!document.getElementById) return;
   if (inmenu && lastmenu==current) return;
   m=document.getElementById("menu-" + current);
   box=document.getElementById(current);
   box.style.visibility="hidden";
   m.style.backgroundColor="#ffffff";
}
function Timeout(current) {
   inmenu=false;
   window.setTimeout("Erase('" + current + "');",500);
}
function Highlight(menu,item) {
   if (!document.getElementById) return;
   inmenu=true;
   lastmenu=menu;
   obj=document.getElementById(item);
   obj.style.backgroundColor="#ffffff";
}
function UnHighlight(menu,item) {
   if (!document.getElementById) return;
   Timeout(menu);
   obj=document.getElementById(item);
   obj.style.backgroundColor="#ffffff";
}
function CheckForm () {

	//Check for words to search, modify "(), [], +" for Perl program where these are reserved marks
                // if enter a pair of (),..., it is considered as two blanfs (may be separate). If only one ( or ), it
                // causes crash.
	if (document.Search.terms.value==""){
		alert("Please enter at least one keyword to search");
		document.Search.terms.focus();
		return false;
	}
	tempStr = document.Search.terms.value		// this is for cgi search. For asp, keep search word as it is. No modification needed

//	tempStr =replaceSubstring(tempStr, "(", "\\\(");
//	tempStr =replaceSubstring(tempStr, ")", "\\\)");
//	tempStr =replaceSubstring(tempStr, "[", "\\\[");
//	tempStr =replaceSubstring(tempStr, "]", "\\\]");
//	tempStr =replaceSubstring(tempStr, "+", "\\\+");
//	tempStr =replaceSubstring(tempStr, "#", "\\\#");
//	tempStr =replaceSubstring(tempStr, "*", "\\\*");

	// for keeping the entered keywords remain as not-modified, use another variable to transfer to Perl
	document.Search.newWords.value = tempStr;
	return true
}
function ClearForm () {
	//Clear search box to avoid see modified keyword
	document.Search.terms.value="";

	return true
}
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
