﻿var cacheHost="http://www.pipi.cn";
var webHost="http://v.pipi.cn";

/*
 * 随机数生成
 * 
 */
function rnd() { 
	rnd.today=new Date(); 	
	rnd.seed=rnd.today.getTime();
	rnd.seed = (rnd.seed*9301+49297) % 233280; 
	return rnd.seed/(233280.0); 
} 
function rand(number) { 
	return Math.ceil(rnd()*number); 
}
/*
 * 设置Cookie
 * 
 */
function SetCookie(name, value)
//set Cookie value
{
	var expdate = new Date();
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	if(expires!=null) expdate.setTime(expdate.getTime() + ( expires * 1000 ));
	document.cookie = name + "=" + encodeURIComponent (value) +((expires == null) ? "" : ("; expires="+ expdate.toGMTString()))
	+((path == null) ? "" : ("; path=" + path)) +((domain == null) ? "" : ("; domain=" + domain))
	+((secure == true) ? "; secure" : "");
}

function GetCookieVal(offset)
//get Cookie value by encode 
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return decodeURIComponent(document.cookie.substring(offset, endstr));
}

/*
 * del Cookie
 * 删除指定的cookie
 */
function DelCookie(name)
{
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires="+ exp.toGMTString();
}
/*
 * get oragin Cookie value
 * 得到指定的cookie值
 */
function GetCookie(name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen)
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return GetCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}
/*
 * 验证用户提交信息
 *
 */
function validateUserWords(str){
	var re=/[\u4E00-\u9FA5]/;
	if(re.test(str)){return true;}  

	return false;
}

/*
 * 显示用户登录信息
 *
 */
function showUserInfo(){
	var nick = GetCookie('usernickname');
	if(nick!=null){
		document.getElementById("userinfolabel").innerHTML="您好!"+nick;
		document.getElementById("useractionlabel").innerHTML="退出";
		document.getElementById("useractionlabel").title="退出";
		document.getElementById("userspacelabel").innerHTML="个人空间";
		document.getElementById("userspacelabel").href=webHost+"/usermanage/myspace.action"
	}
}
/*
 *  判断用户是否登录
 *
 */
function isUserLogined(){
	var nick = GetCookie('usernickname');
	return nick!=null;
}
/*
 * 复制剪贴榜;负责分享功能使用
 *
 */
function copyToClipBoard(){
   var clipBoardContent='';
   clipBoardContent+=document.getElementById("sharecopytxt").value;
   window.clipboardData.setData("Text",clipBoardContent);
   alert("复制成功，请粘贴到你的QQ/MSN上推荐给你的好友   ");
}

/*
 * get浏览器xmlhttp对象
 *
 */
function getXMLHTTPRequest(){
	var request = false;
	try {
	  request = new XMLHttpRequest();
	} catch (trymicrosoft) {
	  try {
		request = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (othermicrosoft) {
		try {
		  request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
		  request = false;
		}
	  }
	}
	return request;
}

/*
 * 复制剪贴榜;负责分享功能使用
 * htmlpath 静态评论第一页路径
 */
function loadFirstReview(htmlpath){
	var request = getXMLHTTPRequest();
	if(!request){
		return ;
	}
	try{
		request.open("GET", htmlpath, true);
		request.onreadystatechange = 
			function(){
				if(request.readyState==4 && request.status==200 ){
					loadReviewCallback(request.responseText);
				}
			};
		request.send();
	}catch(e){

	}
}
/*
 * 将读取的评论内容显示在页面上
 *
 */
function loadReviewCallback(text){
	document.getElementById("reviewlist").innerHTML=text;
}

/*
 * 显示影片指定的动态评论页面
 * movid 影片id
 * pageno 显示页号
 */
function goMovReviewPage(movid,pageno){
	var page=document.getElementById('pageproxy');
	var proxy = page.contentWindow;
	var url = webHost+"/web/MovReviewPage.action?movId="+movid+"&pageno="+pageno+"&dd="+new Date().getTime();
	proxy.loadWebPageText(url,loadReviewCallback);
}
/*
 * 用户对指定影片评分操作
 * movid 影片id
 * 
 */
function doMovieMark(movid){
	var list = document.getElementsByName("MovieMarkSelect");
	var score=0;
	for(var i=0;i<list.length;i++){
		if(list[i].checked){
			score=list[i].value;
		}
	}
	if(score==0){
		alert('请选择该影片的评分值!');
		return ;
	}
	var page=document.getElementById('pageproxy');
	var proxy = page.contentWindow;
	var url = webHost+"/web/MovMark.action";
	var params={movId:movid,score:score};
	if(proxy.postDataWithJsonResult==null){
		window.status="网站动态功能错误";
		return;
	}
	proxy.postDataWithJsonResult(url,params,doMovieMarkCallBack);
}
/*
 * 用户对指定影片评分操作返回处理
 * jsontxt Json格式返回结果
 * 
 */
function doMovieMarkCallBack(jsontxt){
	var jrt = eval('('+jsontxt+')');

	if("false"==jrt['success']){
		alert("错误："+jrt['message']);
	}else{
		alert("评分完成,谢谢您的参与!");
	}
}

/*
 * 用户对指定影片评分操作
 * movid 影片id
 * 
 */
function commentPost(movid,content){
	
	if(""==content){
		alert("请填写评论内容");
		return ;
	}
	var page=document.getElementById('pageproxy');
	var proxy = page.contentWindow;
	var url = webHost+"/web/MovReview.action";
	var params={movId:movid,content:(content)};
	if(proxy.postDataWithJsonResult==null){
		window.status="网站动态功能错误";
		return ;
	}
	proxy.postDataWithJsonResult(url,params,commentPostCallBack);
}
/*
 * 用户发评论操作返回处理
 * jsontxt Json格式返回结果
 * 
 */
function commentPostCallBack(jsontxt,params){
	var jrt = eval('('+jsontxt+')');
	
	if("false"==jrt['success']){
		alert(jrt['message']);
	}else{
		alert("评论完成,谢谢您的参与!");
		document.getElementById("comment_txt").value="";
		goMovReviewPage(params['movId'],1);
	}
}

// Check is empty or null
function isEmpty(str) {
	if(str == null || str == "") return true;
	return false;
}

// radio is checked
function checkRadio(id){
	var obj = document.getElementsByTagName("input");
	for(var i = 0; i < obj.length; i++){
		if(obj[i].type == "radio" && obj[i].id == id){
			if(obj[i].checked == true)
				return true;
		}
	}
	return false;
}


/*
 * 载入用户求片分页
 * pageno分页号
 * 
 */
function goMovRequestPage(pageno){
	var htmlpath = cacheHost+"/request/new/"+pageno+".html";
	var request = getXMLHTTPRequest();
	if(!request){
		return ;
	}
	try{
		request.open("GET", htmlpath, true);
		request.onreadystatechange = 
			function(){
				if(request.readyState==4 && request.status==200 ){
					loadMovRequestPageCallback(request.responseText);
				}
			};
		request.send();
	}catch(e){

	}
}
/*
 * 载入用户求片分页回调函数
 * text 取得的页面内容
 * 
 */
function loadMovRequestPageCallback(text){
	document.getElementById("movrequestlist").innerHTML=text;
}
/*
 * 载入求片第一个分页
 * 
 */
function loadFirstRequestPage(){
	goMovRequestPage(1);
}
/*
 * 载入用户求片排行分页
 * pageno分页号
 * 
 */
function goMovRequestTopPage(pageno){
	var htmlpath = cacheHost+"/request/top/"+pageno+".html";
	var request = getXMLHTTPRequest();
	if(!request){
		return ;
	}
	try{
		request.open("GET", htmlpath, true);
		request.onreadystatechange = 
			function(){
				if(request.readyState==4 && request.status==200 ){
					loadMovRequestTopPageCallback(request.responseText);
				}
			};
		request.send();
	}catch(e){

	}
}
/*
 * 载入用户求片排行分页回调函数
 * text 取得的页面内容
 * 
 */
function loadMovRequestTopPageCallback(text){
	document.getElementById("toprequestlist").innerHTML=text;
}
/*
 * 载入求片排行第一个分页
 * 
 */
function loadFirstTopRequestPage(){
	goMovRequestTopPage(1);
}
/*
 * 用户求片同求操作
 * 
 */
function requestFollow(id) {
	if(isRequestVoted(id)){
		alert("不能重复投票!");
		return ;
	}
	var page=document.getElementById('pageproxy');
	var proxy = page.contentWindow;
	var url = webHost+"/web/RequestFollow.action";
	var params={id:id};
	proxy.postDataWithJsonResult(url,params,requestFollowCallBack);
}
/*
 * 用户求片同求操作回调
 * 
 */
function requestFollowCallBack(jsontxt,params){
	var jrt = eval('('+jsontxt+')');
	
	if("false"==jrt['success']){
		alert(jrt['message']);
	}else{
		document.getElementById('movrequest_'+params['id']).innerHTML=jrt['message'];
		alert("同求完成,谢谢您的参与!");
		logRequestVote(params['id']);
	}
}
function isRequestVoted(id){
	var idstr=GetCookie("request_voted_tracker");
	if(idstr==null||idstr==""){
		return false
	}else{
		var ids = idstr.split(",");
		for(var i=0;i<ids.length;i++){
			if(id==ids[i]){
				return true;
			}
		}
	}
	return false;
}
function logRequestVote(id){
	var idstr=GetCookie("request_voted_tracker");
	if(idstr==null||idstr==""){
		idstr=id;
	}else{
		idstr+=","+id;
	}
	SetCookie("request_voted_tracker",idstr);
}

/*
 * 用户求片发布
 * 
 */
function requestPost(){
	if(!isUserLogined()){
		if(!confirm("你需要在登录(注册)之后才能发表求片信息!")){
			return;
		}else{
			parent.document.location=webHost+'/usermanage/login.jsp';
			return;
		}
	}
	var movname = document.getElementById('reqmovname');
	var reqreason = document.getElementById('reqreason');
	if(movname.value==""||reqreason.value==""){
		alert('请完整填写求片信息');
		return ;
	}
	if(!validateUserWords(reqreason.value)){
		alert('填写信息过于简单');
		return ;
	}

	var page=document.getElementById('pageproxy');
	var proxy = page.contentWindow;
	var url = webHost+"/web/UserRequest.action";
	var params={movName:(movname.value),reason:(reqreason.value)};
	if(proxy.postDataWithJsonResult==null){
		window.status="网站动态功能错误";
		return ;
	}
	proxy.postDataWithJsonResult(url,params,requestPostCallBack);

}
/*
 * 用户求片发布回调
 * 
 */
function requestPostCallBack(jsontxt){
	var jrt = eval('('+jsontxt+')');
	var movname = document.getElementById('reqmovname');
	var reqreason = document.getElementById('reqreason');
	movname.value="";
	reqreason.value="";
	alert(jrt['message']);
}

/*
 * 用户浏览视频记录
 * 
 */

 function trackerMov(trackername,id){
	if(!isUserLogined()){
		return;
	}
	var idstr=GetCookie(trackername);
	if(idstr==null||idstr==""){
		idstr=id;
	}else{
		var ids = idstr.split(",");
		idstr=id;
		for(var i=0;i<ids.length;i++){
			if(id!=ids[i]){
				idstr=idstr+","+ids[i];
			}
			if(i>=100)
				break;
		}
	}
	SetCookie(trackername,idstr,2000000,'/',document.domain);
 }
 /*
 * 删除用户浏览视频记录
 * 
 */
 function delTracker(name,id){
	var idstr=GetCookie(name);
	if(idstr!=null&&idstr!=""){
		var ids = idstr.split(",");
		idstr="";
		var b=true;
		for(var i=0;i<ids.length;i++){
			if(id!=ids[i]){
				if(b){
					idstr=ids[i];
					b=false;
				}else{
					idstr=idstr+","+ids[i];
				}
			}
		}
	}
	SetCookie(name,idstr,2000000,'/',document.domain);
 }
 /*
 * 删除用户视频记录
 * 
 */
 function toDelTracker(name,id){
	if(confirm('确认删除?')){
		delTracker(name,id);
		window.location.reload();
	}
 }
 /*
 * 删除用户视频
 * 
 */
 function toDelUserMov(id){
	if(!confirm('确认删除?')){
		return;
	}
	var request = getXMLHTTPRequest();
	if(!request){
		return ;
	}
	try{
		request.open("GET", webHost+"/web/delUserMov.action?movId="+id, true);
		request.onreadystatechange = 
			function(){
				if(request.readyState==4 && request.status==200 ){
					DelUserMovCallback(request.responseText);
				}
			};
		request.send();
	}catch(e){

	}
 }
 function DelUserMovCallback(text){
	window.location.reload();
 }
 
 /*
 * load movie list callback function;
 *
 */
function loadHomePageListCallback(listhtml){
	var listdiv = document.getElementById('homemovielist');
	if(listdiv){
		listdiv.innerHTML=listhtml;
	}
}

/*
 * load movie list to homepage;
 *
 */
function loadHomePageList(listurl){
	var request = getXMLHTTPRequest();
	if(!request){
		return ;
	}
	try{
		request.open("GET", listurl, true);
		request.onreadystatechange = 
			function(){
				if(request.readyState==4 && request.status==200 ){
					loadHomePageListCallback(request.responseText);
				}
			};
		request.send();
	}catch(e){

	}
}
/*
 * 评论回复
 * 
 */
function doreviewreply(reviewid){
	var reviewtag = document.getElementById('reviewContent_'+reviewid);
	var commentbox = document.getElementById('comment_txt');
	if(reviewtag==null||commentbox==null){ return ; }
	commentbox.value="〖引用:"+reviewtag.innerHTML+"〗";
	commentbox.focus();
	document.getElementById('comment_tip_div').style.display='none';
}

function showPipiDown() {
	document.write('<div class="Download">');
	document.write('<dl>');
	document.write('	<dt>皮皮电影播放器下载</dt>');
	document.write('	<dd>');
		document.write('		<a target="_blank" href="http://dl.pipi.cn/pipi.exe" title="本地">本地</a>&nbsp;|');
	document.write('		<a target="_blank" href="http://www.itxiazai.com/soft/2008/04/18/25405.html" title="IT世界 ">IT世界 </a>&nbsp;|');
	document.write('		<a target="_blank" href="http://rd.qihoo.com/qd.html?u=http%253A%252F%252Fdl.pipi.cn%252Fpipi_safe_36.exe&t=1226293715&c=1&a=9&p=6003&s=0db95951b312b0e8b82a615f8d749013" title="360安全卫士版 ">360安全卫士版</a>|');
	document.write('		<a target="_blank" href="http://www.crsky.com/soft/11320.html" title="霏凡">霏凡</a>|');
	document.write('		<a target="_blank" href="http://dl.pconline.com.cn/html_2/1/109/id=50157&pn=0.html?ad=2352" title="太平洋">太平洋</a>|');
	document.write('		<a target="_blank" href="http://download.tech.qq.com/soft/62/63/45387/index.shtml" title="腾讯">腾讯</a>|');
	document.write('		<a target="_blank" href="http://download.it168.com/179/185/107116/index.shtml" title="IT168">IT168</a>');
	document.write('	</dd>');
	document.write('</dl> ');
	document.write('</div>');
}