//==============================================================
// 程序名称 : ASBLOG
// 程序说明 : AllSheng BLOG
// 程序版本 : v2.0
// 项目主管 : Felix Dong
// 版权所有 : 傲胜工作室（AllSheng Studio）
//==============================================================
// 文件名称 : Include/AS_Public.js
// 文件作用 : 常规功能调用
// 最后更新 : 2007.11
//==============================================================
// 版权声明 : Copyright 2006-2007 AllSheng. All Rights Reserved.
// 官方网站 : Http://Www.AllSheng.Net/
// 技术支持 : Http://bbs.AllSheng.Net/
// 电子邮箱 : Support@AllSheng.Net
//==============================================================

//==============================
//定义一些全局变量以及函数
//==============================
//获取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";
}
//==============================
//侧边栏显示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("LinkSideDiv")		!= null) AjaxUpdate(BlogPath+ActionFile+"Link", "LinkSideDiv");
	if (document.getElementById("TagSideDiv")		!= null) AjaxUpdate(BlogPath+ActionFile+"Tag", "TagSideDiv");
}
//Ajax登录
function AjaxLogin(Mode) {
	ShowAlertBox(2,'成员登录提示','系统正在校验您提交的信息,请稍后',130,330,0,2,true);
	BlurForm("LoginForm");
	ASAjax.postf(
		"LoginForm",
		function(obj) {
			if (obj.responseText == "Finish") {
				hideASBox();
				AjaxUpdate(BlogPath+"Ajax.Asp?Action=SidePanel&Part=Login"+Mode, "Login"+Mode+"SideDiv");
			} else {
				document.getElementById("ASBox_content").innerHTML = obj.responseText;
			}
		}
	);
}
//Ajax显示日历
function ShowCalendar(TheYear,TheMonth) {
	AjaxUpdate(BlogPath+"Ajax.Asp?Action=SidePanel&Part=Calendar&TheYear="+TheYear+"&TheMonth="+TheMonth, "CalendarSideDiv");
}
//Ajax保存评论
function AjaxSaveComment(CommentForm,Part,FromID,SaveCommentButton,Editor) {
	ShowAlertBox(2,'保存评论信息','正在保存您发表的评论,请稍后',130,330,0,2,false);
	BlurForm(CommentForm);
	document.getElementById(SaveCommentButton).disabled = true;
	ASAjax.postf(
		CommentForm,
		function(obj) {
			if (obj.responseText == "Finish") {
				hideASBox();
				document.getElementById("SaveInfo").innerHTML		= "您成功发表评论.感谢您的参与.";
				document.getElementById("SaveInfo").style.display	= "";
				AjaxUpdate(BlogPath+"Ajax.Asp?Action=ReadComment&Part="+Part+"&FromID="+FromID+"&page=200", "ListForPage");
				//AjaxUpdate(BlogPath+"Ajax.Asp?Action=WriteComment&Part="+Part+"&FromID="+FromID, "WriteComment");	//重新载入编辑框
			} else if (obj.responseText == "Wait") {
				document.getElementById("ASBox_content").innerHTML	= "您成功发表评论.感谢您的参与.<br />系统设置为评论审核,所以您的评论需要审核.<br />暂时无法浏览.";
			} else {
				document.getElementById(SaveCommentButton).disabled	= false;
				document.getElementById("ASBox_content").innerHTML	= obj.responseText;
			}
		}
	);
}
//Ajax保存留言
function AjaxSaveGB(GBForm,SaveGBButton,Editor) {
	ShowAlertBox(2,'保存留言信息','正在保存您发表的留言,请稍后',130,330,0,2,false);
	BlurForm(GBForm);
	document.getElementById(SaveGBButton).disabled = true;
	ASAjax.postf(
		GBForm,
		function(obj) {
			if (obj.responseText == "Finish") {
				document.getElementById("ASBox_content").innerHTML = "您成功发表留言.感谢您的参与.";
				document.getElementById("ASBox_close_span").onclick = function () { hideASBox();window.location.href = BlogPath + 'Show.Asp?Part=GB'; }
			} else if (obj.responseText == "Wait") {
				document.getElementById("ASBox_content").innerHTML = "您成功发表留言.感谢您的参与.<br />系统设置为留言审核,所以您的留言需要审核.<br />暂时无法浏览.";
			} else {
				document.getElementById(SaveGBButton).disabled		= false;
				document.getElementById("ASBox_content").innerHTML	= obj.responseText;
			}
		}
	);
}
//Ajax保存友情链接申请
function AjaxSaveLink(LinkForm) {
	BlurForm(LinkForm);
	ASAjax.postf(
		LinkForm,
		function(obj) { document.getElementById("ASBox_content").innerHTML	= obj.responseText; }
	);
}
//Ajax检查日志密码
function AjaxCheckLogPwd(LogID) {
	ShowAlertBox(2,'日志密码认证','正在确认您的通关密码,请稍后',130,330,0,2,false);
	BlurForm("LogPwdForm");
	document.getElementById("LogPwdButton").disabled = true;
	ASAjax.post(
		BlogPath+"Ajax.Asp?Action=CheckLogPwd&LogID="+LogID+"&LogPassword="+document.getElementById("LogPassword").value,
		"",
		function(obj) {
			//利用数组得到多维的Asp返回值.试用阶段.后续版本继续完成.
			var ReturnValue = eval('(' + obj.responseText + ')');
			if (ReturnValue[0] == "Finish") {
				document.getElementById("ASBox_content").innerHTML	= "密码验证通过.正在载入日志内容.";
				document.getElementById("LogPwdDiv").innerHTML		= ReturnValue[1];
				hideASBox();
			} else {
				document.getElementById("LogPwdButton").disabled	= false;
				document.getElementById("ASBox_content").innerHTML	= ReturnValue[0];
			}
		}
	);
}
//==============================
//强制表单所有项目失去焦点
//==============================
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 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("GB2312");
//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) {
			if (obj.responseText == "Finish") {
				document.getElementById("ASBox_content").innerHTML = "数据更新成功!";
			} else if (obj.responseText == "Wrong") {
				document.getElementById("ASBox_content").innerHTML = "数据更新失败!";
			} else {
				document.getElementById("ASBox_content").innerHTML = obj.responseText;
			}
		}
	);
}
//按照要求无刷新更新数据,可用于提取数据
function AjaxUpdate(url, updateobj) {
	ASAjax.get(url, updateobj);
}
//从ASP返回操作语句
function AjaxAction(url) {
	ASAjax.get(
		url,
		function(obj) { eval(obj.responseText); }
	);
}