//
// displayMessage
//
// PARAMS:  sMsgElemID - id for message element.
//          sMessage - message to display.
//          sType - Error class (note empty string denotes 'normal')
//
function displayMessage(sMsgElemID, sMessage, sType)
{
    var msgElem;

    if (sMessage != "")
    {
        msgElem = document.getElementById(sMsgElemID);
        msgElem.firstChild.nodeValue = sMessage;

        msgElem.className = sType;
    }
}

//
// isStringEmpty
//
// PARAMS:  elem - element to be validated.
//          errorMsgElem - elem used to display error msg.
//
function isStringEmpty(elem, errorMsgElemID)
{
    var bEmpty = true;
    if (elem.value.length == 0 || elem.value == null)
    {
        displayMessage(errorMsgElemID, "required", "error");
        elem.focus();
        bEmpty = true;
    }
    else
    {
        bEmpty = false;
        displayMessage(errorMsgElemID, "*", "");
    }

    return bEmpty;
}

// PARAMS:  elem - element to be validated.
//          errorMsgElem - elem used to display error msg.
//
function checkStringField(elem, errorMsgElemID)
{

    var bEmpty = false;

    bEmpty = isStringEmpty(elem, errorMsgElemID);

    if (bEmpty == false)
    {
        return true;
    }

    return false;
}

// PARAMS: elem - radio element to be validated
//         errorMsgElemID - elem used to display error msg.
//
function checkRadioField(elem, errorMsgElemID)
{
    var bEmpty = true;
    for (var i=0; i < elem.length; i++) {
        if (elem[i].checked) {
            bEmpty = false;
        }
    }

    if (bEmpty == true)
    {
        displayMessage(errorMsgElemID, "required", "error");
        elem[0].focus();
    }
    else
    {
        displayMessage(errorMsgElemID, "*", "");
    }

    return !bEmpty;
}

//
function checkUrlField(elem, errorMsgElemID)
{
    var bEmpty = true;
    if (elem.value.length == 0 || elem.value == null || elem.value == "http://" || elem.value == "http://null")
    {
        displayMessage(errorMsgElemID, "required", "error");
        elem.focus();
        bEmpty = true;
    }
    else
    {
        bEmpty = false;
        displayMessage(errorMsgElemID, "*", "");
    }

    if (bEmpty == false)
    {
        return true;
    }

    return false;
}






















// November 26, 2007: Some of these functions are based on the very handy script library found here
// http://www.breakingpar.com/bkp/home.nsf/Category?OpenNavigator&87256B280015193F872571810073C3FD

function addSelectedToList(candidateListField, targetListField) {
   if (candidateListField.length == -1) { // if the candidate list is empty
      alert("You must select an item to add to the list, but the list of candidates is empty!");
   } else {
      var selected = candidateListField.selectedIndex;
      if (selected == -1) {
          alert("You must select an entry to add!");
      } else {
          var newValue = candidateListField[selected].value;
          var newText = candidateListField[selected].text;
          var len = targetListField.length++; // Increase the size of list and return the size
          targetListField.options[len].value = newValue;
          targetListField.options[len].text = newText;
          targetListField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
      }
   } // Ends the check to see if the candidate list was valid
}

function addSelectedToTopOfList(candidateListField, targetListField) {
   if (candidateListField.length == -1) { // if the candidate list is empty
      alert("You must select an item to add to the list, but the list of candidates is empty!");
   } else {
      var selected = candidateListField.selectedIndex;
      if (selected == -1) {
          alert("You must select an entry to add!");
      } else {
          var newValue = candidateListField[selected].value;
          var newText = candidateListField[selected].text;
          var len = targetListField.length;
          targetListField.length++;
          for (i = len; i > 0; i--)
          {
              targetListField.options[i].value = targetListField.options[i-1].value;
              targetListField.options[i].text = targetListField.options[i-1].text;
          }
          targetListField.options[0].value = newValue;
          targetListField.options[0].text = newText;
          targetListField.selectedIndex = 0; // Highlight the one just entered (shows the user that it was entered)
      }
   } // Ends the check to see if the candidate list was valid
}

function selectAll(listField) {
    if (listField.length == -1) {
        // nothing to do, but do not alert
    } else {
        for (var i = 0; i < listField.length; i++) {
            listField.options[i].selected = true;
        }
    }
}

function addToList(listField, newText, newValue) {
   if ( ( newValue == "" ) || ( newText == "" ) ) {
      alert("You cannot add blank values!");
   } else {
       // todo: add new entries to the TOP of the list, not the bottom!
      var len = listField.length++; // Increase the size of list and return the size
      listField.options[len].value = newValue;
      listField.options[len].text = newText;
      listField.selectedIndex = len; // Highlight the one just entered (shows the user that it was entered)
   } // Ends the check to see if the value entered on the form is empty
}

function removeFromList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be removed!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be removed!");
      } else {  // Build arrays with the text and values to remain
         var replaceTextArray = new Array(listField.length-1);
         var replaceValueArray = new Array(listField.length-1);
         for (var i = 0; i < listField.length; i++) {
            // Put everything except the selected one into the array
            if ( i < selected) { replaceTextArray[i] = listField.options[i].text; }
            if ( i > selected ) { replaceTextArray[i-1] = listField.options[i].text; }
            if ( i < selected) { replaceValueArray[i] = listField.options[i].value; }
            if ( i > selected ) { replaceValueArray[i-1] = listField.options[i].value; }
         }
         listField.length = replaceTextArray.length;  // Shorten the input list
         for (i = 0; i < replaceTextArray.length; i++) { // Put the array back into the list
            listField.options[i].value = replaceValueArray[i];
            listField.options[i].text = replaceTextArray[i];
         }
      } // Ends the check to make sure something was selected
   } // Ends the check for there being none in the list
}

function moveUpList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == 0 ) {
               alert("The first entry in the list cannot be moved up.");
            } else {
               // Get the text/value of the one directly above the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected-1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected-1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected-1].text = moveText2;
               listField[selected-1].value = moveValue2;
               listField.selectedIndex = selected-1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}

function moveDownList(listField) {
   if ( listField.length == -1) {  // If the list is empty
      alert("There are no values which can be moved!");
   } else {
      var selected = listField.selectedIndex;
      if (selected == -1) {
         alert("You must select an entry to be moved!");
      } else {  // Something is selected
         if ( listField.length == 0 ) {  // If there's only one in the list
            alert("There is only one entry!\nThe one entry will remain in place.");
         } else {  // There's more than one in the list, rearrange the list order
            if ( selected == listField.length-1 ) {
               alert("The last entry in the list cannot be moved down.");
            } else {
               // Get the text/value of the one directly below the hightlighted entry as
               // well as the highlighted entry; then flip them
               var moveText1 = listField[selected+1].text;
               var moveText2 = listField[selected].text;
               var moveValue1 = listField[selected+1].value;
               var moveValue2 = listField[selected].value;
               listField[selected].text = moveText1;
               listField[selected].value = moveValue1;
               listField[selected+1].text = moveText2;
               listField[selected+1].value = moveValue2;
               listField.selectedIndex = selected+1; // Select the one that was selected before
            }  // Ends the check for selecting one which can be moved
         }  // Ends the check for there only being one in the list to begin with
      }  // Ends the check for there being something selected
   }  // Ends the check for there being none in the list
}























// user types in a form box (search_box) and
// rows in the table referenced by tableId
// are shown/hidden based on what the user
// typed.
function doSearch(search_box, tableId)
{
    var q = search_box.value;

    q = q.toLowerCase();
    var q_ars = new Array();
    q_ars = q.split(' ');

    for(var d = 0; d < q_ars.length; d++)
    {
        if(q_ars[d] == "")
        {
            q_ars.splice(d, 1);
        }
    }
    var tbl = document.getElementById(tableId);
    var rows = tbl.rows;
    for(var i = 0; i < rows.length; i++)
    {
        var cells = rows[i].cells;
        var q_parts_found = "";
        for(var j = 0; j < cells.length - 1; j++)
        {
            var val = cells[j].innerHTML;
            val = val.toLowerCase();

            for(var k = 0; k < q_ars.length; k++)
            {
                if(q_ars[k] != "" && val.indexOf(q_ars[k]) != -1)
                {
                    q_parts_found += k + " ";
                    rows[i].style.display = '';
                }
            }
        }
        var foundCell = 1;
        for(var n = 0; n < q_ars.length; n++)
        {
            if(q_parts_found.indexOf(n) == -1)
            {
                foundCell = 0;
            }
        }
        if(foundCell == 1)
        {
            rows[i].style.display = '';
        }
        else
        {
            rows[i].style.display = 'none';
        }
    }
}
