
//==============================================================
// 文件名稱 : Include/AS_Public.js
// 文件作用 : 常規功能調用
// 最後更新 : 2008.12
//==============================================================

//==============================
//定義一些全局變量以及函數
//==============================
//獲取Event
Object.reEvent = function () {
	return window.event ? window.event : (function (o) {
		while (o && !/^\[object[ A-Za-z]*Event\]$/.test(o.arguments[0])){
			o = o.caller;
		}
		return o.arguments[0];
	})(this.reEvent)
}
//==============================
//改變日誌列表排列方式
//==============================
function ChangeLogListMode(Mode) {
	var ListElement = document.getElementsByName("HiddenLogBrief");
	for (var i=0; i<ListElement.length; i++) {
		if(Mode == "List") {
			ListElement[i].parentNode.style.display = "";
		} else {
			ListElement[i].parentNode.style.display = "none";
		}
	}
	document.getElementById("LogListMode_List").style.fontWeight	= "";
	document.getElementById("LogListMode_Normal").style.fontWeight	= "";
	document.getElementById("LogListMode_"+Mode).style.fontWeight	= "Bold";
}
//==============================
//顯示驗證碼
//==============================
function GetCheckCode(Part) {
	document.getElementById("CheckCodeBox_"+Part).innerHTML = "<img src=\""+BlogPath+"Include/AS_CheckCode.Asp?Part="+Part+"&Random="+Math.random()+"\" width=\"72\" height=\"22\" border=\"0\" align=\"absmiddle\" id=\"CheckCodePic\" title=\"點擊可刷新驗證碼\" class=\"Pointer\" onclick=\"this.src='"+BlogPath+"Include/AS_CheckCode.Asp?Part="+Part+"&Random='+Math.random();\">";
}
//==============================
//側邊欄顯示ShowSidePanel()
//參數:Calendar-日曆;Count-統計;Comment-評論;GB-留言;Link-友情鏈接;
//undefined
//==============================
function ShowSidePanel() {
	var ActionFile = "Ajax.Asp?Action=SidePanel&Part=";
	if (document.getElementById("LoginASideDiv")	!= null) AjaxUpdate(BlogPath+ActionFile+"LoginA", "LoginASideDiv");
	if (document.getElementById("LoginBSideDiv")	!= null) AjaxUpdate(BlogPath+ActionFile+"LoginB", "LoginBSideDiv");
	if (document.getElementById("CalendarSideDiv")	!= null) AjaxUpdate(BlogPath+ActionFile+"Calendar", "CalendarSideDiv");
	if (document.getElementById("CountSideDiv")		!= null) AjaxUpdate(BlogPath+ActionFile+"Count", "CountSideDiv");
	if (document.getElementById("CommentSideDiv")	!= null) AjaxUpdate(BlogPath+ActionFile+"Comment", "CommentSideDiv");
	if (document.getElementById("GBSideDiv")		!= null) AjaxUpdate(BlogPath+ActionFile+"GB", "GBSideDiv");
	//if (document.getElementById("TagSideDiv")		!= null) AjaxUpdate(BlogPath+ActionFile+"Tag", "TagSideDiv");
	//if (document.getElementById("LinkSideDiv")	!= null) AjaxUpdate(BlogPath+ActionFile+"Link", "LinkSideDiv");
}
//==============================
//判斷是否為正確的Email
//==============================
function isEmail(Str) {
	if (Str.length < 6) {
		return false;
	} else {
		var regu = /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;
		if (regu.test(Str)) return true;
		return false;
	}
}
//==============================
//字數限制,中文算兩個字符
//==============================
function getLength(Str) {
	var len = 0;
	var i;
	for (i=0; i<Str.length; i++) {
		if (Str.charCodeAt(i) > 255) {
			len+=2;
		} else {
			len++;
		}
	}
	return len;
}
//==============================
//Ajax登錄
//==============================
function AjaxLogin(Mode) {
	BlurForm("LoginForm");
	ASAjax.postf(
		"LoginForm",
		function(obj) {
			var ReturnValue = eval('(' + obj.responseText + ')');
			if (ReturnValue[0] == "Finish") {
				hideASBox();
				AjaxUpdate(BlogPath+"Ajax.Asp?Action=SidePanel&Part=Login"+Mode, "Login"+Mode+"SideDiv");
			} else if ( ReturnValue[0] == "Wrong_CheckCode") {
				GetCheckCode("Login");
				document.getElementById("ASBox_main").innerHTML = ReturnValue[1];
			} else {
				document.getElementById("ASBox_main").innerHTML = ReturnValue[1];
			}
		}
	);
}
//==============================
//Ajax顯示日曆
//==============================
function ShowCalendar(TheYear,TheMonth) {
	AjaxUpdate(BlogPath+"Ajax.Asp?Action=SidePanel&Part=Calendar&TheYear="+TheYear+"&TheMonth="+TheMonth, "CalendarSideDiv");
}
//==============================
//Ajax保存評論
//==============================
function AjaxSaveComment(Part,FromID,IsHtml,Editor) {
	BlurForm("SaveCommentForm");
	document.getElementById("SaveCommentBtn").disabled = true;
	ASAjax.postf(
		"SaveCommentForm",
		function(obj) {
			var ReturnValue = eval('(' + obj.responseText + ')');
			if (ReturnValue[0] == "Finish") {
				hideASBox();
				document.getElementById("SaveInfo").innerHTML		= ReturnValue[1];
				document.getElementById("SaveInfo").style.display	= "";
				AjaxUpdate(BlogPath+"Ajax.Asp?Action=ViewComment&Part="+Part+"&FromID="+FromID+"&IsHtml="+IsHtml+"&page=200", "ListForPaginate");
				AjaxUpdate(BlogPath+"Ajax.Asp?Action=WriteComment&Part="+Part+"&FromID="+FromID+"&IsHtml="+IsHtml, "WriteComment");	//重新載入編輯框
				GoToScroller("ListForPaginate",300);
			} else if (ReturnValue[0] == "Wait") {
				document.getElementById("ASBox_main").innerHTML	= ReturnValue[1];
			} else if ( ReturnValue[0] == "Wrong_CheckCode") {
				GetCheckCode("Comment");
				document.getElementById("ASBox_main").innerHTML		= ReturnValue[1];
				document.getElementById("SaveCommentBtn").disabled	= false;
			} else {
				document.getElementById("SaveCommentBtn").disabled	= false;
				document.getElementById("ASBox_main").innerHTML		= ReturnValue[1];
			}
		}
	);
}
//==============================
//Ajax保存留言
//==============================
function AjaxSaveGB() {
	BlurForm("SaveGBForm");
	document.getElementById("SaveGBBtn").disabled = true;
	ASAjax.postf(
		"SaveGBForm",
		function(obj) {
			var ReturnValue = eval('(' + obj.responseText + ')');
			document.getElementById("ASBox_main").innerHTML	= ReturnValue[1];
			if (ReturnValue[0] == "Finish") {
				document.getElementById("ASBox_closeimg").onclick = function () { hideASBox();window.location.href = BlogPath + '?GB'; }
			} else {
				document.getElementById("SaveGBBtn").disabled = false;
			}
		}
	);
}
//==============================
//重寫申請友情鏈接框
//==============================
function ReWriteLink() {
	AjaxUpdate(BlogPath + "Ajax.Asp?Action=WriteLink","ASBox_main");
}
//==============================
//Ajax保存友情鏈接申請
//==============================
function AjaxSaveLink() {
	BlurForm("SaveLinkForm");
	ASAjax.postf(
		"SaveLinkForm",
		function(obj) {
			var ReturnValue = eval('(' + obj.responseText + ')');
			document.getElementById("ASBox_main").innerHTML	= ReturnValue[1];
		}
	);
}
//==============================
//Ajax檢查日誌密碼
//==============================
function AjaxCheckLogPwd(LogID) {
	ShowASBox(2,'日誌密碼認證','正在確認您的通關密碼,請稍候',205,425,0,2,false);
	BlurForm("LogPwdForm");
	document.getElementById("LogPwdButton").disabled = true;
	ASAjax.get(
		BlogPath+"Ajax.Asp?Action=CheckLogPwd&LogID="+LogID+"&LogPassword="+encodeURIComponent(document.getElementById("LogPassword").value),
		function(obj) {
			//利用數組得到多維的Asp返回值.試用階段.後續版本繼續完成.
			var ReturnValue = eval('(' + obj.responseText + ')');
			if (ReturnValue[0] == "Finish") {
				document.getElementById("ASBox_main").innerHTML	= "密碼驗證通過.正在載入日誌內容.";
				document.getElementById("LogPwdDiv").innerHTML	= ReturnValue[1];
				hideASBox();
			} else {
				document.getElementById("LogPwdButton").disabled	= false;
				document.getElementById("ASBox_main").innerHTML		= ReturnValue[1];
			}
		}
	);
}
//==============================
//更新用戶的收藏夾
//==============================
function UpdateMemberFav(Mode,LogID) {
	ASAjax.get(
		BlogPath+"Member/Index.Asp?Action=MemberFav&Mode="+Mode+"&LogID="+LogID,
		function(obj) {
			var ReturnValue = eval('(' + obj.responseText + ')');
			document.getElementById("ASBox_main").innerHTML = ReturnValue[1];
		}
	);
}
//==============================
//評論或留言的作者名
//==============================
function WriteAuthorName(InputID,Word) {
	var InputObj = document.getElementById(InputID);
	if ( InputObj != true ) {
		InputObj.value = Word;
	}
}
//==============================
//強製表單所有項目失去焦點
//==============================
function BlurForm(FormID) {
	var formObj = document.getElementById(FormID);
	for (var i=0; i<formObj.elements.length; i++) {
		formObj.elements[i].blur();
	}
}
//==============================
//顯示|隱藏Div
//==============================
function mousePosition(evt){
	if(evt.pageX || evt.pageY){
		return {x:evt.pageX, y:evt.pageY};
	}
	return {
		x:evt.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:evt.clientY + document.body.scrollTop  - document.body.clientTop
	}
}
function ShowBlock(showObj,posObj,hideObj,toppx,leftpx) {
	var divobj		= document.getElementById(showObj);
	var divposition	= document.getElementById(posObj);
	if (divobj.style.display == "none" || divobj.style.visibility == "hidden") {
		var posY = divposition.offsetTop;
		var posH = divposition.clientHeight;
		var posX = divposition.offsetLeft;
		while (divposition = divposition.offsetParent) {
			posY += divposition.offsetTop;
			posX += divposition.offsetLeft;
		}
		divobj.style.top		= posY + posH + toppx + "px";
		divobj.style.left		= posX + leftpx + "px";
		divobj.style.display	= "block";
		divobj.style.visibility	= "visible";
		if (hideObj == true) {
			Object.reEvent().cancelBubble	= true;
			document.onclick = function () {
				var mousePos = mousePosition(Object.reEvent());
				if (divobj.offsetLeft	> mousePos.x || divobj.offsetLeft	+ divobj.offsetWidth	< mousePos.x ||
					divobj.offsetTop	> mousePos.y || divobj.offsetTop	+ divobj.offsetHeight	< mousePos.y) {
					divobj.style.display	= "none";
					divobj.style.visibility = "hidden";
					document.onclick		= null;
				}
			}
		}
	} else {
		divobj.style.display	= "none";
		divobj.style.visibility = "hidden";
	}
}
//==============================
//顯示Tip提示
//==============================
//預判瀏覽器邊界
function ClearEdge(obj,whichedge) {
	var edgeoffset = (whichedge == "rightedge") ? parseInt("9px")*-1 : parseInt("0")*-1;
	if (whichedge == "rightedge") {
		var windowedge = document.body.scrollLeft + document.body.clientWidth - 30;
		tipobj.contentmeasure = tipobj.offsetWidth;
		if (windowedge - tipobj.x < tipobj.contentmeasure)
			edgeoffset = tipobj.contentmeasure + obj.offsetWidth + parseInt("9px");
	} else {
		var windowedge = document.body.scrollTop + document.body.clientHeight - 15;
		tipobj.contentmeasure = tipobj.offsetHeight;
		if (windowedge - tipobj.y < tipobj.contentmeasure)
			edgeoffset = tipobj.contentmeasure - obj.offsetHeight;
	}
	return edgeoffset;
}
function ShowTip(text,pos,tipwidth) {
	var divblock = document.createElement("div");
	divblock.setAttribute("id", "TipBox");
	document.body.appendChild(divblock);
	tipobj = document.getElementById("TipBox");
	if (document.getElementById("TipBox")) {
		tipobj = document.getElementById("TipBox")
		if (tipwidth != "") {
			tipobj.widthobj			= tipobj.style;
			tipobj.widthobj.width	= tipwidth;
		}
		var FinalLeft	= pos.offsetLeft;
		var FinalTop	= pos.offsetTop;
		var parentEl	= pos.offsetParent;
		while (parentEl != null) {
			FinalLeft	= FinalLeft + parentEl.offsetLeft;
			FinalTop	= FinalTop	+ parentEl.offsetTop;
			parentEl	= parentEl.offsetParent;
		}
		tipobj.x				= FinalLeft;
		tipobj.y				= FinalTop;
		tipobj.style.left		= tipobj.x - ClearEdge(pos, "rightedge") + pos.offsetWidth + "px";
		tipobj.style.top		= tipobj.y - ClearEdge(pos, "bottomedge") + "px";
		//tipobj.style.left		= tipobj.x - ClearEdge(pos, "rightedge") + pos.offsetWidth + 18 + "px";
		//tipobj.style.top		= tipobj.y - ClearEdge(pos, "bottomedge") + 8 + "px";
		tipobj.style.visibility	= "Visible";
		//創建一個iframe,用來遮擋<select>
		iframeStr = "<iframe id='iframebox' frameBorder='0' style='filter:alpha(opacity=0);'></iframe>";
		tipobj.innerHTML = iframeStr + text;
		iframeObj = document.getElementById("iframebox");
		iframeObj.style.visibility	= "inherit";
		iframeObj.style.position	= "absolute";
		iframeObj.style.top			= "0px";//不能佔據頁面空間
		iframeObj.style.left		= "0px";//不能佔據頁面空間
		iframeObj.style.marginLeft	= "-1px";
		iframeObj.style.width		= tipobj.clientWidth + 4 + "px";
		iframeObj.style.height		= tipobj.clientHeight + 2 + "px";
		iframeObj.style.zIndex		= "-1";
		pos.onmouseout = function() {
			if (document.getElementById("TipBox")) {
				document.body.removeChild(document.getElementById("TipBox"));
				//if (document.getElementById("TipBox")) {
					//alert("創建的div還沒刪除呢")
				//}
			}
		}
	}
}
//==============================
//錨點間平滑滾動
//==============================
//轉換為數字
function FormatInt(v) {
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
}
//獲取元素信息
function GetPosition(e) {
	var l	= 0;
	var t	= 0;
	var w	= FormatInt(e.style.width);
	var h	= FormatInt(e.style.height);
	var wb	= e.offsetWidth;
	var hb	= e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?FormatInt(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?FormatInt(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?FormatInt(e.currentStyle.borderLeftWidth):0);
	t += e.offsetTop  + (e.currentStyle?FormatInt(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
}
//獲取滾動條信息
function GetWindowScroll() {
	var t, l, w, h;
	if (document.documentElement && document.documentElement.scrollTop) {
		t = document.documentElement.scrollTop;
		l = document.documentElement.scrollLeft;
		w = document.documentElement.scrollWidth;
		h = document.documentElement.scrollHeight;
	} else if (document.body) {
		t = document.body.scrollTop;
		l = document.body.scrollLeft;
		w = document.body.scrollWidth;
		h = document.body.scrollHeight;
	}
	return { t: t, l: l, w: w, h: h };
}
//平滑滾動
function GoToScroller(StopObj,Speed) {
	if (typeof StopObj != "object") { StopObj = document.getElementById(StopObj); }
	if (!StopObj) return;
	var z = this;
	z.StopObj = StopObj;
	z.p = GetPosition(StopObj);
	z.s = GetWindowScroll();
	z.clear = function(){window.clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / Speed;
		if (t >= Speed + z.t) {
			z.clear();
			window.setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll	= function (t, l){window.scrollTo(l, t)};
	z.timer		= window.setInterval(function(){z.step();},13);
}
//==============================
//對象的隱藏和顯示
//==============================
function ShowOrHide(ShowObj) {
	document.getElementById(ShowObj).style.display = document.getElementById(ShowObj).style.display == "none" ? "block" : "none";
}
//==============================
//UBB部分
//==============================
//運行代碼
function RunCode(CodeNum) {
	var bodyTag = document.getElementById(CodeNum).value;
	preWin = window.open('ASUBB-RunCode','','left=0,top=0,width=550,height=400,resizable=1,scrollbars=1,status=1, toolbar=1,menubar=0');
	preWin.document.open();
	preWin.document.write(bodyTag);
	preWin.document.close();
	preWin.document.title="ASUBB-運行代碼";
	//preWin.document.charset="UTF-8";
}
//複製代碼,兼容FF
function CopyContent(Content) {
	if (window.clipboardData) {
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", Content);
	} else if (navigator.userAgent.indexOf("Opera") != -1) {
		window.location = Content;
	} else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		} catch (e) {
			alert("您使用的FireFox瀏覽器安全設置過高,以至於影響程序的正常響應！\n請在瀏覽器地址欄輸入'about:config'並回車\n然後將'signed.applets.codebase_principal_support'設置為'true'");
		}
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext = Content;
		str.data = copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
	alert("內容已經複製到剪切板");
}
//==============================
//Ajax部分
//==============================
var ASAjax = new AJAXRequest;
ASAjax.setcharset("utf-8");
//ASAjax.onrequeststart	= function() {
	//document.getElementById("loadingDiv").style.display="block";
//}
//ASAjax.onrequestend		= function() {
	//document.getElementById("loadingDiv").style.display="none";
//}
//Ajax保存數據
function AjaxSave(formobj) {
	ASAjax.postf(
		formobj,
		function(obj) {
			var ReturnValue = eval('(' + obj.responseText + ')');
			document.getElementById("ASBox_main").innerHTML = ReturnValue[1];
		}
	);
}
//按照要求無刷新更新數據,可用於提取數據
function AjaxUpdate(url, updateobj) {
	ASAjax.get(url, updateobj);
}
//從ASP返回操作語句
function AjaxAction(url) {
	ASAjax.get(
		url,
		function(obj) { eval(obj.responseText); }
	);
}