﻿var lang = new Array();
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
var is_ie6 = (userAgent.indexOf('msie 6') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

function $2(id) {
	return document.getElementById(id);
}

function ucwords(str){
	var new_string;
	if (str.length > 0){
		var lower = str.toLowerCase();
		var lower_array = lower.split(' ');
		var new_array = new Array();
		for (var i in lower_array){
			temp = lower_array[i];
			new_array.push(temp.charAt(0).toUpperCase() + temp.substring(1,temp.length) )
		}
		new_string = new_array.join(" ");
	}
	return new_string;
}

function string_cut(str,num){
	var new_string = str;
	if (str.length > num+3){
		new_string = str.substring(0,num+1) + "..";
	}
	return new_string;
}
function string_replace(str,before,after){
	str = str.replace(new RegExp(before, "gi"), after);
	//str.replace(/microsoft/gi, "W3Schools"));
	return str;	
}
function string_highlight(str,key){
	var pa = new RegExp("("+key+")","ig");
	str = str.replace(pa,"<span class='red'>$1</span>");
	return str;
}
function reinitIframe(plus){	// iframe reinit
	try{
		var iframe = window.top.document.getElementById("content_iframe");
		var menu_td = window.top.document.getElementById("menu_td");
		
		var bHeight = document.body.scrollHeight;
		bHeight = bHeight < 300 ? 300 : bHeight + plus;
		iframe.height =  bHeight;	// iframe
		menu_td.height =  bHeight - 4;
		//menu_td.height = bHeight;	// menu
		window.top.document.title = document.title;
	}catch (ex){}
}
function checkall(form, prefix, checkall) {
	var checkall = checkall ? checkall : 'chkall';
	for(var i = 0; i < form.elements.length; i++) {
		var e = form.elements[i];
		if(e.name && e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
			e.checked = form.elements[checkall].checked;
		}
	}
}


function fetchCheckbox(cbn) {
	return $2(cbn) && $2(cbn).checked == true ? 1 : 0;
}

function getcookie(name) {
	var cookie_start = document.cookie.indexOf(name);
	var cookie_end = document.cookie.indexOf(";", cookie_start);
	return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}

function min(var1,var2){
	if (var1>var2){
		return var2;
	}else{
		return var1;
	}
}

function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
					return true;
			}
		}
	}
	return false;
}

function setcopy(text, alertmsg){
	if(is_ie) {
		clipboardData.setData('Text', text);
		alert(alertmsg);
	} else if(prompt('Press Ctrl+C Copy to Clipboard', text)) {
		alert(alertmsg);
	}
}

function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

function mb_strlen(str) {
	var len = 0;
	for(var i = 0; i < str.length; i++) {
		len += str.charCodeAt(i) < 0 || str.charCodeAt(i) > 255 ? (charset == 'utf-8' ? 3 : 2) : 1;
	}
	return len;
}

function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
	var expires = new Date();
	expires.setTime(expires.getTime() + seconds);
	document.cookie = escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '/')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
}

function strlen(str) {
	return (is_ie && str.indexOf('\n') != -1) ? str.replace(/\r?\n/g, '_').length : str.length;
}

function trim(str) {
	return (str + '').replace(/(\s+)$/g, '').replace(/^\s+/g, '');
}

function _attachEvent(obj, evt, func) {
	if(obj.addEventListener) {
		obj.addEventListener(evt, func, false);
	} else if(obj.attachEvent) {
		obj.attachEvent("on" + evt, func);
	}
}


var Ajaxs = new Array();
var AjaxStacks = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
var attackevasive = isUndefined(attackevasive) ? 0 : attackevasive;
function Ajax2(recvType, waitId) {

	for(var stackId = 0; stackId < AjaxStacks.length && AjaxStacks[stackId] != 0; stackId++);
	AjaxStacks[stackId] = 1;

	var aj = new Object();

	aj.loading = 'Loading...';//public
	aj.recvType = recvType ? recvType : 'XML';//public
	aj.waitId = waitId ? $(waitId) : null;//public

	aj.resultHandle = null;//private
	aj.sendString = '';//private
	aj.targetUrl = '';//private
	aj.stackId = 0;
	aj.stackId = stackId;

	aj.setLoading = function(loading) {
		if(typeof loading !== 'undefined' && loading !== null) aj.loading = loading;
	}

	aj.setRecvType = function(recvtype) {
		aj.recvType = recvtype;
	}

	aj.setWaitId = function(waitid) {
		aj.waitId = typeof waitid == 'object' ? waitid : $(waitid);
	}

	aj.createXMLHttpRequest = function() {
		var request = false;
		if(window.XMLHttpRequest) {
			request = new XMLHttpRequest();
			if(request.overrideMimeType) {
				request.overrideMimeType('text/xml');
			}
		} else if(window.ActiveXObject) {
			var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
			for(var i=0; i<versions.length; i++) {
				try {
					request = new ActiveXObject(versions[i]);
					if(request) {
						return request;
					}
				} catch(e) {}
			}
		}
		return request;
	}

	aj.XMLHttpRequest = aj.createXMLHttpRequest();
	aj.showLoading = function() {
		if(aj.waitId && (aj.XMLHttpRequest.readyState != 4 || aj.XMLHttpRequest.status != 200)) {
			changedisplay(aj.waitId, '');
			aj.waitId.innerHTML = '<span><img src="' + IMGDIR + '/loading.gif"> ' + aj.loading + '</span>';
		}
	}

	aj.processHandle = function() {
		if(aj.XMLHttpRequest.readyState == 4 && aj.XMLHttpRequest.status == 200) {
			for(k in Ajaxs) {
				if(Ajaxs[k] == aj.targetUrl) {
					Ajaxs[k] = null;
				}
			}
			if(aj.waitId) changedisplay(aj.waitId, 'none');
			if(aj.recvType == 'HTML') {
				aj.resultHandle(aj.XMLHttpRequest.responseText);
			} else if(aj.recvType == 'XML') {
				aj.resultHandle(aj.XMLHttpRequest.responseXML);
			// 	aj.resultHandle(aj.XMLHttpRequest.responseXML.lastChild.firstChild.nodeValue, aj);				
			}
			AjaxStacks[aj.stackId] = 0;
		}else if(aj.XMLHttpRequest.readyState == 4) {
			alert("System Error!!  Status: "+ aj.XMLHttpRequest.status);
		}
	}

	aj.get = function(targetUrl, resultHandle) {
		//setTimeout(function(){aj.showLoading()}, 500);
		if(in_array(targetUrl, Ajaxs)) {
			return false;
		} else {
			Ajaxs.push(targetUrl);
		}
		aj.targetUrl = targetUrl;
		aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
		aj.resultHandle = resultHandle;
		var delay = attackevasive & 1 ? (aj.stackId + 1) * 1001 : 100;
		if(window.XMLHttpRequest) {
			setTimeout(function(){
			aj.XMLHttpRequest.open('GET', aj.targetUrl);
			aj.XMLHttpRequest.send(null);}, delay);
		} else {
			setTimeout(function(){
			aj.XMLHttpRequest.open("GET", targetUrl, true);
			aj.XMLHttpRequest.send();}, delay);
		}

	}
	aj.post = function(targetUrl, sendString, resultHandle) {
		//setTimeout(function(){aj.showLoading()}, 500);
		if(in_array(targetUrl, Ajaxs)) {
			return false;
		} else {
			Ajaxs.push(targetUrl);
		}
		aj.targetUrl = targetUrl;
		aj.sendString = sendString;
		aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
		aj.resultHandle = resultHandle;
		aj.XMLHttpRequest.open('POST', targetUrl);
		aj.XMLHttpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		aj.XMLHttpRequest.send(aj.sendString);
	}
	return aj;
}

function changedisplay(obj, display) {
	if(display == 'auto') {
		obj.style.display = obj.style.display == '' ? 'none' : '';
	} else {
		obj.style.display = display;
	}
	return false;
}


function func_htmlspecialchars(string, quote_style) {
	string = string.toString();
	string = string.replace(/&/g, '&amp;');
	string = string.replace(/</g, '&lt;');
	string = string.replace(/>/g, '&gt;');
	if (quote_style == 'ENT_QUOTES') {
		string = string.replace(/"/g, '&quot;');
		string = string.replace(/\'/g, '&#039;');
	} else if (quote_style != 'ENT_NOQUOTES') {
		 string = string.replace(/"/g, '&quot;');
	} 
	return string;
}

function func_htmlspecialchars_decode(string, quote_style) {
	string = string.toString();
	string = string.replace(/&amp;/g, '&'); 
	string = string.replace(/&lt;/g, '<'); 
	string = string.replace(/&gt;/g, '>');  
	if (quote_style == 'ENT_QUOTES') {  
		string = string.replace(/&quot;/g, '"');   
		string = string.replace(/&#039;/g, '\'');  
	} else if (quote_style != 'ENT_NOQUOTES') { 
		string = string.replace(/&quot;/g, '"');
	} 
	return string;
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getInputValue(name){
	return $("input[@name='" + name + "'][@checked]").val();
}
function setInputValue(name,value){
	$("input[@name='" + name + "']").val([value]);
}

function findPos(obj) {
	var curleft = curtop = 0;
	var curWidth = obj.clientWidth;
	var curHeight = obj.clientHeight;
	if (obj.offsetParent) {
		do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	var array = new Array(curleft,curtop,curWidth,curHeight);
	return array;
}


function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	if (is_ie){
		pageWidth = windowWidth;
	}else{
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};


function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
		//alert(1);
	} else if (document.documentElement.scrollLeft || document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
		//alert(2);
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
		//alert(3);
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

function getTagV(obj,name){
	try {
	return obj.getElementsByTagName(name)[0].firstChild.nodeValue;
	} catch(e) {};
}
function getTag(obj,name){
	try {
	return obj.getElementsByTagName(name)[0];
	} catch(e) {};
}
function getTags(obj,name){
	try {
	return obj.getElementsByTagName(name);
	} catch(e) {};
}
function SSroom_code(room_code){
	return Math.floor(room_code.substring(0,1)) < 10 ? "S" + room_code : room_code;
}



//   ************* class_to_chinese ************

var chinese_word = new Array();
chinese_word[80] = "小";		// P
chinese_word[83] = "中";		// S

chinese_word[49] = "一";		// 1
chinese_word[50] = "二";		// 2
chinese_word[51] = "三";		// 3
chinese_word[52] = "四";		// 4
chinese_word[53] = "五";		// 5
chinese_word[54] = "六";		// 6
chinese_word[55] = "七";		// 7

chinese_word[65] = "甲";		// A
chinese_word[66] = "乙";		// B
chinese_word[67] = "丙";		// C
chinese_word[68] = "丁";		// D
chinese_word[69] = "戌";		// E

var chinese_word2 = new Array();	// Sixth form
chinese_word2[65] = "文";		// 文
chinese_word2[83] = "理";		// 理

function class_to_chinese(class_name){
	var return_string = "";
	for(var i=0;i<class_name.length;i++){
		var code = class_name.charCodeAt(i);
		if 	((i==2)&(class_name.charCodeAt(0)==83)&(class_name.charCodeAt(1)>53)){
			return_string += chinese_word2[code];
		}else{
			return_string += chinese_word[code];
		}
	}
	return return_string;
}

//   ************* class_to_chinese ************


var start_time
function time_start(){
	start_time = new Date();
}
function time_end(){
	var end_time = new Date();
	alert(end_time.getTime()-start_time.getTime());	
}
/*
 *@ url: 需求請求的 url
 *@ id : 顯示的 id
 *@ waitid: 等待的 id，默認為顯示的 id，如果 waitid 為空字符串，則不顯示 loading...， 如果為 null，則在 showid 區域顯示
 *@ linkid: 是哪個鏈接觸發的該 ajax 請求，該對象的屬性(如 ajaxdisplay)保存了一些 ajax 請求過程需要的數據。
*/

