// JavaScript Document

function GridAdminSubmit(){
    var err1 = false;
    var err2 = false;
    $each($$('.score_input'), function(obj){
        if( !obj.value && obj.value != '0' )
            err1 = true;
        else if( !is_numeric(obj.value) )
            err2 = true;
    });
    if( err1 )
        alert("Not all scores have been filled");
    if( err2 )
        alert("Only numbers may be entered for scores");
    if( !err1 && !err2 )
        document.GridForm.submit();
    return false;
}

function GridVoteSubmit(){
    var err = false;
    var champ_err = false;
    $each($$('.grid_input'), function(obj){
        if( !obj.value ){
            if( obj.name == 'votes[champ-1]' )
                champ_err = true;
            else
                err = true;
        }
    });
    if( err )
        alert("Not all spaces are filled!");
    else if( champ_err )
        alert("Please click on your choice of winner for the championship match");
    else if( !$('tiebreaker').value )
        alert("You did not enter a tiebreaker score");
    else if( !is_numeric($('tiebreaker').value) )
        alert("You must enter only a number for the tiebreaker score");
    else
        document.GridForm.submit();
    return false;
}

function ChangeChampion(){
    var obj = $('grid_champion')
    var team1 = $('champ-1-1');
    var team2 = $('champ-1-2');
    var team1_id = $$('#champ-1-1 .grid_teamID')[0];
    var team2_id = $$('#champ-1-2 .grid_teamID')[0];
    $each($$('#champ-1 .obj'), function(obj){
        obj.className = 'obj select';
    })
    if( team1_id ){
        if( obj.value == team1_id.value ){
            team1.className = 'obj select win';
        }
    }
    if( team2_id ){
        if( obj.value == team2_id.value ){
            team2.className = 'obj select win';
        }
    }
}

function TeamAdvance(obj){
    var id = obj.id;
    var idSplit = id.split('-');
    var column = idSplit[0];
    var match = idSplit[1];
    var num = idSplit[2];
    var currentMatchInput = $$('#' + column + '-' + match + ' .grid_input')[0];
    var selectedTeamID = $$('#' + column + '-' + match + '-' + num + ' .grid_teamID')[0];
    if( selectedTeamID )
        selectedTeamID = selectedTeamID.value;
    else
        return false;
    var html = $$('#' + id + ' .obj_cont')[0].innerHTML;
    
    if( !html )
        return false;
    
    if( column == '1' )
        var targetColumn = '2';
    else if( column == '2' )
        var targetColumn = '3';
    else if( column == '3' )
        var targetColumn = '4';
    else if( column == '4' )
        var targetColumn = '5';
    else if( column == '5' )
        var targetColumn = 'champ';
    else if( column == '6' )
        var targetColumn = '5';
    else if( column == '7' )
        var targetColumn = '6';
    else if( column == '8' )
        var targetColumn = '7';
    else if( column == '9' )
        var targetColumn = '8';
    
    if( match%2 )
        var targetMatch = parseInt(match)+1;
    else
        var targetMatch = parseInt(match);
    targetMatch = targetMatch / 2;
    
    if( targetColumn == '5' ){
        if( column == '4' )
            targetMatch = 1;
        else if( column == '6' )
            targetMatch = 2;
    }
    
    if( match%2 )
        var targetNum = '1';
    else
        var targetNum = '2';
    
    if( targetColumn == '5' ){
        targetNum = match;
    }
    
    //alert('#' + targetColumn + '-' + targetMatch + '-' + targetNum + ' .obj_cont');
    
    if( column != 'champ' ){
        var targetObj = $$('#' + targetColumn + '-' + targetMatch + '-' + targetNum + ' .obj_cont')[0];
        var targetExistingSelected = $$('#' + targetColumn + '-' + targetMatch + '-' + targetNum + ' .obj_cont' + ' .grid_teamID')[0]
        if( targetExistingSelected ){
            if( targetExistingSelected.value != selectedTeamID ){
                $each($$('.grid_teamID'), function(x){
                    if( x.value == targetExistingSelected.value ){
                        var targetExistingParent = x.getParent();
                        targetExistingColumn = targetExistingParent.getParent();
                        targetExistingColumn = targetExistingColumn.id;
                        targetExistingColumn = targetExistingColumn.split('-');
                        targetExistingColumn = targetExistingColumn[0];
                        if( targetExistingColumn == 'champ'
                           || ( parseInt(targetColumn) < 5 && parseInt(targetColumn) < parseInt(targetExistingColumn) )
                           || ( parseInt(targetColumn) > 5 && parseInt(targetColumn) > parseInt(targetExistingColumn) )
                        ){
                            targetExistingMatch = targetExistingParent.getParent();
                            targetExistingMatch = targetExistingMatch.getParent();
                            if( targetExistingColumn == 'champ' ){
                                targetExistingMatch = targetExistingMatch.getParent();
                                targetExistingMatch = targetExistingMatch.getParent();
                                targetExistingMatch = targetExistingMatch.getParent();
                                targetExistingMatch = targetExistingMatch.getParent();
                            }
                            targetExistingMatch = targetExistingMatch.id;
                            targetExistingInput = $$('#' + targetExistingMatch + ' .grid_input')[0];
                            if(targetExistingInput)
                                if( targetExistingInput.value == x.value )
                                    targetExistingInput.value = '';
                            if( targetColumn != 'champ' ){
                                TEP = new Fx.Tween(targetExistingParent);
                                TEP.onComplete = function(){
                                    targetExistingParent.innerHTML = '';
                                    targetExistingParent.set('opacity', 1);
                                }
                                TEP.start('opacity', 0);
                                ChangeChampion();
                            }
                        }
                        else if( targetExistingColumn == targetColumn ){
                            targetExistingMatch = targetExistingParent.getParent();
                            targetExistingMatch = targetExistingMatch.getParent();
                            targetExistingMatch = targetExistingMatch.id;
                            targetExistingInput = $$('#' + targetExistingMatch + ' .grid_input')[0];
                            if(targetExistingInput)
                                if( targetExistingInput.value != selectedTeamID )
                                    targetExistingInput.value = '';
                        }
                    }
                });
            }
        }
        if( targetExistingSelected ){
            if( targetExistingSelected.value != selectedTeamID ){
                targetObj.set('opacity', 0);
                targetObj.innerHTML = html;
                targetObj.fade('in');
            }
        }
        else{
            targetObj.set('opacity', 0);
            targetObj.innerHTML = html;
            targetObj.fade('in');
        }
    }
    
    currentMatchInput.value = selectedTeamID;
    if( column == 'champ' )
        ChangeChampion();    
    
}