var uniqueRow = 0;

function streamSent(id)
{
  var obj = document.getElementById(id), tmpId = obj.id;
  var eltNew = document.createElement("span");
  eltNew.appendChild(document.createTextNode("Sent..."));
  obj.parentNode.replaceChild(eltNew, obj);
  eltNew.id = tmpId;
  eltNew.className = "sent";
}
function streamComplete(id)
{
  var obj = document.getElementById(id);
  if (obj.tagName.toLowerCase() != "span") { alert("not span"); return; }
  obj.firstChild.nodeValue = "Completed";
  obj.className = "completed";
  window.setTimeout(function(){ putEditField(id); }, 2000);
}
function streamError(id)
{
  var obj = document.getElementById(id);
  obj.firstChild.nodeValue = "Error";
  obj.className = "error";
}
function putEditField(id)
{
  var obj = document.getElementById(id);
  obj.firstChild.nodeValue = "Edit";
  obj.className = "edit";
  obj.onclick = function() { editRow(obj); };
}

function Conn()
{
  var timeout = 0, xmlhttp = null;
  this.id = '';
  this.create = function(sURL)
  {
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlhttp = false;
    }
    if (!xmlhttp)
    {
      try {
        xmlhttp = new XMLHttpRequest();
      } catch (e) {
        xmlhttp = false;
      }
    }
    if (!xmlhttp) return null;
    
    xmlhttp.open("POST", sURL, true);
    
    function getIt(o)
    {
      return function()
      {
        if (xmlhttp.readyState == 4)
        {
          window.clearTimeout(timeout);
          streamComplete(o.id);
        }
      }
    }
    xmlhttp.onreadystatechange = getIt(this);
  }
  this.send = function(sVars)
  {
    if (!xmlhttp) return;
    timeout = window.setTimeout(function(){streamError(this.id);}, 7500);
    streamSent(this.id);
    xmlhttp.send(sVars);
  }
}

function addRow(obj, sibBefore, vals)
{
  if (!sibBefore) sibBefore = null;
  if (!vals) vals = ['',''];
  
  var pobj = obj;
  while (!pobj.id || pobj.id != "tb") pobj = pobj.parentNode;
  
  var tbody = pobj.getElementsByTagName("tbody")[0];
  
  var newTR = document.createElement('tr');
  var newTDs = [document.createElement('td'), document.createElement('td'),
    document.createElement('td')];
  var newInputs = [document.createElement('input'),
    document.createElement('input'), document.createElement('input')];
  
  newTDs[2].className = "cnt";
  
  newInputs[0].type = newInputs[1].type = "text";
  newInputs[0].maxLength = newInputs[1].maxLength = "128";
  newInputs[0].name = "newName";
  newInputs[1].name = "newValue";
  newInputs[0].value = vals[0];
  newInputs[1].value = vals[1];
  
  newInputs[2].type = "button";
  newInputs[2].id = "but"+(++uniqueRow);
  newInputs[2].value = "Save";
  newInputs[2].onclick = function() { saveRow(this); };
  
  for (var i=0,len=newTDs.length; i < len; i++)
    newTDs[i].appendChild(newInputs[i]);
  
  for (var i=0,len=newTDs.length; i < len; i++)
    newTR.appendChild(newTDs[i]);
  
  tbody.insertBefore(newTR, sibBefore);
}

function editRow(obj)
{
  var pobj = obj;
  while (!pobj.tagName || pobj.tagName.toLowerCase() != "tr")
    pobj = pobj.parentNode;
  var inps = pobj.getElementsByTagName("span");
  var myVals = [];
  for (var i=0,len=inps.length; i < len; i++)
      myVals[myVals.length] = inps[i].firstChild.nodeValue;
  addRow(document.getElementById('add'), pobj.nextSibling, myVals); //single table limitation
  pobj.parentNode.removeChild(pobj);
}

function saveRow(obj)
{
  var pobj = obj;
  while (!pobj.tagName || pobj.tagName.toLowerCase() != "tr")
    pobj = pobj.parentNode;
  
  var inps = pobj.getElementsByTagName("input");
  var sVars = "", i=0;
  
  while (inps.length > 1) // remove 2 inputs, keep 1 (the button)
  {
    if (inps[0] && inps[0].type)
    {
      if (inps[0].type == "text")
      {
        sVars += "q"+(++i)+"="+inps[0].value+",";
        
        var newTxt = document.createElement("span");
        newTxt.appendChild(document.createTextNode(inps[0].value));
        inps[0].parentNode.replaceChild(newTxt, inps[0]);
      }
    }
  }
  // if there's a string, send it to the server
  if (sVars.length == 0) return;
  sVars = sVars.substring(0, sVars.length - 1);
  
  var go = new Conn();
  if (!go)
  {
    alert("XMLHTTP support required. [IE6 / Moz / Safari 1.2]");
    return;
  }
  go.id = obj.id;
  go.create("dynrows-submit.php");
  go.send(sVars);
}

