/* Script created by Andreas Brauchli
 *
 * please support open sourced software
 */

var pageHandler = new Object();
var http = getHTTPObject(); // We create the HTTP Object
var isWorking = false;
var container_id = null;
var timeout = 20;

/**
 * Get a new page for a defined container
 * @param cont_id the DOM id of the container
 * @param page the page number to get
 */
function get_page(cont_id, page)
{
  if (!page)
    page = 0;
  query_when_ready (cont_id, "cat="+ cont_id +"&page="+ page);
}

/**
 * Wait for a free line before posting
 * @param cont_id the DOM id of the container
 * @param ajaxargs the ajax query string to post
 */
function query_when_ready(cont_id, ajaxargs)
{
  if (isWorking)
  {
    window.setTimeout ("query_when_ready('"+ cont_id +"','"+ ajaxargs +"')", timeout);
    timeout += 20;
  }
  else
  {
    container_id = cont_id;
    query_json (ajaxargs);
    timeout = 20;
  }
}

/**
 * Async response handler
 */
function handleHttpResponse()
{
  if (http.readyState == 4)
  {
    var doc = eval('(' + http.responseText + ')');

    if (doc.name && doc.name == "JSONRequestError")
    {
      if (doc.message && doc.message == "login required")
      {
        alert ("Ihre Sitzung ist abgelaufen, bitte melden Sie sich neu an");
        location.reload (true);
      }
      else
      {
        alert ("AJAX Error: "+ doc.message);
      }
    }
    else
    {
      var cont = document.getElementById (container_id);
      if (cont)
      {
	handler = pageHandler[container_id];
	if (handler != null)
	  handler(container_id, cont, doc);
      }
    }
    isWorking = false;
    container_id = null;
  }
}

/**
 * Post a raw async query
 */
function query_json (ajaxargs)
{
  if (ajaxargs.length > 0 && !isWorking && http)
  {
    //var sessid = document.getElementsByName("PHPSESSID")[0].value;
    var url = "/php/ajax.php?" + ajaxargs //;+
              //"&PHPSESSID=" + escape (sessid);

    http.open ("GET", url, true);
    http.onreadystatechange = handleHttpResponse;
    isWorking = true;
    http.send (null);
  }
}

/**
 * Post a raw async query without handler
 */
function post_json(ajaxurl)
{
  if (ajaxurl.length > 0 && http)
  {
    if(isWorking) {
        document.body.style.cursor = "wait";
        window.setTimeout("post_json('"+ajaxurl+"')", 20);
	return;
    }
    http.open ("GET", ajaxurl, false); //false: sync. call
    http.send (null);
    document.body.style.cursor = "default";
  }
}

/**
 * Get the Xml Http Object for async communication
 */
function getHTTPObject()
{
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
  {
    try
    {
      xmlhttp = new XMLHttpRequest();
      //xmlhttp.overrideMimeType("text/xml");
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}


