var ajax_url = "/ajax.php";
// time to refresh in seconds
var afid_loop_refresh = 10;
var afid_auto_refresh = 1;


function afidCaptureEnter (id)
{
    document.captureEvents(Event.KEYDOWN);
    document.onkeydown = function (evt) {
        var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
        if (keyCode == 13) {
            document.getElementById(id).submit();
            return false;
        } else {
            return true;
        }
    };

}

function afidLimit (ms_id)
{
    var cmd = document.getElementById('cmd');
    if (cmd) {
        
        var amount_input = document.getElementById("limit_" + ms_id);
        
        if(amount_input){
            
            var amount = amount_input.value;
                        
            if (isNaN(amount) || amount.length == 0) {
                alert('Please enter a numeric value!');
            } else {
        
                if (amount > 0) {
                    cmd.value = 'limit ' + amount;
                } else {
                    cmd.value = 'limit 0';
                }
                cmd.value += ' ' + ms_id;
                afidSendCommand();
                
            }
            
        }
            
    }
}

function afidSendCommand (message_id)
{
    try {
        updateStatus('sending command...');
    } catch (e) {
    }

    if (message_id == undefined) {
        message_id = '';
    }

    var cmd = document.getElementById('cmd' + message_id);
    if (cmd) {
        var pars = 'opt=exec&cmd=' + encodeURIComponent(cmd.value);
        var req = new Ajax.Request(
            ajax_url,
            {
                method: 'get',
                parameters: pars,
                onComplete: afidSendCommandResponse
            }
        );
    }
}


function afidSendCommandResponse (response)
{

    try {
        updateStatus('command completed.');
    } catch (e) {
    }

    try {
        afidRefreshConversation(1);
    } catch (e) {
    }

    // clear command + possibly message
    var cmd = document.getElementById('cmd');
    if (cmd) {
        if (cmd.value != '') {
            cmd.value = '';
            //location.reload();
            afidRefreshConversation(1);
        }
    }
    var msg = document.getElementById('msg');
    if (msg) {
        msg.value = '';
        
        // Hide loader
        var tellyell_container = document.getElementById('tellyell_container');
        var tellyell_loading = document.getElementById('tellyell_loading');
        if(tellyell_container && tellyell_loading){
            tellyell_container.style.display = '';
            tellyell_loading.style.display = 'none';
        }
        
    }    

/*    if (response.responseText != '') {
        location.href = response.responseText;
    }*/
}


function afidRefreshConversation (only_once)
{

    if (afid_auto_refresh == 1 || only_once) {

        // draw status
        updateStatus('Refreshing Loops...');

        // set parameters
        var pars = 'opt=refresh&lp_id=' + lp_id + '&ms_id=' + ms_id;

        if (only_once == 1) {
            var on_success = afidRefreshConversationResponse;
        } else {
            var on_success = afidRefreshConversationRepeatResponse;
        }

        // make ajax request
        conversationUpdater = new Ajax.Updater(
                            'scrollbox',
                            ajax_url,
                            {
                                method: 'get',
                                parameters: pars,
                                onSuccess: on_success
                            });
    } else {
        // refresh the conversation again after time period
        setTimeout(function() {afidRefreshConversation();},(afid_loop_refresh * 1000));
    }
}


function afidRefreshConversationResponse (originalRequest)
{
    // draw status
    updateStatus("Loops refreshed.");
}


function afidRefreshConversationRepeatResponse (originalRequest)
{
    // draw status
    updateStatus("Loops refreshed.");

    // refresh the conversation again after time period
    setTimeout(function() {afidRefreshConversation();},(afid_loop_refresh * 1000));
}


function updateStatus (message)
{
   //statusDiv.innerHTML = message;
   //setTimeout(function() {statusDiv.innerHTML = ''},5000);
}