/* author: Chan Bernard Ki Hong */
/* Last Modified date: 28/04/2005 (Fixed some errors with the original script) */
// This script is the sole intellectual property of the captioned
// author. Unauthorized use of this script is, under all circumstances,
// prohibited.

// Initialization
function Initialization() {
	DetectBrowser();	// Detect browser model using
	DetectBrowserVersion();	// Detect browser version/language using
	// CheckForOptimumView();	// check if homepage can be viewed in an optimum way
	CompleteVars();	// issues a series of "constants" based on browser detection results
	/* var elem = document.getElementById("Banner");
	elem.removeChild(elem.firstChild); */
}

// Detect the browser type using
// Possible Values:
// 0 - Microsoft Internet Explorer
// 1 - Netscape
// 2 - Opera
// 3 - Konqueror
// 4 - Other
var BrowserType;
function DetectBrowser() {
	if (navigator.appName.search(/Internet Explorer/i) != -1) BrowserType = 0;
	if (navigator.appName.search(/Netscape/i) != -1) BrowserType = 1;
	if (navigator.appName.search(/Opera/i) != -1) BrowserType = 2;
	if (navigator.appName.search(/Konqueror/i) != -1) BrowserType = 3;
	if (isNaN(BrowserType)) BrowserType = 4;
}

// Detect browser version using
var BrowserVersion;	// a number indicating browser version
var BrowserLanguage = ""; // either "e" (English) or "c" (Chinese)
function DetectBrowserVersion() {
	BrowserInfo = navigator.appVersion.split(".");
	BrowserVersion = BrowserInfo[0];
	if (BrowserType == 0) {	// For IE, deducing system language is easy
		if (navigator.systemLanguage == "zh-tw" || navigator.systemLanguage == "zh-hk") {
			BrowserLanguage = "c";
		} else {
			BrowserLanguage = "e";
		}
	}
	if (BrowserType == 1) {
		// for Netscape, the 2nd part contains the browser language (but most probably fail)
		bLang = BrowserInfo[1].substring(BrowserInfo[1].indexOf(";") + 1, BrowserInfo[1].indexOf(")"));
		if (bLang.search(/zh-TW/i) != -1) { BrowserLanguage = "c"; } else { BrowserLanguage = "e"; } 
	}
	if (BrowserType == 3) {
		if (navigator.language == "zh_TW") { BrowserLanguage = "c"; } else { BrowserLanguage = "e"; }
	}
	if (BrowserLanguage == "") BrowserLanguage = "e";
}

// Check if browser is capable of viewing pages correctly
function CheckForOptimumView() {
	// This homepage is best viewed with IE 4+ and Netscape 6+
	// Opera 5.02 works somehow (although some DHTML tricks don't work)
	// More browsers are becoming optimum as time goes by ... 
	// If you are using a browser that supports CSS, Javascript as well as DOM,
	// and is not listed here, inform me by email.
	var DisplayPrompt = "";
	if (BrowserType == 0 && BrowserVersion < 4) {	// IE
		DisplayPrompt = (BrowserLanguage == "e")?
		"Your version of Internet Explorer is not recent enough for optimum display of this site.\nPlease upgrade to version 4.0 or above for optimum display.\nSee the 'Support' section for further information.":
		"你的 Internet Explorer 瀏覽器的版本太舊，可能沒法達致最佳的效果。\n請更新你的瀏覽器至版本4.0或以上以獲得最佳效果。\n請瀏覽「技術支援」取得更多相關資訊。";
	}
	if (BrowserType == 1 && BrowserVersion < 5) {	// Netscape
		DisplayPrompt = (BrowserLanguage == "e")?
		"Your version of Netscape is not recent enough for optimum display of this site.\nPlease upgrade to Netscape 6 for optimum display.\nSee the 'Support' section for further information.":
		"你的 Netscape 瀏覽器的版本太舊，可能沒法達致最佳的效果。\n請更新你的瀏覽器至版本6.0或以上以獲得最佳效果。\n請瀏覽「技術支援」取得更多相關資訊。";
	}
	if (BrowserType == 4) {		// unrecognized browsers
		DisplayPrompt = (BrowserLanguage == "e")?
		"We cannot figure out what browser you are using.\nAs a result, the site may not be displayed correctly.\nTo help us better serve you, consider reporting the problems you encounter, if any. Remember to tell us the name and version of the browser you are using.":
		"";	// ouch!! cannot deduce if Chinese version!
	}
	if (DisplayPrompt != "") alert(DisplayPrompt);
}

var isIE4up = false; var isNS6up = false; var isNS4 = false; var isOpera5up = false; var isKonqueror = false;
function CompleteVars() {
	// Here we define three convenient variables for our use.
	// Define more as necessary.
	if (BrowserType == 0 && BrowserVersion >= 4) isIE4up = true;
	if (BrowserType == 1 && BrowserVersion > 4) isNS6up = true;
	if (BrowserType == 1 && BrowserVersion == 4) isNS4 = true;
	if (BrowserType == 2 && BrowserVersion > 4) isOpera5up = true;
	if (BrowserType == 3) isKonqueror = true;		// assume
	//window.alert("isIE4up: "+ isIE4up + "; isNS6up: " + isNS6up + "; isOpera5up: " + isOpera5up);
}

var flgTTDisplayed = false;
var flgTimeoutID = 0;
var tmpObj = ""; var mode = 0;
// the real show/hide code stuff
function OnOffStuff() {
	if (isNS6up || isOpera5up || isKonqueror) {
		document.getElementById(tmpObj).style.visibility = (mode)?'visible':'hidden';
	}
	if (isIE4up) {
		document.all(tmpObj).style.visibility = (mode)?'visible':'hidden';
	}
	flgTTDisplayed = (mode)?true:false;	// Already displayed on screen?
	flgTimeoutID = 0;	// always clear TimeoutID flag
}

// show/hide a particular tooltip
function toggleToolTip(obj, e, status) {
	// set global parameters for OnOffStuff() operation
	tmpObj = "tooltip_" + obj.id;
	linkObj = "A" + obj.id;
	mode = status;
	// Internet Explorer 4+
	if (isIE4up) {
		document.all(tmpObj).style.left = 20 + document.all(obj.id).offsetLeft + e.offsetX;
		document.all(tmpObj).style.top = 80 + document.all('NavBar').offsetTop + document.all(obj.id).offsetTop + e.offsetY;
		if (status) {	// show tooltip
			flgTimeoutID = window.setTimeout(OnOffStuff, 500);
		} else {	// hide tooltip 
			if (!flgTimeoutID) OnOffStuff(); else window.clearTimeout(flgTimeoutID);
		}
	}
	// Netscape Gecko / Opera 5+
	if (isNS6up || isOpera5up || isKonqueror) {
		//document.getElementById(linkObj).style.textDecoration = document.styleSheets[0].cssRules
	
		document.getElementById(tmpObj).style.left = e.clientX + "px";
		document.getElementById(tmpObj).style.top = e.clientY + "px";

		if (status) {	// show tooltip
			document.getElementById(linkObj).style.textDecoration = "underline";
			document.getElementById(linkObj).style.color = "#FFFF00";
			flgTimeoutID = window.setTimeout(OnOffStuff, 500);
		} else {	// hide tooltip 
			document.getElementById(linkObj).style.textDecoration = "none";
			document.getElementById(linkObj).style.color = "#FFFFFF";
			if (!flgTimeoutID) OnOffStuff(); else window.clearTimeout(flgTimeoutID);
		}
	}
}

// Menu Change Phase
var bgColSelected = '#880000';
var bgColUnselected = '#330099';
function ChangePhase(obj, e, status) {
	// Show/Hide ToolTip
	toggleToolTip(obj, e, status);
	// curiously, Opera 5 doesn't work! I shall look into the matter later.
	if (isIE4up || isNS6up || isKonqueror || isOpera5up) {
		obj.style.padding = "4px";	// this is a workaround owing to some problems with Gecko
		obj.style.backgroundColor = (status)?bgColSelected:bgColUnselected;
	}
}

function RunUrl(item) {
	// Netscape Gecko or Opera 5
	// I don't have Opera versions prior to 5. If this code works for
	// previous versions, please notify me by email.
	Initialization()
	if (isOpera5up || isNS6up || isKonqueror) {
		window.location = document.getElementById("A" + item).href;
	}
	if (isIE4up) {	// IE 4+
		window.location = document.all("A" + item).href;
	}
}

// Add bookmark
function AddBookmark(linkURL, Title) {
	if (!isIE4up) {	// IE 4+
		alert((BrowserLanguage == "e")?"You don't seem to be using Microsoft Internet Explorer.\nYou will have to add the bookmark manually.":"看來你不是使用 Internet Explorer 瀏覽器。\n你必須自行把本頁加到你的書籤之上。");
	} else {
		window.external.addFavorite(linkURL, Title);
		alert((BrowserLanguage == "e")?"The site has been added to My Favorites. Thank you.":"謝謝你，本頁已經被加到『我的至愛』上了。");
	}
}

// Opens a pop-up window
function OpenWindow (linkURL, Name, features) {
	window.open(linkURL, Name, features);
}

		// Redirect according to browser
		function BrowserCheck () {
			if (isIE4up) {	// IE
				if (BrowserLanguage == "c") 
					window.location.href = "index.pl?page=index&lang=c";
				else
					window.location.href = "index.pl?page=index&lang=e";
			} else {	// Netscape, Opera and others
				window.location.href = "redirect.pl";
			}
		}

