
var BALLOT_LENGTH = 25;
var current_selection = 0;
var teamcount = 0;

function onSelectionChange(old_sel, new_sel)
{
	var old_cell = document.getElementById('ballot').rows[old_sel].cells[1];
	old_cell.className = "ballot_unselected";
	var new_cell = document.getElementById('ballot').rows[new_sel].cells[1];
	new_cell.className = "ballot_selected";
}


function incrementSelection(increment)
{
	var new_selection = current_selection + increment;
	if (new_selection < 0)
		new_selection = 0;
	else if (new_selection >= BALLOT_LENGTH)
		new_selection = BALLOT_LENGTH - 1;
		
	onSelectionChange(current_selection, new_selection);
	current_selection = new_selection;
}

function setSelection(new_selection)
{
	onSelectionChange(current_selection, new_selection);
	current_selection = new_selection;
}

function onCellClick(cell)
{
	setSelection(cell.parentNode.rowIndex);
   id = cell.id.split('_')[1];
   UpdateDetails(id);
}

function onTeamListClick(cell)
{
   id = teamlist.options[teamlist.selectedIndex].value;
   UpdateDetails(id);
}

function UpdateDetails(id)
{
   AJAXGet("./xml/team-detail.php?id=" + id, detailsCallback);
}

function detailsCallback(http_request)
{	
    if (http_request.readyState == 4) 
    {
        if (http_request.status == 200) 
        {                       
          	root   = http_request.responseXML.firstChild;
        		id     = root.getElementsByTagName("id")[0].textContent;
            url    = root.getElementsByTagName("url")[0].textContent;
            wins   = root.getElementsByTagName("wins")[0].textContent;
            losses = root.getElementsByTagName("losses")[0].textContent;
            rank   = root.getElementsByTagName("rank")[0].textContent;
            name   = root.getElementsByTagName("name")[0].textContent;
            
            document.getElementById('wins').textContent   = wins;
            document.getElementById('losses').textContent = losses;
            document.getElementById('kenpom-rank').textContent   = rank;
            
            urlobject = document.getElementById('teamurl');
            urlobject.href = url;
            urlobject.innerHTML = name;
        } 
        else 
        {
            alert('There was a problem with the request.');
        }
    }
}


function UpdateTeams()
{
	teamcombo = document.getElementById('teamcombo');
	var url = "";
	if (teamcombo.value == "-1" )
		url = "./xml/teams.php";
	else
		url = "./xml/" + teamcombo.value;
	
	AJAXGet(url, teamCallback);
}

function teamCallback(http_request) 
{
	teamlist = document.getElementById('teamlist');
	
    if (http_request.readyState == 4) 
    {
        if (http_request.status == 200) 
        {
        	teamlist.options.length = 0;        	

        	xmldoc = http_request.responseXML.firstChild;
        	elements = xmldoc.getElementsByTagName("team");
        	length = elements.length;
        	for (i = 0; i < length; i++)
        	{
        		element = elements[i]; 
        		id   = element.getElementsByTagName("id")[0].textContent;
        		name = element.getElementsByTagName("name")[0].textContent;
        		if (TeamPresent(id) == false)
        		{
	        		option = new Option(name, id);
	        		teamlist.options.add(option);
        		}
        	}
        } 
        else 
        {
            alert('There was a problem with the request.');
        }
    }
    else
    {
    	if (teamlist.options.length == 0 ||
    	    teamlist.options[0].value != null)
    	{
    		teamlist.options.length = 0;
    		teamlist.options.add(new Option('please wait...', null));
    	}
    }
}

function TeamPresent(id)
{
	for(j = 0; j < BALLOT_LENGTH; j++)
	{
		cell = document.getElementById('ballot').rows[j].cells[1];
		if (cell.getAttribute("id") == "team_" + id)
		{
			return true;
		}
	}
	return false;
}

function addTeam()
{
	var tl = document.getElementById('teamlist');
	option   = tl.options[teamlist.selectedIndex];
	
	cell = document.getElementById('ballot').rows[current_selection].cells[1];
	cell.innerHTML = option.text;
	cell.setAttribute("id", "team_" + option.value);
	/// TODO: add value
	
	tl.options[teamlist.selectedIndex] = null;
	teamcount++;
	if ( teamcount == BALLOT_LENGTH )
		document.getElementById('submit').disabled = false;
	
	incrementSelection(1);
}

function removeTeam()
{
	
}

function submitBallot()
{
	var ballot = "";
	var cell;
	for(var i = 0; i < BALLOT_LENGTH; i++)
	{
		cell = document.getElementById('ballot').rows[i].cells[1];
		ballot += "ballot[" + i + "]=" + cell.id.split('_')[1] + "&";
	}
	
	sub = document.getElementById('submit'); 
	sub.disabled = true;
	sub.value = "Wait";
	
	AJAXPost("commands/submitBallot.php", submitCallback, ballot);
}

function submitCallback(http_request) 
{
	sub = document.getElementById('submit');
    if (http_request.readyState == 4) 
    {
        if (http_request.status == 200) 
        {
 			window.location = "ballot-submitted.php";
 			sub.value = "Success";
        } 
        else 
        {
            alert('There was a problem with the request.\n' + http_request.status);
            sub.value = "Submit";
            sub.disabled = false;
        }
    }
    else
    {
		sub.value += ".";
    }
}

function swapTeams(up)
{
	cell1 = document.getElementById('ballot').rows[current_selection].cells[1];
	if ( up )
	{
		cell2 = document.getElementById('ballot').rows[current_selection - 1].cells[1];
		incrementSelection(-1);
	}
	else
	{
		cell2 = document.getElementById('ballot').rows[current_selection + 1].cells[1];
		incrementSelection(1);
	}
		
	tempname = cell1.innerHTML;
	tempid   = cell1.getAttribute("id");
	
	cell1.innerHTML = cell2.innerHTML;
	cell1.setAttribute("id", cell2.getAttribute("id"));
	
	cell2.innerHTML = tempname;
	cell2.setAttribute("id", tempid);
}