/**
*	Constructor for the class.
*
*	@param {String} dataURL The path to the data that the class
*	will load (OPTIONAL)
*
*	@constructor
*/
function AjaxCall(URL, divName, tableName)
{
	if(URL != undefined)
	{
		this._URL = URL;
	}

	if(divName != undefined)
	{
		this._divName = divName;
	}

	if(tableName != undefined)
	{
		this._tableName = tableName;
	}
}

/**************** Public APIs **********************/

/**
*	Tells the class to load its data and render the results.
*/
AjaxCall.prototype.load = function()
{
	//get a new XMLHTTPRequest and store it in an instance var.
	this._request = this._getXMLHTTPRequest();
	
	//set the var so we can scope the callback
	var _this = this;
	
	//callback will be an anonymous function that calls back into our class
	//this allows the call back in which we handle the response (_onData())
	// to have the correct scope.
	this._request.onreadystatechange = function(){_this._onData()};
	this._request.open("GET", this._generateUrl(), true);
	this._request.send(null);
}

/***************Private Rendering APIs ********************/

//return the URL from which the data will be loaded
AjaxCall.prototype._generateUrl = function()
{
	return this._URL;
}

//Remove the <form ....> </form> tag from the result of the asynchronous call.
AjaxCall.prototype._RemoveFormTag = function(s)
{
    var sTemp1;
    
    sTemp1 = s.substring(s.indexOf('<!-- do not remove this comment -->'),s.length - 1);
    sTemp1 = sTemp1.replace('</form>','');
    sTemp1 = sTemp1.replace('</BODY>','');
    sTemp1 = sTemp1.replace('</HTML','');
    sTemp1 = sTemp1.replace('</HTML>','');

    return sTemp1;
}

//callback for when the data is loaded from the server
AjaxCall.prototype._onData = function()
{
	if(this._request.readyState == 4)
	{
		if(this._request.status == "200")
		{	
				if(this._request.responseText=="")
                    this.HideDiv(this._divName);
                else
                {
                    this.ShowDiv(this._divName);
                    document.getElementById(this._divName).innerHTML = this._RemoveFormTag(this._request.responseText);
                    tbl = document.getElementById(this._tableName);
                    tbl.style.display = 'none';
                    
                    //For market value only - set the as of date
                    dtHiddenField = document.getElementById('MV_AsOfDate');
					
					if(dtHiddenField != null)
                    {
						setDate(dtHiddenField.value);	
                    }
                    
                    dtHiddenField2 = document.getElementById('MV_TotalMV');
					
					if(dtHiddenField2 != null)
                    {
						setTotal(dtHiddenField2.value);	
                    }                    
                }
		}
		else
		{
			this.ShowDiv(this._divName);
			document.getElementById(this._divName).innerHTML = 'Information not available.';
            tbl = document.getElementById(this._tableName);
            tbl.style.display = 'none';			
		}
		
		//clean up
		delete this._request;
	}
}

//Make the <div> visible
AjaxCall.prototype.ShowDiv = function(divid)
{
   if (document.layers) document.layers[divid].visibility="show";
   else document.getElementById(divid).style.visibility="visible";
}

//Hide the <div>
AjaxCall.prototype.HideDiv = function(divid)
{
   if (document.layers) document.layers[divid].visibility="hide";
   else document.getElementById(divid).style.visibility="hidden";
}

/***************Private Data Util Functions ********************/


//returns an XMLHTTPRequest instance (based on browser)
AjaxCall.prototype._getXMLHTTPRequest = function()
{
	var xmlHttp;
	try
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e2)
		{
		}
	}
	
	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined'))
	{
		xmlHttp = new XMLHttpRequest();
	}
	
	return xmlHttp;
}