String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };


/** switches the (style) display on the specified layer */
function switchDisplay (id)
{
   if (document.getElementById)
   {
       el = document.getElementById(id);
       if (el)
       {
            currentDisplay = el.style.display;
            if (currentDisplay == "none")
            {
                 el.style.display = "block";
            }
            else
            {
                 el.style.display = "none";
            }
        }
   }
}


function setDisplay (id, display)
{
   if (document.getElementById)
   {
       el = document.getElementById(id);
       if (el)
       {
            el.style.display = display;
        }
   }
}



function switchIdentifierForm (selectObj)
{
   if (currentElementId) switchDisplay(currentElementId);
   id = selectObj.options[selectObj.selectedIndex].value;
   if (id.length > 0)
   {
      switchDisplay(id);
      currentElementId = id;
      if (selectObj.form.startHex)
      {
         updateFieldValue("start", selectObj.form.startHex.value);
         updateFieldValue("end", selectObj.form.endHex.value);
      }
   }
   else
   {
      currentElementId = null;
   }
}

currentElementId = null;


function updateFieldValue (fieldName, value)
{
   if (document.getElementsByTagName)
   {
      input_a = document.getElementsByTagName("input");
      for (i=0;i<input_a.length;i++)
      {
         input = input_a[i];
         if (input.name == fieldName)
         {
            input.value = value;
         }
      }
   }
}


function setEditSubDir (selectObj)
{
   if (document.getElementById)
   {
      if (selectObj.options[selectObj.selectedIndex].value > 0)
      {
         span = document.getElementById("dirText");
         input = document.forms.subdirForm.dirname;
         if (span && input)
         {
            bits = selectObj.options[selectObj.selectedIndex].text.split("/");
            dirName = bits[bits.length - 1];
            span.innerHTML = "Change " + dirName + " to<br/>";
            input.value = dirName;
            setDisplay("dirP", "block");
         }
      }
      else
      {
            setDisplay("dirP", "none");
      }
   }
}


var numTags = 0;

function addDocTag (li, tag)
{
    if (li.className && li.className == "docTagOn")
    {
       li.className = "";
       numTags--;
       if (li.tagField) li.tagField.value = "";
    }
    else if (numTags < 5)
    {
       li.className = "docTagOn";
       numTags++;
       // find the first available field to put the value in
       for (i=1;i<=5;i++)
       {
          tagField = eval("document.forms['docForm'].tag" + i);
          if (tagField && tagField.value.length == 0)
          {
             tagField.value = tag;
             li.tagField = tagField;
             break;
          }
       }
    }
}

function initTags()
{
   if (document.forms['docForm'])
   {
       for (var i=1;i<=5;i++)
       {
          tagField = eval("document.forms['docForm'].tag" + i);
          if (tagField && tagField.value.length > 0)
          {
             for (j=1;j<=5;j++)
             {
               li = document.getElementById("liTag" + j);
               if (li)
               {
                  test = li.innerHTML.valueOf();
                  if (test.trim() == tagField.value.trim())
                  {
                     li.tagField = tagField;
                     li.className = "docTagOn";
                  }
               }
             }
          }
       }
   }
}
