function AjaxSearchComponent(URL, callbackFunction)  {
    this.URL = URL;
    this.callbackFunction = callbackFunction;
    this.debug = false;
    
    this.bReady = true;
    
    this.aSync = true;
    this.xmlhttp = null;
    
    this.SuffixTemp = "";
    this.bUpdate = false;
    
    this.responseRecived = false; 
    this.responseText = "";
}

var AjaxCall;
var ContentId;
var ContainerElement;

function GetContent(ItemId, sub, Container)
{
	ContentId = parseInt(ItemId);
	ContainerElement = Container;

	if(sub == true)
		AjaxCall = new AjaxSearchComponent(ahref[ContentId], ShowSubContent);
	else
		AjaxCall = new AjaxSearchComponent(ahref[ContentId], ShowContent);
		AjaxCall.debug = true;
		AjaxCall.initiateWatch("");
}

function ShowSubContent()
{
	$(ContainerElement + " .content").html(AjaxCall.responseText + "<br class=\"clear\" />");
}

function ShowContent()
{
	$("#item-" + ContentId + " .content").html(AjaxCall.responseText + "<br class=\"clear\" />");
}

AjaxSearchComponent.prototype.initiateWatch = function(URLsuffix)
{
    if(!this.bReady) {
      this.SuffixTemp = URLsuffix;
      this.bUpdate = true;   
      return;
    }
    this.SuffixTemp = "";
    this.bReady = false;    
    this.xmlhttp=null;
    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
      {
      this.xmlhttp=new XMLHttpRequest();
      }
    // code for IE
    else if (window.ActiveXObject)
      {
      this.xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    if (this.xmlhttp!=null)
      {
      this.xmlhttp.onreadystatechange=this.state_Change.bind(this);
      this.xmlhttp.open("GET",this.URL + URLsuffix,this.aSync);
      this.xmlhttp.send(null);
      }
    else
  {
      if(this.debug) {
          alert("Your browser does not support XMLHTTP.");
      }
  }
}

AjaxSearchComponent.prototype.state_Change = function()
{
// if xmlhttp shows "loaded"
if (this.xmlhttp.readyState==4)
  {
  // if "OK"
  if (this.xmlhttp.status==200)
    {
        this.responseText = this.xmlhttp.responseText;
        this.responseRecived = true; 
        this.callbackFunction();
        this.bReady = true; 
        if(this.bUpdate) {
          this.bUpdate = false;
          this.initiateWatch(this.SuffixTemp);
        }
    }
  else
    {
        if(this.debug) {
             alert("Problem retrieving XML data:" + this.xmlhttp.statusText);
         }
    }
  }
}

function toArray(pseudoArray) {
     var result = [];
     for (var i = 0; i < pseudoArray.length; i++)
         result.push(pseudoArray[i]);
     return result;
}

Function.prototype.bind = function (object) {
     var method = this;
     var oldArguments = toArray(arguments).slice(1);
     return function () {
         var newArguments = toArray(arguments);
         return method.apply(object, oldArguments.concat(newArguments));
     };
}