/*
 *        general.js
 *            Arnold E.
 *            23rd March 2003
 */
//  
function DeleteListItems(lstObject)
{
    var NumListItems;
      
    //  Remove items in list
    NumListItems = lstObject.length;
    for (Index  = 0; Index < NumListItems; Index++) {
         lstObject.options[0].removeNode();
    }
}
 
function ltrim(s)
{
     return s.replace( /^\s*/, "" )
}

function rtrim(s)
{
     return s.replace( /\s*$/, "" );
}

function trim(s)
{
     return rtrim(ltrim(s));
}

//  Called to count the number of items
function NumSelectedItems()
{
     nChecked = 0;

     for (Index = 0; Index < forms[0].length; Index++) {
          elt = forms[0].elements[Index];
          if (elt.type == 'checkbox' && elt.checked) {
              ++ nChecked;
          }
     }

     return nChecked;
}

//  Determine number of selected list items
function NumSelectedListItems(lstObject)
{
     var NumSelectedItems = 0;
     var Index = 0;

     for (Index = 0; Index < lstObject.length; Index++) {
          if (lstObject[Index].selected == true)
              ++ NumSelectedItems;
     }

     return NumSelectedItems;
}

function ResetFields()
{
     var lstObjects;

     txtObjects = document.forms[0].all.tags("input");
     if (txtObjects != null) {
         for (Index = 0; Index < txtObjects.length; Index++) {
              if (txtObjects.item(Index).type == "text" || txtObjects.item(Index).type == "password") {
                  txtObjects.item(Index).value = "";
              }
         }
     }

     txtObjects = document.forms[0].all.tags('textarea');
     if (txtObjects != null) {
         for (Index = 0; Index < txtObjects.length; Index++) {
              txtObjects.item(Index).value = '';
         }
     }
       
     lstObjects = document.forms[0].all.tags('select');
     if (lstObjects != null) {
         for (Index = 0; Index < lstObjects.length; Index++) {
              lstObjects.item(Index).selectedIndex = 0;
         }
     }
}

function ResetFieldsII()
{
     var lstObjects;

     txtObjects = document.forms[0].all.tags("input");
     if (txtObjects != null) {
         for (Index = 0; Index < txtObjects.length; Index++) {
              if (txtObjects.item(Index).type == "text" || txtObjects.item(Index).type == "password") {
                  txtObjects.item(Index).value = "";
              }
         }
     }

     txtObjects = document.forms[0].all.tags('textarea');
     if (txtObjects != null) {
         for (Index = 0; Index < txtObjects.length; Index++) {
              txtObjects.item(Index).value = '';
         }
     }
}

function TrimFields()
{
     var txtObjects;

     txtObjects = document.forms[0].all.tags('input');
     if (txtObjects != null) {
         for (Index = 0; Index < txtObjects.length; Index++) {
              if (txtObjects.item(Index).type == 'text') {
                  txtObjects.item(Index).value = trim(txtObjects.item(Index).value);
              }
         }
     }

     txtObjects = document.forms[0].all.tags('textarea');
     if (txtObjects != null) {
         for (Index = 0; Index < txtObjects.length; Index++) {
              txtObjects.item(Index).value = trim(txtObjects.item(Index).value);
         }
     }
}

function ValidateEmail(object)
{
     var validchars = 'abcdefghijklmnopqrstuvwxyz0123456789@.-_';

     if (ValidateEntry(object, 'Enter email address') == false) return false;

     var EmailAddress = object.value;
       
     for (var i = 0; i < EmailAddress.length; i++) {
          var letter = EmailAddress.charAt(i).toLowerCase();
          if (validchars.indexOf(letter) != -1)
              continue;
                   
          alert('Invalid character in email address: ' + letter);
          object.focus();
          return false;
     }
     
     if (EmailAddress.indexOf('@') == -1 || EmailAddress.indexOf('@') == EmailAddress.length - 1 || 
         EmailAddress.indexOf('.') == -1 || EmailAddress.indexOf('.') == EmailAddress.length - 1) {
         alert('Enter a valid email address');
         object.focus();
         return false;
     }
   
     return true;
}

function ValidateEntry(object, ErrMessage)
{
     //   Removes extra spaces
     str = trim(object.value);

     //   Replace with trimmed value
     object.value = str;
     if (str.length == 0) {
         alert(ErrMessage);
         object.focus();
         return false;
     }
         
     return true;
}

//  Validates a fixed character entry
function ValidateEntryFC(object, NumCharacters, ErrMessage)
{
     //   Removes extra spaces
     str = trim(object.value);
     //   Replace with trimmed value
     object.value = str;
     if (str.length != NumCharacters) {
         alert(ErrMessage);
         object.focus();
         return false;
     }
         
     return true;
}

function ValidateSelect(object, ErrMessage)
{
     if (object.value <= 0) {
         alert (ErrMessage);
         object.focus();
         return false;
     }
         
     return true;
}

function ValidateSelectedIndex(object, RejectIndex, ErrMessage)
{
     if (object.selectedIndex == RejectIndex) {
         alert (ErrMessage);
         object.focus();
         return false;
     }
         
     return true;
}

function ValidateSelectedValue(object, RejectValue, ErrMessage)
{
     if (object.value == RejectValue) {
         alert (ErrMessage);
         object.focus();
         return false;
     }
         
     return true;
}



