/* validate no nonnumeric */
function checkIsNumeric(formName, refArray)
{

  var why = "";

  for (var key in refArray)
  {
	//alert('key:"' + key + '", value:"' + refArray[key] + '"');
	why += isNumeric(formName.elements[key].value, refArray[key]);  
  }

  if (why != "")
  {
    alert(why);
    return false;
  }
  return true;
  //return false;
}

/* validate no nonnumeric */
function isNumeric(string, name) 
{
   var ValidChars = "0123456789";
   var Char;
   var IsNumber = true;

   var returnString = "";

   //alert('string: ' + string + ' name: ' + name + "\n");

   for (i = 0; i < string.length && IsNumber == true; i++) 
      { 
      Char = string.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
	 //IsNumber = false;
	 returnString = "Field \'" + name + "\' contains a non-numeric character:\n\n" + "\'" + string + "\'" + "\n\nIt cannot contain anything other than [0123456789].";  
         }
      }

   return returnString;
}

/* validate pubOrder */
function validatePubOrder()
{
    var why = "";

    why += checkLength(document.createPublication.pubOrder.value);

    for (var i=0; i<test_pubOrder.length; i++)
    {
        if (document.createPublication.pubOrder.value == test_pubOrder[i])
        {
                why += "\'publication number\' must be unique!\n\n";
                why += "That publication number has already been used!\n\n";
/*
                why += "The following publication numbers have already been used:\n\n";
                for (var i=0; i<test_pubOrder.length; i++)
                {
                        why += test_pubOrder[i];
                        why += "\n";
                }
*/
        }
    }

    if (why != "") {
       alert(why);
       return false;
    }
return true;
}

/* check the keyword form */
function checkKeywordForm(keywordInput, keywordName)
{
	//alert(keywordInput);

  var why = "";

  why += checkLength(keywordInput, keywordName, 3);

  if (why != "")
  {
    alert(why);
    return false;
  }
  return true;
}

function checkLength(strng, name, strngLen)
{
        var error = "";
        if (strng.length < strngLen)  { error = "The " + name + " must be at least " + strngLen + " characters long.\n"; }
        return error;
}

/* check whole form */
function checkWholeForm(formName, refArray)
{

  var why = "";

  for (var key in refArray)
  {
	//alert('key:"' + key + '", value:"' + refArray[key] + '"');
	why += checkIsEmpty(formName.elements[key].value, refArray[key]);  
  }

  if (why != "")
  {
    alert(why);
    return false;
  }
  return true;
}

/* see if fields are empty */
function checkIsEmpty(strng, name)
{
  if (strng == "") { return "Please enter a value for: " + name + "\n";
  }
  else { return ""; }
}


/* open a new popup window- in this case- a local file "privacy.html" containing privacy statement */
function openWindow(fileName)
{
	//alert(fileName);
        /* window attributes:
           no toolbar
           no menubar
           make resizable
           set original width and heigt: tall and thin
           add scrollbars
         */
        privacyWindow = window.open(fileName, "helpWindow", "toolbar=no,menubar=no,resizable=1,width=800,height=590,location=no,status=no,scrollbars=1");
        privacyWindow.moveTo(0,0); // set popup location on screen- in upper-left corner
}

/* open/close the navigation clamshell */
function openClose(idToChange) {

         if (document.getElementById) {

             var level = document.getElementById(idToChange).style;

             if (level.display == "block") { level.display = "none"; }
             else { level.display = "block"; }

             return false;
         }
         else {
             return true;
         }

} // end openClose


/* chose to open nav links based on page id */
function openNavigation(pageID)
{
        if ( (pageID == 'pageNewLabForm') || (pageID == 'pageNewPIForm') || (pageID == 'pageNewScreenerForm') 
	   || (pageID == 'pageNewScreenForm') || (pageID == 'pageNewExperimentForm') || (pageID == 'pageNewContactTypeForm') )
        {
                openClose('new');
        }
        else if ( (pageID == 'pageUpdateLabForm') || (pageID == 'pageUpdatePIForm') || (pageID == 'pageUpdateScreenerForm') 
	   || (pageID == 'pageUpdateScreenForm') || (pageID == 'pageUpdateExperimentForm') )
        {
                openClose('update');
        }
        else if ( (pageID == 'pageReportLabForm') || (pageID == 'pageReportPIForm') || (pageID == 'pageReportScreenerForm') 
	   || (pageID == 'pageReportScreenForm') || (pageID == 'pageReportExperimentForm') )
        {
                openClose('report');
        }

        else {}
}

/* change colors onfocus */
function setStyle(x) {
  document.getElementById(x).style.background="#eee";
}
function removeStyle(x) {
  document.getElementById(x).style.background="white";
}



/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}

/*
 * This is sort of a wrapper function to the doHighlight function.
 * It takes the searchText that you pass, optionally splits it into
 * separate words, and transforms the text on the current web page.
 * Only the "searchText" parameter is required; all other parameters
 * are optional and can be omitted.
 */
function highlightSearchTerms(searchText, treatAsPhrase, warnOnFailure, highlightStartTag, highlightEndTag)
{
  // if the treatAsPhrase parameter is true, then we should search for 
  // the entire phrase that was entered; otherwise, we will split the
  // search string so that each word is searched for and highlighted
  // individually
  if (treatAsPhrase) {
    searchArray = [searchText];
  } else {
    searchArray = searchText.split(" ");
  }
  
  if (!document.body || typeof(document.body.innerHTML) == "undefined") {
    if (warnOnFailure) {
      alert("Sorry, for some reason the text of this page is unavailable. Searching will not work.");
    }
    return false;
  }
  
  var bodyText = document.body.innerHTML;
  for (var i = 0; i < searchArray.length; i++) {
    bodyText = doHighlight(bodyText, searchArray[i], highlightStartTag, highlightEndTag);
  }
  
  document.body.innerHTML = bodyText;
  return true;
}


