var g_tAjax;

function ajax_send(sUrl, sArgs)
{
  if (window.XMLHttpRequest) g_tAjax = new XMLHttpRequest();
  else if(window.ActiveXObject) g_tAjax = new ActiveXObject("Microsoft.XMLHTTP");
  else
  {
    alert("Failed to create Ajax object.\nConsider upgrading your browser.");
    return;
  }

  g_tAjax.open("POST", sUrl, true);
  g_tAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  g_tAjax.onreadystatechange = ajax_update;
  g_tAjax.send(sArgs);
}

function ajax_form(tForm, sUrl, sArgs)
{
  var i, tElems;

  tElems = tForm.elements;
  for (i=0; i<tElems.length; i++)
  {
    if (sArgs != "") sArgs += "&";
    sArgs += tElems[i].name + "=" + escape(tElems[i].value);
  }
  ajax_send(sUrl, sArgs);
}

function ajax_update()
{
  if (g_tAjax.readyState == 4) json_parse(g_tAjax.responseText);
}

function json_parse(sJson)
{
  var tJson;

  tJson = eval("(" + sJson + ")");
  if (tJson.error) alert(tJson.errormsg);
  if (tJson.onload) eval(tJson.onload + "(tJson)");
  else json_update(tJson);
  if (tJson.onexit) eval(tJson.onexit);
}

function json_update(tJson)
{
  var sId, tElem;

  if (tJson.values)
  {
    for (sId in tJson.values)
    {
      tElem = document.getElementById(sId);
      if (tElem) tElem.innerHTML = stripslashes(tJson.values[sId]);
    }
  }
  if (tJson.srcs)
  {
    for (sId in tJson.srcs)
    {
      tElem = document.getElementById(sId);
      if (tElem) tElem.src = stripslashes(tJson.srcs[sId]);
    }
  }
}

function stripslashes(sValue)
{
  sValue = sValue.replace(/\\'/g, "'");
  sValue = sValue.replace(/\\"/g, "\"");
  sValue = sValue.replace(/\\\\/g, "\\");
  sValue = sValue.replace(/\\0/g, "\0");
  return sValue;
}

