function recmdToFriendsPage(productId,productName){
 var url = '/product/recmdToFriendsPage.do?';
  var param="productId="+productId+"&productName="+encodeURIComponent(productName);

  new Ajax.Updater("listFriend", url, { method: 'post',parameters: param, evalScripts: true});

}

function recmdToFriends(){
 var url = '/product/recommendToFriends.do?';
 var param=decodeURIComponent(Form.serialize('recommendForm'));
var myAjax = new Ajax.Request( url, { method: 'post', parameters: param, onComplete: recommendResult});

}


function recommendResult(originalRequest){
var status=originalRequest.responseText;
 if(status==null){
   alert("推荐失败，请稍重新尝试!");
   return;
 }
 if(status==-1)
 {
   alert("至少选择一个好友！");
   return;
 }
 if(status==-2){
   alert("推荐失败，请稍重新尝试!");
   return;
 }
 if(status==1){
    alert("推荐成功");
    return;
 }
return;
}


function getPComments( productId, isFriend,typeId,curPage) {

	var url = '/product/pcomment/getProductComments.do?';
 
    if(isFriend!=-1){
      url=url+'isFriend=' + isFriend+'&';
    }
    if(typeId){
    
    url=url+'typeId=' + typeId+'&';
    }
    if(curPage){
     url=url+'pageVO.currentPage=' + curPage+'&';
    }
    url=url+'productId=' + productId;
   
    new Ajax.Updater("goodsinforcom", url, { method: 'post', evalScripts: true});
}




function digSubmitIndexAjax(productId,type) {
   
   var url;
  if(type==1){
     url = "/product/ifHasUsed.do";}
 else if(type==2){
     url = "/product/ifWantToUse.do";
   }else{
   return;
   }
	var pars = 'productId=' + productId;
	var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, onComplete: digCompleteIndex});
}


function digCompleteIndex(originalRequest)
{
   
    var str=originalRequest.responseText.split("AND");
    var response_status=str[0];
    var response_actionType=str[1];

	showDigTextIndex(response_status,response_actionType); 
	  
}

function showDigTextIndex(response_status,response_actionType)
{
 
 if(response_status==-9)
	{
	  alert("请先登录！");
	  return;
	}
 
  if(response_status==1&&response_actionType==1)
	{
	
	
	$("hasUsed").innerHTML ="投票买过";
	
	updateCount('hasBuyCount',1);
	new Effect.Pulsate("hasUsed", {duration:2.0});
	disableA($("hasUsed"));
	}
	
	else if (response_status==-1&&response_actionType==1)
	{
	$("hasUsed").innerHTML ="已经投票";
	
	new Effect.Pulsate("hasUsed", {duration:2.0});
	disableA($("hasUsed"));
	}
	else if(response_status==1&&response_actionType==2)
	{
	
	
	$("wantUse").innerHTML ="投票想买";
	
	updateCount('wantBuyCount',1);
	new Effect.Pulsate("wantUse", {duration:2.0});
	disableA($("wantUse"));
	}
	
	else if (response_status==-1&&response_actionType==2)
	{
	$("wantUse").innerHTML ="已经投票";
	new Effect.Pulsate("wantUse", {duration:2.0});
	disableA($("wantUse"));
	
	
	}

}

function disableA(o){
if(o){
o.onclick="event.cancelBubble=true;event.returnValue=false;return false"
}

}

function setCommentFocus(){
	
	var form = $('commentForm');
	if(form){
		for(var i =0;i<form.elements.length;i++){
			if(form.elements[i].type == "textarea" && form.elements[i].name=="content"){
				form.elements[i].focus();
				return;
			}
		}
	}
}


function logonCallBack(result, callid){
	if(result==null){
		return;
	}
	//var loginArea = $('div_login');
	if(result==true){
		new Ajax.Updater("welcome","/user/loginCommit_2",{method:'get'});
	}else{
		alert('登录失败.');
	}
}

//评论

function ifMakeACommentAry(){

 var param=Form.serialize('commentForm');
	new Ajax.Request(
	  "/product/pcomment/ifMakeACommentAry.do",
	  {method: 'post', 
	   parameters: param, 
	   onSuccess: function(o){
	  
		 var status=o.responseText;
		 if(status==-1){
		 
		    finalComment(); 
		  }
		  else if(status==0){
		  finalComment();
		    
		  }
		  else if(status==1){
		    if(confirm('每个用户对同一个产品只能做出一条点评，您新发表的点评内容和分数将取代原来的点评内容和分数。')){
		      finalComment();
		    }else{
		      return false;
		    }
		  }
	   }
	  }
	);
  
}
function comment(islogin){
	var t_username = '';
	var t_password = '';
	if(!islogin&&$('login_div').innerHTML!=""){
		if($('username').value=='' || $('username').value=='Email或昵称'){
			alert('请填写用户名.')
			$('username').focus();
			return false;
		}else{
			t_username = $('username').value;
		}
		if($('grade1').value==-1||$('grade2').value==-1||$('grade3').value==-1){
			alert('请选择全部投票信息.')
			return false;
		}
		if($('password').value==''){
			alert('请填写密码.')
			$('password').focus();
			return false;
		}else{
			t_password = $('password').value;
		}
	}

	var t_content = $('content').value;
	if(checkCommentContent(t_content) == false){
		return false;
	}

   ifMakeACommentAry();
	
	return false;
}

function finalComment(){
	var param=Form.serialize('commentForm');
	new Ajax.Request(
	  "/product/pcomment/addPComment.do",
	  {method: 'post', 
	   parameters: param, 
	   onSuccess: function(o){
	  
		 commentCallBack(o.responseText);
	   }
	  }
	);
}




function checkCommentContent(content){
    if(content==""){
		alert('请填写评论内容');
		$('content').focus();
		return false;
	}
	if(content.length>2000){
		alert('评论内容不能超过2000字');
		return false;
	}
	if($('grade1').value==-1||$('grade2').value==-1||$('grade3').value==-1){
			alert('请选择全部投票信息.')
			return false;
		}
	return true;
}

function commentCallBack(result){
	
	var item = result;
     if(checkCommentCallBackResult(result,$('content')) == false){
		return ;
	 }
	 
	var loginArea = $('login_div');
	if(loginArea){
		loginArea.innerHTML='';
		loginArea.style.display = "none";
	}
   if(result==-6){
	 
	   window.location.reload(); 
	}else{
	showNewComment(item,$('content'));
	window.scrollTo(0,$('goodsinforcom').offsetTop);

	}
	
	//$('content').value="";
   
}


function showNewComment(item,txtContent){
   
	var newCommentArea = $('div_new_comment');
    var newCommentHTML=item;
	
	newCommentArea.innerHTML = newCommentHTML+newCommentArea.innerHTML;

	//更新页面记数
	updateCount('commentCount',1);
	updateCount('pageCcount',1);



}
function updateCount(oId,num){
	var o = $(oId);
	if(o==undefined || o ==null)return;
	if(isNaN(o.innerHTML)){
		if(num > 0){
			o.innerHTML = num;
		}else{
			o.innerHTML = 0;
		}
	}else{
		o.innerHTML = parseInt(o.innerHTML)+num;
	}
}


function checkCommentCallBackResult(result,txtContent){
	if(result==null){
		return false;
	}
	if(result==10){
	 alert('评论发表成功。');
		return false;
	}
	if(result==-1){
		alert('评论发表失败。'); 
		return false;
	}
	if(result==-2){
		alert('内容不能为空。');
		return false;
	}
	if(result==-4){
		alert('您不能输入太多评论内容，请减少内容。');
		return false;
	}
	if(result==-3){
		alert('您的用户名/密码输入有误。');
		return false;
	}
	if(result==-9){
		alert('您的用户名尚未激活。');
		return false;
	}
	if(result==-5){
		alert('您已经被限制发表评论，若有疑问，请通过网站客服反馈意见，谢谢。');
		return false;
	}
	if(result==-7){
		alert('您的评论含有网站禁止内容，请修改，谢谢！');
		txtContent.value='';
		return false;
	}

	return true;
}


function displayPoint(num,score,imgUrl1,imgUrl2,imgUrl3){
  
  var sr="",s;
  if(isNaN(score)){
   return;
  }else{
    if(score.toString().indexOf(".")>-1){
      var h1=score.toString().indexOf(".");
      var h2=Number(score.toString().substring(h1+1,h1+2));

     if(h2>=0&&h2<=2){
       s=1;
      }
      if(h2>=3&&h2<=7){
       s=2;
      }
      else{
       s=3
      }
    }else{
      s=4;
    }
  }
  for(var i=0;i<num;i++){
    if(i<Math.floor(score))
    sr+="<img src="+imgUrl1+" style='cursor: hand;'/>";
    else{
       if(s==3){
         sr+="<img src="+imgUrl1+" style='cursor:hand;'/>";
         s=-1;
       }
       else if(s==2){
         sr+="<img src="+imgUrl2+" style='cursor: hand;' />";
        s=-1;
       }
       else{
         sr+="<img src="+imgUrl3+" style='cursor:hand;'/>";
         }
    }
  }

  document.write(sr);
  
}

var mypretime=0;

function formsubmit() { 
Today = new Date(); 
var NowHour = Today.getHours(); 
var NowMinute = Today.getMinutes(); 
var NowSecond = Today.getSeconds(); 
var mysec = (NowHour*3600)+(NowMinute*60)+NowSecond; 
	if((mysec-mypretime)>600) 
//600只是一个时间值，就是5分钟内禁止重复提交.
	{ 
		 mypretime=mysec; 
	} 
	else 
	{ 
		alert("按一次就够了，请勿重复提交！请耐心等待！谢谢合作!");
		return false; 
	} 
    return true;
} 

function showGrade(id){
	if ($(id))
	$(id).style.display="block"
}
function hiddenGrade(id){
	if ($(id))
	$(id).style.display="none"
}

function showDesc(fun){
	if(fun=="all"){
		document.getElementById('partDesc').style.display='none';
		document.getElementById('allDesc').style.display='block';
	}
	else{
		document.getElementById('partDesc').style.display='block';
		document.getElementById('allDesc').style.display='none';
	}
}


