function createAJAXObject()
{
  var ajaxReq = null;
  try
  { 
    ajaxReq = new XMLHttpRequest();
  } catch(e) {ajaxReq = null;}
  if (!ajaxReq)
  {
    try
    {
      ajaxReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e) {ajaxReq = null;}
  }
  if (!ajaxReq)
  {
    try
    { 
      ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e) {ajaxReq = null;}
  }
  return ajaxReq;
}

function AjaxInfo()
{
  this.ajaxObject = createAJAXObject();
  this.func = null;
  this.showObj = null;
  this.state = 0;
  
  this.ajaxInfo = function()
  {
    this.ajaxObject = createAJAXObject();
  }
  
  this.preHandler = function(obj, resObj, href, data, preFunc, postFunc)
  {
    if (this.ajaxObject == null)
      return;
    if (this.state > 0)
    {
      return;
    }
    if (postFunc == null)
    {
      return;
    }
    if (resObj != null)
      this.showObj = document.getElementById(resObj);
    else
      this.showObj = null;
    if (preFunc == null || preFunc(obj))
    {
      this.state = 1;
      this.func = postFunc;
      this.ajaxObject.open('POST', href);
      this.ajaxObject.onreadystatechange = ajaxInfo.postHandler;
      this.ajaxObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      this.ajaxObject.setRequestHeader("X-Requested-With", "XMLHttpRequest");
      this.ajaxObject.send(preparePostQuery(data));
      //var data = {'key1': val1, 'key2': val2};
    }
  }
  
  this.postHandler = function()
  {
    if (ajaxInfo.ajaxObject == null || ajaxInfo.ajaxObject.readyState != 4)
      return;
    if (ajaxInfo.state == 0)
    {
      return;
    }
    ajaxInfo.state = 0;
    if (ajaxInfo.func == null)
      return;
    var res = ajaxInfo.ajaxObject.responseText;
    text = get_ajax_text(res);
    params = get_ajax_params(res);
    ajaxInfo.func(text, params);
  }
}

function explode_str(str)
{
  return explode_str(str, '|');
}

function explode_str(str, delim)
{
  var sPos = 0;
  var res = [];
  var part;
  var ePos;
  while((ePos = str.indexOf(delim, sPos)) >= 0)
  {
    part = str.substring(sPos, ePos);
    sPos = ePos + 1;
    res.push(part);
  }
  part = str.substring(sPos);
  res.push(part);
  return res;
}

function get_ajax_text(str)
{
  var delim = '#|params|#';
  var pos = str.lastIndexOf(delim);
  if (pos < 0)
    return str;
  return str.substring(0, pos);
}

function get_ajax_params(str)
{
  var delim = '#|params|#';
  var pos = str.lastIndexOf(delim);
  if (pos < 0)
    return null;
  str = str.substring(pos + delim.length);
  if (str.length == 0)
    return null;
  res = explode_str(str, '|');
  var params = [];
  var tmp;
  for (i = 0; i < res.length; i++)
  {
    tmp = explode_str(res[i], '=');
    if (tmp.length == 2)
      params[tmp[0].replace(/\W/g, '')] = tmp[1].replace(/^\W/g, '').replace(/\W$/g, '');
  }
  return params;
}

var ajaxInfo = new AjaxInfo();

function preparePostQuery(data)
{
  var query = [];
  if (data == null)
    return "";
  for (var key in data)
  {
    try
    {
      if (data[key] === undefined || data[key] === null)
        continue;
      query.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key]));
    } catch (e) {}
  }
  return query.join('&');
}
   
function show_parent(obj)
{
  if (obj == null)
    return;
  var parentObj = obj.parentNode;
  parentObj.style.display = 'block';
}

function hide_parent(obj)
{
  if (obj == null)
    return;
  var parentObj = obj.parentNode;
  parentObj.style.display = 'none';
}

function apply_parent_position(obj_info, obj_act)
{
  if (obj_info == null || obj_act == null)
    return;
  var obj_parent = obj_info.parentNode;
  var obj_info = obj_info.offsetParent;
  var leftpos = obj_act.offsetLeft;
  var toppos = obj_act.offsetTop + obj_act.offsetHeight + 1;
  var parentObj = obj_act;
  while ((parentObj = parentObj.offsetParent) != null)
  {
    toppos  += parentObj.offsetTop;
    leftpos += parentObj.offsetLeft;
  }
  obj_parent.style.left = leftpos + 'px';
  obj_parent.style.top  = toppos + 'px';
}

function ajax_button_hover(obj)
{
  obj.style.backgroundColor = '#f8f8ff';
}

function ajax_button_out(obj)
{
  obj.style.backgroundColor = '#f0f0ff';
}
