﻿
function AjaxLoader(url, args, isForm)
{
  this.Url = url;
  this.Args = args;
  this.UsePost = isForm;
  this.Request = null;
  this.CallBack = null;
  this.Caller = null;
  
  var Me = this;

  function processResults()
  {
  	if (Me.Request.readyState == 4)
		  if (Me.Request.status == 200) 
        Me.CallBack(Me.Request.responseText, Me.Caller);
  }
  this.load = function(inCallback, inYourself)
  { 
    this.CallBack = inCallback;
    this.Caller = inYourself;

    var url = this.Url;
    var method = "POST";
    if (!this.UsePost)
    {
      url += "?" + this.Args;
      method = "GET";
    }
    //printDebug(url);
	  if (window.XMLHttpRequest) 
	  {
	    //alert("firfox" + args);
		  this.Request = new XMLHttpRequest();
		  this.Request.onreadystatechange = processResults;
		  this.Request.open(method, url, true);
		  this.Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		  if (this.UsePost)
		    this.Request.send(this.Args);
		  else
		    this.Request.send(null);
	  // branch for IE/Windows ActiveX version
	  } 
	  else if (window.ActiveXObject) 
	  {
		  this.Request = new ActiveXObject("Microsoft.XMLHTTP");
		  if (this.Request) 
		  {
		    //alert("ie" + args);
			  this.Request.onreadystatechange = processResults;
			  this.Request.open(method, url, true);
  		  this.Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		    if (this.UsePost)
		      this.Request.send(this.Args);
		    else
		      this.Request.send(null);
		  }
	  }  
  }
}
