/*
 * Copyright (c) 2006 AWS Convergence Technologies, Inc. 
 * All rights reserved.
 * Visit http://www.aws.com
 *
 *  WBGDataRequest.js
 *
 * This scripts contains the WBDataRequest resuable class. A DataRequest object
 * takes care of loading data from an XMLHttpRequest object.
 */
// Object Definition
function WBDataRequest(URL,loadCallback)
{
//    alert("WBDataRequest has URL "+ URL + " and loadCallback "+ loadCallback);
    this.url = URL; // Holds the URL for the data request
	this.request; // Holds the XMLHttpRequest Object
	this.loadCallback = loadCallback;
}

// Do not call this directly, only from an initialized DataRequest object.
// Used to reload the information from the local URL.
WBDataRequest.prototype.load = function (str)
{
	// terminate any open active requests from this object.
	if (this.request != null)
	{
		this.request.abort();
		this.request = null;
	}
    
	// Create a new request and fetch the data
	if (window.XMLHttpRequest)
    {
      this.request = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)// code for IE
    {
      this.request = new ActiveXObject("Microsoft.XMLHTTP");
    }
	this.request.onreadystatechange = this.loadCallback;
	
    var rndPrefix;
    var qStr;
    
	if(str)
	{
	    qStr = this.url+str;
	    if (qStr.indexOf("?") > 0)
	        qStr = qStr+"&rnd="+Math.random();
	    else
	        qStr = qStr+"?rnd="+Math.random();
	    
	    //alert("qStr is "+ qStr);
	    
	    try
	    {
	        this.request.open("GET",qStr,true);
	    }
	    catch(e)
	    {
	        alert(e);
	    }    
	}
	else
	{
	    qStr = this.url;
	    if (qStr.indexOf("?") > 0)
	        qStr = qStr+"&rnd="+Math.random();
	    else
	        qStr = qStr+"?rnd="+Math.random();
	    
	    //alert("qStr is "+ qStr);
	    
	    this.request.open("GET",qStr,true);
	}
	//alert("Qstr: " + qStr);
	try{
	this.request.send(null);
	} catch (ex){
	    //alert(ex);
	}
}
