/*
 * Callback variables
 */
var initialiseSelectorCallback = {
    success: function(o) {
        if (o.responseXML !== undefined) {
            var selectEl = getRoundSelectElement();
           
            // Remove existing options
            removeOptions(selectEl);
           
            addPhaseOptions(selectEl, o.responseXML);
            
            setLoadingLabel(o.responseXML);
            setAlertGalleryNotAccessible(o.responseXML);
            setAlertSelectFixture(o.responseXML);
            
            activateSelect(selectEl);
            
            // This code fixes bug in IE 6 and Safari when the browser's
            // back button is pressed to a page contianing the selector.
            var fixtureSelectEl = getFixtureSelectElement();
            var timeSlotSelectEl = getTimeSlotSelectElement();
            addOption(fixtureSelectEl, '-', -1, false);
            addOption(timeSlotSelectEl, '-', -1, false);
        }
    }
}

var roundSelectCallback = {
    success: function(o) {
        if (o.responseXML !== undefined) {
            var fixtureSelectEl = getFixtureSelectElement();
            var legSelectEl = getLegSelectElement();
            var timeSlotSelectEl = getTimeSlotSelectElement();
            var matchId;
            
            // Remove existing options
            removeOptions(fixtureSelectEl);
            
            addFixtureOptions(fixtureSelectEl, o.responseXML);
            
            activateSelect(fixtureSelectEl);
            if (displayLeg(o.responseXML)) {
                activateSelect(legSelectEl);
            } else {
                deactivateSelect(legSelectEl);
            }
            
            matchId = fixtureSelectEl.options[fixtureSelectEl.selectedIndex].value;
            
            if (matchId != -1 && matchId != -2) {
                var postData = "matchId=" + matchId;
                selectedFixtureIndex = fixtureSelectEl.selectedIndex;
                loadingOption(timeSlotSelectEl);
                YAHOO.util.Connect.asyncRequest('POST', "/timeSlotSelector_fixtureSelect.action", fixtureSelectCallback, postData);
            } else {
                selectedFixtureIndex = 0;
            }
        }
    }
}

var fixtureSelectCallback = {
    success: function(o) {
        if (o.responseXML !== undefined) {
            var timeSlotSelectEl = getTimeSlotSelectElement();
            
            // Remove existing options
            removeOptions(timeSlotSelectEl);
            
            addTimeSlotOptions(timeSlotSelectEl, o.responseXML);
            
            activateSelect(timeSlotSelectEl);
        }
    }
}

function roundSelect(resetLeg) {
    var roundSelectEl = getRoundSelectElement();
    var fixtureSelectEl = getFixtureSelectElement();
    selectedRoundIndex = roundSelectEl.selectedIndex;
    var roundId = roundSelectEl.options[selectedRoundIndex].value;
    var matchId = fixtureSelectEl.options[fixtureSelectEl.selectedIndex].value;
    
    // Check if a round has been selected
    if (roundId != -1) {
        var postData = "roundId=" + roundId;
        var fixtureEl = getFixtureSelectElement();
        var legSelectEl = getLegSelectElement();
        var timeSlotSelector = getTimeSlotSelectElement();
        if (matchId != -1) {
            selectedFixtureIndex = fixtureSelectEl.selectedIndex;
            postData += "&matchId=" + matchId;
        }
        if (resetLeg) {
            resetLegSelect();
        } else {
            var leg = legSelectEl.options[legSelectEl.selectedIndex].value;
            postData += "&leg=" + leg;
        }
        loadingOption(fixtureEl);
        // Disable drop downs as response can take some time
        deactivateSelect(fixtureEl);
        deactivateSelect(getLegSelectElement());
        // Check page has a time slot selector.
        if (timeSlotSelector != null) {
            deactivateSelect(getTimeSlotSelectElement());
        }
        YAHOO.util.Connect.asyncRequest('POST', "/timeSlotSelector_roundSelect.action", roundSelectCallback, postData);
    } else if (selectedRoundIndex != null) {
        roundSelectEl.options[selectedRoundIndex].selected = true;
    }
}

function fixtureSelect() {
    var fixtureSelectEl = getFixtureSelectElement();
    var timeSlotSelectEl = getTimeSlotSelectElement();
    var _selectedFixtureIndex = fixtureSelectEl.selectedIndex;
    var matchId = fixtureSelectEl.options[_selectedFixtureIndex].value;
    
    if (matchId != -1) {
        if (matchId != -2) {
            var postData = "matchId=" + matchId;
            selectedFixtureIndex = _selectedFixtureIndex;
            loadingOption(timeSlotSelectEl);
            YAHOO.util.Connect.asyncRequest('POST', "/timeSlotSelector_fixtureSelect.action", fixtureSelectCallback, postData);
        }
    } else {
        fixtureSelectEl.options[selectedFixtureIndex].selected = true;
    }
}

function timeSlotSelect() {
    // Do nothing for now
}

function findYourPhoto() {
    selectedFixtureIndex = getFixtureSelectElement().selectedIndex;
    var matchId = getFixtureSelectElement().options[selectedFixtureIndex].value;
    
    if (matchId == -2) {
        alert(alertGalleryNotAccessible);
        return false;
    } else if (matchId != -1) {
        var timeSlot = getTimeSlotSelectElement().options[getTimeSlotSelectElement().selectedIndex].value;
        var url = "gallery.action?id=" + matchId;
        if (timeSlot != null && timeSlot != -1) {
            url += "&timeSlot=" + timeSlot
        }
        window.location = url ;
        return true;
    } else {
        alert(alertSelectFixture);
        return false;
    }
}