var newThreadURL = "/english/forum/newThread.php";

var ThreadTypes = [
	['一般討論',3,new Array(),"User Discussion"],
	['最新消息',0,new Array(),"News"],
	['回饋建議',2,new Array(),"Suggestions"],
	['問題回報',1,new Array(),"Bugs/Typos"]	];

var CreateThreadTypes = [
	['一般討論',3,new Array(),"User Discussion"],
	['回饋建議',2,new Array(),"Suggestions"],
	['問題回報',1,new Array(),"Bugs/Typos"]	];
var Groups = new Array();

// News Tree
Groups[0] = new Array();
Groups[0][0] = ["顯示所有", -1, new Array(),"Show All"];
Groups[0][1] = ["一般消息",0, new Array(),"Company News"];
Groups[0][2] = ["更新資訊",1, new Array(),"Feature Updates"];
Groups[0][3] = ["新的內容",2, new Array(),"New Content"];

// Bugs and Typos Tree
Groups[1] = new Array();
Groups[1][0] = ["顯示所有", -1, new Array(),"Show All"];
Groups[1][1] = ["網站問題",10, new Array(),"Website Problems"];
Groups[1][2] = ["程式問題",11, new Array(),"Software Problems"];
Groups[1][3] = ["內容問題",12, new Array(),"Content Problems"];
Groups[1][4] = ["題目問題",13, new Array(),"Question Problems"];
// Suggestions Tree
Groups[2] = new Array();
Groups[2][0] = ["顯示所有", -1, new Array(),"Show All"];
Groups[2][1] = ["內容建議", 40, new Array(),"Content Wishlist"];
Groups[2][2] = ["網站建議", 41, new Array(),"Website Suggestions"];
Groups[2][3] = ["練習程式",42, new Array(),"Software Suggestions"];
Groups[2][4] = ["內容改善",43, new Array(),"Content Change Suggestions"];
// User Discussion Tree
Groups[3] = new Array();
Groups[3][0] = ["顯示所有", -1, new Array(),"Show All"];
Groups[3][1] = ["文法/片語", 60, new Array(),"Grammar/Phrases"];
Groups[3][2] = ["聽力/發音",61, new Array(),"Listening/Pronunciation"];
Groups[3][3] = ["專業證照",62, new Array(),"Certifications"];
Groups[3][3][2][0] = ["顯示所有",-1,new Array(),"Show All"];
Groups[3][3][2][1] = ["多益",100,new Array(),"TOEIC"];
Groups[3][3][2][3] = ["雅思",101,new Array(),"IELTS"];
Groups[3][3][2][2] = ["全民英檢",102,new Array(),"GEPT"];
Groups[3][3][2][4] = ["托福",103,new Array(),"TOEFL"];
Groups[3][3][2][5] = ["劍橋博思",104,new Array(),"BULATS"];
Groups[3][4] = ["英文教材",63, new Array(),"English Resources"];
Groups[3][5] = ["英文補習班",64, new Array(),"English Cram Schools"];
Groups[3][6] = ["出國/留學",65, new Array(),"Travel/Studying Overseas"];
Groups[3][7] = ["大學/研究所",66, new Array(),"University/Graduate School"];
Groups[3][8] = ["國中/高中",67, new Array(),"Junior High/High School"];


function openSearch(groupID, login, tags, order){
	var lnk = "/english/Community/my_community.php";
	lnk += "?groupID=" + groupID + "&login="+login + "&tags="+tags +"&order="+order+"&useHiddenFeatures=1";
	window.location = lnk;
}
function populateSearch(element_obj,array_items,prevID, viewSize){
	if(viewSize == undefined) viewSize = 5;
	if(array_items == undefined)array_items = new Array();
	var i, element;
	var index=0;
	while(element_obj.length > 0) element_obj.remove(0);
	if(array_items.length == 0){
		element_obj.disabled = true;
		element_obj.click();
		element_obj.parentNode.className="sel_item hideSearchItem";
	}
	else{
		element_obj.disabled = false;
		element_obj.click();
		element_obj.parentNode.className="sel_item";
	}

	for(i=0;i<array_items.length;i++){
		element=document.createElement('option');
		element.innerHTML = array_items[i][0] + " <span class='eng_text'>" + array_items[i][3] + "</span>";
		element.value = array_items[i][1];
		if(prevID == array_items[i][1])	index = i;
		try{ element_obj.add(element,null); } // standards compliant
		catch(ex){ element_obj.add(element); } // IE only
	}
	element_obj.selectedIndex = index;
	viewSize = Math.min(viewSize, array_items.length);
	element_obj.size = viewSize;
}
function UpdateThread(threadID, group_element, tag_element){
	populateSearch(group_element, Groups[threadID],0,5);
	UpdateGroup(threadID,0,tag_element);
}
function UpdateGroup(threadID,groupIndex,tag_element){
	var array_items;
	if(Groups[threadID][groupIndex] == undefined) array_items = new Array();
	else array_items=Groups[threadID][groupIndex][2];
	
	populateSearch(tag_element,array_items,0,5);
}
function setGroupSelect(threadID, group_element, groupID, viewSize){
	populateSearch(group_element,Groups[threadID], groupID, viewSize)
}
function setTagSelect(threadID, groupIndex, tag_element, tagID, viewSize){
	var array_items;
	if(Groups[threadID][groupIndex] == undefined)array_items = new Array();
	else array_items = Groups[threadID][groupIndex][2];
	populateSearch(tag_element, array_items, tagID, viewSize);
}
///////////////////////////////////////////////////////////////////////
function UpdateThread2(threadID, group_element, tag_element){
	var targetGroup = sliceFirstElement(Groups[threadID]);
	populateNewThread(group_element, targetGroup,0,5);
	UpdateGroup2(threadID,0,tag_element);
}
function UpdateGroup2(threadID,groupIndex,tag_element){
	try{
		var targetGroup = sliceFirstElement(Groups[threadID]);
		var targetTags = sliceFirstElement(targetGroup[groupIndex][2]);
		populateNewThread(tag_element,targetTags,0,5);
	}
	catch(e){
	}
}
function setGroupSelect2(threadID, group_element, groupID){
	var group = sliceFirstElement(Groups[threadID]);
	populateNewThread(group_element,group, groupID)
}
function setTagSelect2(threadID, groupIndex, tag_element, tagID){
	var array_items;
	var group = sliceFirstElement(Groups[threadID]);
	if(group[groupIndex] == undefined)array_items = new Array();
	else{
		array_items = group[groupIndex][2];
		array_items = sliceFirstElement(array_items);
	}
	populateNewThread(tag_element, array_items, tagID);
}
function sliceFirstElement(array_item){
	if(array_item.length > 0) return array_item.slice(1);
	else return;
}
function populateNewThread(element_obj,array_items,prevID){
	if(array_items == undefined)array_items = new Array();
	var i, element;
	var index=0;
	while(element_obj.length > 0) element_obj.remove(0);
	if(array_items.length == 0) element_obj.parentNode.className="sel_item hideSearchItem";
	else element_obj.parentNode.className="sel_item";

	for(i=0;i<array_items.length;i++){
		element=document.createElement('option');
		element.innerHTML = array_items[i][0] + " <span class='eng_text'>" + array_items[i][3] + "</span>";
		element.value = array_items[i][1];
		if(prevID == array_items[i][1])	index = i;
		try{ element_obj.add(element,null); } // standards compliant
		catch(ex){ element_obj.add(element); } // IE only
	}
	element_obj.selectedIndex = index;
	//element_obj.size = 5;
}



function SearchButton(groupSel, postGroupID, areaSel, postAreaID, orderSel, postOrderID, form){	
	document.getElementById(postGroupID).value = document.getElementById(groupSel).selectedIndex;
	//document.getElementById(postAreaID).value = document.getElementById(areaSel).selectedIndex;
	document.getElementById(postOrderID).value = document.getElementById(orderSel).selectedIndex;
	document.getElementById(form).submit();
}

function createThread(form_id){
	var obj = document.getElementById(form_id);
	obj.action = newThreadURL;
	obj.submit();
}

function MenuSelector(){
	this.MenuData = new Array();
	this.SubMenuData = new Array();
	
	
	this.MenuData[0] = [ // Resumes and Applications
		["學校與工作申請","/english/resources/resume_apps.php"],
		["英文履歷","/english/resources/resumes.php"],
		["應徵函,自薦信","/english/resources/cover_letters.php"]];
	this.MenuData[1] = [ // Speeches and Presentations
		["簡報與演講","/english/resources/speech_presentations.php"],
		["英文演講","/english/resources/speeches.php"]];
	this.MenuData[2] = [
		["最佳的學習方式","/english/sub/learning_method.php","How you can help us"],
		["獨家教學軟體","/english/sub/exclusive_software.php","How you can help us"],
		["教學經驗","/english/sub/our_experience.php","How you can help us"],
		["專業教材","/english/sub/teaching_materials.php","How you can help us"],
		["龐大題庫","/english/sub/questions_db.php","How you can help us"]];
	this.MenuData[3] = [
		["關於我們","/english/aboutus/aboutus.php","How you can help us"],
		["公司簡介","/english/aboutus/aboutus.php","How you can help us"],
		["工作團隊","/english/aboutus/ourteam.php","How you can help us"],
		["合作機會","/english/aboutus/partnership.php","How you can help us"],
		["聯絡我們","/english/aboutus/contactus.php","How you can help us"]];

	this.MenuData[4] = [
		["英文家教","/english/sub/tutoring_services.php","How you can help us"],
		["家教服務","/english/sub/tutoring_services.php","How you can help us"],
		["師資與經驗","/english/sub/tutoring_exp.php","How you can help us"],
		["家教時間表","/english/sub/tutoring_time.php","How you can help us"]];
	this.ShowMenu = ShowMenu;
}
function ShowMenu(type, index){
	if(type == 'resume_apps') printMenu(this.MenuData[0],index);
	if(type == 'speech_present') printMenu(this.MenuData[1],index);
	if(type == 'emate_learning') printMenu(this.MenuData[2],index);
	if(type == 'about_us') printMenu(this.MenuData[3],index);
	if(type == 'tutoring') printMenu(this.MenuData[4],index);
}
function printMenu(dat, index){
	var str = "<div class='sb_nav'><div class='sb_navBox'></div><b>";
	if(index>=0) str += "<a class='sb_navTitle' href='" + dat[0][1] + "'>" + dat[0][0] + "</a></b>";
	else str += dat[0][0] + "</b>";
	str += "<div class='sb_navList'><ul>";
	
	for(i=1;i<dat.length;i++){
		if(i!=index) str += "<li><a href='" + dat[i][1] + "'>" + dat[i][0] + "</a></li>";
		else str += "<li class='sb_square'>" + dat[i][0] + "</li>";
	}
	str += "</ul></div><div class='sb_navBtm'></div></div>";
	document.write(str);
}


/* QUICK LINK AREA*/
var CURR_QUICK_LINK = 0;
var THREAD_ID = [3,2,0];
function PopulateQuickSearch(index, navID, linksID){
	var navStr = "";
	var nav_element = document.getElementById(navID);
	navStr += WriteNavItems(0,"Question Type",linksID);
	navStr += WriteNavItems(1,"Suggestions",linksID);
	navStr += WriteNavItems(2,"News / Info",linksID);
	nav_element.innerHTML = navStr;
	
	WriteQuickLinks(index,linksID);
}
function WriteNavItems(index,title,linksID){
	return "<div id='link_"+index+"' onclick='WriteQuickLinks("+index+",\""+linksID+"\");'>"+title+"</div>";
}
function WriteQuickLinks(index,linksID){
	var links = new Array();
	//Thread Type
	
	//Question Type
	links[0] = [
		Groups[3][1], // Grammar/Phrases
		Groups[3][2], // Listening/Pronunciation
		Groups[3][3][2][1], // TOIEC
		Groups[3][3][2][3], // IETLS
		Groups[3][3][2][2], // GEPT
		Groups[3][3][2][4], // TOEFL
		Groups[3][3][2][5], // BULATS
		// ********************************8
		Groups[3][4], // English Resources
		Groups[3][5], // Cram Schools
		Groups[3][6], // Travel / Study Overseas
		Groups[3][7], // Graduate School
		Groups[3][8], // Grade School
		["顯示所有", -1, new Array(),"Show All"]
	];
	//Suggestions
	links[1] =  [
		Groups[2][1], // Wishlist
		Groups[2][2], // Website Suggestion
		Groups[2][3], // Software Suggestions
		Groups[2][4], // Content Suggestions
		["顯示所有", -1, new Array(),"Show All"]
	];
	//News / Info
	links[2] = [
		Groups[0][1], // Company News
		Groups[0][2], // New Features
		Groups[0][3], // New Content
		["顯示所有", -1, new Array(),"Show All"]
	];
	var items = links[index];
	var str = "<div class='link_area'>";
	// XXXXX IF the search includes USER search, then add additional parameters to QuickSearch XXXXX
	for(var i=0;i<items.length;i++){
		str += "<div onclick='QuickSearch("+index+","+items[i][1]+")'>"+items[i][0]+"</div>"
	}
	str += "</div>";
	document.getElementById(linksID).innerHTML = str;
	document.getElementById("link_"+CURR_QUICK_LINK).className = "";
	document.getElementById("link_"+index).className = "navSel";
	CURR_QUICK_LINK = index;
}
function QuickSearch(threadType,groupType,login){
	if(login == undefined) login = "";
	var location = "http://www.e-mate.com.tw/english/Community/my_community.php";
	location += "?threadType="+THREAD_ID[threadType];
	location += "&groupeType="+groupType;
	location += "&ordering=0" + "&useHiddenFeatures=0";
	location += "&login=" + login;
	window.location = location;
	
}
