ODN.stockQuote = function() {
	var qSymbolTf = document.getElementById('txt_qstock_symbol');
	var qSymbol = qSymbolTf.value;
	
	// UAT
	/*var priceURL = "http://210.177.167.128/info/liveinfo_quote.html?symbol=";*/

	// LIVE
	var priceURL = "http://money18.on.cc/info/liveinfo_quote.html?symbol=";

	if (qSymbol != '' && qSymbol.match(new RegExp(/^\d{0,5}$/))) {
		qSymbol = onccLib.digitPad('0',qSymbol, 5);
		window.location = priceURL + qSymbol+"&refer=odn";
	} else {
		alert("請輸入正確股票編號。");
		qSymbolTf.focus();
	}
}
ODN.onFocusStock = function(obj) {if (obj.value == '輸入股票編號') {obj.value = '';}}
ODN.onBlurStock = function(obj) {if (obj.value == '') {obj.value = '輸入股票編號';}}
var MarketStatus = function(st) {
	this.status = '';
	if (st) this.status = st.toUpperCase();
};
Number.prototype.toSymString = function(num) {
	if (this > 0) return '+' + this.toFixed(num).toString();
	return this.toFixed(num).toString();
};
Number.prototype.toPercentString = function(num) {
	if (this > 0) return '+' + this.toFixed(num).toString() + '%';
	else if (isNaN(this)) return new Number(0).toFixed(num) + '%';
	return this.toFixed(num).toString() + '%';
};
Number.prototype.toCommasString = function(num) {
	var x = this.toFixed(num).toString().split('.');
	var x1 = x[0];
	var x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
MarketStatus.prototype.toString = function() {
	var rtnVal = '';
	switch(this.status) {
		case 'CT': rtnVal = '開市中'; break;
		case 'CL': rtnVal = '中午收市'; break;
		case 'DC': rtnVal = '全日收市'; break;
	};
	return rtnVal;
};
MarketStatus.prototype.isOpen = function() {
	return (this.status === 'CT');
}

var M18 = {};
M18.marketStatus = new MarketStatus();

M18.path = {
	append: function(url, params) {
		var urlParams = [];
		var cnt = 0;
		var temp = url.split('?');
		var qs = this.getQs(url);
		for (var key in params) {
			qs[key] = params[key];
		}
		for (var key in qs) {
			if (cnt !== 0) urlParams.push('&');
			urlParams.push(key+'='+qs[key]);
			cnt++;
		}
		return temp[0] + '?'+urlParams.join('');
	},
	get: function(path, daily) {
		var dummy = '';
		return this.append(path, {'t': Math.random()});
	},
	getQs: function(url) {
		var rtnVal = {};
		var temp = url.split('?');
		if (temp.length > 1) {
			var qs =temp[1].split('&');
			for(var i = 0; i < qs.length;i++) {
				var keyVal = qs[i].split('=');
				if (keyVal.length > 1) {
					rtnVal[keyVal[0]] = decodeURIComponent(keyVal[1]);
				}
			}
		}
		return rtnVal;
	},
	market: function(){
		return this.get('http://home.on.cc/js/index/MAIN_s.js');
	},
	index: function() {
		return this.get('http://home.on.cc/js/index/HSI_r.js');
	}
};
M18.loadScript = function(url, callback, cbparams) {
	var head = document.getElementsByTagName("head")[0];
	var script = document.createElement("script");
	var done = false;
	script.src = url;
	script.charset = 'big5';
	// Attach handlers for all browsers
	script.onload = script.onreadystatechange = function() {
		if (!done && (!this.readyState||this.readyState =="loaded"||this.readyState == "complete")) {
			done = true;
			callback(cbparams);
			// Handle memory leak in IE
			script.onload=script.onreadystatechange=null;
			head.removeChild(script);
		}
	};
	head.appendChild(script);
};
M18.indexWidget = {
	index: -1,
	id: '#indexWidget',
	indexVal: null,
	changeVal: null,
	statusVal: null,
	content: null,
	firstLoad: true,
	init: function() {
		this.content = $(this.id);
		if (this.content.length > 0) {
			this.indexVal = this.content.find('tr.index td.val');
			this.statusVal = this.content.find('tr.status td.label');
			this.changeVal = this.content.find('tr.status td.val');
			this.load(true);
			this.loadStatus(true);
		}
	},
	load: function(reload) {
		if (typeof M18['r_HSI'] === 'undefined' || reload === true) {
			M18.loadScript(M18.path.index(), function() { M18.indexWidget.render() });
		} else {
			M18.indexWidget.render();
		}
	},
	loadStatus:  function(reload) {
		if (typeof M18['s_MAIN'] === 'undefined' || reload === true) {
			M18.loadScript(M18.path.market(), function() { M18.indexWidget.updateStatus() });
		} else {
			M18.indexWidget.updateStatus();
		}
	},
	render: function() {
		if (M18['r_HSI']) {
			if (this.index.toFixed(2) != new Number(M18['r_HSI'].value).toFixed(2)) {
				this.index = new Number(M18['r_HSI'].value);
				if (this.firstLoad === false) {
					this.indexVal.addClass('highlight');
					this.changeVal.addClass('highlight');
				}
				this.firstLoad = false;
				this.content.removeClass('up').removeClass('down');
				
				var change =  new Number(M18['r_HSI'].difference);
				if (change > 0) this.content.addClass('up');
				else if (change < 0) this.content.addClass('down');
				
				this.indexVal.html(this.index.toCommasString(2));
				this.changeVal.html(change.toSymString(2) + ' <span class="percent">(' + ((change / (this.index - change))*100).toPercentString(2) + ')</span>');
				setTimeout(function(){ M18.indexWidget.clearHighLight()}, 500);
			}
			
		}
		if (M18.marketStatus.isOpen()) {
			setTimeout(function(){ M18.indexWidget.load(true)}, 10000);
		}
		
	},
	updateStatus: function() {
		var lastStatus = M18.marketStatus.isOpen();
		M18.marketStatus.status = M18['s_MAIN'];
		this.statusVal.html('&nbsp;'+M18.marketStatus+'&nbsp;');
		setTimeout(function(){ M18.indexWidget.loadStatus(true)}, 10000);
		if (lastStatus == false && lastStatus != M18.marketStatus.isOpen()) {
			this.render();
		}
	},
	clearHighLight: function() {
		this.indexVal.removeClass('highlight');
		this.changeVal.removeClass('highlight');
	}
};﻿
