// JavaScript Document

// quot related

function getWeekNum() {
	var tSiteStartDate = new Date();
	tSiteStartDate.setFullYear(2006);
	tSiteStartDate.setMonth(9) // means Oct
	tSiteStartDate.setDate(1);
	tSiteStartDate.setHours(9);
	tSiteStartDate.setMinutes(0);
	tSiteStartDate.setSeconds(0);
	var tToday = new Date();
	var tD = Math.floor((tToday.getTime() - tSiteStartDate.getTime()) / 1000 / 86400 / 7) % 53;
	return tD;
}

// forum related

function initForum() {
	var tDateObj = new Date();
	gebi("formPostMsg").onsubmit = function() {
		forumResetLabel();
		return forumCheckInput();
	}
}
function forumResetLabel() {
	gebi("label_name").style.color = "black";
	gebi("label_email").style.color = "black";
	gebi("label_subject").style.color = "black";
	gebi("label_message").style.color = "black";
}
function forumCheckInput() {
	var tMsg = "";
	var eName = gebi("name");
	var eEmail = gebi("email");
	var eSubject = gebi("subject");
	var eMessage = gebi("message");
	if (eName.value == "") {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "姓名";
		gebi("label_name").style.color = "red";
	}
	var tIndex1 = eEmail.value.indexOf("@");
	var tIndex2 = eEmail.value.indexOf(".");
	if (eEmail.value == "") {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "電郵地址";
		gebi("label_email").style.color = "red";
	} else if (tIndex1 < 1 || tIndex2 < 1) {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "正確的電郵地址";
		gebi("label_email").style.color = "red";
	}
	if (eSubject.value  == "") {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "主題";
		gebi("label_subject").style.color = "red";
	}
	if (eMessage.value  == "") {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "留言";
		gebi("label_message").style.color = "red";
	}
	if (tMsg != "") {
		tMsg = "請輸入"+ tMsg + "!";
		alert(tMsg);
	}
	if (tMsg != "") {
		return false;
	} else {
		return true;
	}
}

// contact related

function initContact() {
	var req = new AX();
	req.onPostDone = function() {
		var tMsg = req.getXML().getElementsByTagName("message")[0];
		var tError = tMsg.getAttribute("error");
		var e = gebi("updateContent");
		if (tError == "ok") {
			e.innerHTML = '<p align="center"><br><br><br>你的訊息已寄出，多謝你的意見。<br><br><br><br><br></p>';
		} else {
			e.innerHTML = '<p align="center"><br><br><br>'+tError+'<br><br><br><br><br></p>';
		}
		//alert(req.getText());
	}
	gebi("form").onsubmit = function() {
		contactResetLabel();
		if (contactCheckInput()) {
			contactDisableForm();
			var tPrompt = ce("p");
			tPrompt.style.color = "red";
			tPrompt.innerHTML = "資料傳送中，請候..."
			var e = gebi("updateContent");
			e.appendChild(tPrompt);
			req.post("form", "contactHandler.asp");
		}
		return false;
	}
}

function contactResetLabel() {
	gebi("label_name").style.color = "black";
	gebi("label_sex_m").style.color = "black";
	gebi("label_sex_f").style.color = "black";
	gebi("label_contactTel").style.color = "black";
	gebi("label_email").style.color = "black";
	gebi("label_schoolName").style.color = "black";
	gebi("label_comment").style.color = "black";
}
function contactDisableForm() {
	gebi("form_name").disabled = true;
	gebi("form_sex_m").disabled = true;
	gebi("form_sex_f").disabled = true;
	gebi("form_contactTel").disabled = true;
	gebi("form_email").disabled = true;
	gebi("form_schoolName").disabled = true;
	gebi("form_comment").disabled = true;
	gebi("confirm_btn").disabled = true;
	gebi("reset_btn").disabled = true;
}
function contactCheckInput() {
	var tMsg = "";
	var eName = gebi("form_name");
	var eSex_m = gebi("form_sex_m");
	var eSex_f = gebi("form_sex_f");
	var eContactTel = gebi("form_contactTel");
	var eEmail = gebi("form_email");
	var eSchoolName = gebi("form_schoolName");
	var eComment = gebi("form_comment");
	if (eName.value == "") {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "姓名";
		gebi("label_name").style.color = "red";
	}
	if (!eSex_m.checked && !eSex_f.checked) {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "姓別";
		gebi("label_sex_m").style.color = "red";
		gebi("label_sex_f").style.color = "red";
	}
	if (eContactTel.value  == "") {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "聯絡電話";
		gebi("label_contactTel").style.color = "red";
	}
	var tIndex1 = eEmail.value.indexOf("@");
	var tIndex2 = eEmail.value.indexOf(".");
	if (eEmail.value == "") {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "電郵地址";
		gebi("label_email").style.color = "red";
	} else if (tIndex1 < 1 || tIndex2 < 1) {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "正確的電郵地址";
		gebi("label_email").style.color = "red";
	}
	if (eSchoolName.value  == "") {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "所屬學校名稱";
		gebi("label_schoolName").style.color = "red";
	}
	if (eComment.value  == "") {
		if (tMsg!="") {tMsg += "、"};
		tMsg += "意見或問題";
		gebi("label_comment").style.color = "red";
	}
	if (tMsg != "") {
		tMsg = "請輸入"+ tMsg + "!";
		alert(tMsg);
	}
	if (tMsg != "") {
		return false;
	} else {
		return true;
	}
}

// material related

function materialMenu(selObj){
  self.location=selObj.options[selObj.selectedIndex].value;
}
function initMaterial(aSelectedIndex) {
	var e = gebi("materialChooser");
	e.selectedIndex = aSelectedIndex;
}

// test related

testType = "mc";
testAns = new Array();
testOptionList = ["a", "b", "c", "d", "e"];
testResult = new Array();
nofOption = 3;
function checkTest() {
	var e = gebi("form").elements;
	if (testType == "mc") {
		for (var i=1; i<=10; i++) {
			for (var j=0; j< nofOption; j++) {
				var tOption = testOptionList[j]
				var tAnsEle = e["q"+i+"_"+tOption];
				if (tAnsEle.checked) {
					var corrAns = testAns[i-1];
					if (tOption == corrAns) {
						testResult[i-1] = true;
					} else {
						testResult[i-1] = false;
					}
				}
			}
		}
	} else if(testType == "fb") {
		for (var i=1; i<=10; i++) {
			if (nofEntry == 2) {
				var corrAnsList = testAns[i-1].split(",");
				var part1 = e["q"+i+"_1"].value;
				var part2 = e["q"+i+"_2"].value;
				if (part1 == "" &&  part2 == "") {
					testResult[i-1] = undefined;
				} else if (part1 == corrAnsList[0] && part2 == corrAnsList[1]) {
					testResult[i-1] = true;
				} else {
					testResult[i-1] = false;
				}
			} else {
				if (e["q"+i].value == "") {
					testResult[i-1] = undefined;
				} else if (e["q"+i].value == testAns[i-1]) {
					testResult[i-1] = true;
				} else {
					testResult[i-1] = false;
				}
			}
		}
	}
}
function hideInput() {
	var e = document.getElementsByTagName("input");
	for (var i=0; i<e.length; i++) { 
		if (e[i].className == "singleLine") {
			e[i].disabled = true;
			e[i].style.borderWidth = 0;
			e[i].style.backgroundColor = "transparent";
		} else {
			e[i].style.display = "none";
		}
	}
}
function showCorrAns() {
	var quList = gebi("questionList").getElementsByTagName("li");
	for (var i=0; i<quList.length; i++) {
		var eCorrAns = quList[i].getElementsByTagName("p")[1];
		eCorrAns.style.display = "block";
		if (testType == "mc") {
			eCorrAns.innerHTML = "正確答案：<b>"+testAns[i].toUpperCase()+"</b>";
		} else {
			eCorrAns.innerHTML = "正確答案：<b>"+testAns[i]+"</b>";
		}
	}
}
function updateMsg() {
	var e = gebi("preMsg");
	e.innerHTML = '<a href="test.html"><img src="images/backTest.gif" alt="返回小測試主頁" width="139" height="32" border="0" align="right"></a><br />你的成績：';
	var e2 = gebi("postMsg");
	e2.innerHTML = '<a href="test.html"><img src="images/backTest.gif" alt="返回小測試主頁" width="139" height="32" border="0" align="right"></a>';
}
function showResult() {
	var t = gebi("testResult");
	t.style.display = "block";
	for (var i = 0; i < 10; i++) {
		var e = gebi("resu"+(i+1));
		if (testResult[i] == undefined) {
			e.innerHTML = ""+(i+1)+". 沒有作答";
		} else if (testResult[i] == true) {
			e.innerHTML = ""+(i+1)+". 答對";
			e.className = "resultCorr";
		} else if (testResult[i] == false) {
			e.innerHTML = ""+(i+1)+". 答錯";
			e.className = "resultWrong";
		}
	}
}
function initTest(aTestID) {
	switch (aTestID) {
		case "s1":
			nofOption = 2;
			testAns = ["b", "b", "a", "b", "a", "b", "b", "a", "a", "b"];
			break;
		case "s2":
			nofOption = 2;
			testAns = ["a", "b", "a", "b", "b", "a", "a", "b", "a", "a"];
			break;
		case "s3":
			nofOption = 2;
			testAns = ["a", "b", "b", "a", "b", "a", "b", "b", "b", "b"];
			break;
		case "s4":
			nofOption = 4;
			testAns = ["a", "c", "d", "c", "d", "a", "a", "b", "b", "c"];
			break;
		case "s5":
			nofOption = 4;
			testAns = ["c", "b", "d", "a", "b", "c", "d", "b", "c", "b"];
			break;
		case "s6":
			nofOption = 2;
			testAns = ["a", "b", "b", "a", "a", "b", "a", "a", "b", "a"];
			break;
		case "p1":
			nofEntry = 2;
			testType = "fb";
			testAns = ["g,j", "d,r", "h,f", "ch,q", "k,z", "sh,h", "x,s", "zh,d", "c,d", "s,c"];
			break;
		case "p2":
			nofOption = 4;
			testAns = ["b", "a", "b", "d", "a", "c", "d", "d", "d", "c"];
			break;
		case "p3":
			nofOption = 4;
			testAns = ["d", "a", "a", "b", "c", "c", "a", "a", "a", "b"];
			break;
		case "p4":
			nofOption = 4;
			testAns = ["b", "b", "d", "d", "c", "c", "b", "a", "b", "a"];
			break;
		case "p5":
			nofEntry = 1;
			testType = "fb";
			testAns = ["zh", "ch", "sh", "ch", "z", "c", "r", "zh", "r", "s"];
			break;
		case "p6":
			nofOption = 4;
			testAns = ["b", "c", "a", "c", "d", "b", "c", "d", "c", "a"];
			break;
		case "wp1":
			nofOption = 4;
			testAns = ["c", "b", "b", "b", "a", "c", "c", "d", "a", "b"];
			break;
		case "wp2":
			nofOption = 4;
			testAns = ["c", "b", "d", "a", "b", "c", "c", "d", "a", "b"];
			break;
		case "wp3":
			nofOption = 4;
			testAns = ["a", "b", "b", "c", "d", "b", "b", "b", "b", "c"];
			break;
		case "wp4":
			nofOption = 4;
			testAns = ["c", "b", "d", "a", "b", "c", "a", "d", "d", "b"];
			break;
		case "wp5":
			nofOption = 4;
			testAns = ["d", "b", "c", "b", "a", "c", "a", "d", "b", "d"];
			break;
		case "wp6":
			nofOption = 4;
			testAns = ["c", "b", "d", "a", "d", "c", "c", "b", "d", "b"];
			break;
		case "kp1":
			nofOption = 3;
			testAns = ["b", "c", "b", "a", "c", "b", "c", "a", "b", "c"];
			break;
		case "kp1":
			nofOption = 3;
			testAns = ["b", "c", "b", "a", "c", "b", "c", "a", "b", "c"];
			break;
		case "kp2":
			nofOption = 3;
			testAns = ["b", "c", "b", "a", "b", "c", "b", "c", "c", "a"];
			break;
		case "kp3":
			nofOption = 3;
			testAns = ["a", "c", "b", "a", "c", "b", "c", "b", "a", "b"];
			break;
		case "kp4":
			nofOption = 3;
			testAns = ["b", "c", "a", "a", "b", "c", "b", "c", "a", "c"];
			break;
		case "kp5":
			nofOption = 3;
			testAns = ["b", "b", "c", "a", "c", "c", "b", "c", "a", "b"];
			break;
		case "kp6":
			nofOption = 3;
			testAns = ["b", "a", "c", "c", "a", "b", "c", "c", "b", "a"];
			break;
	}
	gebi("btnCheck").onclick = function() {
		window.scroll(0, 0);
		hideInput();
		checkTest();
		updateMsg();
		showResult();
		showCorrAns();
	};
	
}

// general usage

function popUpWin(aPopUp) {
	var winList = new Array();
	winList.push(['legal', 'http://www.ilongman.com/eng/legal.asp', 'legalpoppup','width=500,height=450']);
	winList.push(['privacy', 'http://www.ilongman.com/eng/privacy.asp', 'privacypoppup','width=500,height=450']);
	winList.push(['infringement', 'http://www.ilongman.com/eng/infringement.asp', 'infringementpoppup','width=500,height=450']);
	winList.push(['disclaimer', 'http://www.ilongman.com/eng/disclaimer.asp', 'disclaimerpoppup','width=500,height=450']);
	winList.push(['sitemap', 'http://www.ilongman.com/eng/sitemap/index.asp', 'popupsitemap','width=631,height=395']);
	for (var i=0; i<winList.length; i++) {
		var tR = winList[i];
		if (aPopUp == tR[0]) {
			window.open(tR[1],tR[2],tR[3]);
		}
	}
}

function navGo(aElement) {
	resetMenuHilite();
	var tSectionName = aElement.id.split("_")[1];
	if (tSectionName == "resource") {
		window.parent.mainFrame.location.href = tSectionName+"_p1a.asp";
	} else if (tSectionName == "forum") {
			window.parent.mainFrame.location.href = tSectionName+".asp";
	} else if (tSectionName == "extend") {
		window.parent.mainFrame.location.href = tSectionName+"_p1a.html";
	} else if (tSectionName == "quot") {
		window.parent.mainFrame.location.href = tSectionName+"_w"+getWeekNum()+".html";
	} else {
		window.parent.mainFrame.location.href = tSectionName+".html";
	}
}
function headGo(aElement) {
	var tSectionName = aElement.className;
	if (tSectionName != "") {
		if (tSectionName == "resource") {
			window.parent.mainFrame.location.href = tSectionName+"_p1a.asp";
		} else if (tSectionName == "forum") {
			window.parent.mainFrame.location.href = tSectionName+".asp";
		} else if (tSectionName == "extend") {
		window.parent.mainFrame.location.href = tSectionName+"_p1a.html";
	} else if (tSectionName == "quot") {
		window.parent.mainFrame.location.href = tSectionName+"_w"+getWeekNum()+".html";
		} else {
			window.parent.mainFrame.location.href = tSectionName+".html";
		}
	}
}
function initHead() {
	var tSectionName = URLResolver.getPara("sect");
	if (tSectionName != "") {
		var tElSectionHead = gebi("sectionHead");
		tElSectionHead.className = tSectionName;
	}
}
function initNav() {
	var tSectionName = URLResolver.getPara("sect");
	if (tSectionName != "") {
		var tElMenuHilite = gebi("nav_"+tSectionName);
		if (tElMenuHilite) {
			tElMenuHilite.className = "hilite";
		}
	}
}
function initMain(aGoDefault) {
	var tName = URLResolver.getFilenameBody();
	var tSectionName = tName.split("_")[0];
	var tPageName = tName.split("_")[1];
	if (aGoDefault != undefined) {
		window.self.location.href = tSectionName+"_"+aGoDefault+".html";
	} else {
		updateSection(tSectionName);
	}
}
function initGo(aDefaultSection) {
	var tSectionName = URLResolver.getPara("go");
	if (tSectionName == "") {
		tSectionName = aDefaultSection;
	}
	window.self.location.href = tSectionName+".html";
}

function updateSection(aSectionName) {
	//alert("updateSection : "+aSectionName);
	if (window.parent.topFrame) {
		var tElSectionHead = window.parent.topFrame.document.getElementById("sectionHead");
		if (tElSectionHead) {
			tElSectionHead.className = aSectionName;
		}
	}
	resetMenuHilite();
	if (window.parent.leftFrame) {
		var tElMenuHilite = window.parent.leftFrame.document.getElementById("nav_"+aSectionName);
		if (tElMenuHilite) {
			tElMenuHilite.className = "hilite";
		}
	}
}
function resetMenuHilite() {
	if (window.parent.leftFrame) {
		if (window.parent.leftFrame.document) {
			var tLinkList = window.parent.leftFrame.document.getElementsByTagName("a");
			for (var i = 0; i < tLinkList.length; i++) {
				tLinkList[i].className = "";
			}
		}
	}
}

/// js patching
fixArray();
fixXMLHttpRequest();

// add indexOf in Array
function fixArray() {
	a = ["aa"];
	try {
		b = a.indexOf("aa");
	} catch(e) {
		Array.prototype.indexOf = function(aObj) {
			for (var i=0; i<this.length; i++) {
				if (this[i] == aObj) {
					return i;
				}
			}
			return -1;
		}
	}
}

// add XMLHttpRequest
function fixXMLHttpRequest() {
	if (!window.XMLHttpRequest) {
		window.XMLHttpRequest = function() {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					return null;
				}
			}
		}
    }
}

//URL class

function URLResolver() {};

URLResolver.getPara = function(aParaName) {
	var tQueryA = window.parent.location.href.split("?");
	if (tQueryA.length > 1 && tQueryA[1] != "") {
		var tValuePairA = tQueryA[1].split("&");
		for (var i=0; i<tValuePairA.length; i++) {
			var tVP = tValuePairA[i].split("=");
			if (tVP.length > 1) {
				if (tVP[0] == aParaName) {
					return tVP[1];
				}
			}
		}		
	}
	return "";
}

URLResolver.getFilenameBody = function(aURL) {
	var t = URLResolver.getFilename(aURL);
	var tLocD = t.lastIndexOf(".");
	if (tLocD == -1) {
		return t;
	} else if (tLocD == 0) {
		return "";
	} else {
		return t.substring(0, tLocD);
	}
}
URLResolver.getFilenameExt = function(aURL) {
	var t = URLResolver.getFilename(aURL);
	var tLocD = t.lastIndexOf(".");
	if (tLocD == -1) {
		return "";
	} else {
		return t.substring(tLocD+1);
	}
}
URLResolver.getFilename = function(aURL) {
	if (aURL == undefined) {
		var t = document.URL.split("?")[0].split("#")[0];
	} else {
		var t = aURL.split("?")[0].split("#")[0];
	}
	var tLocS = t.lastIndexOf("/");
	if (tLocS >= 0) {
		var s2 = t.substring(tLocS+1);
		var tLocBS = s2.lastIndexOf("\\");
		if (tLocBS >= 0) {
			return s2.substring(tLocBS+1);
		} else {
			return s2;
		}
	} else {
		return t
	}
}	

// XMLWrapper

function AX() {
	this.request;
	this.status = "";
	this.onLoadDone = function() {};
	this.onPostDone = function() {};
	this.request = new XMLHttpRequest();
}
AX.prototype.getXML = function() {
	if (this.status == "done") {
		return this.request.responseXML;
	}
}
AX.prototype.getText = function() {
	if (this.status == "done") {
		return this.request.responseText;
	}
}
AX.prototype.load = function(aURL) {
	this.status = "loading";
	var ts = this;
	this.request.open("GET", aURL, true);
	this.request.onreadystatechange = function() {
		if (ts.isStatusOK()) {
			ts.status = "done";
			ts.onLoadDone();
		} else {
			ts.status = "error";
		}
	}
	this.request.send(null);
}
AX.prototype.post = function(aFormID, aURL) {
	this.status = "loading";
	var ts = this;
	this.request.open("POST", aURL, true);
	this.request.onreadystatechange = function() {
		if (ts.isStatusOK()) {
			ts.status = "done";
			ts.onPostDone();
		} else {
			ts.status = "error";
		}
	}
	this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.request.send(getFormSerial(aFormID));
}
AX.prototype.isStatusOK = function() {
	if (this.request.readyState == 4) {
		if (this.request.status == 200 || this.request.status == 0 || this.request.status == undefined) {
			if (this.request.responseText != null) {
				return true;
			}
		}
	}
	return false;
}

function getRandomString() {
	return "s"+Math.floor(Math.random()*(99999999+1));
}

// global functions

function getFilenameObj() {
	var s = URLResolver.getFilenameBody();
	var tPos = s.lastIndexOf('_');
	if (tPos != -1) {
		var tLang = s.substr(tPos+1, 2);
		if (tLang != "tc" && tLang != "en" && tLang != "sc") {
			tLang = "";
		}
		var tLocation = s.substring(0, tPos)
	} else {
		var tLang = "";
		var tLocation = s;
	}
	var tLocA = tLocation.split("_");
	var tS = tLocA[0];
	if (tLocA.length > 1) {
		var tSub = tLocA[1];
	} else {
		var tSub = "";
	}
	return {location:tLocation, section:tS, subSection:tSub, lang:tLang};
}
// getElementById shortcut
function gebi(aId) {
	return document.getElementById(aId);
}
function ce(aTagName) {
	return document.createElement(aTagName);
}
function ctn(aText) {
	return document.createTextNode(aText);
}
function br() {
	return document.createElement("br");
}

function updateElement(aId, aInnerHTML) {
	var tDiv = gebi(aId);
	tDiv.innerHTML = aInnerHTML;
}

function getFormSerial(aFormID) {
	var tForm = gebi(aFormID);
	var c = tForm.elements.length;
	var tResult = "";
	for (var i=0; i<c; i++) {
		var tName = tForm.elements[i].name;
		var tType = tForm.elements[i].type;
		if ((tType == "radio" && tForm.elements[i].checked) || tType != "radio") {
			var tValue = encodeURIComponent(tForm.elements[i].value);
			if (i != 0) {
				tResult += "&";
			}
			tResult += tName + "=" + tValue;
		}
	}
	return tResult;
}

