addEvent(window, "load", preparetriggers);

var req=null;
var console=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("GET", url, true);
      reqArray[count - 1].send(null);
  }
}

function processReqChange(){
  var ready=req.readyState;
  var data=null;
  if (ready==READY_STATE_COMPLETE){
    data=req.responseText;
  }else{
    data="loading...["+ready+"]";
  }
  //debug(data);
  toConsole(data);
}

function toConsole(data){
  if (console!=null){
    var newline=document.createElement("div");
    console.appendChild(newline);
    newline.innerHTML = data;
  }
}

function preparetriggers(startingPoint, newName) {
  var inputs;
  var thisInput;
  var loop;

  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") {
        thisInput.addEventListener("click", processInput, 0);
      } else {
        thisInput.addEventListener("blur", processInput, 0);
        thisInput.addEventListener("focus", processInput, 0);
      }
    }

    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);
        }
      }
    }
  }

  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;
      thisInput.addEventListener("focus", processSelect, 0);
      thisInput.addEventListener("change", processSelect, 0);
    }


    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;
      thisInput.addEventListener("blur", processInput, 0);
      thisInput.addEventListener("focus", processInput, 0);
    }
  }

  //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;
      thisInput.addEventListener("click", processDelete, 0);
    }

    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 = pathtoxml + "?action=xmldbupdate&table=" + idArray[0] + "&primarykeyid=0" + "&fields=" + idArray[1] + "&value1=" + 0;

  debug(xmlString);

  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")
      theValue = theValue.replace(/\+/g,"%2B")

      xmlString = pathtoxml + "?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;


      debug(xmlString);

      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 = pathtoxml + "?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;
      delete theInput.oldvalue;

      xmlString = pathtoxml + "?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") {

      //debug(theInput.id);
      var idArray = theInput.id.split(";");

      theFieldCount = (idArray.length - 3) / 2 + 1;

      if (theInput.checked) {
        theValue = 1;
      } else {
        theValue = 0;
      }

      xmlString = pathtoxml + "?action=xmldbupdate&table=" + idArray[0] + "&primarykeyid=" + idArray[1] + "&fields=" + theFieldCount + "&id=" + theInput.id + "&field1=" + idArray[2] + "&value1=" + theValue;

      for(loop = 1; loop < theFieldCount;loop++) {
        count++;
        xmlString = xmlString + "&field" + (count) + "=" + idArray[(count * 2) - 1] + "&value" + (count) + "=" + idArray[(count * 2)];
      }

      debug(xmlString);

      theTD = getParent(theInput, "td");
      theTD.setAttribute("class","updating");

      loadXMLDoc2(xmlString, updateScreen);
    }
  } else {
    if (theEvent.type == "focus") {
      theInput.oldvalue = theInput.value;
    } else {
      if (theEvent.type == "blur" || theEvent.type == "change") {
        if (theInput.oldvalue != "undefined" &&  theInput.value != theInput.oldvalue) {
          var idArray = theInput.id.split(";");

          delete theInput.oldvalue;

          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")
            theValue = theValue.replace(/\+/g,"%2B")

            xmlString = pathtoxml + "?action=xmldbupdate&table=" + idArray[0] + "&primarykeyid=" + idArray[1] + "&fields=" + theFieldCount + "&id=" + theInput.id + "&field1=" + idArray[2] + "&value1=" + theValue;

            for(loop = 1; loop < theFieldCount;loop++) {
              count++;
              xmlString = xmlString + "&field" + (count) + "=" + idArray[(count * 2) - 1] + "&value" + (count) + "=" + idArray[(count * 2)];
            }

            debug(xmlString);

            theTD = getParent(theInput, "td");

            if (theTD) {
              theTD.setAttribute("class","updating");
            } else {
              theInput.setAttribute("class","updating");
            }

            loadXMLDoc2(xmlString, updateScreen);
          }
        }
      }
    }
  }
}

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 idArray = theSelect.id.split(";");

      if (idArray.length >= 4) {
        theFieldCount = ((idArray.length - 4) / 2) + 1;

        theValue = theSelect.value;

        theValue = theValue.replace(/\#/,"%23")
        theValue = theValue.replace(/\&/,"%26")
        theValue = theValue.replace(/\+/g,"%2B")

        xmlString = pathtoxml + "?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");
        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];

      //debug("XML STRING : " + xmlString);
      //loadXMLDoc2(xmlString, updateScreen);
      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(";");

    theElement = document.getElementById(idArray[2]);
    theElement.oldstyle = theElement.style;
    theElement.setAttribute("style","background: #EE0000;");
    if (confirm("Are you sure you want to delete?")) {
      xmlString = pathtoxml + "?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);

      //debug(data);

      if (/<status>(.+)<\/status>/.exec(data) != undefined) {
        theStatus = /<status>(.+)<\/status>/.exec(data)[1];
        //debug(theStatus);
        //debug(data);
        switch (theStatus) {
      case "suggestion":
      var ss = document.getElementById('search_suggest');
      ss.innerHTML = '';
      var str = /<data>(.+)<\/data>/.exec(data)[1];
      str = str.split(";");
      var size = 0;
      for(i=0; i < str.length; i++) {
      var suggest = '<div onmouseover="suggestOver(this);" ';
      suggest += 'onmouseout="suggestOut(this);"  onclick="setSearch(this.innerHTML);">' + str[i] + '</div>';
      ss.innerHTML += suggest;
      if(str[i].length >= 20){
        var size = 1;
      }
      }
      document.getElementById("search_suggest").style.border="1px solid #A5ACB2";
      if(size == 0){
        document.getElementById("search_suggest").style.width="205px";
      }else{
        document.getElementById("search_suggest").style.width="";
      }
      break;
          case "selectlist":
            //Prase 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(cs[i].oldvalue);
                  } else {
                    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":
            theId = /<id>(.+)<\/id>/.exec(data)[1];
      var idArray = theId.split(";");
      
      //if the changed field is 'Overwrite' in MaintainOrder menu, get the new ExtPrice and display it.
      if (/<extprice>(.+)<\/extprice>/.exec(data) != undefined) {
        var overwritePrice;
        var element;
        overwritePrice = /<extprice>(.+)<\/extprice>/.exec(data)[1];
        element = document.getElementById(idArray[0]+';'+idArray[1]+';ExtPrice');
        element.innerHTML = overwritePrice;
      }
      
            theInput = document.getElementById(theId);

            if (theInput != "undefined") {
              if (/<newid>(.+)<\/newid>/.exec(data) != undefined) {
                debug("New ID.  Re iding!");
                var theTable = getParent(theInput, "table");
                var theRow = getParent(theInput, "tr");

                if ((' '+theRow.className+' ').indexOf("multiadd") != -1) {
                  var newRow;
                  newRow = theTable.insertRow(theTable.rows.length);
                  //newRow.setAttribute("id",theRow.id);
                  newRow.setAttribute("class",theRow.className);
                  newRow.setAttribute("valign",theRow.valign);

                  //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.className+' ').indexOf("multiadd") != -1) {
                  theRow.id = "record~" + theNewId;
                }
                var fields = document.getElementsByName(theInput.name);
                var thisField;
                for (namesLoop=0;namesLoop<fields.length;namesLoop++) {
                  thisField = fields[namesLoop];
                  //debug("name - " + thisField.name);
                  //debug("Re-name- " + thisField.id);
                  if ((' '+thisField.className+' ').indexOf("dataselect") != -1) {
                    idArray = thisField.id.split(";");
                    idArray[2] = theNewId;
                    thisField.id = idArray.join(";");
                  } 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 = pathtoxml + "?action=xmldbrowupdate&storedid=" + theTable.id + "&primarykeyid=" + idRowArray[1] + "&rowid=" + theRow.id;

                  loadXMLDoc2(xmlString, updateScreen);
                } else {
                  //debug("Row updates is off");
                }
              }

              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++) {
              debug(listLoop);
              if (listArray[listLoop * 2] != pivotField) {
                debug(table + ";" + listArray[listLoop * 2] + ";" + pivotField);
                theElement = document.getElementById(table + ";" + listArray[listLoop * 2] + ";" + pivotField);
              } else {
                debug(table + ";" + listArray[listLoop * 2]);
                theElement = document.getElementById(table + ";" + listArray[listLoop * 2]);
              }
              switch (theElement.nodeName) {
                case "INPUT":
                  if (theElement.type == "checkbox") {
                    debug("Value = " + listArray[(listLoop * 2) + 1]);
                    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:
                  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" : //debug("Deleting row!");
                          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.");
			if(/<id>(.+)<\/id>/.exec(data)){
		      theId = /<id>(.+)<\/id>/.exec(data)[1];
              theElement = document.getElementById(theId);
			  if(/<oldvalue>(.+)<\/oldvalue>/.exec(data)){
			    var oldValue = /<oldvalue>(.+)<\/oldvalue>/.exec(data)[1];
			    theElement.value = oldValue;
			  }else{
			    theElement.value = '';
			  }
	        }
            break;
          case "functionok":
            debug(data);
            var reason = /<reason>(.+)<\/reason>/.exec(data)[1];
            alert(reason);
            break;
          default:
            alert("Un-expected status.  Please see debug data.");
            debug(data, 2);
            break;
        } //switch (theStatus)
		
		if(/<errorMsg>(.+)<\/errorMsg>/.exec(data)){
	      var theErrors = /<errorMsg>(.+)<\/errorMsg>/.exec(data)[1];
		  var errorArray = theErrors.split(";");
		  var theElement = document.getElementById('errormsg');
		  theElement.innerHTML = "<b>Error</b><table>";
		  for(var i = 0; i < errorArray.length; i++){
		    theElement.innerHTML += "<tr><td>" + errorArray[i] + "</td></tr>";
		  }
		  theElement.innerHTML += "</table>";
		  theElement.style.border = "1px solid";
		  theElement.width = "40%";
	    }else{
	      var theElement = document.getElementById('errormsg');
		  theElement.innerHTML = "";
		  theElement.style.border = "0px";
	    }
	  }else {
        debug(data, 2);
        alert("Update failed.  Please see debug messages for further info.");
        break;
      }
    }
  }
}

//Click function
function setSearch(value) {
  document.getElementById('userinput').value = value.replace(/\&amp;/g,"\&");;
  document.getElementById('search_suggest').innerHTML = '';
  document.getElementById('search_suggest').style.border = "0px";
}

function suggestOver(value){
  value.className = 'suggest_link_over';
}

function suggestOut(value){
  value.className = 'suggest_link';
}

function searchSuggest(){
  var key = escape(document.getElementById('userinput').value);
  var table = escape(document.getElementById('table').value);
  var field = escape(document.getElementById('field').value);
  xmlString = pathtoxml + "?action=searchsuggestion&key=" + key + "&table=" + table + "&field=" + field;
  loadXMLDoc2(xmlString, updateScreen);
}
