// Fichier en UTF8

/*
Copyright 2007 Michael Minault

Use, modification, and distribution are subject to the
Cigognes Website License (See accompanying file LICENSE.txt)
*/

function findNodeWithId(parentNode, nodeId)
    {
    var nodes = parentNode.childNodes;
    for (var i = 0; i < nodes.length; i++)
        {
        var node = nodes[i];
        if ( node.attributes )
            {
            var idAttribute = node.attributes.getNamedItem('id');
            if ( idAttribute )
                {
                if ( idAttribute.nodeValue == nodeId )
                    {
                    return node;
                    }
                }
            }
        }
    
    return null;
    }

function nodeInnerValue(node)
    {
    serializer = new XMLSerializer();
    outerValue = serializer.serializeToString(node);
    innerBegin = outerValue.indexOf('>') + 1;
    innerEnd = outerValue.lastIndexOf('<');
    return outerValue.substring(innerBegin, innerEnd);
    }

function updateCell(parentCell, cellId)
    {
    var cellNew = findNodeWithId(parentCell, cellId);
    if ( !cellNew ) return;
    var cellOld = document.getElementById(cellId);
    cellOld.innerHTML = nodeInnerValue(cellNew);
    }

function reportStatus(pathPrefix, pilotId, statusImage, statusText)
    {
    var imageElement = '<img src="' + pathPrefix + 'images/' + statusImage + '.gif" title="' + statusText + '"/>';
    var statusElement = document.getElementById('update' + pilotId);
    if ( statusImage == 'refreshing' )
        {
        statusElement.innerHTML = imageElement;
        }
    else
        {
        statusElement.innerHTML = '<a href="javascript:updatePilot(\'' + pathPrefix + '\', \'' + pilotId + '\')">'
            + imageElement
            + '</a>';
        }
    }

function reportStatus2(pathPrefix, pilotId, actualPilotId, statusImage, statusText)
    {
    reportStatus(pathPrefix, pilotId, statusImage, statusText);
    
    if ( actualPilotId != pilotId )
        {
        reportStatus(pathPrefix, actualPilotId, statusImage, statusText);
        }
    }

function updatePilot(pathPrefix, pilotId)
    {
    var POLL_TIME = 1; // sec
    var TIME_OUT = 40; // sec
    
    reportStatus(pathPrefix, pilotId, 'refreshing', 'Mise à jour en cours');
    
    var xhReq = new XMLHttpRequest();
    
    var xhReqTimeout = setTimeout
        (
        function()
            {
            if (xhReq.readyState != 0 && xhReq.readyState != 4)
                {
                xhReq.abort();
                reportStatus(pathPrefix, pilotId, 'refresh_error', 'timeout');
                }
            },
        TIME_OUT * 1000
        );
    
    xhReq.onreadystatechange = function()
        {
        if (xhReq.readyState != 4)
            {
            return;
            }
        
        clearTimeout(xhReqTimeout);
        
        if (xhReq.status != 200)
            {
            reportStatus(pathPrefix, pilotId, 'refresh_error', xhReq.statusText);
            return;
            }
        
        xmlRosterUpdate = xhReq.responseXML;
        
        //alert( (new XMLSerializer()).serializeToString(xmlRosterUpdate) );
        
        idElement = xmlRosterUpdate.getElementsByTagName('Id')[0];
        if ( idElement )
            {
            actualPilotId = idElement.firstChild.nodeValue;
            
            if ( actualPilotId != pilotId )
                {
                reportStatus(pathPrefix, actualPilotId, 'refreshing', 'Mise à jour en cours');
                }
            }
        else
            {
            actualPilotId = pilotId;
            }
        
        exception = xmlRosterUpdate.getElementsByTagName('Exception')[0];
        if ( exception )
            {
            reportStatus2(pathPrefix, pilotId, actualPilotId, 'refresh_error', exception.firstChild.nodeValue);
            return;
            }
        
        member = xmlRosterUpdate.getElementsByTagName('member')[0];
        if ( member )
            {
            updateCell(member, 'tomCur' + actualPilotId);
            updateCell(member, 'fightCur' + actualPilotId);
            updateCell(member, 'bombCur' + actualPilotId);
            }
        
        result = xmlRosterUpdate.getElementsByTagName('result')[0];
        if ( !result )
            {
            reportStatus2(pathPrefix, pilotId, actualPilotId, 'refresh_error', 'Pas de résultat dans la réponse');
            return;
            }
        
        if ( result.firstChild.nodeValue != 1 )
            {
            reportStatus2(pathPrefix, pilotId, actualPilotId, 'refresh_error', 'Mise à jour déja en cours');
            return;
            }
        
        updateStatus = xmlRosterUpdate.getElementsByTagName('status')[0];
        if ( !updateStatus )
            {
            reportStatus2(pathPrefix, pilotId, actualPilotId, 'refresh_error', 'Pas de status dans la réponse');
            return;
            }
        
        if ( updateStatus.firstChild.nodeValue == 'pending' )
            {
            setTimeout("updatePilot('" + pathPrefix + "', '" + pilotId + "')", POLL_TIME * 1000);
            }
        else if ( updateStatus.firstChild.nodeValue == 'done' )
            {
            reportStatus(pathPrefix, actualPilotId, 'refresh_done', 'A jour');
            
            if ( actualPilotId != pilotId )
                {
                // Passer à un autre pilote
                setTimeout("updatePilot('" + pathPrefix + "', '" + pilotId + "')", POLL_TIME * 1000);
                }
            }
        else if ( updateStatus.firstChild.nodeValue == 'allDone' )
            {
            reportStatus(pathPrefix, pilotId, 'refresh_done', 'Tous à jour');
            }
        else
            {
            reportStatus2(pathPrefix, pilotId, actualPilotId, 'refresh_error', 'Status inconnu');
            }
        };
    
    var url = "roster_update.php5";
    if ( pilotId != 'All' )
        {
        url = url + "?pilotId=" + pilotId;
        }
    xhReq.open("POST", url, true);
    xhReq.send(null);
    };
