function CreateElement (htmlCode)
{
  if (isIE)
  {
    return document.createElement (htmlCode)
  }
  else
  {
    var firstSpace;
    var argString;
    var args;
    var nArgs;
    var atData;
    var newElement;
    
    nArgs = 0;
    htmlCode = htmlCode.replace (/(^<)|(>$)/g, "");
    firstSpace = htmlCode.indexOf (" ");
    if (firstSpace < 0)
    {
      newElement = document.createElement(htmlCode); 
    }
    else
    {
      newElement = document.createElement(htmlCode.substring (0, firstSpace));
      argString = htmlCode.substring (firstSpace + 1, htmlCode.length);
      args = argString.match (/[^ =]+\s*=\s*"[^"]*"/g);
      //alert (args);
      if (args != null) nArgs = args.length;
      else alert ("HTML ERROR in CreateElement: \""+htmlCode+"\"");
      
      for (index = 0; index < nArgs; index++)
      {
        atData = args[index].match (/([^ =]+)\s*=\s*"([^"]*)"/)
        var name = atData[1].toLowerCase ()
        //if (atData[2] == "true") atData[2] = true
        //else if (atData[2] == "false") atData[2] = false
        if (name == "value")
        {
          newElement.value = atData[2]
        }
        else
        {
          newElement.setAttribute(atData[1].toLowerCase (), atData[2])
        }
      }
    }
    return newElement;
  }
}

function GetEndOfBody ()
{
  var body = document.getElementsByTagName ("body")[0]
  while (body.lastChild && body.lastChild.tagName != "SCRIPT") {body = body.lastChild}
  return body
}

function RemoveElements ()
{
  var elem;
  var index;
  var args = RemoveElements.arguments
  for (index = 0 ; index < args.length; index++)
  {
    elem = document.getElementById (args[index]);
    if (elem != null)
    {
      elem.removeNode (true);
    }
  }
}

function CreateIPEditBox (name, value, length, size, onChange, isRequired, requiredText)
{
  return CreateEditBox (name, value, length, size, "IpOnly", onChange, isRequired, requiredText);
}

function CreateNumbersOnlyEditBox (name, value, length, size, onChange, isRequired, requiredText)
{
  return CreateEditBox (name, value, length, size, "NumbersOnly", onChange, isRequired, requiredText);
}

function CreatePhoneNumberEditBox (name, value, length, size, onChange, isRequired, requiredText)
{
  return CreateEditBox (name, value, length, size, 'CheckPhoneInput', onChange, isRequired, requiredText);
}

function CreateCheckBox (name, value, onClick, forceNavUpdate, rebootOnChange)
{
  var checkBox;
  var checkBoxHelper;
  var checked = (value == "True" ? " checked" : "" );
  var newElement = document.createDocumentFragment();

  if (onClick == null) onClick = "";
  onClick = 'UpdateCheckBoxHelper(document.getElementById(\''+name+'\'), this);'+onClick;
  if (forceNavUpdate) onClick += 'UpdateNavReloadCB(this);'
  // Create the checkbox
  checkBox = CreateElement ('<input type="checkbox" id="'+name+
    'checkbox" name="'+name+'checkbox"'+checked+' onclick="'+onClick+'">')
  SetCommonAttributes (checkBox, rebootOnChange);
  // For Netscape
  checkBox.checked = (value == "True");
  newElement.appendChild (checkBox);
  // Create the helper
  checkBoxHelper = CreateElement ('<input type="hidden" id="'+name+
    '" name="'+name+'" value="'+value+'">');
  newElement.appendChild (checkBoxHelper);
  return newElement;
}

function CreateInput (type, name, value, length, size, dataValidation,
  onChange, isRequired, requiredText, rebootOnChange)
{
  var dataValidationCmds = "";
  var onChangeCmds = "";
  var thisObj = "document.getElementById ('"+name+"')";
  var requiredFunc = (isRequired) ? 'VerifyNotBlank ('+thisObj+', \''+requiredText+'\');' : "";
  var newElement;
  
  if (onChange == null) onChange = "";
  if (dataValidation != null) dataValidation += '('+thisObj+');'
  if (length == null) length = "";
  if (size == null) size = (length == "") ? "" : length * 1.4;
  if (size > kMaxEditBoxLength) size = kMaxEditBoxLength;
   
  dataValidationCmds = AddToAndList (dataValidationCmds, dataValidation, requiredFunc);
  onChangeCmds = AddToAndList (onChangeCmds, "ValidateField (this)", onChange);
  if (onChangeCmds != "") onChangeCmds = "return ("+onChangeCmds+")";
  //alert ("dataValidationCmds = \""+dataValidationCmds+"\"")
    
//  if (rebootOnChange) AddFootnoteIndecator ();
  newElement = CreateElement ('<input name="'+name+'" type="'+type+'" id="'+
    name+'" vFunction="'+dataValidationCmds+'" onchange="'+onChangeCmds+
    '" value="'+value+'" size="'+size+'" maxlength="'+length+'">');
  SetCommonAttributes (newElement, rebootOnChange);
  return newElement;
}

function CreateEditBox (name, value, length, size, dataValidation, 
  onChange, isRequired, requiredText, rebootOnChange)
{
  return CreateInput ("text", name, value, length, size, dataValidation,
  onChange, isRequired, requiredText, rebootOnChange)
}

function CreatePasswordEditBox (name, value, length, size, dataValidation, 
  onChange, isRequired, requiredText, rebootOnChange)
{
  return CreateInput ("password", name, value, length, size, dataValidation,
  onChange, isRequired, requiredText, rebootOnChange)
}

function RemoveChildren (element)
{
  if (typeof element == "string") element = document.getElementById(element)
  while (element.firstChild) {element.removeChild(element.firstChild)}
}

function CreateLink (link, label)
{
  var newElement;
  var newOption;

  newElement = CreateElement ('<a href="'+link+'">');
  newElement.innerText = label; /* TODO: Fix NS */
  return newElement;
}

function CreateSelect (name, values, onChange, forceTranslations, rebootOnChange, setValue)
{
  var options;
  var sOption;
  var aOption;
  var index;
  var choiceFile = (typeof forceTranslations == "string") ? forceTranslations : name;
  var newElement;
  var newOption;
  var defaultValue = ""
  var isFistLineADup = false
  var useSetValue = false;
  if (onChange == null) onChange = "";
  if (setValue != null) useSetValue = true;
  var selectedIndex = 0

  newElement = CreateElement ('<select id="'+name+'" name="'+name+'" onchange="'+onChange+'">');
  if (values != null && values != "")
  {
    options = values.split ("?");
    for (index = 0; index < options.length; index++)
    {
      
      aOption = options[index].split("|");
      value = aOption[0]
      newOption = newElement.appendChild (CreateElement('<option' +
          ' value="' + value + '">'));
      newOption.text = TranslateOption (choiceFile, aOption[0])
      if(useSetValue)
        if (aOption.length > 1 && value == setValue) selectedIndex = index
      else
        if (aOption.length > 1 && aOption[aOption.length-1] == kSelected) selectedIndex = index
      // If this is the first line save it as the default
      if (index == 0)
      {
        defaultValue = newOption.value
      }
      // If this is not the first line check to see if it is a dup of the first line
      // so that we can remove the fist line when we are done.
      else
      {
        if (!isFistLineADup) isFistLineADup = newOption.value == defaultValue
      }
    }
    if (isFistLineADup)
    {
      newElement.options[0] = null
      if (selectedIndex != 0) --selectedIndex
    }
  
    newElement.selectedIndex = selectedIndex
  
  }
  //SetCommonAttributes (newElement, rebootOnChange);
  return newElement;
}

function CreateRow ()
{
  var index;
  var startIndex = 0;
  var tabed = 0;
  var name = "";
  var dataName = "";
  var titleName = "";
  var args;
  var newElement;
  var rowElement;
  var style = "";
  var usecols = false;
  var dataColumns = 1;
  var columns = 1;
  var width = "40%";

//  args = arguments[startIndex].match (/^\s*([^ =]+)\s*=\s*(.+)\s*$/);
  while (/.+=.+/.test(arguments[startIndex]))
  {
    args = arguments[startIndex].split("=");
    if (/(usecols)/i.test(args[0]))
    {
      eval (args[0] + " = /true/i.test('"+args[1]+"')");
    }
    else if (/name/i.test(args[0]))
    {
      name = args[1];

    }
    else if (/tabed/i.test(args[0]))
    {
      eval (args[0]+" = "+args[1]+"");
    }
    else
    {
      eval (args[0]+" = '"+args[1]+"'");
    }
    startIndex++;
  }
  dataName = name+"data"
  titleName = name+"label"
  var noWrap = (columns == 1) ? "nowrap " : "";
  // TODO: Fix setting the class for IE.

  rowElement = CreateElement ('<tr id="'+name+'">');
  rowElement.className = style;
  newElement = rowElement.appendChild (CreateElement ('<td '+noWrap+'colspan="'+columns+'" id="'+titleName+'" width="'+width+'">'));
  for (index = 0; index < tabed; index++)
  {
    newElement.insertAdjacentHTML ("afterBegin", "&nbsp;&nbsp;&nbsp;&nbsp;");
  }
  newElement.appendChild (eval (arguments[startIndex++]));
  if (!usecols)
  {
    newElement = rowElement.appendChild (CreateElement ('<td colspan="'+dataColumns+'" id="'+dataName+'">'));
    for (index = startIndex; index < arguments.length; index++)
    {
      if (index != startIndex) newElement.insertAdjacentHTML ("beforeEnd", "&nbsp;");
  //    newElement.insertAdjacentNode ("beforeEnd", eval (arguments[index]));
      newElement.appendChild (eval (arguments[index]));
    }
  }
  else
  {
    for (index = startIndex; index < arguments.length; index++)
    {
      newElement = rowElement.appendChild (CreateElement ('<td colspan="'+dataColumns+'" id="'+dataName+(index-startIndex)+'">'));
      newElement.appendChild (eval (arguments[index]));
    }
  }
  return rowElement;
}

function UpdateRowTitle (rowName, newTitle, tabed)
{
  var titleName = rowName+"title";
  var titleElement = document.getElementById (titleName);

  if (tabed == true)
  {
    newTitle = '&nbsp;&nbsp;&nbsp;&nbsp;'+newTitle;
  }

  if ( titleElement != null)
  {
    titleElement.innerHTML = newTitle;
  }
  else
  {
    alert ("HTML ERROR in UpdateRowTitle: "+titleName+" not found");
  }
}

function ShowHideRow (name, startID, show, rowData)
{
  var startElement;
  var newRow;
  var afterIndex;

  if (show)
  {
    // If the element does not exits add it
    if (document.getElementById (name) == null)
    {
      startElement = document.getElementById (startID);
      newRow = eval ("CreateRow ('name=" + name + "', " + rowData + ");");
      startElement.insertAdjacentElement ("afterEnd", newRow);          
    }
  }
  // otherwise hide it
  else
  {
    // If the element exists remove it
    if (document.getElementById (name) != null)
    {
      RemoveElements (name);
    }
  }
}

function ShowHideRowElement (name, bShow, elementDes, where)
{
  var startElement;

  if (bShow)
  {
    // If the element does not exits add it
    if (document.getElementById (name) == null)
    {
      startElement = document.getElementById (where);
      startElement = startElement.insertAdjacentElement ("afterEnd", eval (elementDes));          
      startElement.insertAdjacentHTML ("beforeBegin", "&nbsp");          
    }
  }
  // otherwise hide it
  else
  {
    // If the element exists remove it
    if (document.getElementById (name) != null)
    {
      RemoveElements (name);
    }
  }
}

//   This script gives Netscape 6 the following IE methods:
//   removeNode(),replaceNode(),swapNode(),applyElement(),contains(),
//   insertAdjacentText(),insertAdjacentHTML(),insertAdjacentElement()

if(self.Node&&self.Node.prototype){
Node.prototype.removeNode=remove_Node;
Node.prototype.replaceNode=replace_Node;
Node.prototype.swapNode=swap_Node;
Element.prototype.applyElement=apply_Element;
Element.prototype.contains=_contains;
Element.prototype.insertAdjacentText=insertAdj_Text;
Element.prototype.insertAdjacentHTML=insertAdj_HTML;
Element.prototype.insertAdjacentElement=insertAdj_El;
Element.prototype.insert__Adj=insert__Adj;
}

function remove_Node(a1){
  var p=this.parentNode;
  if(p&&!a1)
  {
    var df=document.createDocumentFragment();
    for(var a=0;a<this.childNodes.length;a++){
      df.appendChild(this.childNodes[a])
    }
    p.insertBefore(df,this)
  }
  return p?p.removeChild(this):this;
}

function replace_Node(a1){return this.parentNode.replaceChild(a1,this)}

function swap_Node(a1){
  var p=a1.parentNode;
  var s=a1.nextSibling;
  this.parentNode.replaceChild(a1,this);
  p.insertBefore(this,s)
  return this;
}

function apply_Element(a1,a2){
if(!a1.splitText){
a1.removeNode();
if(a2&&a2.toLowerCase()=="inside"){
for(var a=0;a<this.childNodes.length;a++){
a1.appendChild(this.childNodes[a])
}
this.appendChild(a1)
}
else{
var p=this.parentNode;
p.insertBefore(a1,this);
a1.appendChild(this);
}
return a1;
}
}

function _contains(a1){
var r=document.createRange();
r.selectNode(this);
return r.compareNode(a1)==3;
}

function insertAdj_Text(a1,a2){
var t=document.createTextNode(a2||"")
this.insert__Adj(a1,t);
}

function insertAdj_HTML(a1,a2){
var r=document.createRange();
r.selectNode(this);
var t=r.createContextualFragment(a2);
this.insert__Adj(a1,t);
}

function insertAdj_El(a1,a2){
this.insert__Adj(a1,a2);
return a2;
}

function insert__Adj(a1,a2){
var p=this.parentNode;
var s=a1.toLowerCase();
if(s=="beforebegin"){p.insertBefore(a2,this)}
if(s=="afterend"){p.insertBefore(a2,this.nextSibling)}
if(s=="afterbegin"){this.insertBefore(a2,this.childNodes[0])}
if(s=="beforeend"){this.appendChild(a2)}
}
