 /****************************************************************************************************
 * *
 * *        File Name		:hotelComment.js
 * *        Creator		: zhuzhijia
 * *        Create Time		: 2008-10-30
 * *        Functional Description  :  酒店点评相关JS
 * *        Remark      : 
 * *
 * *  Copyright (c) eLong Corporation.  All rights reserved. 
 * ****************************************************************************************************/  
     
     //页面ajax请求url
     var requestUrl="http://dianping.elong.com/HotelComment/HotelCommentPage/PublishRequest.aspx";
   
     var isVote=false;
     var voteName=null;
     
    //发表页面初始化 
    function initPage()
    {
        //dojo.byId('divHotelName').innerHTML="&nbsp;&nbsp;发表对 &lt;"+dojo.byId('hHotelName').value+"&gt; 的评论（<span class='red'>*</span>为必填项）";
       $("#btnpublish").attr("disabled", true);
       checkUserStatus();
    }

    //提交点评的callback
    function publishCallBack(data)
    {
        $("#progressbar").progressbar('option', 'value', 100);
        if(data=="ok")
        {           
            var msg= '<b>点评提交成功！</b></br></br>';
            msg+='<p>';
            msg+='非常感谢您对<b>';
            msg += $("#hHotelName")[0].value + '</b>的点评，请点击下面的按钮查看该酒店的所有点评。</br></br></p>';

            var title = '点评系统提示';
            var str = "<div><span>" + msg + "</span><br/><br/>";
            str += "<input type=\"button\" value=\"返回查看详细页\" onclick=\"hideDialog()\"/>";
            str += "</div>";

            var dialogDiv = document.createElement("div");
            dialogDiv.setAttribute("id", "input-dialog");
            dialogDiv.innerHTML = "<div>" + str + "</div>";
            document.body.appendChild(dialogDiv);

            $("#input-dialog").dialog({
                title: title,
                width: 500,
                height: 230,
                maxHeight: 230,
                maxWidth: 500,
                modal: true,
                close: function(event, ui) {
                    hideDialog();
                }
            });

            $("#input-dialog").dialog("open");
            
            //djAlerta(title, str);
            
            //commentAlert(msg,true);
        }
        else if(data=="no")
        {
            commentAlert("发表失败",false);
        }
        else if(data=="hasdirtyword")
        {
            commentAlert("您的点评内容已经提交，经我们审核后会发表在点评频道，请稍后再查看。", false);
        }
        else{
            nologinNotice(data);
        }
    }
        
    //检查页面输入 
    function checkEmpty()
    {  
        if(isVote)
        {
            if(getRadioBoxValue('radioCommend') == "undefined" )
            {
                commentAlert("请您选择：是否愿意将此酒店推荐给您的朋友。",false);
                return false;
            }
        }
        if ($("#txtCommentContent")[0].value == "") {
            commentAlert("发表内容不能为空，请填写您的评论后再提交。", false);
            return false;
        }
        if ($("#txtCommentContent")[0].value.length > 2000) {
            commentAlert("点评内容最多不超过2000汉字,请您稍做修改后再行提交。", false);
            return false;
        }        
        if ($("#txtNickName")[0].value == "") {
            commentAlert("您选择了使用昵称发表，请填写您的昵称后再提交。", false);
            $("#txtNickName").focus();
            return false;
        }
        if ($("#txtNickName")[0].value.length > 10) {
            commentAlert("昵称最多不超过10汉字", false);
            $("#txtNickName").focus();
            return false;
        }
        return true;   
    }
        
    //提交点评
    function publishHotelComment() {
        if ($("#btnpublish").attr("disabled") == true) {
            return false;
        }
        else {
            var nurl = requestUrl + "?AReqID=publish&hotelID=" + $("#hHotelID")[0].value + "&rand=" + Math.random();

            if (checkEmpty()) {
                $("#progressbar").progressbar({
                    value: 0
                });
                if (isVote) {
                    var vcontent = voteName;
                    var vcontent = eval('(' + voteName + ')');
                    $.ajax({
                        type: "post",
                        url: nurl,
                        data: vcontent,
                        success: publishCallBack
                    });
                }
                else {
                    $.ajax({
                        type: "post",
                        url: nurl,
                        data: { txtCommentContent: encodeURI($("#txtCommentContent")[0].value), IsAnonymity: 1, nickName: encodeURI($("#txtNickName")[0].value) },
                        success: publishCallBack
                    });
                }

                setTimeout(function() {
                    var value = $("#progressbar").progressbar('option', 'value');
                    $("#progressbar").progressbar('option', 'value', value + 10);
                }, 200);
            }
            return false;
        }
    }
    
    //获取投票UI页面值
    function getVoteName()
    { 
         $.ajax( {
            type: "GET",
            url: requestUrl+"?AReqID=votename&rand="+Math.random(),
            success: function(data){ 
                   voteName = data;
            }
            //timeout: 2000
        });
    }
    
    //获取投票UI页面创建
    function setVote()
    {
        $.ajax({
            type: "GET",
            url: requestUrl + "?AReqID=vote&rand=" + Math.random(),
            success: function(data) {
                alert(data);
            }
        });
    }
    
    //检查用户状态
    function checkUserStatus()
    {
         $.ajax( {
            type: "GET",
            url: requestUrl+"?AReqID=checkuser&hotelID="+$("#hHotelID")[0].value+"&rand="+Math.random(),
            success: nologinNotice
            //timeout: 2000
        } );
    }
    
    // 用户状态相关处理
    //  返回标识符(nologin:未登录;vote:允许投票;Comment;允许点评);
    function nologinNotice(n)
    {
        switch(n)
           {
               case "nologin":
                    var loginurl=$("#loginurl")[0].value
                    loginDialogs(loginurl, true)
                    //dojo.byId('btnpublish').disabled="disabled";
                   break;
               case "vote" :  
                    //setVote();
                    isVote=true;
                    getVoteName();
                    $("#btnpublish").attr("disabled", false);
                    //dojo.byId('spannum3').innerHTML="3";
                    //dojo.byId('spannum4').innerHTML="4";
                    break;
                case "Comment":
                    $("#btnpublish").attr("disabled", false);
                    break;
                case "":
                    commentAlert("网络问题，请稍后重试",false);
                    break;
                default:
                    commentAlert(n,false);
                    break;
           }
    }
    
    //取Radio值
    function  getRadioBoxValue(radioName)  
    {  
        var obj = document.getElementsByName(radioName);            
        for(i   =   0;   i   <   obj.length;   i++)  
        {
            if(obj[i].checked)    {  
              return   obj[i].value;  
        }  
        }          
        return "undefined";        
    } 
     //关闭提示框 并提示到页
     function hideDialog()
     {
         $("#btnpublish").attr("disabled", true);
         $("#input-dialog").dialog("destroy");
         var dialogDiv = document.getElementById("input-dialog");
         document.body.removeChild(dialogDiv);
         $("#progressbar").progressbar('destroy');
         window.location.href = $("#hHotelDetails")[0].value;
     }
     //关闭提示框
     function hide() {
         $("#input-dialog").dialog("close");
     }
     //检查字符串长度
    function checkStrLen(str)
    {
        var i,sum;
        sum=0;
        for(i=0;i<str.length;i++)
        {
            if ((str.charCodeAt(i)>=0) && (str.charCodeAt(i)<=255))
                sum=sum+1;
            else
                sum=sum+2;
        }
        return sum;
    }
        
   //提示框
   //msg:提示内容(带格式),是否跳转到详细页(是true,否false)
    function commentAlert (msg,redirect) {
        var title='点评系统提示';
        var str = "<div><span>" + msg + "</span><br/><br/>";
        if (redirect) {
            str += "<input type=\"button\" value=\"返回查看详细页\" onclick=\"hideDialog()\"/>";
        }
        else {
            str += "<input type=\"button\" value=\"返回\" onclick=\"hide()\"/>";
        }
        str += "</div>";
        djAlerta(title,str);
    }
    
  
   
   //登录及判断用户状态提示框
   function loginDialogs(src,redirect)
   {
   
        var iframe='<table><tr><td><iframe frameborder=0  src="'+src+'" width=480 height=250px ></iframe></td></tr>';
        if(redirect){
            iframe += '<tr><td align=center><input type=\"button\" onclick=\"hideDialog();\" value=\"返回查看详细页\"/></td></tr>';
        }
        else{
            iframe += '<tr><td align=center><input type=\"button\" onclick=\"destroy();\" value=\"返   回\"/></td></tr>';
        }
        iframe+='</table>';
        if (redirect) {
            LogInDia("点评系统提示", iframe);
        }
        else{
            LogInDia("点评系统提示", iframe);
        }
    }
    function destroy() {
        $("#input-login").dialog("destroy");
        var logindiv = document.getElementById("input-login");
        document.body.removeChild(logindiv);
    }
   function LogInDia(title, content) {
       var logindiv = document.createElement("div");
       logindiv.setAttribute("id", "input-login");
       logindiv.innerHTML = "<div>" + content + "</div>";
       document.body.appendChild(logindiv);
       $("#input-login").dialog({
           title: title,
           close: function(event, ui) {
               destroy();
           },
           width: 580,
           height: 340,
           maxHeight: 350,
           maxWidth: 600,
           modal: true
       });

       $("#input-login").dialog("open");
   }
   //关闭后跳转到指定URL的提示框,title:标题 msg:内容 cancelurl:跳转url 
   function djAlert(title,msg)
   {    
        var dialogDiv = document.createElement("div");
        dialogDiv.setAttribute("id","input-dialog");
        dialogDiv.innerHTML = "<div>"+msg+"</div>";
        document.body.appendChild(dialogDiv);

        $("#input-dialog").dialog({
            title: title,
            close: function(event, ui) {
                $("#input-dialog").dialog("destroy");
                document.body.removeChild(dialogDiv);
                $("#progressbar").progressbar('destroy');
            },
            width: 350,
            height: 150,
            maxHeight: 150,
            maxWidth: 350
        });
        
        $("#input-dialog").dialog("open");
   }
   
    //提示框,titulo:标题,msg:内容　
    function djAlerta (titulo,msg) {
        djAlert(titulo,msg);
    }

    //带URL提示框 titulo:标题,url:要显示的页面
    function djPopUp (titulo,url) 
    {
        djAlert(titulo,msg);
    }
    
    //筛选价格和星级
    function setFilter(type,currentIndex)
    {
        if(type=="star")
        {
            dojo.byId('Star').value=currentIndex;
            setCurrentPageIndex(0);
        }
        else if(type=="price")
        {
            dojo.byId('Price').value=currentIndex;
            setCurrentPageIndex(0);
        }
    }
    
    //排序
    function setSortType(type,id)
    {
        document.getElementById("SortType").value=type;
        document.getElementById("VoteModelItemID").value=id;
        document.getElementById("Type").value=2;
        document.forms["aspnetForm"].action="FiltListComment.html";
        document.forms["aspnetForm"].submit(); 
    }
    
    //分页
    function setCurrentPageIndex(num)
    {
        document.getElementById("CurrentPageIndex").value=num;
        document.forms["aspnetForm"].action="FiltListComment.html";
        document.forms["aspnetForm"].submit();
    }
    
    //城市查找
    function queryByCity()
    {
         document.getElementById("ReqType").value="";
        document.forms["aspnetForm"].action="listComment.html";
        document.forms["aspnetForm"].submit();
    }