
CustomAjax = function()
{
    this.url = "";
    this.xmlHttp = null;
    this.onOk = null;    
}

CustomAjax.prototype.handleStateChange = function()
{
   var messageObj ="";
   if (this.xmlHttp.readyState == 4)
   {
       if (this.xmlHttp.status == 200)
       {
            if(this.onOk != null && typeof(this.onOk) == "function") this.onOk(this.xmlHttp);
       }
   }
}

CustomAjax.prototype.createXMLHttpRequest = function()
{
    try
    {
        return new ActiveXObject("MSXML2.XMLHTTP")
    }
    catch(err)
    {
    }
    try
    {
        return new ActiveXObject("Microsoft.XMLHTTP")
    }
    catch(err)
    {
    }
    try
    {
        return new XMLHttpRequest
    }
    catch(err)
    {
    }     
    return null
}

CustomAjax.prototype.sendRequest = function(pQueryString)
{
    var self = this;
    this.xmlHttp = this.createXMLHttpRequest();	
    var targetUrl = this.url + "?" + pQueryString;
	this.xmlHttp.onreadystatechange = function()
	{
	    self.handleStateChange.call(self);
	}
	this.xmlHttp.open("Get",targetUrl,true);
	this.xmlHttp.send(null);
}

CustomAjax.prototype.getTextboxValue = function(pTextboxId)
{
    if(pTextboxId == null || pTextboxId == "") return "";
    var txtBox = document.getElementById(pTextboxId);
    if(txtBox == null || txtBox.value == "") return "";
    return txtBox.value.replace(/^\s+|\s+$/,"");
}

CustomAjax.prototype.setTextboxValue = function(pTextboxId,pValue)
{
    if(pTextboxId == null || pTextboxId == "") return;
    var txtBox = document.getElementById(pTextboxId);
    if(txtBox == null) return;
    txtBox.value = pValue;
}

ClosePriceAjax = function(pTickerBoxId,pDateBoxId,pPriceBoxId)
{
    this.ticker = "";
    this.tickerBoxId = pTickerBoxId;
    this.dateBoxId   = pDateBoxId;
    this.priceBoxId  = pPriceBoxId;    
}

ClosePriceAjax.prototype = new CustomAjax();
ClosePriceAjax.prototype.constructor = ClosePriceAjax;

ClosePriceAjax.prototype.getClosePrice = function()
{
    if(this.ticker == "") this.ticker = this.getTextboxValue(this.tickerBoxId);
    if(this.ticker == "") return;  
    var date = "";
    if(this.dateBoxId != null && this.dateBoxId != "")
    {
        var isValid = document.getElementById(this.dateBoxId).getAttribute("isValid");  //the isValid attribute will be false if the date is invalid.
        if(isValid == false) return;
        date = this.getTextboxValue(this.dateBoxId);
        if(date == "") return;
    }
    var query="sTicker=" + this.ticker + "&sDate=" + date;
    this.url = "GetClosePrice.aspx";
    this.onOk = this.getClosePriceHandler;
    this.sendRequest(query);
}

ClosePriceAjax.prototype.getClosePriceHandler = function(pXmlHttp)
{
    if(pXmlHttp.responseText == "") return;
    var oldPrice = this.getTextboxValue(this.priceBoxId);
    if(oldPrice != "") return;
    this.setTextboxValue(this.priceBoxId,pXmlHttp.responseText);
}