﻿var cht_dlg = null;
var cht_dlgsmile = null;
var cht_dlgsmileAns = null;
var cht_blnContinue = true;

var cht_gnQuesID;
var cht_DgLeftPos = 0;
var cht_DgTopPos = 0;
var cht_DgWidthPos = 0;
var cht_DgHeightPos = 0;
var isNextPage =  false;
var cht_gcurPage = 1;
function cht_OpenPopup(evt, strUserId, nQuesID)
{   
    AjaxClass.CheckAnsAmount(nQuesID, Cht_OpenPopup_Cbk);
    cht_gnQuesID = nQuesID;   
    
    if (!evt) var evt = window.event;   
    if (evt.pageX || evt.pageY) {
          cht_DgHeightPos = document.body.clientHeight + window.scrollY;
          cht_DgWidthPos = document.body.clientWidth + window.scrollX;            
          cht_DgLeftPos = evt.pageX;   
          cht_DgTopPos = evt.pageY;          
      }
      else if (evt.clientX || evt.clientY) {
          cht_DgHeightPos = (document.body.clientHeight + document.body.scrollTop);
          cht_DgWidthPos = document.body.clientWidth + document.body.scrollLeft;   
          cht_DgLeftPos = evt.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
          cht_DgTopPos = evt.clientY + document.body.scrollTop + document.documentElement.scrollTop;
      }
}
function Cht_OpenPopup_Cbk(res)
{        
    if(res.value==false)
    {
        alert("Số lượng câu trả lời vượt qua mức cho phép. Nếu bạn muốn tiếp tục hỏi, vui lòng tạo câu hỏi mới.");
        return false;
    }
    
    var is_ie = ( /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent) );
    var fckDlg = 0;
    
    if(gUserName =="")
    {
        alert("Bạn phải đăng nhập trước khi tham gia chat");
        return;
    }
    
    if(lockUser == "True")
    {
        alert("User này tạm thời đã bị khoá. Liên hệ với Admin để được giúp đỡ.");
        return false;
    }
    
	if((cht_DgLeftPos + 300 ) >= cht_DgWidthPos)
	{	    
	    cht_DgLeftPos = cht_DgLeftPos - 310;
	    if(cht_DgLeftPos < 0)cht_DgLeftPos=5;
	}		
	
    if((cht_DgTopPos + 180 ) >= cht_DgHeightPos)
	{
	     cht_DgTopPos = cht_DgTopPos - 190 ;
	    if(cht_DgTopPos < 0)cht_DgTopPos = 5;	    
	}
		    
    
    if(cht_dlg!=null)
    {
        cht_dlg.hide();
    }
    
    var dlg_top;
    cht_dlg=new Dialog("","cht_closeDlg()"); 
    
    var dlg_title = "";
    
    if (is_ie) 
    {
        dlg_title = "<span style=\"margin-top:10px;\" unselectable=\"on\">Trả lời câu hỏi</span>";        
    }
    else
    {
        dlg_title = "<span style=\"margin-top:10px; width:320px;\" unselectable=\"on\">Trả lời câu hỏi</span>";        
    }
    
    var str ="";
    str += "<div class='cht_h5 cht_f4'>&nbsp;</div>";
    str += "    <div class='cht_pdlr4'>";
    str += "        <div style='border:1px solid #c5c5c5; background:#f5f5f5; padding:0px 2px 1px 2px'>";
    str += "            <div align='right'><div id='dvIcSmileAns' title='Biểu tượng vui' class='cht_ic_smile' onclick='ShowHideSmilePnlAns(event, this);'>&nbsp;</div></div>";
    str += "            <div id='divAnsInput' align='center'>";
    str += "                <textarea id='txt_Anwser' align='center' class='cht_txtCtrlAns' rows='5' onkeyup='EnterSendMessage(event, 1, " + cht_gnQuesID + ")'></textarea>";
    str += "            </div>";
    str += "        </div>";
    str += "</div>";
    
    //str += "<div class='cht_h5 cht_f4'>&nbsp;</div>";
    //str += "<div id='dvShowSmileAns' align='left'><ul style='list-style:none'><li class='cht_pt cht_smile' onclick='ShowHideSmilePnlAns(event, this);' style='display:inline'>Biểu tượng vui</li><li onclick='ShowHideSmilePnlAns();' class='cht_pt' style='display:inline'><div class='cht_icsmile'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div></li></ul></div>";
    //str += "<div id='dvSmileIcAns' style='display:none' >"+gstrSmile+"</div>";
    str += "<div class='cht_h5 cht_f4'>&nbsp;</div>";
    str += "<div align='center'>";
    str += "    <div id='btnSendAns' class='cht_btn_oran cht_txtc cht_f9' onclick='btnSendAnws_Click(" + cht_gnQuesID + ");'>Trả lời</div>";
    str += "</div>";    
    
    cht_dlg.create({name:"cht_dlgAns"
                ,title:dlg_title
                ,message:str
                ,width:320
                ,left:cht_DgLeftPos
                ,top:cht_DgTopPos
                ,bgclass:"cht_beta"
                ,barclass:"cht_tlebar"
                ,TitleTopLeft_css:"cht_tdBgrTLDlg"
                ,TitleTopCenter_css:"cht_tdBgrTCDlg"
                ,TitleTopRight_css:"cht_tdBgrTRDlg"
                ,TitleMiddleLeft_css:"cht_tdBgrMLDlg"
                ,TitleMiddleCenter_css:"cht_tdBgrMCDlg"
                , TitleMiddleRight_css:"cht_tdBgrMCRlg"
                ,TitleBottomLeft_css:"cht_tdBgrBLRlg"
                , TitleBottomCenter1_css:"cht_tdBgrBLRlg1"
                , TitleBottomCenter2_css:"cht_tdBgrBLRlg2"
                , TitleBottomCenter3_css:"cht_tdBgrBLRlg3"
                ,TitleBottomRight_css:"cht_tdBgrBRRlg" });	
                				
	cht_dlg.showAt(cht_DgLeftPos+5,cht_DgTopPos+10);
	
	$get('txt_Anwser').focus();
}

function cht_closeDlg(el)
{
    if(!el){ el=cht_dlg;}
    if(el != null){el.hide();}
    Countdict_config=1000;
    cht_closeDlgSmileAns();     
    blnShowSmileAns = false;
}

var cht_timeCount ;
function btnSendQuestion_Click()
{       
    if(gUserName=="")
    {
        alert("Bạn phải đăng nhập trước khi tham gia chat!");
        return false;
    }
    
    if(lockUser == "True")
    {
        alert("User này tạm thời đã bị khoá. Liên hệ với Admin để được giúp đỡ.");
        return false;
    }
    
    if(cht_blnContinue==false)
    {
        alert("Sau 5 giây bạn mới được phép gửi tiếp");
        return;
    }
    
    var strQues = $get(txtQues).value;    
    if (strQues.length > 0)
    {
        if(strQues.length>nMaxChar)
        {
            alert("Bạn không được nhập quá " + nMaxChar + " ký tự ");            
            $get(txtQues).value = strQues.substring(0, (nMaxChar-1));
            return false;
        }
        
        $get("divWait").style.display="";
        
        cht_blnContinue = false;
        cht_timeCount=setTimeout("StopProcess()", 5000);
        window.clearInterval(timeGetMsg);
        timeGetMsg = "";
        var blnChatType = $get(hdnChatType).value;        
        AjaxClass.InsertMessageQues(n_ChatMode, gUserName, $get(txtQues).value, blnChatType, cbk_SendQuestion);
    }
    else
    {
        alert("Bạn chưa nhập nội dung Chat");
        $get(txtQues).value = "";
        return;
    }
    
}

function cbk_SendQuestion(res)
{       
    timeGetMsg = window.setInterval("cht_GetMessages();", gReqTimeOut);    
    if(res.value==null)
    {
        $get("divWait").style.display="none";         
        return;
        
    }
    var nResult = res.value;
    if(nResult == -2)
    {
        alert("Bạn không được nhập các từ tục tĩu hoặc liên quan đến chính trị");                
    } 
    else if(nResult>0)
    {   
        //$get('txtCurPage').value="1";
        cht_gcurPage = 1;
        cht_GetMessages();        
        $get(txtQues).value = "";
        cht_closeDlg();
    }
    $get("divWait").style.display="none";    
    cht_closeDlgSmile();    
    
    if($get("dvIcSmile") != null)
    {
        $get("dvIcSmile").className = "cht_ic_smile";
    }
    blnShowSmile = false;
    
    $get(txtQues).focus();
}

function btnSendAnws_Click(nQuesID)
{   
   if(gUserName=="")
    {
        alert("Bạn phải đăng nhập trước khi tham gia chat!");
        cht_closeDlg();
        return false;
    }
       
   if(lockUser == "True")
    {
        alert("User này tạm thời đã bị khoá. Liên hệ với Admin để được giúp đỡ.");
        cht_closeDlg();
        return false;
    } 
   var strAns = $get("txt_Anwser").value;    
   if (strAns.length > 0)
    {
        if(strAns.length > nMaxChar)
        {            
            alert("Bạn không được nhập quá " + nMaxChar + " ký tự ");
            $get("txt_Anwser").value = strAns.substring(0, (nMaxChar-1));
            return false;
        }
        
        $get("divWait").style.display="";        
        
        window.clearInterval(timeGetMsg);
        timeGetMsg = "";
        AjaxClass.InsertMessageAns(nQuesID, gUserName, $get("txt_Anwser").value, cbk_SendAnws);
    }
    else
    {
        alert("Bạn chưa nhập nội dung Chat");
        $get("txt_Anwser").value= "";
        return;
    }
}

function cbk_SendAnws(res)
{    
    timeGetMsg = window.setInterval("cht_GetMessages();", gReqTimeOut);
    var nResult = res.value;    
    if(nResult == null)
    {
        $get("divWait").style.display="none";        
        return;
    }
    
    if(nResult == -2)
    {
        alert("Bạn không được nhập các từ tục tĩu hoặc liên quan đến chính trị");
    }
    else if(nResult>0)
    { 
        isNextPage = true;
        cht_gcurPage = 1;               
        cht_GetMessages();        
        $get("txt_Anwser").value= "";
        cht_closeDlg();                
    }    
    $get("divWait").style.display="none";
    
    $get(txtQues).focus();
}

function cht_GetMessages()
{
    //var strCurPage = $get('txtCurPage').value;
    /*if(cht_gcurPage == null)
        strCurPage = 1;*/
   AjaxClass.GetMessages(n_ChatMode, cht_gcurPage, oldMaxId, isNextPage, cht_isAdmin, cbk_GetMessages);
}

function cbk_GetMessages(res)
{
      
    if(res.error != null)
        return;
        
    if(res.value == null)
        return;
        
    if(res.value[2] == "")
        return;
    
    
    if(res.value[2] == "-1")
    {               
        isNextPage = false;         
        return;
    }
    else if(res.value[2] == "-2")
    {
        window.clearInterval(timeGetMsg);
        timeGetMsg = "";
        $get('alertNewCont').style.display = "";        
        isNextPage = false; 
        return;
    }
    
    if(cht_gcurPage == 1)
    {    
        oldMaxId = res.value[0];        
        $get('alertNewCont').style.display = "none";
    }
    
    $get(litMessages).innerHTML = res.value[1];
    
    if(res.value[2] != "")
    {
        $get(divQuesBPParentTop).style.display="";
        $get(divQuesBreakPageTop).style.display="";
        $get(divQuesBreakPageTop).innerHTML = res.value[2]; 
        
        var strHTML = res.value[2];       
        $get(divQuesBPParentBot).style.display="";
        $get(divQuesBreakPageBot).style.display="";
        $get(divQuesBreakPageBot).innerHTML = strHTML; 
    }    
    
    isNextPage = false; 
}

function CheckAcceptAnsAmount(nQuesID, nAcceptAns)
{
    var nAmount = AjaxClass.CheckAnsAmount(nQuesID).value;
    if(nAmount == nAcceptAns)
        return false;
    else
        return true;
}

function SelectPage(nPage)
{
    isNextPage = true;
    //$get("txtCurPage").value = nPage;
    cht_gcurPage = nPage;
    cht_GetMessages();
}

function NextPage()
{
    isNextPage = true;   
    cht_gcurPage = cht_gcurPage + 1;
    cht_GetMessages();
}
function PriviousPage()
{   
    cht_gcurPage = cht_gcurPage - 1 ;
    isNextPage = true;
       
    cht_GetMessages();
}

function SearchrWithEnterKey(evt, objThis)
{
    if(evt.keyCode == 13)
    {
        isNextPage = true;
        //$get("txtCurPage").value = objThis.value;
        //$get("txtCurPageBot").value = objThis.value;
        cht_gcurPage = objThis.value;
        cht_GetMessages();
    }
}

function RefreshPage()
{
    timeGetMsg = window.setInterval("cht_GetMessages();", gReqTimeOut);
    //$get("txtCurPage").value = "1";
    cht_gcurPage = 1;
    oldMaxId = "0";    
    isNextPage = false;
    cht_GetMessages();
}

function CalHelpChat()
{
    return;
}

var cht_gmode = 0;
function ShowHideTxt(nMode)
{       
    var objTxt = $get("divTxtCtrl");    
    
    if(objTxt.style.display=="" && cht_gmode == nMode)
    {        
        objTxt.style.display="none";
        objTxt.style.display="none";
        
        $get('btnQuesL').className="cht_btn_l cht_pt";
        $get('btnQuesR').className="cht_btn_r cht_pt";
        $get('btnQuesC').className="cht_btn_c cht_pt";
        
        $get('btnChatL').className="cht_btn_l cht_pt";
        $get('btnChatR').className="cht_btn_r cht_pt";
        $get('btnChatC').className="cht_btn_c cht_pt";
        return;
    }
    else
    {
        objTxt.style.display="";
        objTxt.style.display="";         
    }
       
    if(nMode == 0)
    {
        $get('btnQuesL').className="cht_btn_s_l cht_pt";
        $get('btnQuesR').className="cht_btn_s_r cht_pt";
        $get('btnQuesC').className="cht_btn_s_c cht_cl_B cht_pt";
        
        $get('btnChatL').className="cht_btn_l cht_pt";
        $get('btnChatR').className="cht_btn_r cht_pt";
        $get('btnChatC').className="cht_btn_c cht_pt";
        
        $get(hdnChatType).value = "0";
    }
    else
    {
        $get('btnQuesL').className="cht_btn_l cht_pt";
        $get('btnQuesR').className="cht_btn_r cht_pt";
        $get('btnQuesC').className="cht_btn_c cht_pt";
        
        $get('btnChatL').className="cht_btn_s_l cht_pt";
        $get('btnChatR').className="cht_btn_s_r cht_pt";
        $get('btnChatC').className="cht_btn_s_c cht_pt cht_cl_B";
        
        $get(hdnChatType).value = "1";
    }
    
    cht_gmode = nMode;
    $get(txtQues).focus();
}

function EnterSendMessage(evt, mode, userId)
{
    if(evt.keyCode==13 && evt.shiftKey==false)
    {   
        if(lockUser=="True")return;                    
        if(mode==0)        
            btnSendQuestion_Click();
        else
            btnSendAnws_Click(userId);
    }
}

function btnDeleteQuesAnsById(strMode, strID)
{
    if(!confirm("Bạn muốn xoá message này?")) return;
    
    if(strMode=='ANS')    
        AjaxClass.DeleteMessageSmutty("",strID);
    else    
        AjaxClass.DeleteMessageSmutty(strID, "");        
    
    isNextPage=true;
    cht_GetMessages();
    
}

function StopProcess()
{
    clearTimeout(cht_timeCount);
    cht_blnContinue = true;
}

function $get(elem)
{
    return document.getElementById(elem);
}

function cht_trim(str, chars) {
	return cht_ltrim(cht_rtrim(str, chars), chars);
}
 
function cht_ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function cht_rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function SelectSmile(strFile)
{
    cht_closeDlgSmile();    
    if($get("dvIcSmile") != null)
    {
        $get("dvIcSmile").className = "cht_ic_smile";
    }
    blnShowSmile = false;
    
    var temp = $get(txtQues).value;    
    $get(txtQues).focus();
    $get(txtQues).value = temp + " " + strFile + " ";    
}

function SelectSmileAns(strFile)
{
    cht_closeDlgSmileAns();    
    if($get("dvIcSmileAns") != null)
    {
        $get("dvIcSmileAns").className = "cht_ic_smile";
    }
    blnShowSmileAns = false;
    
    var temp = $get("txt_Anwser").value;    
    $get("txt_Anwser").focus();
    $get("txt_Anwser").value = temp + " " + strFile + " ";    
}
var blnShowSmile = false;
function ShowHideSmilePnl(evt,obj)
{
    if(gUserName =="")
    {   
        return;
    }
    
    cht_closeDlg()
    cht_closeDlgSmileAns()
    if($get("dvIcSmileAns") != null)
    {
        $get("dvIcSmileAns").className = "cht_ic_smile";
        blnShowSmileAns = false;
    }
    
    if(!blnShowSmile)
    {
        if($get("dvIcSmile") !=null)
        {
            $get("dvIcSmile").className = "cht_ic_smileact";        
        }
        blnShowSmile = true;
    }
    else
    {
        $get("dvIcSmile").className = "cht_ic_smile";
        cht_closeDlgSmile(cht_dlgsmile);
        blnShowSmile = false;
        return;
    }
    
    var cht_DgLeftPos = getElementLeft(obj);
    var cht_DgTopPos = getElementTop(obj);    
    if (!evt) var evt = window.event;   
    if (evt.pageX || evt.pageY) 
    {
        cht_DgHeightPos = document.body.clientHeight + window.scrollY;
        cht_DgWidthPos = document.body.clientWidth + window.scrollX;  
    }
    else if (evt.clientX || evt.clientY) 
    {
        cht_DgHeightPos = (document.body.clientHeight + document.body.scrollTop);
        cht_DgWidthPos = document.body.clientWidth + document.body.scrollLeft;   
    }    
    var fckDlg = 0;
    if((cht_DgLeftPos + 300 ) >= cht_DgWidthPos)
    {	    
        cht_DgLeftPos = cht_DgLeftPos - 196;
        cht_DgTopPos = cht_DgTopPos - 98;
        if(cht_DgLeftPos < 0)cht_DgLeftPos=5;
    }		

    if((cht_DgTopPos + 180 ) >= cht_DgHeightPos)
    {
        cht_DgTopPos = cht_DgTopPos - 190 ;
        if(cht_DgTopPos < 0)cht_DgTopPos = 5;	    
    }
    if(cht_dlgsmile!=null)
    {
        cht_dlgsmile.hide();
    }
    
    var dlg_top;
    cht_dlgsmile=new Dialog("","cht_closeDlgSmile()"); 
    
    var dlg_title = "";

    var str =$get(dvSmileIc).innerHTML;

    cht_dlgsmile.create({name:"cht_dlgSmileQues"
                ,title:dlg_title
                ,message:str
                ,width:245
                ,left:cht_DgLeftPos
                ,top:cht_DgTopPos
                ,bgclass:""
                ,barclass:"cht_smile_bar"
                ,TitleTopLeft_css:""
                ,TitleTopCenter_css:""
                ,TitleTopRight_css:""
                ,TitleMiddleLeft_css:""
                ,TitleMiddleCenter_css:""
                , TitleMiddleRight_css:""
                ,TitleBottomLeft_css:""
                , TitleBottomCenter1_css:""
                , TitleBottomCenter2_css:""
                , TitleBottomCenter3_css:""
                ,TitleBottomRight_css:"" });	
                				
	cht_dlgsmile.showAt(cht_DgLeftPos+5,cht_DgTopPos+10);
}

function cht_closeDlgSmile(el)
{
    if(!el){el=cht_dlgsmile; }
    
    if(el!=null){ el.hide();}
    
    Countdict_config=1000;
}
var blnShowSmileAns = false;
function ShowHideSmilePnlAns(evt,obj)
{    
    cht_closeDlgSmile();
    $get("dvIcSmile").className = "cht_ic_smile";
    blnShowSmile = false;
    
    if(!blnShowSmileAns)
    {
        if($get("dvIcSmileAns") !=null)
        {
            $get("dvIcSmileAns").className = "cht_ic_smileact";        
        }
        blnShowSmileAns = true;
    }
    else
    {
        $get("dvIcSmileAns").className = "cht_ic_smile";
        cht_closeDlgSmileAns(cht_dlgsmileAns);
        blnShowSmileAns = false;
        return;
    }
    
    
    var cht_DgLeftPos = getElementLeft(obj);
    var cht_DgTopPos = getElementTop(obj);    
    if (!evt) var evt = window.event;   
    if (evt.pageX || evt.pageY) 
    {
        cht_DgHeightPos = document.body.clientHeight + window.scrollY;
        cht_DgWidthPos = document.body.clientWidth + window.scrollX;  
    }
    else if (evt.clientX || evt.clientY) 
    {
        cht_DgHeightPos = (document.body.clientHeight + document.body.scrollTop);
        cht_DgWidthPos = document.body.clientWidth + document.body.scrollLeft;   
    }    
    var fckDlg = 0;
    if((cht_DgLeftPos + 300 ) >= cht_DgWidthPos)
    {	    
        cht_DgLeftPos = cht_DgLeftPos - 225;
        cht_DgTopPos = cht_DgTopPos - 75;
        if(cht_DgLeftPos < 0)cht_DgLeftPos=5;
    }		

    if((cht_DgTopPos + 180 ) >= cht_DgHeightPos)
    {
        cht_DgTopPos = cht_DgTopPos - 190 ;
        if(cht_DgTopPos < 0)cht_DgTopPos = 5;	    
    }
    if(cht_dlgsmileAns!=null)
    {
        cht_dlgsmileAns.hide();
    }
    
    var dlg_top;
    cht_dlgsmileAns=new Dialog("","cht_closeDlgSmileAns()"); 
    
    var dlg_title = "";

    var str = "<div id='dvSmileIcAns' >"+gstrSmile+"</div>";

    cht_dlgsmileAns.create({name:"cht_dlgSmileAns"
                ,title:dlg_title
                ,message:str
                ,width:245
                ,left:cht_DgLeftPos
                ,top:cht_DgTopPos
                ,bgclass:""
                ,barclass:"cht_smile_bar"
                ,TitleTopLeft_css:""
                ,TitleTopCenter_css:""
                ,TitleTopRight_css:""
                ,TitleMiddleLeft_css:""
                ,TitleMiddleCenter_css:""
                , TitleMiddleRight_css:""
                ,TitleBottomLeft_css:""
                , TitleBottomCenter1_css:""
                , TitleBottomCenter2_css:""
                , TitleBottomCenter3_css:""
                ,TitleBottomRight_css:"" });	
                				
	cht_dlgsmileAns.showAt(cht_DgLeftPos+5,cht_DgTopPos+10);
}

function cht_closeDlgSmileAns(el)
{
    if(!el){el=cht_dlgsmileAns; }
    
    if(el!=null){ el.hide();}
    
    Countdict_config=1000;
}

function getElementLeft(Elem) 
{	
	var elem = Elem;
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
	while (tempEl != null) 
	{
		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
	}
	return xPos;	
}

function getElementTop(Elem) 
{	
    var elem = Elem;
	yPos = elem.offsetTop;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		yPos += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
	}
	return yPos;	
}