var ie = document.all ? 1 : 0;
var ns4 = document.layers ? 1 :0;
var dodiv = 0;
var alphaChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var digitChars = "0123456789";
var asciiChars = alphaChars + digitChars + "!\"#$%&'()*+,-./:;<=>?@[\]^_`{}~";

function isASCII(str)
{
	var v_len = str.length;
	var i;
	for (i = 0; i < v_len; i++)
	{
		if (asciiChars.indexOf(str.charAt(i)) == -1)
		return false;
	}
	return true;
}

function assertChecked(element, title)
{
	if(element == null)
		return true;
		
	if(!element.length)	
		element = new Array(element);
		
	for(var  i = 0; i < element.length; i++)
	{
		if(element[i].checked)
			return true;
	}
	
	alert("请选择 \"" + title + "\"！");
	element[0].scrollIntoView();
	
	return false;
}

function checkNotEmpty(element, title, alertIfInvalid)
{
	if(element == null)
		return true;
		
	if(alertIfInvalid == null)	
		alertIfInvalid = true;
		
	if(element.value == "")
	{
		if(alertIfInvalid)
		{
			alert("\"" + title + "\" 内容不能为空！");

			//inner Editor
			en = element.name;
			ie = document.all("BUDDY_" + en);
			if(ie)
			{
				try
				{
					ie.scrollIntoView();
					ie.focus();
				}
				catch(e2)
				{
				}
			}
			else
			{
				try
				{
					element.scrollIntoView();
					element.focus();
				}
				catch(e2)
				{
				}
			}
		}
		return false;
	}
	
	return true;
}

function checkEquals(e1, t1, e2, t2, alertIfInvalid)
{
	if(alertIfInvalid == null)	
		alertIfInvalid = true;
		
	if(e1 != null && e2 != null && e1.value != e2.value)
	{
		if(element.value == "")
		{
			alert("\"" + t1 + "\" " + "与 \"" + t2 + "\" 内容不一致！");
			e2.focus();
		}
		return false;
	}
	return true;
}

function changeOrgin()
{
	if(tipFrm.ORIGINMODE.selectedIndex == 0)
	{
		tipFrm.PROVIDER.disabled = true;
	}
	else
	{
		tipFrm.PROVIDER.disabled = false;
	}
	
	return true;
}

function checkProvider()
{
	if(tipFrm.ORIGINMODE.selectedIndex == 1 && tipFrm.PROVIDER.value == "")
	{
		alert("\"稿源出处\" 内容不能为空！");
		tipFrm.PROVIDER.focus();
		return false;
	}
	return true;	
}

function checkYear(year, title)
{
	var temp = parseInt(year.value,10);
	if(!isNaN(temp) && temp>1000 && temp<9999)
		return true;	

	alert("\"" + title + "\" 不正确!");
	year.focus();
	return false;
}

// check if the item is email or not
function checkEmail(element, title)
{
	var str = element.value;
	var resultStr = str.replace(/ /gi, "");
	var atIndex = resultStr.indexOf("@");
	var dotIndex = resultStr.lastIndexOf(".");
	
	if( resultStr == "" || 
		!isASCII(resultStr) || 
		dotIndex == -1 ||
		resultStr.lastIndexOf("@") != atIndex || 
		resultStr.charAt(atIndex+1) == "." ||
		atIndex <= 0 || 
		dotIndex < atIndex || 
		dotIndex >= resultStr.length - 1)
	{
		alert("\"" + title + "\" 内容不是合法的邮件地址！");
		element.focus();
		
		return false;
	}
	
	return true;
}

function getFileTitleFromFilePath(fileName)
{
	li1 = fileName.lastIndexOf('.');
	if(li1 < 0)
		li1 = fileName.length;
	
	li2 = fileName.lastIndexOf('\\') + 1;
	li3 = fileName.lastIndexOf('/') + 1;
	
	li4 = (li2 < li3 ? li3 : li2);
	
	return fileName.substring(li4, li1);
}

function showMainPage()
{
	top.location.href = "/main.jsp";
}

function showManagePage()
{
	top.location.href = "/manage/";
}

function showPersonalPage()
{
	top.location.href = "/personal.jsp";
}

function refreshMenuPage()
{
	try
	{
		if(top != null && top.frames.length != 0 && top.frames("leftMenu") != null)
		{
			top.frames("leftMenu").location.href = top.frames("leftMenu").location.href;
		}
	}
	catch(e)
	{
	}
}

function switchSysBar()
{
	if (switchPoint.innerText == 3 )
	{
		switchPoint.innerText = 4;
		document.all("frmTitle").style.display = "none";
	}
	else
	{
		switchPoint.innerText = 3;
		document.all("frmTitle").style.display = "block";
	}
}

function clearBoard(bid)
{
	if(confirm("是否确认清空该版块?\r\n此操作将删除该版块下所有的文章和投票。"))
	{
		location.href = "/master/clearBoard.jsp?bid=" + bid;
	}
}

function deleteBoard(bid)
{
	if(confirm("是否确认删除该版块?\r\n\r\n此操作将同时删除该版块下的所有子版块及帖子。"))
	{
		location.href = "/manage/deleteBoard.jsp?bid=" + bid;
	}
}

function deleteChildrenBoards(bid)
{
	if(confirm("是否确认删除该版块下的所有子版块?"))
	{
		location.href = "/manage/deleteBoard.jsp?bid=" + bid + "&withMe=0";
	}
}

function publishTip(tipid)
{
	if(confirm("是否确认发表该草稿?"))
	{
		location.href = "/user/publishTip.jsp?tipid=" + tipid;
	}
}

function deleteTip(tipid)
{
	if(confirm("是否确认删除该帖子?\r\n\r\n如果删除主帖，该贴的所有回复和连载内容都会被自动删除。"))
	{
		location.href = "/user/deleteTip.jsp?tipid=" + tipid;
	}
}

function deleteReply(tipid, topid)
{
	if(confirm("是否确认删除该回复?\r\n\r\n如果删除该帖，该贴的所有回复内容都会被自动删除。"))
	{
		location.href = "/user/deleteTip.jsp?tipid=" + tipid + "&topid=" + topid;
	}
}

function toggleBody(tr)
{
	var table = tr.parentElement;
	var collapsed = table.rows(1).style.display == "none";
	var image = tr.all("node");
	image.src = (collapsed ? "/images/open.gif" : "/images/closed.gif");
	table.rows(1).style.display = (collapsed ? "block" : "none");
	table.rows(2).style.display = (collapsed ? "block" : "none");
}

function setBestTip(bid, tipid, enable)
{
	if(confirm(enable == 1 ? "是否确认将该记录收录至精华区?" : "是否确认将该记录返回至普通区?"))
	{
		location.href = "/master/setTip.jsp?bid=" + bid + "&tipid=" + tipid + "&op=1&enable=" + enable;
	}
	else
	{
		masterFunctionList.selectedIndex = 0;
	}
}

function setTopTip(bid, tipid, enable)
{
	if(confirm(enable == 1 ? "是否确认将该记录置顶?" : "是否确认取消置顶该记录?"))
	{
		location.href = "/master/setTip.jsp?bid=" + bid + "&tipid=" + tipid + "&op=2&enable=" + enable;
	}
	else
	{
		masterFunctionList.selectedIndex = 0;
	}
}

function setLoneTip(bid, tipid, enable)
{
	if(confirm(enable == 1 ? "是否确认将该记录设置为不可回复?" : "是否确认该记录设置为可回复?"))
	{
		location.href = "/master/setTip.jsp?bid=" + bid + "&tipid=" + tipid + "&op=4&enable=" + enable;
	}
	else
	{
		masterFunctionList.selectedIndex = 0;
	}
}

function checkTip(bid,tipid)
{
	if(confirm("是否确认审批该记录?"))
	{
		location.href = "/master/setTip.jsp?bid=" + bid + "&tipid=" + tipid + "&op=3";
	}
}

function download(id)
{
	window.open(
		"/shared/download.jsp?id=" + id,
		"downloadWindow",
		"width=510,height=328,status=0,resizable=1,titlebar=0,center=1");	
}

function toggle(nodeName)
{
	node = document.all.item(nodeName);
	
	if(node != null)
	{
		if(node.style.display == "none")
		{
			node.style.display = "block";
		}
		else
		{
			node.style.display = "none";
		}
		node.scrollIntoView();		
	}
}

function insertUpload(pathField, titleField, idField, parameters)
{
	var params = new Array(null, null, -1, parameters, window);

	var oFile = showModalDialog(
		"/editor/dialogs/upload.htm",
		params,
		"dialogWidth:450px;dialogHeight:150px;status:0;help:0;center:1");
		
	if(oFile && oFile.actionStatus == 200)
	{
		if(pathField)
		{
			pathField.value = oFile.filePath;
		}
		if(titleField)
		{
			titleField.value = oFile.title + oFile.fileSize;
		}
		if(idField)
		{
			idField.value = oFile.id;
		}
	}
	
	return oFile;
}

function doSearch(searchFrm, keywords)
{
	searchFrm.keywords.value = keywords;
	if(searchFrm.page)
		searchFrm.page.value = 1;
	searchFrm.submit();	
}

function doSearch2(searchFrm, a)
{
	searchFrm.keywords.value = a.innerText;
	if(searchFrm.page)
		searchFrm.page.value = 1;
	searchFrm.submit();	
}

function gotoPage(searchFrm, page)
{
	searchFrm.page.value = page;
	searchFrm.submit();	
}

function selectProvider(a)
{
	tipFrm.PROVIDER.value = a.innerText;
}

function selectWriter(a)
{
	tipFrm.WRITER.value = a.innerText;
}

function formatAll()
{
	highlightCodes();
	setImageBehavior();
}

function highlightCodes()
{
	var e1 = document.getElementsByTagName("TEXTAREA");
	if(e1 == null || e1.length == 0)
		return;
		
	if(!e1.length)
		e1 = new Array(e1);
		
	var e2 = new Array();
	for(var i = 0 ; i < e1.length; i++)
	{
		var ta = e1[i];
		//if(ta.readOnly)
		if(ta.value.length < 40000)
		{
			ta.className = "cpp";
			e2[e2.length] = ta;
		}
	}
	
	dp.SyntaxHighlighter.HighlightAll(e2, true, true, false, null, null);
}

function setImageBehavior()
{
	var e1 = document.getElementsByTagName("IMG");
	if(e1 == null || e1.length == 0)
		return;
		
	if(!e1.length)
		e1 = new Array(e1);
		
	var e2 = new Array();
	for(var i = 0 ; i < e1.length; i++)
	{
		var ta = e1[i];
		if(ta.width > 100 && ta.height > 100 && ta.onclick == null)
		{
			//查找是否包含在<a>内部
			var pn = ta.parentNode;
			if(pn == null || pn.tagName != "A")
			{
				ta.className = "vcerImage";
				ta.title = "[点击查看大图]";
				ta.attachEvent("onclick", onImageClick);
			}
		}
	}
	
	dp.SyntaxHighlighter.HighlightAll(e2, true, true, false, null, null);
}

function onImageClick()
{
	window.open(event.srcElement.src);
}