var mDatabaseLoc;
var mAllowUpdate;

/** *******************************************************
 *  getXmlElementText(xml,name)
 * 
 *  params: xml     - The xml element
 *          name    - The name of the field to get
 *  
 *  return: The text from the element
 * ********************************************************/
function getXmlElementText(xml,name)
{
    var element = xml.getElementsByTagName(name)[0];
    
    if ( (element) && (element.hasChildNodes()) )
    {
        return element.childNodes[0].nodeValue;
    }
    return "";
}

/** *******************************************************
 *  convertToHtml(value)
 * 
 *  params: value - The value to write to the element
 *  
 *  return: N/A
 * ********************************************************/
function convertToHtml(value)
{
    var newValue = "";

    // If it starts with Http:// - make it into a link
    if (value.indexOf("Http://") == 0)
    {
        newValue = "<a href=\""+value+"\">"+value+"</a>";
    }
   
    return (newValue.length>0)?newValue:value;
}

/** *******************************************************
 *  setDomElement(id,value,type)
 * 
 *  params: id    - The Document Id of the element
 *          value - The value to write to the element
 *          type  - The type of the element to be write to 
 *  
 *  return: N/A
 * ********************************************************/
function setDomElement(id,value,type)
{
    //alert("setDomElement("+id+","+value+","+type+")");

    var element = document.getElementById(id);
    if (element)
    {
        switch(type)
        {
        case "value"    : element.value=value; break;
        case "checked"  : element.checked=value; break;

        case "html"     : value = convertToHtml(value);
        default         : element.innerHTML=value;
        }
    }
}

/** *******************************************************
 *  updateDomElement(id,value)
 * 
 *  params: id    - The Document Id of the element
 *          value - The value to write to the element
 *  
 *  return: N/A
 * 
 *  Note: This determins the type by whether it can be
 *        updated or not
 * ********************************************************/
function updateDomElement(id,value)
{
    setDomElement(id,value,((mAllowUpdate)?"value":"html"));
}

/** *******************************************************
 *  updateElement(xml,id)
 * 
 *  params: xml   - The xml element
 *          id    - The Id of the element
 *                  (this has to match the xml and dom)
 *  
 *  return: N/A
 * ********************************************************/
function updateElement(xml,id)
{
    var info = getXmlElementText(xml,id); 
    
    if (info.length > 0) 
    {
        updateDomElement(id,info);
    }
}

/** *******************************************************
 *  updateTableElement(xml,name,index)
 * 
 *  params: xml   - The xml element
 *          id    - The Id of the element
 *                  (this has to match the xml and dom)
 *          index - The index of the table
 *  
 *  return: N/A
 * ********************************************************/
function updateTableElement(xml,id,index)
{
    var info = getXmlElementText(member,id); 
    
    if (info.length > 0) 
    {
        updateDomElement(id+index,info);
    }
}

/** *******************************************************
 *  addElement(xml,id)
 * 
 *  params: xml   - The xml element
 *          id    - The Id of the element
 *                  (this has to match the xml and dom)
 *  
 *  return: N/A
 * ********************************************************/
function addElement(xml,id)
{
    var info = getXmlElementText(xml,id); 
    
    if (info.length > 0) 
    {
        var h = id.charAt(0).toUpperCase()+id.substr(1,id.length-1);
        setDomElement(id+"h",h+":","html");
        updateDomElement(id,info);
    }
}

/** *******************************************************
 *  getXmlHttpObject()
 * 
 *  params: N/A
 *  
 *  return: The xmlHttp handler
 * ********************************************************/
function getXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
      // Firefox, Opera 8.0+, Safari
      xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
      // Internet Explorer
      try
      {
          xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
      catch (e)
      {
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    }
    return xmlHttp;
}


/** *******************************************************
 *  formatCommandLink(id,cmd,link)
 * 
 *  params: id   - The id of the object to be used
 *          cmd  - The command to run on the object
 *          link - What's displayed as the command
 *  
 *  return: The command string to be displayed
 * ********************************************************/
function formatCommandLink(id,cmd,link)
{
    var htmlStr = "<a class=\"cmdLink\" href=# onclick=\"";
    htmlStr = htmlStr+"return update('"+cmd+"','"+id+"')";
    htmlStr = htmlStr+"\">"+link+"</a>\n";
    return htmlStr;
}

/** *******************************************************
 *  formatCommand(id,cmd)
 * 
 *  params: id   - The id of the object to be used
 *          cmd  - The command to run on the object
 *  
 *  return: The command string to be displayed
 * ********************************************************/
function formatCommand(id,cmd)
{
    return formatCommandLink(id,cmd,cmd);
}

/** *******************************************************
 *  alertError(errorStr)
 * 
 *  params: errorStr - The error to be displayed
 *  
 *  return: N/A
 * ********************************************************/
function alertError(errorStr)
{
    alert ("Error: "+errorStr);
}

/** *******************************************************
 *  init(db,update)
 * 
 *  params: db     - The location of the database to use
 *          update - Indicates if the fields can be updated
 *  
 *  return: N/A
 * ********************************************************/
function initDbCall(db,update)
{
    mDatabaseLoc = db;
    mAllowUpdate = update;
}

/** *******************************************************
 *  stripLeadingZero(value)
 * 
 *  params: value - The value to strip
 *  
 *  return: stripped value
 * ********************************************************/
function stripLeadingZero(value)
{
    if (value.charAt(0) == '0')
    {
        return value.substring(1)
    }
    return value;
}

