var objColumn,orgColor;		/*记录栏目ID和原始颜色*/
var nTimerID;				/*定时器标志*/
var cnt =0;					/*提交计数*/

// 若主栏存在子栏，则设定主栏目的默认内容为其第一个子栏目的内容
function setDefault()
{
	var objA;
	try
	{
		objA = $("show_submenu").getElementsByTagName("A");
		objA[0].focus();
		objA[0].click();	
	}
	catch (e){};
}

// 设置当前选定栏目背景色
// 说明:column_tip0=原栏目属性，column_tip1=提醒css属性
function setColumnTip(obj)
{
	if (objColumn !=null && obj == objColumn)
	{
		return(false);
	}
	if (objColumn != null)
	{
		objColumn.className="column_tip0";
	}
	objColumn = obj;
	obj.className="column_tip1";		// 给当前栏目加上提醒CSS属性
	return(true);
}

// 发送添加留言，并返回结果
function SendMsg()
{
	var sName,sContext,sLink;
	sName = escape($("gname").value);
	sContext = escape($("gcontext").value);
	sLink = "gbook_do.asp?action=0&gname=" + sName + "&gcontext=" + sContext;
	new Ajax.Updater("result",sLink,{evalScripts:true,onComplete:nTimerID=setTimeout("ClearResult()",3000)});
	return(false);
}

// 清除返回结果信息
function ClearResult()
{
	$("result").innerHTML = "";
	NavPage("1");
	clearTimeout(nTimerID);
}

function NavPage(sPage)
{
	new Ajax.Updater("msg","gbook_do.asp?action=1&page=" + sPage,{evalScripts:true});
	return(true);
}

/*
功能: 封装功能后的new Ajax.Updater方法,简化函数写法并可获取当前事件发送者
参数: obj=发送命令对象;sDiv=填充内容的容器;sURL=请求的页面
*/
function Updater(obj,sDiv,sURL)
{
	var newObj = obj.getAttribute("isFather");
	if ( newObj != undefined )
	{
		switchVisible($(newObj));
	}
	else
	{
		var iStart = sURL.indexOf('?');
		var sAffix = (iStart ==-1)?'':sURL.substr(iStart+1,sURL.length-iStart);
		// 为当前页面的hash加上其栏目ID和当前页号的地址
		new Ajax.Updater(sDiv,sURL,{evalScripts:true,onLoading:showLoading(true)});
	}
}

function switchVisible(obj)
{
	var e = $(obj);
	//隐藏其元素下包含的LI元素
	for(var i=0; i< e.childNodes.length;i++)
	{
		var element = e.childNodes[i];
		if ( element.tagName.toUpperCase() == "LI")
		{
			element.style.display = element.style.display ==''?'none':'';
		}
	}
}



function getMultiSelect()
{
	var s = document.getElementsByTagName("INPUT");
	var ids = '';
	for(var i=0;i<s.length;i++)
	{
		if (s[i].getAttribute("type") == "checkbox" && s[i].checked==true)
		{
			ids += s[i].id + ' ';
		}
	}
	return(ids.replace(/d/g,''));
}

function isMultiSelect()
{
	var s = document.getElementsByTagName("INPUT");
	var ids="", iCount=0;
	for(var i=0;i<s.length;i++)
	{
		if (s[i].getAttribute("type") == "checkbox" && s[i].checked==true)
		{
			iCount++;
		}
	}
	if (iCount<1)
		return(false);
	else
		return(true);
}

function reloadAndClose()
{
	//var obj = parent.document.all.doload;		// 采用Open打开的窗口
	var obj = window.dialogArguments;			// 采用ShowModalDialog打开的对话框
	if ( obj == undefined)						// 操作在编辑窗口中执行
	{
		obj = parent.parent.document.all.doload;
		parent.close();
	}
	obj.click();
	self.close();
}


//重载页面最新数据
function reloadNoClose()
{
	var obj = parent.opener.document.all.doload;	// 采用Open打开的窗口
	//var obj = parent.window.dialogArguments;				// 采用ShowModalDialog打开的对话框
	if ( obj == undefined)							// 操作在编辑窗口中执行
	{
		obj = parent.parent.document.all.doload;
	}
	obj.click();
}

function showMsgAndClose(sMsg)
{
	alert(sMsg);
	self.close();
}

function quickpost(oBtn,event) 
{ 
	if((event.ctrlKey && event.keyCode == 13)||(event.altKey && event.keyCode == 83)) 
	{ 
		cnt++; 
		if (cnt==1)
		{ 
			oBtn.click();
		}
		else
		{
			alert('提交处理中...'); 
		} 
	}
}

function clearme(objBox)
{
	if (typeof(objBox) == "object")
	{
		objBox.value = "";
	}
	else if (typeof(objBox) == "string")
		{
			var objs = objBox.split(",");
			for(var i=0;i<objs.length;i++) $(objs[i]).value="";
		}
		else
			return(false);
}

function disableme(objBox)
{
	if (typeof(objBox) == "object")
	{
		objBox.disabled = true;
	}
	else if (typeof(objBox) == "string")
		{
			var objs = objBox.split(",");
			for(var i=0;i<objs.length;i++) $(objs[i]).disabled = true;
		}
		else
			return(false);
}

function setcheck()
{
	$("cccc").value = "4444";
}

function enableme(objBox)
{
	if (typeof(objBox) == "object")
	{
		objBox.disabled = false;
	}
	else if (typeof(objBox) == "string")
		{
			var objs = objBox.split(",");
			for(var i=0;i<objs.length;i++) $(objs[i]).disabled = false;
		}
		else
			return(false);
}

function postdone()
{
	cnt = 0;
}

// 选择所有复选框checkbox
function selectAll()
{
	var s = document.getElementsByTagName("INPUT");
	var ids = '';
	for(var i=0;i<s.length;i++)
	{
		if (s[i].getAttribute("type") == "checkbox" )
		{
			s[i].checked = !s[i].checked;
		}
	}
}

// 给PageController内当前页号着色
function colorCurPage(nPage)
{
	var as = $("PageController");		/*FF是大小写敏感的，这里的元素ＩＤ必须为实际定义值*/
	if (as == null) return;
	var as = as.getElementsByTagName("a");
	for(var i=0; i<as.length;i++)
	{
		if (as[i].innerHTML == nPage.toString())
		{
			as[i].className = 'curpage';
			return;
		}
	}
	return;
}

function getGoogleSearch(sText)
{
	var url = "http://www.google.cn/search?hl=zh-CN&q= " + sText;
	new Ajax.Updater("gsearch",url,{});
}

// 将文章标题及链接地址加入到导航栏
//  \42表示引号
function addTitleToNav(sDiv,nCid, nId,nPage)
{
	var eReload = "new Ajax.Updater(\42" + sDiv + "\42,\42show.asp?cid=" + nCid + "&id=" + nId + "&p=" + nPage +  "\42,{evalScripts:true});";
	try
	{
		$("nav").innerHTML += "<a href='javascript:void(0)' title='刷新本文' onclick='" + eReload + "'>" + $("etitle").innerHTML + "</a>";
	}
	catch (e)
	{
	};
}

// 使IE浏览器页面的元素置最小高度
function setMinHeight()
{
	try
	{
		if ($("main_body") != undefined )
		{
			if ($("main_body").clientHeight - 0 < 600)
			{
				$("main_body").style.height = "600px";
			}
		}

	}catch (e){};
}

// 清除当前页面地址的HASH内容
function clearLocationHash()
{
	location.hash = "#";
}

function loadComment(sID)
{
	new Ajax.Updater("comment","comment.asp?eid=" + sID,{evalScripts:true});
}

function doSubmitComment(sId)
{
	var mycheck = new cFormValidEx("cauthor:w<30,cemail:e>2,csite:u>5,ccontext:w<250");
	if (mycheck.getCheck() == false) return;
	var sURL = "comment_do.asp?a=a&eid=:id&author=:a&email=:e&site=:s&ip=:i&pdate=:p&context=:c&c=:c2";

	sURL = sURL.replace(/:id/i,sId).replace(/:a/i,escape($("cauthor").value)).replace(/:e/i,escape($("cemail").value)).replace(/:s/i,escape($("csite").value)).replace(/:i/,$("cip").value).replace(/:p/,$("cpdate").value).replace(/:c/,escape($("ccontext").value)).replace(/:c2/,$("cccc").value);
	new Ajax.Updater("comment_do",sURL,{evalScripts:true});
	//new Ajax.Request(sURL,{evalScripts:true,onComplete:loadComment(sId)});
	return(false);
}

function fireSearch(obj,event)
{
	if (event.keyCode == 13)		// 按下ENTER键
	{
		getSearch(obj.value);
	}
}

function getSearch(sText)
{
	if (sText.length < 2)
	{
		alert('您提交的搜索文本长度不够！');
		return;
	}

    if ($("ads_left") != null)
    {
        top.document.getElementById("ads_left").src = "cast.html?s=" + sText;
    }
	showLoading(true);
	new Ajax.Updater("main_body","show.asp?cid=*&s=" + escape(sText),{evalScripts:true});
}

function loadIndexPage(objDiv)
{
	try
	{

		if (location.search.length == 0)
		{
			if ($("theme") != undefined)
			{
				var s=$("theme").getElementsByTagName("A");
				setColumnTip(s[0]);
			}
			new Ajax.Updater(objDiv,"index.asp",{evalScripts:true});			
		}
		else
		{
				new Ajax.Updater(objDiv,"show.asp?" + location.search,{evalScripts:true});
		}
	}
	catch(e)
	{
	}
}

function colorSearchText(s)
{
}

function showLoading(bShow)
{
	try
	{		
		$('loading').style.display = bShow?'':'none';
		//if (bShow) $("show_main").innerHTML = "";
	}
	catch (e){};
}

// 加入翻译链接按钮，语言可以根据参数自定义
function showTransButton(containerID, url)
{
    try
    {
        var aryLang = ["en", "fr", "de", "ja"]; 
        var tipsHash = {"en": "English version",
                        "fr": "Française version",
                        "ja": "日本语",
                        "de": "Deutsch version"
                       };

        // not existed then created element which is named "trans"
        if ($(containerID) == undefined)
        {
            if ($("neighbor") != undefined)
            {
                new Insertion.After("neighbor", "<div id='trans'></div>");
            }
            else if ($("nav") != undefined)
            {
                new Insertion.After("nav", "<div id='trans'></div>");
            }
        }
        $(containerID).innerHTML = '';
        for(var i = 0; i < aryLang.length; i++)
        {
            $(containerID).innerHTML += "<a href='/cgi-bin/trans.php?lang=" + aryLang[i] + "&s=" + url + "' title='" + tipsHash[aryLang[i]] + "' target='_blank'>" + tipsHash[aryLang[i]] + "</a>&nbsp;&nbsp;&nbsp;";
        }
    }catch(e){}
}
