function WebChatClient(session_id,participant_id,entryURL)
{
    this.RESULT_OK = "RESULT_OK";
    this.RESULT_FAILED = "RESULT_FAILED";
    this.RESULT_NO_SESSION = "RESULT_NO_SESSION";

    this.session_id = session_id;
    this.participant_id = participant_id;
    this.lastChatMessage = 0;
    this.previousLastChatMessage = -1;
    this.lastParticipantJoined = 0;
    this.message = Array();
    this.participant = Array();
    
    this.entryURL = entryURL;
    this.onChatMessagesReceived = null;
    this.onSendMessageCompleted = null;
    this.onParticipantsReceived = null;
    this.onResultNoSession = null;
    this.onAfterUpdate = null;
    
    this.getInitialJsonDataObject = function(){
        var jsonData = new Object();
        jsonData.clientLastChatMessage = this.lastChatMessage;
        jsonData.clientLastParticipantJoined = this.lastParticipantJoined;
        jsonData.session_id = this.session_id;
        jsonData.participant_id = this.participant_id;

        return jsonData;
    }
    
    this.sendMessage = function(message){

        var jsonData = this.getInitialJsonDataObject();
        jsonData.method = "sendMessage";
        jsonData.message = message;
        
        var request = new Request.JSON({
            url: this.entryURL,
            noCache: true,
            link: 'chain',
            method: 'get',
            onSuccess: function(responseJSON,responseText){
                if( responseJSON.resultCode == this.instance.RESULT_OK )
                {
                    if( this.instance.onSendMessageCompleted != null )
                    {
                        this.instance.onSendMessageCompleted(this.instance);
                    }
                }
                else
                {
                    alert("WebChatClient::sendMessage(): Failed on resultCode: " + responseJSON.resultCode);
                }
                this.instance.parseResponseJSON(responseJSON);
            },
            onFailure: function(xhr){
                alert("WebChatClient::sendMessage(): Failed");
            },
            onException: function(headerName,value){
                alert("WebChatClient::sendMessage(): Exception: " + headerName + "=" + value);
            }
        });
        request.instance = this;
        request.send("WebChatJSON=" + JSON.encode(jsonData));
    }

    this.update = function(){

        var jsonData = this.getInitialJsonDataObject();
        jsonData.method = "update";

        var request = new Request.JSON({
            url: this.entryURL,
            noCache: true,
            link: 'chain',
            method: 'get',
            onSuccess: function(responseJSON,responseText){
                this.instance.parseResponseJSON(responseJSON);

                if( this.instance.onAfterUpdate != null )
                {
                    this.instance.onAfterUpdate(this.instance,responseJSON);
                }

            },
            onFailure: function(xhr){
                //alert("WebChatClient::update(): Failed");
            },
            onException: function(headerName,value){
                //alert("WebChatClient::update(): Exception: " + headerName + "=" + value);
            }
        });
        request.instance = this;
        request.send("WebChatJSON=" + JSON.encode(jsonData));
    }

    this.hasChatMessageById = function(id){
        for( var i = 0; i < this.message.length; i++ )
        {
            if( this.message[i][0] == id )
            {
                return true;
            }
        }
        return false;
    }

    this.parseResponseJSON = function(responseJSON){
        try
        {
            if( responseJSON.participants != null )
            {
                this.lastParticipantJoined = responseJSON.serverLastParticipantJoined;
                this.participant = Array();
                for( var i = 0; i < responseJSON.participants.length; i++ )
                {
                    this.participant[this.participant.length] = Array(responseJSON.participants[i][0],responseJSON.participants[i][1],responseJSON.participants[i][2]);
                }

                if( this.onParticipantsReceived != null )
                {
                    this.onParticipantsReceived(this.participant,this);
                }
            }
        }
        catch( error )
        {
            
        }
        if( responseJSON.chatMessages != null )
        {
            var newMessages = Array();
            for( var i = 0; i < responseJSON.chatMessages.length; i++ )
            {
                if( this.hasChatMessageById(responseJSON.chatMessages[i][0]) == false )
                {
                    this.message[this.message.length] = responseJSON.chatMessages[i];
                    newMessages[newMessages.length] = responseJSON.chatMessages[i];
                }
            }

            if( this.onChatMessagesReceived != null )
            {
                this.onChatMessagesReceived(newMessages,this);
            }

            this.previousLastChatMessage = this.lastChatMessage;
            this.lastChatMessage = responseJSON.serverLastChatMessage;
        }
        if(( responseJSON.resultCode == this.RESULT_NO_SESSION ) && ( this.onResultNoSession != null ))
        {
            this.onResultNoSession(this);
        }
    }

    this.getParticipantById = function(id){
        for( var i = 0; i < this.participant.length; i++ )
        {
            if( this.participant[i][0] == id )
            {
                return this.participant[i];
            }
        }
        return null;
    }
}
