﻿/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/

var filesDir = "../chatRoomDir/";

function XHConn()
{

  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, filesDir + sURL+"?"+sVars, true);
        sVars = "";     
    }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          
     
          
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


var lastLineId = 0;

var conLogin = new XHConn();
var readData = new XHConn();
var sendData = new XHConn();
var getInitalData = new XHConn();
var imHere = new XHConn();

window.onload = function()
{
    conLogin.connect("loginAuto.aspx","GET","",LoginAutoRes);    
    getInitalData.connect("getIntialData.aspx","GET","",GetInitialDataRes);
}
window.onunload= function()
{
    if(!document.all)
        Logout();
}

window.onbeforeunload= function()
{        
    Logout();
}

function Logout()
{
        conLogin.connect("logout.aspx","GET","",LogoutRes);
}

function LogoutRes()
{

}

function LoginMember()
{
    
    var rem = "";
    
    if( document.getElementById('chatRoomWindowLoginChkRemeberMe').checked == true )
        rem = "t";        
   
    conLogin.connect("login.aspx","GET","n="+document.getElementById('chatRoomWindowLoginTxtNickName').value + 
    "&p=" + document.getElementById('chatRoomWindowLoginTxtPassword').value + "&r="+rem,LoginMemberRes);    
}

function LoginGuest()
{
    conLogin.connect("loginGuest.aspx","GET","",LoginGuestRes);
}


function LoginAutoRes(txt)
{
    var tt = txt.responseText;
        
    if(tt =="y")
    {
        LogedInGood();
    }
    else
    {
        LogedInBad();
    }
        
}

function LoginGuestRes(txt)
{

    
    var tt = txt.responseText;  
           
    if(tt =="OK")
    {
        LogedInGood(); 
    }
    else
    {
        LogedInBad();
    }
}


function LoginMemberRes(txt)
{
    if(txt.responseText == "OK")
    {
        LogedInGood();
    }
    else
    {
        LogedInBad();
    }
}

function LogedInGood()
{
        setInterval("imHere.connect('imHere.aspx','GET','',ImHereRes);",55000);
        EnableChatRoom();  
        CloseWindow('chatRoomWindowLogin');
}

function LogedInBad()
{
        DisableChatRoom();
        OpenWindow('chatRoomWindowLogin');
}


function GetInitialDataRes(txt)
{

    var aSplit = txt.responseText.split('~+~');
    
    var bSplit = aSplit[0].split('~`~');
    
    for(var i = 0 ; i < bSplit.length - 1 ; i++)
    {
        document.getElementById('chatRoomRightSide').innerHTML +="<b id=chatRoomRightSideUser-"+bSplit[i]+">"+ bSplit[i] + "<br></b>";
    }
    
    var cSplit = aSplit[1].split('~`~');
    
    for(var i = 0 ; i < cSplit.length -1 ; i = i + 3)
    {
        document.getElementById('chatRoomLeftSide').innerHTML += "<b>" + cSplit[i] + " : </b>" + cSplit[i+1] + "<br>";
    }
    
    lastLineId = aSplit[2];
       
    readData.connect("readMsgs.aspx","GET","l="+lastLineId,readMsgsRes);
}

function readMsgsRes(txt)
{
   
   
    if(txt.responseText != "n")
    {
        var aSpliter = txt.responseText.split('~`~');
                
        for(var i = 0 ; i < aSpliter.length -1 ; i = i + 4 )
        {
            lastLineId++;
            
            if(aSpliter[i+1] == "msg")
            {
              document.getElementById('chatRoomLeftSide').innerHTML += "<b>" + aSpliter[i] + " : </b>" + aSpliter[i+2] + "<br>";
            }
            else if(aSpliter[i+1] == "joi")
            {
              document.getElementById('chatRoomRightSide').innerHTML += "<b id=chatRoomRightSideUser-" + aSpliter[i] + ">"+ aSpliter[i] + "<br>";
            }
            else if(aSpliter[i+1] == "lft")
            {
               document.getElementById('chatRoomRightSide').removeChild(document.getElementById('chatRoomRightSideUser-' + aSpliter[i]));
            }

        }
    }
    
    setTimeout("readData.connect('readMsgs.aspx','GET','l='+lastLineId,readMsgsRes);",2000);
   
}


function OpenWindow(eleId)
{
    document.getElementById(eleId).style.display = "block";
}

function CloseWindow(eleId)
{
    document.getElementById(eleId).style.display = "none";
}

function DisableChatRoom()
{
    document.getElementById('chatRoomTextBox1').disabled = "disabled";
    document.getElementById('chatRoomBtnSend').disabled = "disabled";
}

function EnableChatRoom()
{
    document.getElementById('chatRoomTextBox1').disabled = "";
    document.getElementById('chatRoomBtnSend').disabled = "";
}



function ChatRoomSendMsg(text)
{
    var intResult1 = 0;
    var intResult2 = 1;
    
    intResult1 = text.indexOf('~`~');
    intResult2 = text.indexOf('~+~');
    
    if( (intResult1 != -1) || (intResult2 != -1) )
    {
        alert("please don't use ~`~ or ~+~ combinations . thanks .");
    }
    else if( text == "" )
    {
        alert("please don't send empty messages . thanks .");
    }
    else
    {
        sendData.connect("addMsg.aspx","GET","m="+text,ChatRoomSendMsgRes);
        document.getElementById('chatRoomTextBox1').value = "";
        document.getElementById('chatRoomTextBox1').select();
    }
    
}

function ChatRoomSendMsgRes(txt)
{
    if(txt.responseText != "y")
    {
        alert("error");
    }
    
}


function ImHereRes(txt)
{

}

//EVENTS

document.getElementById('chatRoomTextBox1').onkeypress = function(e)
{
    
    var key = "";
    if(!e) e = window.event;
    if( e.keyCode )  key = e.keyCode ;
    else if ( e.which ) key = e.which ; 
    
    if ( key == 13 )
    {
          ChatRoomSendMsg(document.getElementById('chatRoomTextBox1').value);
              return false;    

    }
    

}