//Some edit fields want to handle the "return" key being pressed.
//They will call this function and provide a callback
//function which handles the "return" key however they want.
function handleKeyPress(e, callback)
{
        var keycode;
        if (window.event)
        {
            keycode = window.event.keyCode;
        }
        else if (e)
        {
            keycode = e.which;
        }
        else return true;

        if (keycode == 13)
        {
            callback();
            
            //Added to prevent forms being automatically submitted
            return false;
        }
        
        return true;      
}

//And this does the same thing but
//passes two parameters. Since the code
//is so short, I won't combine this with the
//function above.
function handleKeyPressWithParams(e, callback, param1, param2)
{
        var keycode;
        if (window.event)
        {
            keycode = window.event.keyCode;
        }
        else if (e)
        {
            keycode = e.which;
        }
        else return true;

        if (keycode == 13)
        {
           callback(param1, param2);
        }
}


//This function fetches the text contained in an XML node
   function getTextFromNode(node)
   {
       if (node)
       {
           if (node.firstChild)
           {
               return node.firstChild.data;
           }
      }

      return "";
   }

   //This function finds a named child of an XML node and returns the text therein.
   function getTextFromChildNode(node, childname)
   {
     if (node)
     {
	     var collChildren = node.getElementsByTagName(childname);

	     if (collChildren.length > 0)
	     {
	        var nodeChild = collChildren.item(0);

	        return getTextFromNode(nodeChild);
	     }
     }

     //TODO: Is this return value checked?
     return -1;
   }

//An empty function, used as a callback
function emptyFunction()
{
}

   //If the current node is not an element node,
   //this function moves on to the next sibling
   //which is an element. If there's no such sibling,
   //the function will fail.
   function getNextElementNode(node)
   {
       while (node && node.nodeType != 1)
       {
           node = node.nextSibling;
       }
 
       return node;
   }


   function emptyTable(tablename) 
   {
       var tbl = document.getElementById(tablename);

       var lastRow = tbl.rows.length - 1;

       while (lastRow > -1)
       {
           tbl.deleteRow(lastRow);
           lastRow --;
       }
   }

   function getClickedElement(e)
   {
         if (!e)
         {
             e = window.event;
         }

         var elemClicked;

         if (e.target)
         {
             elemClicked = e.target;
         }
         else
         {
             elemClicked = e.srcElement;
         }
 
         return elemClicked;
   }

   function getElementOrFailGracefully(strID)
   {
        var elem = document.getElementById(strID);

        if (elem)
        {
            return elem;
        }
        else
        {
            alert("Error: element " + strID + " does not exist");
        }
   }
