var intervalID;
var xmlHttp;
var isLogged = false;
var rowCount = 0; 

// ######################################################################################################
// Testing in LiveChat project use "ChatAJAX.aspx" else production change to "LiveChat/ChatAJAX.aspx"  ##   
// ######################################################################################################    
	
function displayLiveChat()
{
    document.all.idChatMain.style.pixelLeft = 150;
    document.all.idChatMain.style.pixelTop = 316;
    document.all.idChatMain.style.display = 'block';
    	
    createXMLHttpRequest();	

    getRoom(document.all.ctl00_ContentPlaceHolder1_txtFirstName.value, document.all.ctl00_ContentPlaceHolder1_txtFromCity.value + " " + document.all.ctl00_ContentPlaceHolder1_txtFromZipCode.value, document.all.ctl00_ContentPlaceHolder1_txtToCity.value + " " + document.all.ctl00_ContentPlaceHolder1_txtToZipCode.value);
}
function checkEnterKey(type)
{
    if (document.all.idChatMain != null)
    {
        if (document.all.idChatMain.style.display == "block" && window.event.keyCode == 13)
        {
            if (type == "EXECUTE")
                sendMessage();
            // Stop from submitting form
            window.event.returnValue = false
        }
    }
}
function createXMLHttpRequest()  
{
  try {
     // Mozilla, Safari, ... 
     xmlHttp = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       // Microsoft new version
       xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         // Microsoft older version
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (e) {}  
     }
   }
   // This will be called every 1 second to find if a message has been received or sent
   startTimer();
   
   if (document.all.ctl00_ContentPlaceHolder1_UcChat1_Type.value == "Admin")
        document.all.cmdExit.style.display = "none";
}	
function getRoom(_username,_fromCityZip,_toCityZip)
{
    document.all.txtMsg.focus();
    sendAJAX("LiveChat/ChatAJAX.aspx?type=GetRoom" + document.all.ctl00_ContentPlaceHolder1_UcChat1_Type.value + "&CurrentUserID=" + document.all.ctl00_ContentPlaceHolder1_UcChat1_CurrentUserID.value + "&UserName=" + _username + "&FromCityZip=" + _fromCityZip + "&ToCityZip=" + _toCityZip);
}

function Leave()
{
    if (document.all.idChatMain != null)
    {
	    stopTimer();
	    deleteTableRows("idChatTable");
        document.all.idChatMain.style.display = 'none';	
        sendAJAX("LiveChat/ChatAJAX.aspx?type=LeaveRoom" + document.all.ctl00_ContentPlaceHolder1_UcChat1_Type.value + "&CurrentUserID=" + document.all.ctl00_ContentPlaceHolder1_UcChat1_CurrentUserID.value);
    }
}

function deleteTableRows(tableID)
{
    var table = document.getElementById(tableID);

    for(var i = table.rows.length - 1; i > -1; i--)
    {
        table.deleteRow(i);
    }
    // Reset row count to zero for next chat session
    rowCount = 0;
}

function sendMessage()
{
    if (document.all.txtMsg.value.length > 0)
    {
        sendAJAX("LiveChat/ChatAJAX.aspx?type=SendMessageFrom" + document.all.ctl00_ContentPlaceHolder1_UcChat1_Type.value + "&txtMsg=" + document.all.txtMsg.value + "&CurrentUserID=" + document.all.ctl00_ContentPlaceHolder1_UcChat1_CurrentUserID.value + "&UserName=" + document.all.ctl00_ContentPlaceHolder1_txtFirstName.value)
	}
	else
	{
	    alert("Please enter information to send");
	}
    // Clear and focus on text input
    document.all.txtMsg.focus();
    document.all.txtMsg.value="";
}

function startTimer()
{
	intervalID = window.setInterval("updateMessage()",1000);
} 
function stopTimer()
{
	window.clearInterval(intervalID);
	intervalID="";
}
function updateMessage()
{
    sendAJAX("LiveChat/ChatAJAX.aspx?type=UpdateFrom" + document.all.ctl00_ContentPlaceHolder1_UcChat1_Type.value + "&CurrentUserID=" + document.all.ctl00_ContentPlaceHolder1_UcChat1_CurrentUserID.value)
}
// #########################################################################################################
// ################################### AJAX RETURN MESSAGES ################################################
// #########################################################################################################
function sendAJAX(value)  
{
    // true = async calls, false = sync calls
    try
    {
	xmlHttp.open("GET", value, true);
	xmlHttp.onreadystatechange = handleStateChange;	
	xmlHttp.send(null);
    }
    catch(e){} // Ignore error
}
function handleStateChange()  
{
	if ( xmlHttp.readyState == 4 )  {
		if ( xmlHttp.status == 200 )  {
		    if (xmlHttp.responseText.length > 0)
		    {
		        callback(xmlHttp.responseText);
            }

		}
	}
}
function callback(result)
{
    insertTableRow(result);
}
function insertTableRow(message)
{
    var rowColor = null;
    var tableRow = document.all["idChatTable"].insertRow(rowCount);
    // Add 1 for next message to display
    rowCount++;
    
    tableRow.bgColor = rowColor;
    cellCount = 0;
    // Build table cell
    cell = tableRow.insertCell();
    cell.align="left";
    cell.width = "272";
    cell.innerHTML = message;  
    // Scroll to bottom 
    document.all.idChatDiv.doScroll();
}


