var CHINADOMAIN = "etnetchina.com";
var CHINADOMAIN2 = "etnet.com.cn";
var CHINALINK = 'http://adsserver.etnet.com.hk/etNetAds/adclick.php?bannerid=1865&source=&dest=http%3A%2F%2Fwww.etnetchina.com%2F';

/*-----  Function: cookies -----*/
function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";domain=<?php echo $COOKIE_DOMAIN;?>;path=/;expires="+exdate.toGMTString());
}
function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
/*-----  Function: domain -----*/
function getDomain(url)
{
	if (url==""||url==null) return "";
	var url = url.replace(/http:\/\//, "");
	var urlpath = url.indexOf("/");
	url = (urlpath!=-1)?url.substring(0,urlpath):url;
	return url;
}
/*-----  Function: automatically redirect Simplified Chinese browser to china site -----*/
function LangRedirect()
{
	var referrerDomain = getDomain(document.referrer);
	var userLang = (navigator.language)?navigator.language:navigator.userLanguage;		// IE:userLanguage/FF:language
	userLang = userLang.toLowerCase();
	// skip if locale isn't zh-cn
	if (userLang!='zh'&&userLang!='zh-cn'&&userLang!='zh-chs')
	{
		return;
	}
	// if locale is zh-cn
	if (referrerDomain.indexOf(CHINADOMAIN)!=-1 || referrerDomain.indexOf(CHINADOMAIN2)!=-1)
	{
		setCookie("siteloc", "etnetcomhk", 30);
		return;
	}
	else
	{
		var siteloc = getCookie("siteloc");
		if (siteloc=="")
		{
			top.location.href=CHINALINK;
			return
		}
		if (siteloc=="etnetchina")
		{
			top.location.href=CHINALINK;
			return;
		}
		if (siteloc=="etnetcomhk")
		{
			return;
		}
	}
}
/*-----  Function: go to china site -----*/
function goChinaSite()
{
	setCookie('siteloc', 'etnetchina', 365);
	top.location.href=CHINALINK;
}
/*-----  Function: show ads -----*/
function phpads_deliverActiveX(content)
{
	document.write(content);
}
function AdZone(zid)
{
	if (!document.phpAds_used) document.phpAds_used = ',';
	phpAds_random = new String (Math.random());
	phpAds_random = phpAds_random.substring(2,11);
	document.write ("<" + "script language='JavaScript' type='text/javascript' src='");
	document.write ("ht"+"tp://adsserver.etnet.com.hk/etNetAds/adjs.php?n=" + phpAds_random);
	document.write ("&amp;what=zone:"+zid+"");
	document.write ("&amp;exclude=" + document.phpAds_used);
	if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
	document.write ("'><" + "/script>");
}
function textlink_AdZone(zid) {
	if (!document.phpAds_used) document.phpAds_used = ',';
	phpAds_random = new String (Math.random());
	phpAds_random = phpAds_random.substring(2,11);
	document.write ("<" + "script language='JavaScript' type='text/javascript' src='");
	document.write ("http://adsserver.etnet.com.hk/etNetAds/adjs.php?n=" + phpAds_random);
	document.write ("&amp;what=zone:"+zid+"&amp;block=1");
	document.write ("&amp;exclude=" + document.phpAds_used);
	if (document.referrer)
		document.write ("&amp;referer=" + escape(document.referrer));
	document.write ("'><" + "/script>");
}
/*-----  Function: add bookmarks -----*/
function addBookmark(title, url)
{
	if (window.sidebar)
	{
		window.sidebar.addPanel(title, url, "");	// Mozilla Firefox Bookmark
	}
	else if(window.external)
	{
		window.external.AddFavorite(url, title);	// IE Favorite
	}
	else if(window.opera && window.print)
	{
		return true;	// Opera Hotlist
	}
}
/*-----  Function: show RTSS/IQ menu -----*/
function showMenu(divID, status)
{
	var divObj = document.getElementById(divID);
	var parentObj = divObj.parentNode;
	var frameObj = divObj+"_shimeFrame";
	try
	{
		var shimframe = document.getElementById(frameObj);
		shimframe.src="";
	} catch (err)
	{
		var shimframe = document.createElement('iframe');
		shimframe.id = frameObj;
		parentObj.appendChild(shimframe);
	}
	shimframe.frameborder = 0;
	shimframe.border = 0;
	shimframe.scrolling = 'no';
	shimframe.style.position = 'absolute';
	shimframe.style.top = 0;
	shimframe.style.left = 0;
	shimframe.style.visibility = 'hidden';
	shimframe.style.zIndex = -1;
	if (status)
	{
		divObj.style.visibility = 'visible';
		divObj.style.zIndex = 10;
		shimframe.style.top = divObj.style.top;
		shimframe.style.left = divObj.style.left;
		shimframe.style.zIndex = divObj.style.zIndex - 1;
		shimframe.style.width = parseInt(divObj.offsetWidth)+'px';
		shimframe.style.height = parseInt(divObj.offsetHeight)+'px';
		if (navigator.appName.indexOf('Microsoft Internet Explorer')<0)
		{
			shimframe.style.width = (parseInt(shimframe.style.width)-4)+'px';
			shimframe.style.height = (parseInt(divObj.offsetHeight)-4)+'px';
		}
		shimframe.style.visibility = 'visible';
	}
	else
	{
		shimframe.style.visibility = 'hidden';
		shimframe.style.zIndex = -1;
		divObj.style.visibility = 'hidden';
		divObj.style.zIndex = -1;
		parentObj.removeChild(shimframe);
	}
}
/*-----  Function: show RTSS/IQ menu -----*/
function iqLogin()
{
	var iqwin = "";
	var iqloginlink = 'http://www.etnet.com.hk/iq/iq_redirect.html';
	//var iqloginlink = 'http://202.62.221.247/iqservice/jsp/IQ_Web/Login.jsp?lang=tc&UIstyle=full';
	iqloginlink += '?';
	if (iqwin=="" || iqwin.closed)
	{
		iqwin=window.open(iqloginlink, 'IQ_Login', 'toolbar=no,scrollbars=yes,resizable=yes,width=800,height=530');
	}
	iqwin.focus();
}
/*-----  Function: form validation -----*/
function isPositiveInteger(x)
{
	result = (isNaN(x) || x < 1 || x.toString().indexOf(".") != -1)? false: true;
	return result;
}
function isPositiveNumber(x)
{
	result = (isNaN(x) || x < 0)? false: true;
	return result;
}
function trim(x)
{
	return (x.replace(/^\s+|\s+$/g, ''));
}
function alertError(type, field)
{
	errormessage =new Array('正確代號', '關鍵字');
	if (type > 0)
	{
		alert('請輸入' + errormessage[type - 1]);
		document.getElementById(field).select();
		document.getElementById(field).focus();
	}
}
function clearCommonInput()
{
	commoninput = document.getElementById("commoninput").value;
	if (commoninput == '代號' || commoninput == '關鍵字')
	{
		document.getElementById("commoninput").value = '';
	}
}
function changeCommonInput()
{
	commoninput = document.getElementById("commoninput").value;
	if (commoninput == '' || commoninput == '代號/中文/英文' || commoninput == '關鍵字')
	{
		commonselect = document.getElementById("commonselect").value;
		//document.getElementById('commoninput').style.color = '#888888';
		if (commonselect.indexOf('news_search.php') != -1)
		{
			document.getElementById('commoninput').value = '關鍵字';
			document.getElementById('commoninput').maxLength = '10';
		}
		else
		{
			document.getElementById('commoninput').value = '代號/中文/英文';
			document.getElementById('commoninput').maxLength = '5';
		}
	}

	document.getElementById("commoninput").select();
	document.getElementById("commoninput").focus();
}
function validateCommonSearch()
{
	commoninput = trim(document.getElementById('commoninput').value);
	commonselect = document.getElementById('commonselect').value;
	if ((commonselect.indexOf('news_search') == -1 && isPositiveInteger(commoninput)) || (commonselect.indexOf('news_search.php') >= 0 && commoninput != '關鍵字'))
	{
		if ( commonselect.indexOf('news_search.php') >= 0 )
		{
			var link = commonselect + '?keyword=' + commoninput;
		}
		else
		{
			var link = commonselect + '?code=' + commoninput;
		}
		document.getElementById("commonsearchform").action = link;
		return true;
	}
	else
	{
		error = (commonselect.indexOf('news_search') == -1)? 1: 2;
		alertError(error, 'commoninput');
	}
	return false;
}

function validateCommonSearch1()
{
	commoninput = trim(document.getElementById('commoninput').value);
	commonselect = document.getElementById('commonselect').value;
	if ((commonselect.indexOf('news_search') == -1 && isPositiveInteger(commoninput)) || (commonselect.indexOf('news_search.php') >= 0 && commoninput != '關鍵字'))
	{
		if ( commonselect.indexOf('news_search.php') >= 0 )
		{
			var link = commonselect + '?keyword=' + commoninput;
		}
		else
		{
			var link = commonselect + '?code=' + commoninput;
		}
		document.getElementById("commonsearchform").action = link;
		return true;
	}
	else
	{
		error = (commonselect.indexOf('news_search') == -1)? 1: 2;
		window.location="../stocks/company_search.php" ;
	}
	return false;
}


/*-----  Function: layer control -----*/
function hiddenLayers(hides, show)
{
	document.getElementById(show).style.display = "block";
	hide = hides.split(',');
	for (i=0; i<hide.length; i++)
	{
		document.getElementById(hide[i]).style.display = "none";
	}
}

function inputBoxFocus() 
{
	if (isIE6()) 
	{
		document.getElementById("commoninput").focus();
		//document.getElementById("commoninput").select();
	}
}

function isIE6() {
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	if (browser=="Microsoft Internet Explorer" && b_version.indexOf('MSIE 6')>0) 
	{
		return true;
	}
	return false;
}

function iframeSrc(objID, srcParh)
{
	var obj = document.getElementById(objID);
	if (obj.src == window.location || obj.src == "" || obj.src == null)
	{
		obj.src = srcParh;
	}
}

//Execute redirecting to china site
//LangRedirect();

/*-----  Function: refresh realtime quote page -----*/
function refreshpage(url){
	//window.location = url
	window.location = "quote.php?code=" + url
}

function reloadpage(url){
	setTimeout("refreshpage('" + url + "')", 500);
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->