function trimString(s) {
	var l = 0, r = s.length - 1;
	while (l < s.length && s.charAt(l) == ' ') l++;
	while (r > l && s.charAt(r) == ' ') r--;
	return s.substring(l, r + 1);
}

function beforeSubmitSnapshot(f, txt) {
	blurInputSymbol(document.getElementById("companyForm_s"), txt);
	return true;
}

function performSymbolLookup(txt) {
	var f = document.forms["stockSearchForm"];
	blurInputPhrase(document.getElementById("stockSearchForm_stockcode"), txt);
	f.submit();
}

function blurInputSymbol(e, txt) {
	var newValue = trimString(e.value);
	if (newValue.length == 0 || newValue === txt) {
		e.form.elements["s"].value = "";
		e.value = txt;
		e.style.color = "#99ccff";
	} else {
		e.form.elements["s"].value = newValue;
		e.style.color = "#000000";
	}
}

function focusInputSymbol(e, txt) {
	if (trimString(e.value) == txt) e.value = "";
	e.style.color = "#000000";
}

function blurInputPhrase(e, txt) {
	var newValue = trimString(e.value);
	if (newValue.length == 0 || newValue === txt) {
		e.form.elements["stockcode"].value = "";
		e.value = txt;
		e.style.color = "#99ccff";
	} else {
		e.form.elements["stockcode"].value = newValue;
		e.style.color = "#000000";
	}
}

function focusInputPhrase(e, txt) {
	if (trimString(e.value) == txt) e.value = "";
	e.style.color = "#000000";
}

function getViewport() {
	var doc = window["document"];
	var scrollRoot = doc.compatMode === "BackCompat" ? doc.body || doc.getElementsByTagName("body")[0]: doc.documentElement;
	return {w: scrollRoot.clientWidth, h: scrollRoot.clientHeight};
}

function fixIEMinWidth() {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		var minWidth = 800, vp = getViewport();
		document.getElementById("SnapshotContainer").style.width = 
			(vp.w <= minWidth ? "750": Math.round(vp.w * 0.95)) + "px";
	}
}

function fixStockGroupFrame() {
	var vp = getViewport();
	var winH = vp.h;
	if (winH > 0) winH -= (170 + 50);
	if (winH < 640) winH = 640;
	document.getElementById("stockGroupFrame").style.height = winH + "px";
	document.getElementById("stockGroupFrame").style.display = "";
}

function fixSnapshotQuoteTableWidth() {
	var maxWidth = 270, vp = getViewport();
	document.getElementById("SnapshotLeftSection").style.width =
		(vp.w * 0.3 > maxWidth ? maxWidth: Math.round(vp.w * 0.3)) + "px";
}

function addResizeEvent(func) {
	func();
	if (window.addEventListener)
		window.addEventListener("resize", func, false);
	else if (window.attachEvent)
		window.attachEvent("onresize", func);
}
