﻿
//==============================================================
// 文件名称 : 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); }
	);
}