// created by evenzyh in 090601
var searchReq = createReq();
var selectIndex = -2;
var itemCount ;
var market = "";
var subidstr = "";
function createReq() {
	var httpReq;
	if (window.XMLHttpRequest) {
		httpReq = new XMLHttpRequest();
		if (httpReq.overrideMimeType) {
			httpReq.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			httpReq = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			try {
				httpReq = new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
			}
		}
	}
	return httpReq;
}

function searchSuggest(e, subid) {
	var keyCode;
	if (window.event) {
		keyCode = e.keyCode;
	} else if (e.which) {
		keyCode = e.which;
	}
	if (keyCode == '38' || keyCode == '40') {// up and down
		if (itemCount >= 0)
			if (document.getElementById("search_suggest" + subidstr).style.display == "")
				changeSelect(keyCode);
		
			else
				showSearch();
	} else if (keyCode == '13') {// enter
		if (document.getElementById("search_suggest" + subidstr).style.display == "") {
			getSelectIndex();
			if (selectIndex >= 0) {
				setSearch(document.getElementById("suggest" + selectIndex).innerHTML);
			}
		} else {
			var theform = document.getElementById("stockinputform") ? document
					.getElementById("stockinputform") : document
					.getElementById("quote");
			if(theform==document.getElementById("stockinputform"))
			theform.submit();
		}
	} else {
		if (subid)
			subidstr = subid;
		else
			subidstr = '';
		init();
		var str = document.getElementById("IDStock" + subidstr).value;
		//alert(str);
		market = getMarket();
		if (str.length > 0) {
			searchReq.open("get", "../share/stock/search.php?market=" + market
					+ "&searchText=" + encodeURIComponent(str), true);

			searchReq.onreadystatechange = handleSearchSuggest;
			searchReq.send(null);
			
		}
	}
	
}

function handleSearchSuggest() {
	itemCount=-1;
	if (searchReq.readyState == 4) {
		if (searchReq.status == 200) {
			var suggestText = document.getElementById("search_suggest"
					+ subidstr);
			var sourceText = searchReq.responseText;
			//alert(sourceText);
			sourceText = sourceText.split('|');
			if (sourceText.length > 1) {
				suggestText.style.display = "";
				suggestText.innerHTML = "";
				var suggestWide = 0;
				for ( var i = 0; i < sourceText.length - 1; i += 2) {
					var s = '<div id="suggest' + i + '" onmouseover="javascript:mouseover(this);"';
					s += ' onmouseout="javascript:mouseout(this);" ';
					s += ' onclick="javascript:setSearch(this.innerHTML);" ';
					s += ' class="suggest_link" text-align:right>';
					var str =sourceText[i] + '&nbsp;&nbsp;&nbsp;'
							+ sourceText[i + 1];
						suggestWide = LengthOf(str);

					s += str + '</div>';
					suggestText.innerHTML += s;
					itemCount = i;
				}
				suggestText.style.width = suggestWide * 5 + 'px';
				suggestText.innerHTML += '<div class="closesearch" onclick="closeSearch();">close</div>';
				selectIndex = -2;
				
			} else {
				suggestText.style.display = "none";
			}
		}
	}
}

function mouseover(obj) {
	getSelectIndex();
	if (selectIndex >= 0 && selectIndex <= itemCount)
		suggestOut(document.getElementById("suggest" + selectIndex));
	suggestOver(obj);
	getSelectIndex();
}

function mouseout(obj) {
	suggestOut(obj);
	selectIndex = -1;

}

function getSelectIndex() {
	for ( var i = 0; i <= itemCount; i += 2) {
		if (document.getElementById("suggest" + i).className == 'suggest_link_over') {
			selectIndex = i;
			return;
		} else
			selectIndex = -2;
	}
}

function changeSelect(keyCode) {
	getSelectIndex();
	if (selectIndex >= 0 && selectIndex <= itemCount)
		suggestOut(document.getElementById("suggest" + selectIndex));

	if (keyCode == '38') {
		if (selectIndex <= 0)
			selectIndex = itemCount;
		else
			selectIndex -= 2;
	} else if (keyCode == '40') {
		if (selectIndex >= itemCount)
			selectIndex = 0;
		else
			selectIndex += 2;
	}
	suggestOver(document.getElementById("suggest" + selectIndex));
}

function LengthOf(strTemp) {
	var len = 0
	for ( var i = 0; i < strTemp.length; i++) {
		if (strTemp.charCodeAt(i) > 255) {
			len += 2
		} else {
			len += 1
		}
	}
	return len;
}

function suggestOver(div_value) {
	div_value.className = "suggest_link_over";
}

function suggestOut(div_value) {
	div_value.className = "suggest_link";
}

function setSearch(obj) {
	if (market != "HK" && market != "US") {
		setMarket(trim(obj.split('&nbsp;&nbsp;&nbsp;')[0]).substring(0, 2));
	}
	document.getElementById("IDStock" + subidstr).value =trim(obj
			.split('&nbsp;&nbsp;&nbsp;')[0]).substring(2);
	closeSearch();
	// document.getElementById("IDStock"+subidstr).focus();
}

function closeSearch() {
	document.getElementById("search_suggest" + subidstr).style.display = 'none';

}

function showSearch() {
	document.getElementById("search_suggest" + subidstr).style.display = '';
}

function outOfSuggest() {
	if(itemCount>=0){
		if (selectIndex < 0)
			selectIndex = 0;
		if (selectIndex >= 0 && selectIndex <= itemCount)
			setSearch(document.getElementById("suggest" + selectIndex).innerHTML);
		else
			closeSearch();
	}
}
function getMarket() {
	var obj;
	obj = document.getElementsByName("market" + subidstr);// radio check
	if (obj != null) {
		var i;
		for (i = 0; i < obj.length; i++) {
			if (obj[i].checked) {// selected
				return obj[i].value;
			}
		}
	}
	obj = document.getElementById("market" + subidstr);// select option
	if (obj != null) {
		if (obj.options)
			return obj.options[obj.selectedIndex].value;

	}
}
function setMarket($mark) {// only use for sz000001
	var obj;
	obj = document.getElementsByName("market" + subidstr);// radio check
	if (obj != null) {
		var i;
		for (i = 0; i < obj.length; i++) {
			if (obj[i].checked) {// selected
				obj[i].value = $mark;
			}
		}
	}
	obj = document.getElementById("market" + subidstr);// select option
	if (obj != null) {
		if (obj.options)
			obj.options[obj.selectedIndex].value = $mark;

	}
}

function init() {
	var searchinput = document.getElementById("IDStock" + subidstr);
	var w = searchinput.offsetWidth;
	var h = searchinput.offsetHeight;
	var t = searchinput.offsetTop;
	var l = searchinput.offsetLeft;
	var d = document.getElementById("search_suggest" + subidstr);
	d.style.border = "1px solid #000";
	d.style.background = "#FFFFFF";
	d.style.position = "absolute";
	d.style.left = l + "px";
	d.style.top = (t + h) + "px";
}

function searchCommit(){
	var market=getMarket();
	var	IDStock=document.getElementById("IDStock").value;
	if(!((market=="HK"&&IDStock.length>4)||(market=="US"&&IDStock.length>=3)||((market=="SH"||market=="SZ")&&IDStock.length==6))){
		outOfSuggest();	
	}else{
		setSearch(market+IDStock);
		
	}
	document.getElementById("stockinputform").submit();
}
function trim(str){  
    // 用正則表達式將前後空格  
    // 用空字符串替代。  
    return str.replace(/(^\s*)|(\s*$)/g, "");  
}