﻿function ContentValidate()
{
    if($('txtContent').value.length==0)
    {
        alert("您还没有填写评论内容！");
        return false;
    }
    var Value=$('txtContent').value;
    if(Value.length<10)
    {
        alert("您的评论小于了10个字！")
        return false;
    }
    if(Value.length>5000)
    {
        alert("您的评论大于了5000个字！");
        return false;
    }
   return true;
}
function getRadioValue(str){
    for (var i = 0; i < document.getElementsByName(str).length; i++){
	    if (document.getElementsByName(str)[i].checked){
		    return document.getElementsByName(str)[i].value;
	    }
    }
    return "";
}
function RadioValidate(sName,sTitle)
{
    var obj=document.getElementsByName(sName);
    for(var i=0;i<obj.length;i++)
    {
        if(obj[i].checked)
        {
            return true;
        }
    }
    alert(sTitle);
    return false;
}
function AvgValidate()
{
    var Value=document.getElementById("txtAvg").value;
    if(!IsNull('txtAvg'))
    {
        alert("您还没有填写人均！");
        return false;
    }
    var Rex=/^[0-9]*$/;
    if(Rex.test(Value))
    {
        return true;
    }
    alert("人均请输入整数");
    return false;
}
function IsNull(sId)
{
    var Value=document.getElementById(sId).value;
    if(Value!="")
    {
        return true;
    }
    return false;
}
function ContentLength()
{
    var val=$('txtContent').value;
    var obj=document.getElementById("leftnum");
    obj.innerHTML=(5000-val.length);
}
function PostComment(){
    var resname=$('h-mu-name').value;
    if(resname.length<2){
        alert("")
    }
    if(!IsNull('adentityId')) {
        alert("系统未找到您要点评的餐厅！");
        return false;    
    }
    if(!RadioValidate('rblAppraise','您还没有对餐厅做评价！')){
        return false;
    }
    if(!IsNull('taste')) {
        alert("您还没有为餐厅口味打分呢！");
        return false;    
    }
    if(!IsNull('environment')) {
        alert("您还没有为餐厅环境打分呢！");
        return false;    
    }
    if(!IsNull('service')) {
        alert("您还没有为餐厅服务打分呢！");
        return false;    
    }
    if(!AvgValidate()) return false;
    if(!ContentValidate()) return false;

	Mbox.openLite('<p class="Doing" style="line-height:30px;">正在发表您的评论，请稍候......</p>',280,30);
	
	PostData = 'do=postcomment&ad='+$('adentityId').value
	    +'&ap='+getRadioValue('rblAppraise')
	    +'&ta='+$('taste').value
	    +'&en='+$('environment').value
	    +'&se='+$('service').value
	    +'&av='+$('txtAvg').value
	    +'&di='+$('txtDish').value.cnEncode()
	    +'&pa='+$('txtParking').value.cnEncode()
	    +'&co='+$('txtContent').value.cnEncode()
	    +"&ti="+$('txtTitle').value.cnEncode();
	    
	new AjaxReq({
		url: "/postcomm.aspx",
		method: 'post',
		data: PostData,
		onSuccess: function(data) {
			Mbox.close();
			if(data.code==200){
				location.href='/'+$('adentityId').value+'/comment.aspx';
			}
			else if(data.code==401){
			    alert(data.msg);
			}
		}
	}).send();
}
function CloseBox(url){
    Mbox.close();
    window.location.href=url;
}