
  
function WriteMessage()
{
    if (Name == "")
    {
        ShowError("Please enter your name or handle.");
        $w('txtName').focus();
        return;
    }
    
    $w('btnSend').disabled = true;
    
    ChatService.WriteMessage($w('txtMessage').value,Name,ChatId,OnWriteCallback,OnError);
}
function OnWriteCallback(Result)
{
    $w('btnSend').disabled = false;
    
    var Ctl = $w('txtMessage');
    Ctl.value = '';
    Ctl.focus();
    
    GetMessages(true);
}
function GetMessages(Force)
{
    // Don't allow requests to fire too frequently
     if (Force!=true && new Date().getTime() - PingFrequency < LastCallbackTime.getTime() )
       return;
              
    // Keep track of last callback time     
    LastCallbackTime = new Date();        
    
    // *** Get a new instance to make sure there's no overlap
    ChatService.GetMessages(ChatId,Name,GetMessagesCallback,OnError);
}
function GetMessagesCallback(Result)
{        
    // *** Chain callback - Get next messages in x Seconds
    window.setTimeout(GetMessages,PingFrequency);

    if (Result == null || Result == "")
        return;
    
    var Ctl = $w('lblMessages');
    
    var Message = Ctl.innerHTML;
    Ctl.innerHTML = Message + Result;        
    
    $w('divMessages').scrollTop = 999999;     
}
function OnGetMessagesError(Error)
{
    // *** Chain callback - Get next messages in x Seconds
    window.setTimeout(GetMessages,PingFrequency);    
    OnError(Error);
}
function GetActiveUsers()
{
    ChatService.GetActiveUsers(ChatId,GetActiveUsersCallback);   
}
function GetActiveUsersCallback(Result)
{
    $w('divUserList').innerHTML = Result;
}
function PopupChatList()
{
     ChatService.GetChatListHtml(GetChatListCallback);   
}
function GetChatListCallback(Result)
{
    if (Result == null || Result == "")
        return;
    
    var Ctl = new wwControl('divChatList');    
    Ctl.setHtml(Result + 
                    "<hr /><div style='text-align:right'><a href='javascript:SetChatId()'>Close</a></div>" );

    Ctl.element.style.display='';
    Ctl.element.position='absolute';
    
    var CtlChatId = new wwControl("txtChatId");
    CtlChatId.element.style.position='absolute';
    var txtBounds = CtlChatId.getBounds();
    CtlChatId.element.style.position='';
    
    var Bounds = { x: 0, y: 0, height: 0, width: 0 }
    Bounds.y = txtBounds.y + txtBounds.height;
    Bounds.x = txtBounds.x ;
    Bounds.width = txtBounds.width  - 12;
    Bounds.height = -1;  // don't set
    
    Ctl.setBounds(Bounds);
    
    Ctl.element.style.zIndex = 99;

    Ctl.showShadow(.25,3);
}
function SetChatId(NewChatId)
{
    if (NewChatId != null)
    {  
        $w('txtChatId').value = NewChatId;
        ChatId = NewChatId;
    }
    
    GetActiveUsers();
    
    if (ChatId != "")
    {
        var Ctl = $w('lblMessages');
        Ctl.innerHTML = '';
    }
    
    var Ctl = new wwControl("divChatList");
    if (Ctl.isNull)
       return;
    Ctl.hideShadow();
}
function NameChanged(NewName)
{
    if (NewName != Name)
    {
        // *** If we have a new name
        Name = NewName;
        ClearMessages();
    }
}
function ChatIdChanged(NewChatId)
{
    if (NewChatId != ChatId)
    {
        ChatId = NewChatId;
        ClearMessages();       
    }
}
function ClearMessages()
{
        var Ctl = $w('lblMessages');
        Ctl.innerHTML = "";
        $w('divMessages').scrollTop = 999999; 
}
function OnError(Result)
{
   ShowError("An error occurred: "  + Result.message);
   $w('btnSend').disabled = false;
}
function ShowError(Message,ShowInfoMessage)
{
    var Ctl = new wwControl("lblErrorMessage");
    if (ShowInfoMessage)
        Ctl.setHtml(" <img src='../images/info.gif'> " + Message );
    else
        Ctl.setHtml( " <img src='../images/warning.gif'>  &nbsp;&nbsp;" + Message );
    
    Ctl.element.style.position="absolute";
    Ctl.fadein(4);
    
    window.setTimeout("HideError()",5000);
}
function HideError()
{
    var Ctl = new wwControl("lblErrorMessage");
    Ctl.fadeout(2);
}

// *** Startup Code *** 

// *** Check active users on a lengthy fixed interval
window.setInterval("GetActiveUsers()",20000);

// *** Run a slow safety loop in case the main message chaining
//     gets stuck. 
window.setInterval("GetMessages(true)",25000);

// *** Initial display of values
window.setTimeout("GetActiveUsers()",1000);
window.setTimeout("GetMessages()",500);
     