//***** New Window Routines **************************************************
// Open a window that will load the specified help file.
function help_win(sUrl) {
  oWin = window.open(sUrl, "HelpWindow", "menubar=no,scrollbars=yes,toolbar=no,width=500,height=300");
  oWin.focus()
}

// Open a window that will load the specified preview page.
function preview_win(sUrl) {
  oWin = window.open(sUrl, "PreviewWindow", "menubar=no,scrollbars=yes,toolbar=no,width=800,height=600");
  oWin.focus()
}

// Open a window that will load the specified dialog box of a set size.
function dialog_win(sUrl, iWidth, iHeight) {
  oWin = window.open(sUrl, "DialogBox", "menubar=no,scrollbars=no,toolbar=no,status=yes,width="+iWidth+",height="+iHeight);
  oWin.focus()
}

// Open a window that will load the specified popup of a set size.
function popup_win(sFile, iWidth, iHeight) {
  oWin = window.open(sFile, "PopupBox", "menubar=no,scrollbars=yes,toolbar=no,status=yes,resizable=yes,width="+iWidth+",height="+iHeight);
  oWin.focus()
}

//***** Basic Helper Routines ************************************************
// Based on the name/id passed in set a pointer to the element, style, etc.
function getObject(sID) {
  if (document.getElementById) {
    this.oElement = document.getElementById(sID);
    this.oStyle = this.oElement.style;
  } else if (document.all) {
    this.oElement = document.all[sID];
    this.oStyle = this.oElement.style;
  } else if (document.layers) {
    this.oElement = document.layers[sID];
    this.oStyle = this.oElement;
  }
}

// Disable an item on the page based on it's id.
function disableID(sID) {
  var oObj = new getObject(sID);
  oObj.oElement.disabled = true;
}

// Enable an item on the page based on it's id.
function enableID(sID) {
  var oObj = new getObject(sID);
  oObj.oElement.disabled = false;
}

// Check a radio button based on it's id.
function checkRadio(sID) {
  var oObj = new getObject(sID);
  oObj.oElement.checked = true;
}

// Test if a radio button is checked based on it's id.
function testRadio(sID) {
  var oObj = new getObject(sID);
  return oObj.oElement.checked;
}

// Change an image on the page based on it's id.
function setImage(sID, sImage) {
  var oObj = new getObject(sID);
  oObj.oElement.src = sImage;
}

// Change an image on the page based on it's id.
function setBackgroundImage(sID, sImage) {
  var oObj = new getObject(sID);
  oObj.oStyle.backgroundImage = "url('"+sImage+"')";
}

//***** Stylistic Routines ***************************************************
// Change the color of a specific block.
function changeClass(oForm, sFormItem, sID) {
  var oObj = new getObject(sID);
  var oClass = (oForm.elements[sFormItem].checked)?'Hi':'Low';
  oObj.oElement.className = oClass;
  return true;
}

  // Set the color of an item on the page.
  function setColor(sID, sColor) {
    var oObj = new getObject(sID);
    oObj.oStyle.color = sColor;
  }

  // Set the font for an item on the page.
  function setFont(sID, sFont) {
    var oObj = new getObject(sID);
    oObj.oStyle.fontFamily = sFont;
  }

  // Set the HTML for an item on the page.
  function setHTML(sID, sHTML) {
    var oObj = new getObject(sID);
    oObj.oElement.innerHTML = sHTML;
  }

  // Get the HTML for an item on the page.
  function getHTML(sID) {
    var oObj = new getObject(sID);
    return oObj.oElement.innerHTML;
  }

//***** Confirmation Routines ************************************************
// Confirm removal of an object and then jump to a URL.
function removeJump(sURL) {
  if (confirm('Are you sure you want to remove this item permanently?')) {
    location.href=sURL;
  }
}

//***** Form Routines ********************************************************
// Set the value of a textarea form item.
function setTextArea(sID, sValue) {
  var oObj = new getObject(sID);
  oObj.oElement.value = sValue;
}

// Limit a textarea to a specific number of characters.
function limitText(oField, iMax) {
  if (oField.value.length > iMax)
    oField.value = oField.value.substring(0, iMax);
}

// Only allow the form to be submitted once.
function oneClick(sID) {
  var oObj = new getObject(sID);
  oObj.oElement.value = 'Working... Please Wait';
  oObj.oElement.disabled = true;
  oObj.oElement.form.Action[0].value = "Confirm Order";
  oObj.oElement.form.submit();
  return true;
}

// Submit the form when the active checkbox is toggled.
function makeActive(oForm) {
  oForm.Action[0].value = "Active";
  oForm.submit();
  return true;
}

// Submit the form in another frame.
function submitFrame(sFrame, sAction) {
  parent.frames[sFrame].document.forms[0].Action.value = sAction;
  parent.frames[sFrame].document.forms[0].submit();
  return true;
}

// Set the focus to a specific field based on the name.
function setFocusByName(sElement) {
  if (document.forms[0].elements[sElement] != null) {
    document.forms[0].elements[sElement].focus();
  }
}

// Recursive function to select a pulldown menu item based on entered text.
function autoSelect(sListID, sTextID, iPos) {
  var oList = new getObject(sListID);
  var oText = new getObject(sTextID);
  var sSearch = '';
  var sSubText = '';
  var bFound = false;
  var sSelection = '';

  // Make sure we have a position to start from.
  if (isNaN(iPos)) {
    iPos = 1;
  }

  // Get the current search text and subtext for this search.
  sSearch = oText.oElement.value.toString();
  sSubText = sSearch.substring(0, iPos).toLowerCase();

  // Search for a selection matching the current subtext.
  for (var iIndex = 0; iIndex < oList.oElement.length; iIndex++ ) {
    sSelection = oList.oElement.options[iIndex].text.toString();
    sSelection = sSelection.substring(0, iPos).toLowerCase();
    if (sSubText == sSelection) {
     oList.oElement.options[iIndex].selected = true;
     bFound = true;
     break;
    }
  }

  // If we haven't matched everything then keep going.
  if (bFound && (iPos < sSearch.length )) {
    iPos++;
    autoSelect(sListID, sTextID, iPos);
  } else if (!bFound) {
     oList.oElement.options[0].selected = true;
  }
}

// Submit on Pulldown selection, ie select-jump.
function submitSelect(oForm) {
  oForm.submit();
}

// Submit the form to a newly opened window.
function submitNewWindow(oForm, sName, sUrl, iWidth, iHeight) {
  oWinC=window.open('', sName, 'menubar=no,scrollbars=yes,toolbar=no,resizable=yes,width='+iWidth+',height='+iHeight);
  oForm.target=sName;
  oForm.action=sUrl;
  oWinC.focus()
  return true;
}

function submitTop(oForm) {
  oForm.target='_top';
  return true;
}

//***** Debug Routines *******************************************************
//<p><a href="#" onClick="dbgPrintForm();">Debug</a>
function dbgPrintForm() // dbgPrintForm class
{
  var dbgwinf;
  var str='';
  for (var j=0;j<document.forms.length;j++)
  {
    var f=document.forms[j];
    var t='<td>&nbsp;</td>';
    str+='<table border="1"><thead>'
      if (f.name)
        str+='<tr><td colspan="5">document.forms['+j+'].name: "'+ f.name +'"</td></tr>';
      else
        str+='<tr><td colspan="5">document.forms['+j+'].name: (undefined)</td></tr>';
    str+='<tr><th>element</th><th>type</th><th>name</th><th>value</th><th>id</th></tr></thead>';
    if (!document.forms)
      str+='<tbody><tr><td> -- no form detected or no form fields -- </td></tr></tbody>';
    else
    {
      for (var i=0;i<f.length;i++)
      {
        var e = f.elements[i];
        str += '<tr><td>' + i + '</td>';
        if (e.type)   str += '<td>' + e.type + '</td>';
        else      str += t;
        if (e.name)   str += '<td>' + e.name + '</td>';
        else      str += t;
        if (e.value)  str += '<td>' + e.value+ '</td>';
        else      str += t;
        if (e.id)   str += '<td>' + e.id   + '</td>';
        else      str += t;
        str += '</tr>';
      }
    }
    str += '</tbody></table>';
  } // end for loop
  if (!dbgwinf || dbgwinf.closed || true) {
    dbgwinf = window.open("","dbgPrintFormwin","width=400,height=650,resizable=yes,scrollbars=yes");
    var d = dbgwinf.document;
    d.open();
    d.write(str);
    d.close();
    d.title='Form data window';
    if(d.focus) d.focus();
  }
}