Event.observe(window, 'load', function() {
    Event.observe('btnAdd', 'click', btnAdd_Click);
    Event.observe('btnRemove', 'click', btnRemove_Click);
    Event.observe('btnSearch', 'click', btnSearch_Click);
    Event.observe('btnModifySearch', 'click', btnModifySearch_Click);
    Event.observe('btnNewSearch', 'click', btnNewSearch_Click);
    
    checkServerHealth();
});
function checkServerHealth() {
    displayMessage(true);
    
    var url = "ContainerStatusAndMovements_process.aspx?action=checkhealth&seed=" + Math.random() * 100000;
    new Ajax.Request (url, {
	    method: "get",
	    onSuccess: function (transport) {
	        if (transport.responseText == '1') {
	            displayMessage(false);
                $('pnlSearch').show();
                $('txtContainerNumber').focus();
            }
            else  {
                displayMessage(false);
                $('pnlUnavailable').show();
            }
	    },
	    onFailure: function (transport) {
		    $('pnlUnavailable').show();
	    }
	    });
}
function btnAdd_Click() {
    var containerNumber = $F('txtContainerNumber');
    if (containerNumber) {
        var count = $('lstContainers').options.length;
        $('lstContainers').options[count] = new Option(containerNumber, containerNumber);
        $('txtContainerNumber').value = '';
        $('txtContainerNumber').focus();
        
        $('lstContainers').title = "Current containers (" + $('lstContainers').options.length + ")";
    }
}
function btnRemove_Click() {
    // do we have a selected container?
    if ($F('lstContainers')) {
        var list = $('lstContainers');
        for (var i = list.options.length - 1; i >=0; i--) {
            if (list.options[i].selected)
                list.options[i].remove();
        }
        
        if (list.options.length > 0)
            list.title = "Current containers (" + list.options.length + ")";
        else
            list.title = "Current containers (empty)";
    }
}
function btnSearch_Click() {
    // do we have any containers in our list?
    var list = $('lstContainers');
    if (list.options.length > 0) {
        // yes, process it
        
        // create container list for processing
        var containers = "(";
        for (var i = 0; i < list.options.length; i++) {
            containers += "'" + list.options[i].value + "', ";
        }
        
        containers = containers.substring(0, containers.length - 2) + ")";
        
        // submit query via AJAX for search
        var url = "ContainerStatusAndMovements_process.aspx?action=search&containerlist=" + containers + "&seed=" + Math.random() * 100000;
        $('btnSearch').disabled = true;
		$('pnlSearch').hide();
        displayMessage(true);
        
        new Ajax.Request (url, {
		    method: "get",
		    onSuccess: function (transport) {
		        // re-enable search button
		        $('btnSearch').disabled = false;
		        displayMessage(false);
		        
		        // display search results
		        $('pnlResults').show();
		        $('pnlContainerList').update(transport.responseText);
		        					    
		    },
		    onFailure: function (transport) {
		        displayMessage(false);
		        $('pnlSearch').show();
			    alert ("An error occured. Status: " + transport.status);
		    }
		    });
    } else {
        // no, inform user
        alert ('Please provide at least one container number before searching');
    }	        
}
function loadStats(ctId) {
    // submit query via AJAX for search
    var url = "ContainerStatusAndMovements_process.aspx?action=query&ctid=" + ctId+ "&seed=" + Math.random() * 100000;
    
    var items = $$('tr.selectedRow');
    if (items.length > 0)
        items[0].removeClassName('selectedRow');
        
    $('row'+ctId).addClassName('selectedRow');
    
    if ($('dwellTimeTable')) {
        new Effect.Fade('dwellTimeTable');
        new Effect.Fade('movementTable');
    }
    
    displayMessage(true);
    
    new Ajax.Request (url, {
	    method: "get",
	    onSuccess: function (transport) {
	        // display search results
	        $('pnlContainerStatistics').update(transport.responseText);			        					    
	        displayMessage(false);
	        $('pnlContainerStatistics').show();            
	    },
	    onFailure: function (transport) {
	        displayMessage(false);
		    alert ("An error occured. Status: " + transport.status);
	    }
	    });
}
function displayMessage(showMessage) {
    if (showMessage) {
        $('loadingMessage').show();
    } else
        $('loadingMessage').hide();
}
function btnModifySearch_Click() {
    $('pnlResults').hide();
    $('pnlSearch').show();
    $('txtContainerNumber').focus();
    $('pnlContainerStatistics').update('');
}
function btnNewSearch_Click() {
	$('txtContainerNumber').value = '';
	$('lstContainers').options.length = 0;
	$('pnlResults').hide();
    $('pnlSearch').show();
    $('txtContainerNumber').focus();
    $('pnlContainerStatistics').update('');
    $('lstContainers').title = "Current containers (empty)";
}

