/*#REV:LizardCMS-614af563d6-110064-72cae5fa5e17-6a7a596672e0c827af164837812667b0 2 2009-11-07 14:55:41 c2bcc57b5836b54684ec7dc33d76486d */
function Ajax(readyStateFunction)
{
  var reqMethod = 'POST';
  var async     = true;
  var reqUrl    = APPLICATION_PATH + INDEXFILE;
  var page      = 'ajax';
  var sendText  = false;

  this.setMethod = function(val)
  {
    if (typeof(val)=='string')
      reqMethod = val;
  }
  this.setAsync = function(val)
  {
    if (typeof(val)=='boolean')
      async = val;
  }
  this.setUrl = function(val)
  {
    if (typeof(val)=='string')
      reqUrl = val;
  }
  this.setPage = function(val)
  {
    if (typeof(val)=='string')
      page = val;
  }
  this.setSendText = function(val)
  {
    if (typeof(val)=='boolean')
      sendText = val;
  }

  //implementation for selectNodes and selectSingleNode with XPath
  if (document.implementation.hasFeature("XPath", "3.0"))
  {
  	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
  	{
  		if( !xNode ) { xNode = this; }

  		var oNSResolver = this.createNSResolver(this.documentElement)
  		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
  		var aResult = [];
  		for( var i = 0; i < aItems.snapshotLength; i++)
  		{
  			aResult[i] =  aItems.snapshotItem(i);
  		}

  		return aResult;
  	}
  	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
  	{
  		if( !xNode ) { xNode = this; }

  		var xItems = this.selectNodes(cXPathString, xNode);
  		if( xItems.length > 0 )
  		{
  			return xItems[0];
  		}
  		else
  		{
  			return null;
  		}
  	}
  }

  var httpRequest = false;

  if (window.XMLHttpRequest) // Mozilla, Safari,...
  {
    httpRequest = new XMLHttpRequest();
    if (httpRequest.overrideMimeType)
      httpRequest.overrideMimeType('application/xml');
  }
  else if (window.ActiveXObject) // IE
  {
    try
    {
      httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e)
    {
      try
      {
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }

  this.send = function(params)
  {
    if((typeof(readyStateFunction)=='function') && async)
      httpRequest.onreadystatechange = doReadyState;
    httpRequest.open(reqMethod, reqUrl, async);
    httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    httpRequest.send('page='+page+'&'+params);
    if (!async)
      if (typeof(readyStateFunction)=='function')
        readyStateFunction(httpRequest.responseXML);
      else
        return httpRequest;
    return true;
  }

  function doReadyState()
  {
    if ((httpRequest.readyState == 4) && (httpRequest.status == 200))
    {
      if (sendText)
        readyStateFunction(httpRequest.responseText);
      else
        readyStateFunction(httpRequest.responseXML);
    }
  }

  Function.prototype.execScripts = function(tag)
  {
    //scripts
    var scripts = tag.getElementsByTagName('script');
    if (scripts != null)
    {
      for (var i=0; i<scripts.length; i++)
      {
        if ((scripts[i].getAttribute('src') != null) && (scripts[i].getAttribute('src') != '')) //js-source
        {
          var newtag = document.createElement('script');
          newtag.setAttribute('src', scripts[i].getAttribute('src'));
          document.getElementsByTagName('body')[0].appendChild(newtag);
        } else //inline-script
        {
          eval(scripts[i].innerHTML);
        }
      }
    }
  }

  return true;
}
