var ajax = new AJAX();ajax.setcharset("GBK");

function viewNewsList(){
	set($("newslist"),"新闻加载中...")
	var handleUrl = "inc/ajax.asp?action=newslist";
	ajax.get(
		handleUrl,
		viewNewsListCallBack
	);	
}

function viewNewsListCallBack(obj){
	var resultText=obj.responseText
	if (resultText=="err"){
		set($("newslist"),"<li><font color='red'>发生错误...</font></li>")	
	}else if(resultText=="no"){
		set($("newslist"),"<li>没有数据</li>")	
	}else{
		set($("newslist"),resultText)		
	}
}

function viewNewsContent(id){
	if($("searchtype"))$("searchtype").style.display="none";
	alertFrontWin(101,700,400,20,"新闻内容加载中...")
	var handleUrl = "inc/ajax.asp?action=newscontent&id="+id;
	ajax.get(
		handleUrl,
		function(obj) {
			var resultText=obj.responseText
			if (resultText=="err"){
				set($("msgbody"),"<li><font color='red'>发生错误...</font></li>")	
			}else{
				var result=resultText.split('__maxcms2__')
				set($("msgtitle"),result[0]);set($("msgbody"),result[1])		
			}
		}
	);	
}

function reportErr(id){
	alertFrontWin(101,400,160,50,"")
	set($("msgtitle"),"影片报错")
	set($("msgbody"),"<form id='reporterr' action='/"+sitePath+"inc/ajax.asp?action=reporterr' method='post'><input type='hidden' name='errid' value='"+id+"' /><div><input type='text' id='m_errauthor' name='m_errauthor' value='匿名' size='10'><br><textarea id='m_errcontent'  name='m_errcontent' rows=5 cols=40></textarea><br><input type='button' value='影片报错' onclick='submitErr()' class='btn'></div></form>")	
}

function alertNormal(str,pic){
	alertFrontWin(103,250,100,50,"")
	set($("msgtitle"),"提示信息")
	set($("msgbody"),"<img src='/"+sitePath+"images/"+pic+"' style='vertical-align:middle;margin-right:8px'>"+str+"<div class='cent'><input type='button' value='确   定' onclick='closeWin()' class='btn'></div>")		
}

function alertNormal2(str){
	alertFrontWin(103,250,100,0,"")
	set($("msgtitle"),"提示信息")
	set($("msgbody"),"<img src='/"+sitePath+"images/smile.gif' style='vertical-align:middle;margin-right:8px'>"+str+"<div class='cent'><input type='button' value='确   定' onclick='closeWin()' class='btn'></div>")		
}

function submitErr(){
	if($("m_errauthor").value.length<1){$("m_author").value='匿名';}
	if($("m_errcontent").value.length<1){alert('请填写报错内容');return false;}
	ajax.postf(
		$("reporterr"),
		function(obj){if(obj.responseText=="ok"){closeWin();alertNormal('报错成功,感谢您的支持','smile.gif');}else{alert('发生错误');}}
	);
}

function viewComment(id,page){
	ajax.get(
		"/"+sitePath+"comment.asp?id="+id+"&page="+page,
		function(obj) {
			if (obj.responseText=="err"){
				set($("comment"),"<font color='red'>发生错误</font>")	
			}else{
				set($("comment"),obj.responseText)	
			}
		}
	);		
}

function submitComment(id){
	if($("m_author").value.length<1||$("m_content").value.length<1){alert('请填写完整');return false;}
	ajax.postf(
		$("f_comment"),
		function(obj){if(obj.responseText=="ok"){hide("addform");viewComment(id,1);alertNormal('评论成功','smile.gif');}else if(obj.responseText=="havecomment"){alertNormal('请不要连续提交多次评论！','cry.gif');}else{alert('发生错误');}}
	);
}

function diggVideo(id,div){
	ajax.get(
		"/"+sitePath+"inc/ajax.asp?id="+id+"&action=digg",
		function (obj){
			var returnValue=obj.responseText
			if (!isNaN(returnValue)){alertNormal('牛啊，又被您顶了一下！','smile.gif');set($(div),'[ <em>共'+returnValue+'次</em> ]');}else if(obj.responseText=="err"){alert('顶失败')}else if(obj.responseText=="havescore"){alertNormal('你已经操作过了！','cry.gif')}	
		}
	);	
}

function treadVideo(id,div){
	ajax.get(
		"/"+sitePath+"inc/ajax.asp?id="+id+"&action=tread",
		function (obj){
			var returnValue=obj.responseText
			if(!isNaN(returnValue)){alertNormal('踩死我啦！','cry.gif');set($(div),'[ <em >共'+returnValue+'次</em> ]');}else if(obj.responseText=="err"){alert('踩失败')}	else if(obj.responseText=="havescore"){alertNormal('你已经操作过了！','cry.gif')}	
		}
	);	
}

function alertFrontWin(zindex,width,height,alpha,str){
	openWindow(zindex,width,height,alpha)
	set($("msgbody"),str)
}

function getAspParas(suffix){
	var urlParas=location.search;
	return urlParas.substring(1,urlParas.indexOf(suffix)).split('-')
}


function getHtmlParas(suffix){
	var urlParas=location.search;
	return urlParas.substring(1,urlParas.length).split('-')
}

function handleParas(para1,para2){
	var i,fromArray,len1,len2,urlArray,j,dataStr,dataArray
	if (isNaN(para1) || isNaN(para2)){alert('参数错误');return false}
	fromArray=VideoInfoList.split('$$$')
	len1=fromArray.length;if(para2>len1-1){alert('来源坐标越界');return false}
	for (i=0;i<len1;i++){if (para2==i){urlArray=fromArray[i].split('$$')[1].split('#');len2=urlArray.length;if(para1>len2-1){alert('数据坐标越界');return false};for (j=0;j<len2;j++){if (para1==j){dataStr=urlArray[j];dataArray=dataStr.split('$');return dataArray}}}}
}


function regexpSplice(url,pattern,spanstr) {
   pattern.exec(url);
   return (RegExp.$1+spanstr+ RegExp.$2);
}

function getPageValue(pageGoName){
	var pageGoArray,i,len,pageValue
	pageGoArray=getElementsByName('input',pageGoName) ; len=pageGoArray.length
	for(i=0;i<len;i++){
		pageValue=pageGoArray[i].value;
		if(pageValue.length>0){return pageValue;}
	}
	return ""
}

function getPageGoUrl(maxPage,pageDiv,type){
	var str,goUrl
	var url=location.href
	pageNum=getPageValue(pageDiv)
	//alert(pageNum)
	if (pageNum.length==0||isNaN(pageNum)){alert('输入页码非法');return false;}
	if(pageNum>maxPage){pageNum=maxPage;}
	if(pageNum<1){pageNum=1;}
	switch (type){
		case 1 :
			str=(pageNum==1)?'':"-"+pageNum;
			goUrl=regexpSplice(url,/(http:\/\/\S+\?\d+)[-]{0,1}\d*(.html)/,str);
			break;
		case 2 :
			str=(pageNum==1)?'':pageNum;
			goUrl=regexpSplice(url,/(http:\/\/\S+?)[\d]*(.html)/,str);
			break;
		case 3 :
			str=(pageNum==1)?'':"_"+pageNum;
			goUrl=(url.split('_').length<2)?regexpSplice(url,/(http:\/\/\S+\d+?)(.html)/,str):regexpSplice(url,/(http:\/\/\S+\d+?)_\d+(.html)/,str);
			if(goUrl.indexOf('http://')==-1){goUrl=regexpSplice(url,/(http:\/\/\S+_\d+?)(.html)/,str);}
			break;
	}
	top.location.href=goUrl;
}

function goSearchPage(maxPage,pageDiv,searchtype,searchword){
	var pageNum=$(pageDiv).value
	if (pageNum.length==0||isNaN(pageNum)){alert('输入页码非法');return false;}
	if(pageNum>maxPage){pageNum=maxPage;}
	if(pageNum<1){pageNum=1;}
	top.location.href='?page='+pageNum+'&searchword='+searchword+'&searchtype='+searchtype;
}

function leaveWord(){
	if($("m_author").value.length<1||$("m_content").value.length<1){alert('昵称和内容必须填写');return false;}
	if($("m_qq").value.length>0){if(!checkInput($("m_qq").value,"num")){return false;}};
	if($("m_mail").value.length>0){if(!checkInput($("m_mail").value,"mail")){return false;}};
	ajax.postf(
		$("f_leaveword"),
		function(obj){if(obj.responseText=="ok"){viewLeaveWordList(1);}else if(obj.responseText=="haveleave"){alertNormal('请不要连续提交多次留言！','cry.gif');}else{alert('发生错误1');}}
	);
}

function viewLeaveWordList(page){
	view('leavewordlist');
	ajax.get(
		"/"+sitePath+"gbook.asp?action=list&page="+page,
		function(obj) {
			if (obj.responseText=="err"){
				set($("leavewordlist"),"<font color='red'>发生错误2</font>")	
			}else{
				set($("leavewordlist"),obj.responseText)	
			}
		}
	);	
}

function loginLeaveWord(){
	if($("m_username").value.length<1||$("m_pwd").value.length<1){alert('用户名密码不能为空');return false;}
	ajax.postf(
		$("f_loginleaveword"),
		function(obj){if(obj.responseText=="ok"){closeWin();alertNormal2('登陆成功','smile.gif');setLoginState();viewLeaveWordList(1);}else if(obj.responseText=="no"){alert('用户名密码不正确');}else if(obj.responseText=="err"){alert('发生错误');}else{setLoginState();}}
	);
}

function setLoginState(){
	ajax.get(
		"/"+sitePath+"gbook.asp?action=state",
		function (obj){
			set($('adminleaveword'),obj.responseText);
		}
	);			
}

function logOut(){
	ajax.get(
		"/"+sitePath+"gbook.asp?action=logout",
		function (obj){set($('adminleaveword'),'成功退出');}
	);
	setLoginState();viewLeaveWordList(1);
}

function delLeaveWord(id,page,type){
	ajax.get(
		"/"+sitePath+"gbook.asp?action=del&id="+id+"&type="+type,
		function (obj){
			if (obj.responseText=="ok"){viewLeaveWordList(page)}else if(obj.responseText=="err"){alert('发生错误3')}else{viewLeaveWordList(page);}
		}
	);
}

function replyLeaveWord(id,page){
	alertFrontWin(101,400,160,50,"")
	set($("msgtitle"),"回复留言")
	set($("msgbody"),"<form id='replyleaveword'  action='/"+sitePath+"gbook.asp?action=reply&id="+id+"' method='post'><div><textarea id='m_replycontent'  name='m_replycontent' rows=7 cols=50></textarea><br><input type='button' value='回  复' onclick='submitReply("+page+")' class='btn' /> &nbsp;<span style='background-color:#CCCCCC;cursor:pointer;' onclick=$('m_replycontent').value+='[URL][/URL]'><b>[URL]</b></span></div></form>")	
}

function viewLoginState(){
	alertFrontWin(101,300,100,50,"")
	set($("msgtitle"),"登陆留言板")
	set($("msgbody"),"<form id='f_loginleaveword'  action='/"+sitePath+"gbook.asp?action=login' method='post'  ><table><tr><td>用户名：</td><td><input type='input'  name='m_username' id='m_username' size=10 value=''/></td></tr><tr><td><input type='hidden' value='ok' name='m_login' />密码：</td><td><input type='password' id='m_pwd' name='m_pwd' size=10 value=''/></td></tr><tr><td><input type='submit' value='登  陆' class='btn'  onclick='loginLeaveWord();return false;'  /></td></tr></table></form>")	
}

function submitReply(page){
	if($("m_replycontent").value.length<1){alert('回复不能为空');return false;}
	ajax.postf(
		$("replyleaveword"),
		function(obj){if(obj.responseText=="ok"){closeWin();viewLeaveWordList(page)}else if(obj.responseText=="err"){alert('发生错误4');}else{viewLeaveWordList(page);}}
	);
}

function getVideoHit(vid){
	ajax.get(
		"/"+sitePath+"inc/ajax.asp?action=hit&id="+vid,
		function (obj){
			var result=obj.responseText
			if(result=="err"){set($('hit'),'发生错误')}else{set($('hit'),result);}
		}
	);				
}

function addFavorite(sURL, sTitle){
	try{ window.external.addFavorite(sURL, sTitle);}
		catch (e){
			try{window.sidebar.addPanel(sTitle, sURL, "");}
			catch (e)
				{alert("加入收藏失败，请使用Ctrl+D进行添加");}
		}
}

function setHome(obj,vrl,url){
    try{obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
	this.style.behavior='url(#default#homepage)';this.setHomePage(url);}
        catch(e){
            if(window.netscape){
                try{netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");}  
                   catch (e){alert("此操作被浏览器拒绝！请手动设置");}
                   var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
                   prefs.setCharPref('browser.startup.homepage',vrl);
             }
      }
}

function addFace(id) {
	$('m_content').value += '[maxcms:' + id +']';
}
