/* random */
function getEle(id){
	return document.getElementById(id);
}
// LOGIN BAR FUNCTIONS
function NotLoginBar(div_element,errorMsg, currPage){
	var barStr =
		"<div id='noLoginWrapper'>" +
		"<div id='nologinmsg'>" + 
			"<div id='errormsg'>"+errorMsg+"</div>" +
			"<span class='msg'>您好，歡迎您登入系統 或 <a href=\"javascript:new_user();\" class='normalText expand_self'> 立即註冊新帳號 </a></span>" +
		"<div id='help' style='padding-left:2px;' onclick='open_help();'>Help</div>" +
		"<form id='login' class='login_area' method='POST' action='"+currPage+"'>" +
			"<div class='login_area'>" +
			"<input type='hidden' name='login' value='true'>" +
			"<div class='input_bk'>帳號:<input class='input' id='name' type='text' name='account' maxlength='50' tabindex=1></div>" +
			"<div class='input_bk'>密碼:<input class='input' id='password' type='password' name='password' maxlength='50' tabindex=2></div>" +
			"<input type='submit' tabindex=3 value='登入'>" +
		"</div></div></form></div>";
	getEle(div_element).innerHTML = barStr;
}
function LoggedInBar(div_element, login, level, current_sel){
	var barStr = 
		"<div id='loginWrapper'>" +
		"<form id='logout' name='logout' class='login_area' method=POST action='/'>" + 
			"<div class='user_title'>歡迎您！<div class='name'>" + login + "</div>" +
			"<div class='lvl_display'><a class='lvlsprite lvl_" + level + "' href='javascript:open_level(\"" + login + "\");'>Level</a></div></div>" +
			"<div class='link_area'>" + 
				"<a href='/english/courses/courses.php'>我的課程</a>" +
				"<a href='/english/account/progress.php?user=" + login + "'>我的進度</a>" +
				"<a href='/english/account/my_home.php?user=" + login + "'>我的資料</a>" +
				"<a href='/english/account/classmates.php'>同班同學</a>" +
				"<a href='#' onclick='document.forms[\"logout\"].submit();'>帳號登出</a>" +
				"<input type='hidden' name='logout' value='登出'>" +
				"<div id='help' onclick='open_help();'>Help</div>" +
			"</div>" + 
		"</div>" + 
		"</form>";
	getEle(div_element).innerHTML = barStr;
}
function new_user(){
	open_pop("/english/account/new.php",730);
}
function open_level(user){
	open_pop("/english/help/level_disp.php",800);
}
function open_help(){
	open_pop('/english/help/help_main.php', 800);
}
function open_pop(link,height){
	getEle("pop_layer").innerHTML = "<div id='popup_data'><iframe id='iframe' src='" + link + "' scrolling='no' width='100%' frameborder='0' ALLOWTRANSPARENCY='true'><p>Your browser does not support iframes.</p></iframe></div>";
	getEle("popup_bkgd").className="popup_bkgd_show";
	getEle("pop_layer").style.visibility = "visible";
	getEle("pop_layer").style.opacity = "1";
}
function hidePopup(){
	this.parent.closePopup();
}
function closePopup(){
	getEle("popup_bkgd").className="popup_bkgd_hide";
	getEle("popup_data").innerHTML = "";
}
function closePopupFreezeParent(){
	getEle("popup_bkgd").className="popup_bkgd_freeze";
	getEle("popup_data").innerHTML = "";
	var t=setTimeout("redirect(this.parent.window.location)",800);

}
function resizeIframe(){
	var height_child = getEle("iframe").contentDocument['body'].offsetHeight;
	var height_parent = document['body'].offsetHeight;
	// resized background should be AT LEAST the the size of the iframe, and should cover the ENTIRE webpage body.
	var height = Math.max(height_child, height_parent);
	getEle("iframe").style.height= height + "px";
	getEle("popup_bkgd").style.height= height + "px";
	//recurse into the previous iframe window parent
	this.parent.resizeParent(height);
}
//recursive resizing to ensure parent window can fit child window
function resizeParent(height){
	var actual = getEle("iframe").style.height.replace("px","");
	//alert("height: "+height+"iframe"+ actual);
	if(height <= actual){
		//alert("ok");
	}
	else{
		//alert("increase parent");
		getEle("iframe").style.height= height + "px";
		getEle("popup_bkgd").style.height= height + "px";
		this.parent.resizeParent(height);
	}
}
function readNews(news_id){
	open_pop("/english/News/showNews_pop.php?id="+news_id,800);
}
function open_student(login){
	open_pop("/english/account/my_home.php?user="+login+"&pop=1", 730);
}
function redirect(url){
	window.location = url;
}
