var browser = navigator.userAgent;
var http_request = false;
var check_postresult;

if (browser.match("Firefox")=="Firefox") client = "Firefox";
else if (browser.match("MSIE")=="MSIE") client = "MSIE";
else if (browser.match("Safari")=="Safari") client = "Safari";
else client = "Other";

function md5(s){
	function Z(n,c){return(n<<c)|(n>>>(32-c))}
	function Y(q,a,b,x,s,t){return X(Z(X(X(a,q),X(x,t)),s),b)}
	function A(a,b,c,d,x,s,t){return Y((b&c)|((~b)&d),a,b,x,s,t)}
	function B(a,b,c,d,x,s,t){return Y((b&d)|(c&(~d)),a,b,x,s,t)}
	function C(a,b,c,d,x,s,t){return Y(b^c^d,a,b,x,s,t)}
	function D(a,b,c,d,x,s,t){return Y(c^(b|(~d)),a,b,x,s,t)}
	function X(x,y){var l=(x&0xFFFF)+(y&0xFFFF),m=(x>>16)+(y>>16)+(l>>16);return(m<<16)|(l&0xFFFF)}
	
	var w=s.length*8,i,hx="0123456789abcdef",L=(((w+64)>>>9)<<4)+15,x=Array(L+15);
	for(i=0;i<x.length;++i)x[i]=0;
	for(i=0;i<w;i+=8)x[i>>5]|=(s.charCodeAt(i/8)&255)<<(i%32);
	x[w>>5]|=0x80<<((w)%32);
	x[L-1]=w;
	
	var a=1732584193,b=-271733879,c=-1732584194,d=271733878;
	for(i=0;i<L;i+=16){
		var oa=a,ob=b,oc=c,od=d;
		a=A(a,b,c,d,x[i],7,-680876936);d=A(d,a,b,c,x[i+1],12,-389564586);c=A(c,d,a,b,x[i+2],17,606105819);b=A(b,c,d,a,x[i+3],22,-1044525330);
		a=A(a,b,c,d,x[i+4],7,-176418897);d=A(d,a,b,c,x[i+5],12,1200080426);c=A(c,d,a,b,x[i+6],17,-1473231341);b=A(b,c,d,a,x[i+7],22,-45705983);
		a=A(a,b,c,d,x[i+8],7,1770035416);d=A(d,a,b,c,x[i+9],12,-1958414417);c=A(c,d,a,b,x[i+10],17,-42063);b=A(b,c,d,a,x[i+11],22,-1990404162);
		a=A(a,b,c,d,x[i+12],7,1804603682);d=A(d,a,b,c,x[i+13],12,-40341101);c=A(c,d,a,b,x[i+14],17,-1502002290);b=A(b,c,d,a,x[i+15],22,1236535329);
		a=B(a,b,c,d,x[i+1],5,-165796510);d=B(d,a,b,c,x[i+6],9,-1069501632);c=B(c,d,a,b,x[i+11],14,643717713);b=B(b,c,d,a,x[i],20,-373897302);
		a=B(a,b,c,d,x[i+5],5,-701558691);d=B(d,a,b,c,x[i+10],9,38016083);c=B(c,d,a,b,x[i+15],14,-660478335);b=B(b,c,d,a,x[i+4],20,-405537848);
		a=B(a,b,c,d,x[i+9],5,568446438);d=B(d,a,b,c,x[i+14],9,-1019803690);c=B(c,d,a,b,x[i+3],14,-187363961);b=B(b,c,d,a,x[i+8],20,1163531501);
		a=B(a,b,c,d,x[i+13],5,-1444681467);d=B(d,a,b,c,x[i+2],9,-51403784);c=B(c,d,a,b,x[i+7],14,1735328473);b=B(b,c,d,a,x[i+12],20,-1926607734);
		a=C(a,b,c,d,x[i+5],4,-378558);d=C(d,a,b,c,x[i+8],11,-2022574463);c=C(c,d,a,b,x[i+11],16,1839030562);b=C(b,c,d,a,x[i+14],23,-35309556);
		a=C(a,b,c,d,x[i+1],4,-1530992060);d=C(d,a,b,c,x[i+4],11,1272893353);c=C(c,d,a,b,x[i+7],16,-155497632);b=C(b,c,d,a,x[i+10],23,-1094730640);
		a=C(a,b,c,d,x[i+13],4,681279174);d=C(d,a,b,c,x[i],11,-358537222);c=C(c,d,a,b,x[i+3],16,-722521979);b=C(b,c,d,a,x[i+6],23,76029189);
		a=C(a,b,c,d,x[i+9],4,-640364487);d=C(d,a,b,c,x[i+12],11,-421815835);c=C(c,d,a,b,x[i+15],16,530742520);b=C(b,c,d,a,x[i+2],23,-995338651);
		a=D(a,b,c,d,x[i],6,-198630844);d=D(d,a,b,c,x[i+7],10,1126891415);c=D(c,d,a,b,x[i+14],15,-1416354905);b=D(b,c,d,a,x[i+5],21,-57434055);
		a=D(a,b,c,d,x[i+12],6,1700485571);d=D(d,a,b,c,x[i+3],10,-1894986606);c=D(c,d,a,b,x[i+10],15,-1051523);b=D(b,c,d,a,x[i+1],21,-2054922799);
		a=D(a,b,c,d,x[i+8],6,1873313359);d=D(d,a,b,c,x[i+15],10,-30611744);c=D(c,d,a,b,x[i+6],15,-1560198380);b=D(b,c,d,a,x[i+13],21,1309151649);
		a=D(a,b,c,d,x[i+4],6,-145523070);d=D(d,a,b,c,x[i+11],10,-1120210379);c=D(c,d,a,b,x[i+2],15,718787259);b=D(b,c,d,a,x[i+9],21,-343485551);
		
		a=X(a,oa);
		b=X(b,ob);
		c=X(c,oc);
		d=X(d,od);
	}
	b=[a,b,c,d];
	s="";
	for(i=0;i<16;i++)s+=hx.charAt((b[i>>2]>>((i%4)*8+4))&0xF)+hx.charAt((b[i>>2]>>((i%4)*8))&0xF);
	return s
}

function trcolor(object,color) {
	 object.style.backgroundColor = color;
}

function plz_waiting() {
/*
	w = screen.availWidth / 2;
	h = screen.availHeight / 3;
	h = h + document.body.scrollTop;

	document.getElementById('plz_waiting').style.left = w - 75 + "px";
	document.getElementById('plz_waiting').style.top = h + "px";
	document.getElementById('plz_waiting').style.display = "inline";
*/
	if (document.documentElement && document.documentElement.scrollTop)
		theTop = document.documentElement.scrollTop;
	else 
		theTop = document.body.scrollTop

		
	w = screen.availWidth / 2;
	h = screen.availHeight / 3;
	h = h + theTop;
	waiting = document.getElementById('plz_waiting');
	try {
		waiting.style.left = w - 75 + "px";
	} catch (e) {
		return;
	}
	waiting.style.top = h + "px";
	waiting.style.display = "inline";
//	window.status = "h: "+h+", w:"+w
	
}

function jumpboard() {

	rr = document.getElementById('jb').value;
	result = rr.split(",");
	board = result[0];
	if (board == 0) return false;
	if (rr.indexOf("adult")>=0) {
		if (!confirm("您要進入的討論區為成人專區, 如果您未滿 18 歲, 請勿進入。\n\n該討論區已經依據台灣網站分級推廣基金會規定作標示。")) {
			document.jbb.reset();
			return false;
		}
	}
	plz_waiting();
	if (board == "-1") window.location="/forum/forum.php?action=folder";
	else window.open('/forum/forum.php?FID='+board,'_self');

}

function show(ID) {

	var show = "show"+ID;
	var hide = "hide"+ID;
	var info = "info"+ID;
	
	document.getElementById(show).style.display="none";
//	document.getElementById(hide).style.display="inline";
	document.getElementById(info).style.display="inline";

}

function hide(ID) {

	var hide = "hide"+ID;
	var show = "show"+ID;
	var info = "info"+ID;
	
	document.getElementById(hide).style.display="none";
	document.getElementById(info).style.display="none";
	document.getElementById(show).style.display="inline";

}

function postjumppage(page,msgID,FID,fp) {

	plz_waiting();
	window.location = "/forum/forum.php?action=viewreply&msg="+msgID+"&FID="+FID+"&fp="+fp+"&click=yes&page="+page;

}

function putsmile(code,id) {

	var txt = document.getElementById(id).value;
	document.getElementById(id).value = txt + "[smile:"+code+"]";
	
}

function warnlink(desc,link) {

	if (confirm(desc)) {
		window.open(link,"_blank");
	}

}


function sethttprequest() {
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
}

function checkpost(text,email,key) {

	sethttprequest();
	var email = escape(email);
	var url = "/forum/ajax.php?action=checkpost&IP=&checkcode="+text+"&email="+email;
	alert(url);
	http_request.onreadystatechange = checkpostresult;
	http_request.open('GET',url, false);
	http_request.send(null);

}

function checkpostresult() {

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var check_postresult = http_request.responseText;
		} 
	}

}

function IsEmail(strEmail) {
  var objRe = /^[_a-z0-9.]+@([_a-z0-9]+\.)+[a-z0-9]{2,3}$/;
  if(objRe.test(strEmail)) return true;
  return false;
}

function showpagelist(ID) {

	var SID = "pagelist_"+ID;
	try {
		ss = document.getElementById(SID);
		ss.style.display = "inline";
	} catch(err) {
	}

}

function hidepagelist(ID) {

	var SID = "pagelist_"+ID;
	try {	
		ss = document.getElementById(SID);
		ss.style.display = "none";
	} catch(err) {
	}

}

function showreply(ID,FID,page) {

	window.location = "forum.php?action=viewreply&msg="+ID+"&FID="+FID+"&fp="+page+"&click=yes";

}

function resizeimg(Id) {

	img = document.getElementById(Id);
	w = img.width;
	if (w > 700) {
		resize = 700 / w;
		var h = img.height * resize;
		var w = w * resize;
		img.width = w;
		img.height = h;
	}

}

function errorimg(Id,img,altproxy) {


//	img = document.getElementById(Id);
//	img.src = "/forum/images/error_pic.jpg";
//	img.height = 71;
//	img.width = 106

//	alert(client);
//	if (client=="MSIE") {
		code = document.getElementById("src_"+Id);
//		code.innerHTML = "<img src='http://www.proxystorm.com/index.php?q="+altproxy+"&hl=1011101001'>";
		code.innerHTML = "<img src='"+altproxy+"'>";
//		code.innerHTML = "<iframe  width=700 height=400  src='"+img+"'  ' /><br />"+img;
//		code.innerHTML = "無法載入 <a href='"+img+"' target='_blank'>"+img+"</a>";
/*		
		r = Math.random();
		if (r <= 0.33) code.innerHTML = "<img src='http://www.proxydrop.com/index.php?q="+altproxy+"&hl=1011101001'>";
		if (r <= 0.66) code.innerHTML = "<img src='http://www.proxydrop.org/index.php?q="+altproxy+"&hl=1011101001'>";
		else code.innerHTML = "<img src='http://www.proxydrop.net/index.php?q="+altproxy+"&hl=1011101001'>";
*/		
/*
		plz_waiting();
		img = encodeURI(img);
		var url = "/forum/ajax.php?action=getpic&img="+img;
	//	alert(url);
		sethttprequest();
		http_request.open('GET',url, false);
		http_request.onreadystatechange = function () {
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {
					var newimg = http_request.responseText;
	//				alert(newimg);
					code.innerHTML = "<img src='"+newimg+"'>"
				}
			}
		}
		http_request.send(null);
		document.getElementById('plz_waiting').style.display = "none";
*/
//	} else {
//	alert(code.outerHTML);
//	code.outerHTML = code.outerHTML.replace("http://","http://invis.free.anonymizer.com/http://");
//	code.outerHTML = "<img src='"+img+"'>";
//		code.innerHTML = "<iframe  width=700 height=400  src='"+img+"'  ' />"+img;
//	}
}

function resizeiframe(ff) {

	height = ff.scrollHeight;
	width = ff.scrollWidth;
//	alert("h: "+height+" ,w: "+width);
	if (width > 700) width = 700;
	ff.height = height;
	ff.width = width;

}

function noESC(event) {
	if (client == "MSIE") {
		try {
			if (event.keyCode == 27) {
				alert("抱歉, ESC 按鍵的功能已經被取消了");
				return false;
			}
		} catch(err) {
		}
	}
}


function search(FID) {

	key = document.getElementById("keyword");
	from = document.getElementById("searchfrom");
	if (key.value.length < 2) {
		alert("搜尋的關鍵字元太短, 請用長一點的關鍵字搜尋");
		key.focus();
		return;
	}
	if (from.value == "google") {
		document.getElementById('gs_q').value = key.value;
		document.forms.google_search.submit();
		return;
	}
	plz_waiting();
	SetCookie("forum_search_keyword",key.value,0,"/forum/","club1069.com",0);
	SetCookie("forum_search_from",from.value,0,"/forum/","club1069.com",0);
	var keyword = encodeURI(key.value);
	window.location = "forum.php?FID="+FID+"&keyword="+keyword+"&from="+from.value;

}

function cancelsearch(FID) {

	plz_waiting();
	DeleteCookie("forum_search_keyword","/forum/","club1069.com");
	DeleteCookie("forum_search_from","/forum/","club1069.com");
	window.location = "forum.php?search=cancel&FID="+FID;

}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function SetCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape((value)) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */

function GetCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return (unescape(dc.substring(begin + prefix.length, end)));
}
/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */

function DeleteCookie(name, path, domain)
{
    if (GetCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function showIP(IP) {

	window.open("/forum/showIP.php?IP="+IP,"_blank","width=800,height=400,scrollbars=yes,status=yes,directories=no,location=no,menubar=no,resizable=yes,titlebar=no,toolbar=no");

}

function sugggestsearch (FID) {

	var keyword = document.getElementById('keyword');
	if (keyword.value.length < 2) return;

	var from = document.getElementById('searchfrom');
	var word = escape(keyword.value);
	var suggestsearch_keyword = document.getElementById('search_keyword').value;
	if (suggestsearch_keyword == word) return;
//	alert(suggestsearch_keyword+","+word);
	var url = "/forum/ajax.php?action=search&keyword="+word+"&type="+from.value+"&FID="+FID;
	document.getElementById('search_keyword').value = word;
//	alert(url);

	sethttprequest();
	http_request.onreadystatechange = checksuggestresult;
	http_request.open('GET',url,true);
	http_request.send(null);

}

function checksuggestresult() {

	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if (window.ActiveXObject) {
				xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async=false;
				xmlDoc.loadXML(http_request.responseText);
			} else if (document.implementation && document.implementation.createDocument) {
				xmlDoc= document.implementation.createDocument("","",null);
				var parser = new DOMParser(); 
				xmlDoc = parser.parseFromString(http_request.responseText, "text/xml");
			}

			ret_arr = xmlDoc.getElementsByTagName("result");
			var ret_txt;
			var total_result = ret_arr.length;
//			if (total_result < 5) return;
			for(i=0;i<total_result;i++) {
				ret_txt = ret_txt + ret_arr[i].firstChild.nodeValue + "<br>\n";
			}
			document.getElementById("suggest_result_box").style.display = 'inline';
			document.getElementById("suggest_result").innerHTML = ret_txt;

		}
	}
}

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}

var event_total = 0;;
var eventID = 0;
var currenteventID;
var scrolltext_timed;
var scrolltext_pause_timed;
var scrolltext_hide_timed;
var scrolltext_show_flag = false;
var scrolltext_restart_timed;

function showscrolltext() {

	if (event_total == 0)  event_total = allhotnews.length
	if (event_total == 0) {
		document.getElementById('scrolltext_div').style.display = "none";
		return;
	}
	clearTimeout(scrolltext_timed);
	
	type = "<b>["+allhotnews[eventID].type+"]</b>  ";
	document.getElementById('scrolltext_content').innerHTML = "<nobr>"+allhotnews[eventID].name+"</nobr>";
	document.getElementById('scrolltext_type').value = allhotnews[eventID].type;
	currenteventID = eventID;

	delay = 5000;
	currrentID = eventID;
	eventID = eventID + 1;
	if (eventID >= event_total) eventID = 0;
	scrolltext_timed = setTimeout("showscrolltext()",delay);
	
}

function scrolltext_popup(eid) {

	w = screen.availWidth / 2 - 400;
	h = screen.availHeight;
	if (allhotnews[eid].start) window.open("/event/showevent.php?ID="+allhotnews[eid].ID,"event","width=800,height="+h+"resizable=no,scrollbars=yes,top=0,left="+w);
	else if (allhotnews[eid].style=="topnews") window.open("/topnews/read.php?ID="+allhotnews[eid].ID,"event","width=500,height="+h+"resizable=no,scrollbars=yes,top=0,left="+w);
	
}

function scrolltext_pause() {

	clearTimeout(scrolltext_restart_timed);
	
	scrolltext_pause_timed = setTimeout(function() {
	
		clearTimeout(scrolltext_timed);
		clearTimeout(scrolltext_restart_timed);
		clearTimeout(scrolltext_pause_timed);
		if (scrolltext_show_flag) return;
		scrolltext_show_flag = true;
		h = event_total * 30;
		var c = "";
		for(i=currenteventID;i<event_total;i++) {
			type = "<b>["+allhotnews[i].type+"]</b>  ";
			c += "<div style='width:790px;overflow:hidden;white-space:nowrap;color:blue;text-decoration:none;'  onclick='scrolltext_popup("+i+")' onmouseover='this.style.fontWeight=\"bold\";this.style.cursor=\"pointer\"' onmouseout='this.style.fontWeight=\"normal\"'>"+ allhotnews[i].name + "</div>";
		}
		for(i=0;i<currenteventID;i++) {
			type = "<b>["+allhotnews[i].type+"]</b>  ";
			c += "<div style='width:790px;overflow:hidden;white-space:nowrap;color:blue;text-decoration:none;'  onclick='scrolltext_popup("+i+")' onmouseover='this.style.fontWeight=\"bold\";this.style.cursor=\"pointer\"' onmouseout='this.style.fontWeight=\"normal\"'>"+ allhotnews[i].name + "</div>";
		}
		c += "<div style='width:790px;text-align:center;'  onclick='scrolltext_restart()' onmouseover='this.style.fontWeight=\"bold\";this.style.cursor=\"pointer\"' onmouseout='this.style.fontWeight=\"normal\"'>&lt;&lt;關閉&gt;&gt;</div>";
		document.getElementById('scrolltext_content').innerHTML = c;
		document.getElementById('scrolltext_content').style.lineHeight = "30px";
		document.getElementById('scrolltext_type').value = "活動資訊";
		
	},500);
	
}

function scrolltext_restart() {
	
	clearTimeout(scrolltext_pause_timed);
	clearTimeout(scrolltext_restart_timed);
//	else return;
	scrolltext_restart_timed = setTimeout(function() {
		scrolltext_show_flag = false;
		document.getElementById('scrolltext_content').style.height= "29px";
		document.getElementById('scrolltext_content').style.fontWeight='normal';
		document.getElementById('scrolltext_content').style.color='';
		document.getElementById('scrolltext_content').style.cursor='';
		if (eventID > 0) eventID = eventID - 1;
		showscrolltext();
	}, 250);
}

function initpage() {
//	showscrolltext();
}

