PageData = new function() {
	this.stock_lastTD = null;
	this.stock_changeTD = null;
	this.stock_pchangeTD = null;
	this.stock_prev_closeTD = null;
	this.stock_openTD = null;
	this.stock_volumeTD = null;
	this.stock_exchangeTD = null;
	this.ajaxreq = null;
	
};

IRApp = new function() {

	this.init = function() {
		PageData.stock_lastTD = document.getElementById("stock_last");
		PageData.stock_changeTD = document.getElementById("stock_change");
		PageData.stock_pchangeTD = document.getElementById("stock_pchange");		
		PageData.stock_prev_closeTD = document.getElementById("stock_prev_close");			
		PageData.stock_openTD = document.getElementById("stock_open");		
		PageData.stock_volumeTD = document.getElementById("stock_volume");			
		PageData.stock_exchangeTD = document.getElementById("stock_exchange");			
		PageData.ajaxreq = new Ajax();
		
		x = new Date();
		x = x.getTime();
		PageData.ajaxreq.doGet('/static/stock.xml?x=' + x, IRApp.displayStock, 'xml');
	};

	this.displayStock = function(resp) {

			stockData = XMLParse.xml2ObjArray(resp, 'ROW');

			stocklast = stockData[0].LASTPRICE;
			roundlast = parseFloat(stocklast).toFixed(2); 

			tempText = document.createTextNode(roundlast);
			PageData.stock_lastTD.appendChild(tempText);

			change = stockData[0].CHANGE;
			round_change = parseFloat(change).toFixed(2);

			if (round_change > 0) {
				updown_symbol = "+";
			}
			else if (round_change < 0) {
				updown_symbol = "-";
				round_change = round_change.substr(1);
			}
			else {
				updown_symbol = "";
			}

			if (updown_symbol == "") {
				change_full_text = round_change;
			}
			else {
				change_full_text = updown_symbol + " " + round_change;
			}
		
			tempText = document.createTextNode(change_full_text);
			PageData.stock_changeTD.appendChild(tempText);

			pchange = stockData[0].PCHANGE;
			round_pchange = parseFloat(pchange).toFixed(2);

			if (round_pchange < 0) {
				round_pchange = " " + round_pchange.substr(1);
								
				tempImage = document.createElement("img");
				tempImage.src = "http://www.boeing.com/images/smdown.gif";
				tempImage.alt="stock down";
			}
			else if (round_pchange > 0) {
				round_pchange = " " + round_pchange;
				
				tempImage = document.createElement("img");
				tempImage.src = "http://www.boeing.com/images/smup.gif";
				tempImage.alt="stock up";
			}
			else {
				//do nothing
			}

			tempText = document.createTextNode(round_pchange);
			if (round_pchange != 0) {
				PageData.stock_pchangeTD.appendChild(tempImage);
			}
			PageData.stock_pchangeTD.appendChild(tempText);

			prev_close = stockData[0].PREVIOUSCLOSE;
			round_prev_close = parseFloat(prev_close).toFixed(2); 

			tempText = document.createTextNode(round_prev_close);
			PageData.stock_prev_closeTD.appendChild(tempText);

			stockopen = stockData[0].OPEN;
			roundopen = parseFloat(stockopen).toFixed(2); 

			tempText = document.createTextNode(roundopen);
			PageData.stock_openTD.appendChild(tempText);

			volume_commas = addCommas(stockData[0].VOLUME);
			
			tempText = document.createTextNode(volume_commas);
			PageData.stock_volumeTD.appendChild(tempText);

			tempText = document.createTextNode(stockData[0].EXCHANGE);
			PageData.stock_exchangeTD.appendChild(tempText);
	};

	this.cleanup = function() {
		PageData.stock_lastTD = null;
		PageData.stock_changeTD = null;
		PageData.stock_pchangeTD = null;
		PageData.stock_prev_closeTD = null;
		PageData.stock_openTD = null;
		PageData.stock_volumeTD = null;
		PageData.stock_exchangeTD = null;
	};
	
};

window.onload = IRApp.init;
window.onunload = IRApp.cleanup;

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
