function AjaxStockWatcher(oElement) {
    //Get the element we'll append to.
    //If one's not specified, use the document's <body/>
	this.toAppend = (oElement) ? oElement : document.body;
	
	//Create the table that'll house our data
	this.table = document.createElement("table");
	
	//Assign its CSS class
	this.table.className = "ajaxStockWatcher-table";
	
	//and append it to toAppend
	this.toAppend.appendChild(this.table);
	
	//For the timeout
	this.timer = null;
	
	//Begin polling.
	this.poll();
}

AjaxStockWatcher.prototype.poll = function() {
    //Pointer to the current object.
    var oThis = this;
    
    var oReq = zXmlHttp.createRequest();
        oReq.onreadystatechange = function () {
        if (oReq.readyState == 4) {
            if (oReq.status == 200 || oReq.status == 304) {
                oThis.handleResponse(oReq.responseText);
            }
        }
    };

    oReq.open("GET", "stockproxy.php", true);
    oReq.send(null);
};

AjaxStockWatcher.prototype.handleResponse = function(sJson) {
    //Parse the JSON string
    var oResult = sJson.parseJSON();

    //Delete the existing stocks shown.   
    while (this.table.rows.length > 0)
        this.table.deleteRow(0);

    if (!oResult.error) {
        //No error. Display the information.
        var oRow1 = this.table.insertRow(0);
        var tdName1 = oRow1.insertCell(0);
        tdName1.className = "header-stockName1";
        tdName1.appendChild(document.createTextNode("Index"));
        tdName1 = oRow1.insertCell(1);
        tdName1.className = "header-stockName";
        tdName1.appendChild(document.createTextNode("Price"));
        tdName1 = oRow1.insertCell(2);
        tdName1.className = "header-stockName";
        tdName1.appendChild(document.createTextNode("Change"));
        tdName1 = oRow1.insertCell(3);
        tdName1.className = "header-stockName";
        tdName1.appendChild(document.createTextNode("Percent"));
        tdName1 = oRow1.insertCell(4);
        tdName1.className = "header-stockName";
        tdName1.appendChild(document.createTextNode("Time"));

        for (var i = 1; i <= oResult.stocks.length; i++) {
            var oStock = oResult.stocks[i];

            //Insert a new row
            var oRow = this.table.insertRow(i);

            //Add a cell for the stock's name
            var tdName = oRow.insertCell(0);
            tdName.className = "ajaxStockWatcher-stockName";

            //And the last trade amount.
            var tdLastTrade = oRow.insertCell(1);
            tdLastTrade.className = "ajaxStockWatcher-lastTrade";

            //And the change
            var tdChange = oRow.insertCell(2);
            tdChange.className = "ajaxStockWatcher-change";
            
            var tdpercent = oRow.insertCell(3);
            tdpercent.className = "ajaxStockWatcher-change";

            var tdtime = oRow.insertCell(4);
            tdtime.className = "ajaxStockWatcher-lastTrade";

//            if (oStock.change != null) {
            if (oStock){
              if( oStock.change) {
                tdChange.className  += (parseFloat(oStock.change) > 0) ? " ajaxStockWatcher-change-up" : " ajaxStockWatcher-change-down";
                tdpercent.className += (parseFloat(oStock.change) > 0) ? " ajaxStockWatcher-change-up" : " ajaxStockWatcher-change-down";

                tdChange.appendChild(document.createTextNode(oStock.change));

                var num = (oStock.change / oStock.prev) * 100;
                var result = Math.abs(num.toFixed(2));
                tdpercent.appendChild(document.createTextNode("(" + result + "%)"));
              }
              
              if( oStock.time ) {
                tdtime.appendChild(document.createTextNode(oStock.time));
              } else {
                tdtime.appendChild(document.createTextNode(""));
              }

              var aLinkToYahoo = document.createElement("a");
              aLinkToYahoo.appendChild(document.createTextNode(oStock.companyName));
              if( oStock.symbol == 'ETGLX' ){
                aLinkToYahoo.href = "http://quote.morningstar.com/fund/f.aspx?t=" + oStock.symbol;
              } else {
                aLinkToYahoo.href = "http://finance.yahoo.com/q?s=" + oStock.symbol;
              }
              aLinkToYahoo.target = "_blank";

              //Append the data to the <td/>s
              tdName.appendChild(aLinkToYahoo);
              tdLastTrade.appendChild(document.createTextNode(oStock.lastTrade));
            }
            //var oRow1 = this.table.insertRow(i+1);
            //var tdDesc = oRow.insertCell(0);


            //tdDesc.className = "ajaxStockWatcher-stockName";
            //tdDesc.appendChild = document.createTextNode("S&P 500 Index: The S&P 500 is a value weighted index of stocks traded on the New York Stock Exchange and NASDAQ. Almost all of the stocks included in the index are among the 500 American stocks with the largest market capitalizations.");
        }
    } else { //An error occurred. Probably network related.
        //Insert a new row
        var oRow = this.table.insertRow(0);

        //Add a cell, and text to tell the user
        //an error occurred
        var tdError = oRow.insertCell(0)
        tdError.colSpan = 3;
        tdError.appendChild(document.createTextNode("An error occurred. Attempting to reconnect..."));
    }

    //Pointer to the current object.
    var oThis = this;

    //For the timeout
    var doSetTimeout = function() {
        oThis.poll();
    };

    //Do the timeout
    this.timer = setTimeout(doSetTimeout, 30000);
}

AjaxStockWatcher.prototype.stopPoll = function () {
    //Stop the polling
    clearTimeout(this.timer);
};
