addEvent(window, "load", preparetriggers);

var lastKeyUp = new Date();
var req=null;
var reqArray = new Array();
var READY_STATE_UNINITIALIZED=0;
var READY_STATE_LOADING=1;
var READY_STATE_LOADED=2;
var READY_STATE_INTERACTIVE=3;
var READY_STATE_COMPLETE=4;

function loadXMLDoc(url,functiontocall) {
  if (window.XMLHttpRequest) {
      req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
      req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req) {
      //req.onreadystatechange = processReqChange;
      req.onreadystatechange = functiontocall;
      req.open("GET", url, true);
      req.send(null);
  }
}

function loadXMLDoc2(url,functiontocall) {
  var count=0;
  if (window.XMLHttpRequest) {
      count=reqArray.push(new XMLHttpRequest());
  } else if (window.ActiveXObject) {
      count=reqArray.push(new ActiveXObject("Microsoft.XMLHTTP"));
  }

  if (reqArray[count - 1] != "undefined") {
      //req.onreadystatechange = processReqChange;
      reqArray[count - 1].onreadystatechange = functiontocall;
      reqArray[count - 1].open("POST", url, true);
      reqArray[count - 1].send(null);
  }
}

function preparetriggers(startingPoint, newName) {
  var inputs;
  var thisInput;
  var loop;

  sortables_init(startingPoint);

  if (!newName) {
    newName = "";
  }

  if (startingPoint == "undefined" || startingPoint.type == "load") {
    startingPoint = document;
  }

  if (!startingPoint.getElementsByTagName) return;

  inputs = startingPoint.getElementsByTagName("input");

  //Go through all the inputs
  for (loop=0;loop<inputs.length;loop++) {
    thisInput = inputs[loop];
    //For all inputs that have a class of claimQty
    if ((' '+thisInput.className+' ').indexOf("datafield") != -1) {
      thisInput.name = thisInput.name + newName;

      if (thisInput.type == "checkbox") {
        if (thisInput.addEventListener) {
          thisInput.addEventListener("click", processInput, 0);
        } else {
          thisInput.attachEvent("onclick", processInput, 0);
        }
      } else {
        if (thisInput.addEventListener) {
          thisInput.addEventListener("blur", processInput, 0);
        } else if (thisInput.attachEvent) {
          thisInput.attachEvent("onblur", processInput);
        } else {
          alert("Can't add events.  Failing");
        }

        if (thisInput.addEventListener) {
          thisInput.addEventListener("focus", processInput, 0);
        } else if (thisInput.attachEvent) {
          thisInput.attachEvent("onfocus", processInput);
        } else {
          alert("Can't add events.  Failing");
        }
      }
    }

    if ((' '+thisInput.className+' ').indexOf("pivotField") != -1) {
      thisInput.name = thisInput.name + newName;

      if (thisInput.type == "checkbox") {
        if ((' '+thisInput.className+' ').indexOf("pivotMaster") != -1) {
          thisInput.addEventListener("click", processPivotMaster, 0);
        } else {
          thisInput.addEventListener("click", processPivot, 0);
        }
      } else {
        if ((' '+thisInput.className+' ').indexOf("pivotMaster") != -1) {
          thisInput.addEventListener("change", processPivotMaster, 0);
        } else {
          thisInput.addEventListener("change", processPivot, 0);
        }
      }
    }

    //Check for read only field class
    if ((' '+thisInput.className+' ').indexOf("readonlyfield") != -1) {
      thisInput.name = thisInput.name + newName;

      if (thisInput.addEventListener) {
        thisInput.addEventListener("blur", processRO, 0);
      } else if (thisInput.attachEvent) {
          thisInput.attachEvent("onblur", processRO);
      } else {
        alert("Can not add events");
      }

      if (thisInput.addEventListener) {
        thisInput.addEventListener("keyup", processRO, 0);
      } else if (thisInput.attachEvent) {
          thisInput.attachEvent("onkeyup", processRO);
      } else {
        alert("Can not add events");
      }
    }

    //Check for suggest field class
    if ((' '+thisInput.className+' ').indexOf("suggest") != -1) {
      thisInput.addEventListener("keyup", searchSuggest, 0);
    }
  }

  inputs = startingPoint.getElementsByTagName("select");

  //Go through all the inputs
  for (loop=0;loop<inputs.length;loop++) {
    thisInput = inputs[loop];
    //For all inputs that have a class of claimQty
    if ((' '+thisInput.className+' ').indexOf("datafield") != -1) {
      thisInput.name = thisInput.name + newName;
      thisInput.addEventListener("change", processInput, 0);
    }

    if ((' '+thisInput.className+' ').indexOf("dataselect") != -1) {
      thisInput.name = thisInput.name + newName;
      if (thisInput.addEventListener) {
        thisInput.addEventListener("focus", processSelect, 0);
        thisInput.addEventListener("change", processSelect, 0);
      } else if (thisInput.attachEvent) {
        thisInput.attachEvent("onfocus", processSelect);
        thisInput.attachEvent("onchange", processSelect);
      } else {
        alert("Can't add event in prepare triggers");
      }
    }


    if ((' '+thisInput.className+' ').indexOf("pivotField") != -1) {
      thisInput.name = thisInput.name + newName;

      if ((' '+thisInput.className+' ').indexOf("pivotMaster") != -1) {
        thisInput.addEventListener("change", processPivotMaster, 0);
      } else {
        thisInput.addEventListener("change", processPivot, 0);
      }
    }
  }

  //Repeat for "textarea"
  inputs = startingPoint.getElementsByTagName("textarea");
  //Go through all the inputs
  for (loop=0;loop<inputs.length;loop++) {
    thisInput = inputs[loop];
    //For all inputs that have a class of claimQty
    if ((' '+thisInput.className+' ').indexOf("datafield") != -1) {
      thisInput.name = thisInput.name + newName;
      if (thisInput.addEventListener) {
        thisInput.addEventListener("blur", processInput, 0);
        thisInput.addEventListener("focus", processInput, 0);
      } else {
        thisInput.attachEvent("onblur", processInput);
        thisInput.attachEvent("onfocus", processInput);
      }
    }
  }

  //Repeat for "button"
  inputs = startingPoint.getElementsByTagName("button");
  //Go through all the inputs
  for (loop=0;loop<inputs.length;loop++) {
    thisInput = inputs[loop];
    //For all inputs that have a class of claimQty

    if ((' '+thisInput.className+' ').indexOf("deletebutton") != -1) {
      thisInput.name = thisInput.name + newName;
      if (thisInput.addEventListener) {
        thisInput.addEventListener("click", processDelete, 0);
      } else if (thisInput.attachEvent) {
        thisInput.attachEvent("onclick", processDelete);
      } else {
        alert("Can't add event in preparetriggers");
      }
    }

    if ((' '+thisInput.className+' ').indexOf("addrecordbutton") != -1) {
      thisInput.name = thisInput.name + newName;
      thisInput.addEventListener("click", processAddRecord, 0);
    }
  }
}

function processAddRecord(e) {
  var theEvent = e || event;
  var theButton = theEvent.srcElement || theEvent.target;
  var theValue;
  var loop;
  var count = 1;

  var idArray = theButton.id.split(";");

  xmlString = getpathtoxml() + "?action=xmldbupdate&table=" + idArray[0] + "&primarykeyid=0" + "&fields=" + idArray[1] + "&value1=" + 0;

  loadXMLDoc2(xmlString, processAddRecordReturn);
}

function processAddRecordReturn() {
  var data=null;
  var theStatus;
  var theId;
  var theNewId;
  var theInput;
  var theElement;
  var theRow;
  var theTD;
  var loop;
  var namesLoop;
  var cellLoop;
  var count = reqArray.length;
  var children;
  var childLoop;
  var theChild;

  for (loop = 0; loop < reqArray.length; loop++) {
    if (reqArray[loop].readyState==READY_STATE_COMPLETE) {
      data=reqArray[loop].responseText;
      reqArray.splice(loop, 1);

      if (/<status>(.+)<\/status>/.exec(data) != undefined) {
        theStatus = /<status>(.+)<\/status>/.exec(data)[1];
        theNewId = /<newid>(.+)<\/newid>/.exec(data)[1];
        alert("New Record Added: " + theNewId);
      }
    }
  }
}

function processPivotMaster(e) {
  var theTD;
  var theEvent = e || event;
  var theInput = theEvent.srcElement || theEvent.target;
  var theValue;
  var theFieldCount;
  var loop;
  var namesLoop;
  var count = 1;

  if (theInput.type == "checkbox") {
    if (theEvent.type == "click") {
    }
  } else {
    if (theEvent.type == "change") {
      var idArray = theInput.id.split(";");
      theValue = theInput.value;

      theValue = theValue.replace(/\#/g,"%23")
      theValue = theValue.replace(/\&/g,"%26")
      theValue = theValue.replace(/\n/g,"%0A")

      xmlString = getpathtoxml() + "?action=xmldbget&table=" + idArray[0] + "&pivotField=" + idArray[1] + "&pivotValue=" + theValue;

      var fields = document.getElementsByName(theInput.name);
      var thisField;
      for (namesLoop=0;namesLoop<fields.length;namesLoop++) {
        thisField = fields[namesLoop];
        var idArray = thisField.id.split(";");
        xmlString = xmlString + "&field" + (namesLoop + 1) + "=" + idArray[1];
      }
      xmlString = xmlString + "&fields=" + namesLoop;

      loadXMLDoc2(xmlString, updateScreen);
    }
  }
}

function processPivot(e) {
  var theTD;
  var theEvent = e || event;
  var theInput = theEvent.srcElement || theEvent.target;
  var theValue;
  var loop;

  if (theInput.type == "checkbox") {
    if (theEvent.type == "click") {
      var idArray = theInput.id.split(";");
      var theKeyField = document.getElementById(idArray[0] + ";" + idArray[2]);

      if (theInput.checked) {
        theValue = 1;
      } else {
        theValue = 0;
      }

      xmlString = getpathtoxml() + "?action=xmldbupdate&table=" + idArray[0] + "&altkey=" + idArray[2] + "&primarykeyid=" + theKeyField.value + "&fields=" + 1 + "&id=" + theInput.id + "&field1=" + idArray[1] + "&value1=" + theValue;

      theTD = getParent(theInput, "td");
      theTD.setAttribute("class","updating");

      loadXMLDoc2(xmlString, updateScreen);
    }
  } else {
    if (theEvent.type == "change") {
      var idArray = theInput.id.split(";");
      var theKeyField = document.getElementById(idArray[0] + ";" + idArray[2]);

      theValue = theInput.value;

      xmlString = getpathtoxml() + "?action=xmldbupdate&table=" + idArray[0] + "&altkey=" + idArray[2] + "&primarykeyid=" + theKeyField.value + "&fields=" + 1 + "&id=" + theInput.id + "&field1=" + idArray[1] + "&value1=" + theValue;

      theTD = getParent(theInput, "td");
      theTD.setAttribute("class","updating");

      loadXMLDoc2(xmlString, updateScreen);
    }
  }
}

function processInput(e) {
  var theTD;
  var theEvent = e || event;
  var theInput = theEvent.srcElement || theEvent.target;
  var theValue;
  var theFieldCount;
  var loop;
  var count = 1;

  if (theInput.type == "checkbox") {
    if (theEvent.type == "click") {
      var idArray = theInput.id.split(";");

      theFieldCount = (idArray.length - 3) / 2 + 1;

      if (theInput.checked) {
        theValue = 1;
      } else {
        theValue = 0;
      }

      var theFieldArray = idArray[2].split("~");
      var theField = theFieldArray[0];

      xmlString = getpathtoxml() + "?action=xmldbupdate&table=" + idArray[0] + "&primarykeyid=" + idArray[1] + "&fields=" + theFieldCount + "&id=" + theInput.id + "&field1=" + theField + "&value1=" + theValue;

      for(loop = 1; loop < theFieldCount;loop++) {
        count++;
        var theValueArray = idArray[(count * 2)].split("~");
        var theValue = theValueArray[0];
        var theFieldArray = idArray[(count * 2) - 1].split("~");
        var theField = theFieldArray[0];

        xmlString = xmlString + "&field" + (count) + "=" + theField + "&value" + (count) + "=" + theValue;
      }


      theTD = getParent(theInput, "td");
      if (theTD) {
        theTD.setAttribute("class","updating");
      }

      loadXMLDoc2(xmlString, updateScreen);
    }
  } else {
    if (theEvent.type == "focus") {
      theInput.oldvalue = theInput.value;
    } else {
      if (theEvent.type == "blur" || theEvent.type == "change") {
        var suggest = document.getElementById(theInput.id + "~suggest");
        if (suggest) {
          suggest.innerHTML = "";
        }
        if (theInput.oldvalue != "undefined" &&  theInput.value != theInput.oldvalue) {
          var idArray = theInput.id.split(";");

          if (idArray.length >= 3) {
            theFieldCount = ((idArray.length - 3) / 2) + 1;

            theValue = theInput.value;

            theValue = theValue.replace(/\#/g,"%23")
            theValue = theValue.replace(/\&/g,"%26")
            theValue = theValue.replace(/\n/g,"%0A")

            var theFieldArray = idArray[2].split("~");
            var theField = theFieldArray[0];

            xmlString = getpathtoxml() + "?action=xmldbupdate&table=" + idArray[0] + "&primarykeyid=" + idArray[1] + "&fields=" + theFieldCount + "&id=" + theInput.id + "&field1=" + theField + "&value1=" + theValue;

            for(loop = 1; loop < theFieldCount;loop++) {
              count++;

              var theValueArray = idArray[(count * 2)].split("~");
              var theValue = theValueArray[0];
              var theFieldArray = idArray[(count * 2) - 1].split("~");
              var theField = theFieldArray[0];

              xmlString = xmlString + "&field" + (count) + "=" + theField + "&value" + (count) + "=" + theValue;
            }

            theTD = getParent(theInput, "td");

            if (theTD) {
              theTD.setAttribute("class","updating");
            } else {
              theInput.setAttribute("class","updating");
            }

            loadXMLDoc2(xmlString, updateScreen);
          }
        } else {
          delete(theInput.oldvalue);
        }
    }
    }
  }
}

function searchSuggestRO(e) {
  var theTD;
  var theEvent = e || event;
  var theInput = theEvent.srcElement || theEvent.target;
  var theValue;

  if (theEvent.type == "keyup") {
    theValue = theInput.value;
    if (theValue.length > 0) {
      var idArray = theInput.id.split(";");

      xmlString = getpathtoxml() + "?action=xmlsuggestro&table=" + idArray[0] + "&field=" + idArray[1] + "&value=" + theValue + "&id=" + theInput.id;

      loadXMLDoc2(xmlString, updateScreen);
    } else {
      document.getElementById(theInput.id + "~suggest").innerHTML = "";
    }
  }
}

function searchSuggest(e) {
  var theTD;
  var theEvent = e || event;
  var theInput = theEvent.srcElement || theEvent.target;
  var theValue;

  if (theEvent.type == "keyup") {
    theValue = theInput.value;
    if (theValue.length > 0) {
      var idArray = theInput.id.split(";");
      var theFieldArray = idArray[2].split("~");
      var theField = theFieldArray[0];

      xmlString = getpathtoxml() + "?action=xmlsuggest&table=" + idArray[0] + "&field=" + theField + "&value=" + theValue + "&id=" + theInput.id;

      loadXMLDoc2(xmlString, updateScreen);
    } else {
      document.getElementById(theInput.id + "~suggest").innerHTML = "";
    }
  }
}

function processSelect(e) {
  var theTD;
  var theEvent = e || event;
  var theSelect = theEvent.srcElement || theEvent.target;
  var theValue;
  var theFieldCount;
  var loop;
  var count = 1;

  switch (theEvent.type) {
    case "change":
      var tempArray = theSelect.id.split("~");
      var idArray = tempArray[0].split(";");

      if (idArray.length >= 4) {
        theFieldCount = ((idArray.length - 4) / 2) + 1;

        theValue = theSelect.value;

        theValue = theValue.replace(/\#/,"%23")
        theValue = theValue.replace(/\&/,"%26")

        xmlString = getpathtoxml() + "?action=xmldbupdate&table=" + idArray[0] + "&primarykeyid=" + idArray[2] + "&fields=" + theFieldCount + "&id=" + theSelect.id + "&field1=" + idArray[3] + "&value1=" + theValue;

        for(loop = 1; loop < theFieldCount;loop++) {
          count++;
          xmlString = xmlString + "&field" + (count) + "=" + idArray[(count * 2)] + "&value" + (count) + "=" + idArray[(count * 2) + 1];
        }

        //debug(xmlString);

        theTD = getParent(theSelect, "td");
        if (theTD) {
          theTD.setAttribute("class","updating");
        }

        loadXMLDoc2(xmlString, updateScreen);
      }
      break;
    case "focus":
      var idArray = theSelect.id.split(";");
      // Why does the 2 work for all tables except the SYSTEM_TABLES_FIELDS ??
      xmlString = pathtoxml + "?action=xmlselectupdate&fieldid=" + idArray[1] + "&id=" + theSelect.id + "&primarykey=" + idArray[2];

      break;
  }
}

function processDelete(e) {
  var theTD;
  var theEvent = e || event;
  var theButton = theEvent.srcElement || theEvent.target;
  var theElement;

  if (theEvent.type == "click") {
    var idArray = theButton.id.split(";");

    if (idArray[1] == 0) {
      alert("You can't delete this row.  It does not exist: " + theButton.id);
    } else {
      theElement = document.getElementById(idArray[2]);
      theElement.oldstyle = theElement.style;
      theElement.setAttribute("style","background: #EE0000;");
      if (confirm("Are you sure you want to delete?")) {
        xmlString = getpathtoxml() + "?action=xmldbdelete&table=" + idArray[0] + "&primarykeyid=" + idArray[1] + "&iddelete=" + idArray[2];

        loadXMLDoc2(xmlString, updateScreen);
      } else {
        theElement.setAttribute("style",theElement.oldstyle);
      }
    }
  }
}

function updateScreen() {
  var data=null;
  var theStatus;
  var theId;
  var theNewId;
  var theInput;
  var theElement;
  var theRow;
  var theTD;
  var loop;
  var namesLoop;
  var cellLoop;
  var count = reqArray.length;
  var children;
  var childLoop;
  var theChild;

  for (loop = 0; loop < reqArray.length; loop++) {
    if (reqArray[loop].readyState==READY_STATE_COMPLETE) {
      data=reqArray[loop].responseText;
      reqArray.splice(loop, 1);

      if (/<status>(.+)<\/status>/.exec(data) != undefined) {
        theStatus = /<status>(.+)<\/status>/.exec(data)[1];
        switch (theStatus) {
          case "selectlist":
            //Parse the values out of data
            theId = /<id>(.+)<\/id>/.exec(data)[1];
            list = /<list>(.+)<\/list>/.exec(data)[1];
            var listArray = list.split(";");
            var theSelect = document.getElementById(theId);

            //Backup the value
            var theValue = theSelect.value;

            //remove all options
            for(;theSelect.length > 0;) {
              theSelect.remove(0);
            }

            //add options back in from the db.
            var newOption;

            newOption = document.createElement("option");
            newOption.value = 0;
            newOption.innerHTML = "none";
            theSelect.add(newOption, null);

            for (childLoop = 0; childLoop < listArray.length / 2; childLoop++) {
              newOption = document.createElement("option");
              newOption.value = listArray[childLoop * 2];
              newOption.innerHTML = listArray[(childLoop * 2) + 1];
              theSelect.add(newOption, null);
            }

            //restore the value
            theSelect.value = theValue;
            break;
          case "rowupdate":
            var rowId = /<row>(.+)<\/row>/.exec(data)[1];
            var theList = /<list>(.+)<\/list>/.exec(data)[1];
            listArray = theList.split(";");

            var theRow = document.getElementById(rowId);
            var listLoop = 0;
            //Iterate through all the fields in the row
            for (listLoop = 1; listLoop <= theRow.cells.length; listLoop++) {
              var cs = theRow.cells[listLoop - 1].childNodes;
              var l = cs.length;
              //Iterate through all the child nodes
              if (cs.length == 0) {
                theRow.cells[listLoop - 1].innerHTML = listArray[listLoop];
              } else {
                for (var i = 0; i < l; i++) {
                  if ((cs[i].oldvalue) && (cs[i].oldvalue != cs[i].value)) {
                    //debug("Not updating dirty value");
                  } else {
                    //debug("Old Value = " + cs[i].oldvalue);
                    //debug("Value = " + cs[i].value);
                    switch (cs[i].nodeType) {
                      case 1: //ELEMENT_NODE
                        if (cs[i].type == 'checkbox') {
                          if(listArray[listLoop] != 0) {
                            cs[i].checked = true;
                          } else {
                            cs[i].checked = false;
                          }
                        } else {
                          cs[i].value = listArray[listLoop];
                        }
                        break;
                    case 3: //TEXT_NODE
                        theRow.cells[listLoop - 1].innerHTML = listArray[listLoop];
                        break;
                      default:
                        debug("Don't know what to do with nodeType = " + cs[i].nodeType);
                        break;
                    }
                  }
                }
              }
            }

            break;
          case "success":
            //debug(data);
            theId = /<id>(.+)<\/id>/.exec(data)[1];

            theInput = document.getElementById(theId);

            if (theInput != "undefined") {
              delete(theInput.oldvalue);

              if (/<newid>(.+)<\/newid>/.exec(data) != undefined) {
                var theTable = getParent(theInput, "tbody");
                var theRow = getParent(theInput, "tr");

                if (theRow) {
                  if ((' '+theRow.className+' ').indexOf("multiadd") != -1) {
                    var newRow;
                    newRow = theTable.insertRow(theTable.rows.length);
                    newRow.setAttribute("class",theRow.className);
                    newRow.setAttribute("valign",'top');
  
                    //Check new tags and add appropriate triggers
  
                    var inputs;
                    var thisInput;
                    var inputsloop;
  
                    for(cellLoop = 0; cellLoop < theRow.cells.length; cellLoop++) {
                      newRow.insertCell(newRow.cells.length);
                      newRow.cells[cellLoop].innerHTML = theRow.cells[cellLoop].innerHTML;
                    } //If multi add class for the row!
  
                    preparetriggers(newRow, "A");
                  } else {
                    theNewId = 0;
                  }
                }

                theNewId = /<newid>(.+)<\/newid>/.exec(data)[1];

                var theRow = getParent(theInput, "tr");
                if (theRow) {
                  if ((' '+theRow.className+' ').indexOf("multiadd") != -1) {
                    theRow.id = "record~" + theNewId;
                  }
                }
                var fields = document.getElementsByName(theInput.name);
                var thisField;
                //debug("INput name = " + theInput.name);
                for (namesLoop=0;namesLoop<fields.length;namesLoop++) {
                  thisField = fields[namesLoop];
                  //debug(thisField.className);
                  if ((' '+thisField.className+' ').indexOf("dataselect") != -1) {
                    idArray = thisField.id.split(";");
                    idArray[2] = theNewId;
                    thisField.id = idArray.join(";");
                  } else if ((' '+thisField.className+' ').indexOf("deletebutton") != -1)  {
                    idArray = thisField.id.split(";");
                    idArray[1] = theNewId;
                    idArray[2] = 'record~' + theNewId;
                    thisField.id = idArray.join(";");
                    //debug("New delete id = " + thisField.id) + " the new ID was " + theNewId;
                  } else {
                    idArray = thisField.id.split(";");
                    idArray[1] = theNewId;
                    thisField.id = idArray.join(";");
                  }
                }
              }

              //If the row is of class rowupdates then update the whole row after this change is done.
              var theRow = getParent(theInput, "tr");
              if (theRow) {
                if ((' '+theRow.className+' ').indexOf("rowupdates") != -1) {
                  var idArray = theInput.id.split(";");
                  var theTable = getParent(theInput, "table");
                  var idRowArray = theRow.id.split("~");

                  var xmlString = getpathtoxml() + "?action=xmldbrowupdate&storedid=" + theTable.id + "&primarykeyid=" + idRowArray[1] + "&rowid=" + theRow.id;

                  loadXMLDoc2(xmlString, updateScreen);
                } else {
                }
              }

              theTD = getParent(theInput, "td");
              if(theTD) {
                theTD.setAttribute("class","data");
              } else {
                theInput.setAttribute("class","");
              }
            } else {
              debug ("Error, ID " + theId + " not found!");
            }
            break;
          case "xmlgetsuccess":
            var table = /<table>(.+)<\/table>/.exec(data)[1];
            var pivotField = /<pivotField>(.+)<\/pivotField>/.exec(data)[1];
            var list = /<list>(.+)<\/list>/.exec(data)[1];
            //debug(list);

            listArray = list.split(";");
            var listLoop = 0;
            var theElement;
            for (listLoop = 0; listLoop < (listArray.length / 2); listLoop++) {
              if (listArray[listLoop * 2] != pivotField) {
                theElement = document.getElementById(table + ";" + listArray[listLoop * 2] + ";" + pivotField);
              } else {
                theElement = document.getElementById(table + ";" + listArray[listLoop * 2]);
              }

              var theNodeName = theElement.nodeName;
              theNodeName = theNodeName.toLowerCase();

              switch (theNodeName) {
                case "input":
                  if (theElement.type == "checkbox") {
                    if (listArray[(listLoop * 2) + 1] != 0) {
                      theElement.checked = true;
                    } else {
                      theElement.checked = false;
                    }
                  } else {
                    theElement.value = listArray[(listLoop * 2) + 1];
                  }
                  break;
                case "select":
                  theElement.value = listArray[(listLoop * 2) + 1];
                  break;
                default:
                  debug("Node Name = " + theNodeName);
                  theElement.innerHTML = listArray[(listLoop * 2) + 1]
              }
            }
            break;
          case "deleted":
            alert("Record Deleted.");
            theId = /<id>(.+)<\/id>/.exec(data)[1];
            theElement = document.getElementById(theId);
            switch (theElement.tagName.toLowerCase()) {
              case "tr" : var theTable = getParent(theElement, "table");
                          theTable.deleteRow(theElement.rowIndex);
                          break;
              default: debug("Unknown type.  " + theElement.tagName);
                      break;
            }
            break;
          case "failed":
            debug(data, 2);
            var reason = /<reason>(.+)<\/reason>/.exec(data)[1];
            alert("Update failed." + reason + " Please see debug messages for further info.");
            break;
          case "functionok":
            var reason = /<reason>(.*)<\/reason>/.exec(data)[1];
            var check = /<refresh>(.*)<\/refresh>/.exec(data);
            if (check) {
              var forceRefresh = check[1];
            } else {
              var forceRefresh = "Nothing";
            }
            if (reason) {
              alert(reason);
            }
            var link = /<link>(.*)<\/link>/.exec(data)[1];
            if (link) {
              var element = document.getElementById(link);
              var contents = /<contents>(.*)<\/contents>/.exec(data)[1];
              contents = contents.replace(/\~CR\~/g, "\n");
              element.innerHTML = contents;

              preparetriggers(element, 'none')
              prepareLinkedData(element)
              prepareFunctionButtons(element);
              preparefiltertriggers(element);
            }

            if (forceRefresh) {
              window.location.reload();
            }
            break;
          case "suggest":
            theId = /<id>(.+)<\/id>/.exec(data)[1];
            theList = /<list>(.+)<\/list>/.exec(data)[1];
            var searchDrop = document.getElementById(theId + "~suggest");
            searchDrop.innerHTML = '';
            var str = theList.split(";");
            for(i=0; i < str.length - 1; i++) {
                    //Build our element string.  This is cleaner using the DOM, but
                    //IE doesn't support dynamically added attributes.
                    var suggest = '<div id="' + theId + "~suggest~" + i + '" ';
                    suggest += 'class="suggest_link">' + str[i] + '</div>';
                    searchDrop.innerHTML += suggest;
            }

            var addEvents;
            for(i=0; i < str.length - 1; i++) {
                    addEvents = document.getElementById(theId + "~suggest~" + i);
                    addEvents.addEventListener("mouseover", suggestOver, 0);
                    addEvents.addEventListener("mouseout", suggestOut, 0);
                    addEvents.addEventListener("click", setSearch, 0);
            }
            break;
          case "suggestro":
            theId = /<id>(.+)<\/id>/.exec(data)[1];
            theList = /<list>(.+)<\/list>/.exec(data)[1];
            var searchDrop = document.getElementById(theId + "~suggest");
            searchDrop.innerHTML = '';
            var str = theList.split(";");
            for(i=0; i < str.length - 1; i++) {
                    //Build our element string.  This is cleaner using the DOM, but
                    //IE doesn't support dynamically added attributes.
                    var suggest = '<div id="' + theId + "~suggest~" + i + '" ';
                    suggest += 'class="suggest_link">' + str[i] + '</div>';
                    searchDrop.innerHTML += suggest;
            }

            var addEvents;
            for(i=0; i < str.length - 1; i++) {
                    addEvents = document.getElementById(theId + "~suggest~" + i);
                    addEvents.addEventListener("mouseover", suggestOver, 0);
                    addEvents.addEventListener("mouseout", suggestOut, 0);
                    addEvents.addEventListener("click", suggestClose, 0);
            }
            break;
          default:
            alert("Un-expected status.  Please see debug data.");
            debug(data, 2);
            break;
        } //switch (theStatus)
      } else {
        debug(data, 2);
        //alert("Update failed.  Please see debug messages for further info." + data);
        break;
      }
    }
  }
}

//Mouse Functions for suggest
//Mouse over function
function suggestOver(e) {
  var theEvent = e || event;
  var theDrop = theEvent.srcElement || theEvent.target;

  var i;
  var idArray = theDrop.id.split("~");
  var targetArray = new Array();

  for(i=0;i < idArray.length - 2;i++) {
    targetArray[i] = idArray[i];
  }

  idJoined = targetArray.join("~");
  var targetField = document.getElementById(idJoined);

  targetField.value = theDrop.innerHTML;

  theDrop.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(e) {
  var theEvent = e || event;
  var theDrop = theEvent.srcElement || theEvent.target;

  var i;
  var idArray = theDrop.id.split("~");
  var targetArray = new Array();

  for(i=0;i < idArray.length - 2;i++) {
    targetArray[i] = idArray[i];
  }

  idJoined = targetArray.join("~");
  var targetField = document.getElementById(idJoined);

  theDrop.className = 'suggest_link';
}
//Click function
function setSearch(e) {
  var theEvent = e || event;
  var theDrop = theEvent.srcElement || theEvent.target;

  var i;
  var idArray = theDrop.id.split("~");
  var targetArray = new Array();
  var count = 0;

  for(i=0;i < idArray.length - 2;i++) {
    targetArray[i] = idArray[i];
  }

  idJoined = targetArray.join("~");
  var newValue = theDrop.innerHTML;
  document.getElementById(idJoined + "~suggest").innerHTML = '';

  var targetField = document.getElementById(idJoined);
  targetField.value = newValue;
}

//Mouse over function
function suggestClose(e) {
  var theEvent = e || event;
  var theDrop = theEvent.srcElement || theEvent.target;

  var i;
  var idArray = theDrop.id.split("~");
  var targetArray = new Array();

  for(i=0;i < idArray.length - 2;i++) {
    targetArray[i] = idArray[i];
  }

  idJoined = targetArray.join("~");
  var targetField = document.getElementById(idJoined + "~suggest");

  targetField.innerHTML = "";
}

function processRO(e) {
  var theTD;
  var theEvent = e || event;
  var theInput = theEvent.srcElement || theEvent.target;

  var dropDown = document.getElementById(theInput.id + "~suggest");
  dropDown.innerHTML = "";
}
