

// JavaScript File



var updateTimer;
var checkinNumber;

var checkinDelay = 1500;
var usersListInterval = 4;

var lastUsersUpdate;

function StartChat()
{
    lastMessageID = -1;
    lastUsersUpdate = usersListInterval;

    checkinNumber = 0;
    clearInterval(updateTimer);
    updateTimer = setInterval("GetUpdate();", checkinDelay);
    
    if (userColor == "")
        SetUserColor("#000000");
}


function GetUpdate()
{
    checkinNumber++;
    
    // prevents a common memory leak
    if (checkinNumber == 25)
    {
        clearInterval(updateTimer);
    
        checkinNumber = 0;
        updateTimer = setInterval("GetUpdate();", checkinDelay);
    }
    
    if (isUpdating)
        return;
    
    SetUpdating();
    
    var postbackUrl = "/appchat/ajax/update.aspx";
    postbackUrl += "?roomid=" + roomID;
    postbackUrl += "&userid=" + userID;
    postbackUrl += "&lastmsg=" + lastMessageID;
    postbackUrl += "&updateusers=";
    
    if (lastUsersUpdate == usersListInterval)
    {
        lastUsersUpdate = 1;
        postbackUrl += "true";
    }
    else
    {
        lastUsersUpdate++;
        postbackUrl += "false";
    }
    
    GetAjaxContent(postbackUrl, HandleUpdate);
}

function HandleUpdate(responseText)
{
    if (responseText == "ERROR")
        return;
    
    if (responseText == "APPLICATION_RESET")
    {
        setTimeout("SubmitLogin();", 2000);
        return;
    }

	var updateParts = responseText.split('[break]');
	var index = 0;
	var safety = 0;
	
	// Read up until the message section starts
	while (updateParts[index] != "--------------------MESSAGES--------------------" && safety < 1000 && index < updateParts.length)
	{
		index++;
		safety++;
	}
	index++;
	
	// Handle the message updates
	if (updateParts[index] != "--------------------USERS--------------------")
	{
		AddMessages(unescape(updateParts[index]));
		index++;
	}
	
	// Read up until the users list updates
	while (updateParts[index] != "--------------------USERS--------------------" && safety < 1000 && index < updateParts.length)
	{
		index++;
		safety++;
	}
	index++;
	
	// Handle the users list updates
	if (updateParts[index] != "--------------------REDIRECTS--------------------")
	{
		SetUsers(unescape(updateParts[index]));
		index++;
	}
	
	// Read up until the redirects
	while (updateParts[index] != "--------------------REDIRECTS--------------------" && safety < 1000 && index < updateParts.length)
	{
		index++;
		safety++;
	}
	index++;
	
	// Handle the redirects
	if (updateParts[index] != "--------------------INVITATIONS--------------------")
	{
		HandleRedirects(unescape(updateParts[index]));
		index++;
	}
	
	// Read up until the invitations
	while (updateParts[index] != "--------------------INVITATIONS--------------------" && safety < 1000 && index < updateParts.length)
	{
		index++;
		safety++;
	}
	index++;
	
	// Handle the invitations
	if (updateParts[index] != null)
	{
		HandleInvitations(unescape(updateParts[index]));
		index++;
	}
	
	SetIdle();
}

