var countEastC = 0;
var countEastG = 0;
var countEastF = 0;
var countWestC = 0;
var countWestG = 0;
var countWestF = 0;
var linkColor;

var countVotes = 1;
var formTarget = "/components/allStarBallot/AS_CastVote.jhtml";

function countFavEastForwards()
{
	numFavPlayers = 0;
  for( x = eastForwardOffset; x < totalNumForwards; x++ )
  {
  	// needed for netscape
  	if ( document.ballot.elements[x].name.charAt(0) != "_" )
  	{
	  	if ( document.ballot.elements[x].checked )
	  		numFavPlayers++;   
	  }
  }
  countEastF = numFavPlayers;
  return numFavPlayers;
}


function validateEastForwards()
{
	if ( countFavEastForwards() > maxNumberOfForwards )
  {
    alert( "You are not permitted to chose more than " + maxNumberOfForwards + " forwards." );
    return false;
  }
  else
    return true;
}

function countFavEastGuards()
{
  numFavPlayers = 0;
  for( x = eastGuardOffset; x < (totalNumGuards + eastGuardOffset); x++ )
  {  	
  	// needed for netscape
  	if ( document.ballot.elements[x].name.charAt(0) != "_" )
  	{
	    if ( document.ballot.elements[x].checked )
	      numFavPlayers++;
	  }
  }
  countEastG = numFavPlayers;
  return numFavPlayers;
}


function validateEastGuards()
{
	if ( countFavEastGuards() > maxNumberOfGuards )
  {
    alert( "You are not permitted to chose more than " + maxNumberOfGuards + " guards." );
    return false;
  }
  else
    return true;
}

function countFavEastCenters()
{
  numFavPlayers = 0;
  for( x = eastCenterOffset; x < (totalNumCenters + eastCenterOffset); x++ )
  {
  	// needed for netscape
  	if ( document.ballot.elements[x].name.charAt(0) != "_" )
  	{
	    if ( document.ballot.elements[x].checked )
	      numFavPlayers++;
	  }
  }
  countEastC = numFavPlayers;
  return numFavPlayers;
}


function validateEastCenters()
{
	if ( countFavEastCenters() > maxNumberOfCenters )
  {
    alert( "You are not permitted to chose more than " + maxNumberOfCenters + " center." );
	countEastC--;
    return false;
  }
  else
    return true;
}

function countFavWestForwards()
{
  numFavPlayers = 0;
  for( x = westForwardOffset; x < (westForwardOffset + totalNumForwards); x++ )
  {
  	// needed for netscape
  	if ( document.ballot.elements[x].name.charAt(0) != "_" )
  	{
	    if ( document.ballot.elements[x].checked )
	      numFavPlayers++;
	  }
  }
  countWestF = numFavPlayers;
  return numFavPlayers;
}


function validateWestForwards()
{
	if ( countFavWestForwards() > maxNumberOfForwards )
  {
    alert( "You are not permitted to chose more than " + maxNumberOfForwards + " forwards." );
	countWestF--;
    return false;
  }
  else
    return true;
}

function countFavWestGuards()
{
  numFavPlayers = 0;
  for( x = westGuardOffset; x < (totalNumGuards + westGuardOffset); x++ )
  {
  	// needed for netscape
  	if ( document.ballot.elements[x].name.charAt(0) != "_" )
  	{
	    if ( document.ballot.elements[x].checked )
	      numFavPlayers++;
	 	}
  }
  countWestG = numFavPlayers;
  return numFavPlayers;
}


function validateWestGuards()
{
	if ( countFavWestGuards() > maxNumberOfGuards )
  {
    alert( "You are not permitted to chose more than " + maxNumberOfGuards + " guards." );
	countWestG--;
    return false;
  }
  else
    return true;
}

function countFavWestCenters()
{
  numFavPlayers = 0;
  for( x = westCenterOffset; x < (totalNumCenters + westCenterOffset); x++ )
  {
  	// needed for netscape
  	if ( document.ballot.elements[x].name.charAt(0) != "_" )
  	{
	    if ( document.ballot.elements[x].checked ) {
			numFavPlayers++;
		}
	  }
  }
  countWestC = numFavPlayers;
  return numFavPlayers;
}


function validateWestCenters()
{
	if ( countFavWestCenters() > maxNumberOfCenters )
  {
    alert( "You are not permitted to chose more than " + maxNumberOfCenters + " center." );
    countWestC--;
	return false;
  }
  else
    return true;
}

function clearAll()
{
	//Reset Checkboxes
    for( x = 0; x < document.forms[1].elements.length; x++ )
  {
    document.forms[1].elements[x].checked = false;
  }
  
  //Reset WriteIn Ballot
  document.ballot.writeinballot.selectedIndex = 0;
  
  document.ballot.hashcode.value = "";
}

function validateForm()
{

	//Refresh counts
	countFavEastCenters();
	countFavEastGuards();
	countFavEastForwards();
	countFavWestCenters();
	countFavWestGuards();
	countFavWestForwards();

	//Determine if at least one vote
	if(countEastC > 0 || countEastG > 0 || countEastF > 0 || countWestC > 0 || countWestG > 0 || countWestF > 0) {
		setVoteCookie();
	}
	else
	{
		alert( "You must vote for at least one player.");
		return false;
	}
	
	if(document.ballot.hashcode.value.length == 0) {
		alert("Please validate your vote by entering the appropriate code.");
		document.ballot.hashcode.focus();
		return false;
	}
	else {
		SetCookie ("AllStarHashcode", document.ballot.hashcode.value, null, "/");
	}
	
	return true;
}

function drawEasternForwards() {

	currentLocation = eastForwardOffset;
	targetLocation = eastForwardOffset + totalNumForwards;

	validateString = "validateEastForwards()";
	checkboxString = "eastForward";
	linkColor = "playerLink";

	playerLoop(currentLocation, targetLocation, linkColor );
}

function drawEasternGuards() {

	currentLocation = eastGuardOffset;
	targetLocation = eastGuardOffset + totalNumGuards;

	validateString = "validateEastGuards()";
	checkboxString = "eastGuard";
	linkColor = "playerLink";

	playerLoop(currentLocation, targetLocation, linkColor );
}

function drawEasternCenters() {

	currentLocation = eastCenterOffset;
	targetLocation = eastCenterOffset + totalNumCenters;

	validateString = "validateEastCenters()";
	checkboxString = "eastCenter";
	linkColor = "playerLink";

	playerLoop(currentLocation, targetLocation, linkColor);
}

function drawWesternForwards() {

	currentLocation = westForwardOffset;
	targetLocation = westForwardOffset + totalNumForwards;

	validateString = "validateWestForwards()";
	checkboxString = "westForward";
	linkColor = "playerLinkWest";

	playerLoop(currentLocation, targetLocation, linkColor );

}

function drawWesternGuards() {

	currentLocation = westGuardOffset;
	targetLocation = westGuardOffset + totalNumGuards;

	validateString = "validateWestGuards()";
	checkboxString = "westGuard";
	linkColor = "playerLinkWest";

	playerLoop(currentLocation, targetLocation, linkColor );

}

function drawWesternCenters() {
	currentLocation = westCenterOffset;
	targetLocation = westCenterOffset + totalNumCenters;

	validateString = "validateWestCenters()";
	checkboxString = "westCenter";
	linkColor = "playerLinkWest";

	playerLoop(currentLocation, targetLocation, linkColor);
}

function playerLoop(start, end, linkColor) {

for( x = start; x < end; x++ ) {
	document.write("<tr>");

	if(getVote(x)) {
		document.write("<td width=15 bgcolor=#183484><input type=\"checkbox\" onClick=\"return " + validateString + "\"  value=\"" + x + "\"  name=\"" + checkboxString + "\" checked=true></td>");
	}
	else {
		document.write("<td width=15 bgcolor=#18348><input type=\"checkbox\" onClick=\"return " + validateString + "\"  value=\"" + x + "\"  name=\"" + checkboxString + "\"></td>");
	}

	document.write("<td width=\"200\" bgcolor=\"#183484\" class=\"playerNameText\">");

	document.write("<a class=\""+ linkColor + "\" href=\"javascript:open_window_ex('http://www.wnba.com/playerfile/" + playerHomepage[x] + "/','',450,825)\">");

	if((playerFname[x].length + playerLname.length + playerTeamName[x].length) > 72)
		document.write(playerFname[x].substring(0,1) + ".&nbsp;" + playerLname[x] + "</a>,&nbsp;");
	else
		document.write(playerFname[x] + "&nbsp;" + playerLname[x] + "</a>,&nbsp;");
	
	document.write("<a class=\"" + linkColor + "\" href=\"javascript:open_window_ex('" + playerTeamHomepage[x] + "','',400,775)\">" + playerTeamName[x] + "</a></td>");

	document.write("<td width=\"75\" bgcolor=\"#183484\" align=\"right\" class=\"playerNameText\">");
	if (playerHighlightAvi[x]!='default')
	{ 
		document.write("<a class=\"" + linkColor + "\" href=\"?nav=ArticleList#top\" onclick=\"asAviWin(escape('http://playlist.yahoo.com/makeplaylist.dll?SID=" + playerHighlightAvi[x] + "'), escape('792k'), 'http://origin.nba.com/media/summerlove_234x60_v05.gif','http://www.wnba.com/summer_love/');return false;\">highlights</a>");
		}
		else
		{
		document.write("<a class=\"" + linkColor + "\" href=\"/highlights/index.html\">highlights</a>");	
	}		
	document.write("</td></tr>");
}
}

//We pass each players ID through as a hidden variable
function updatePlayerFormVar(playerId) {
	
	switch (countVotes) 
 	{
   		case 1 :
			document.ballot.player1.value = playerId;
			countVotes++;
			break;
   		case 2 :
			document.ballot.player2.value = playerId;
			countVotes++;
      		break;
		case 3 :
			document.ballot.player3.value = playerId;
			countVotes++;
      		break;
		case 4 :
			document.ballot.player4.value = playerId;
			countVotes++;
      		break;
		case 5 :
			document.ballot.player5.value = playerId;
			countVotes++;
      		break;
		case 6 :
			document.ballot.player6.value = playerId;
			countVotes++;
      		break;
		case 7 :
			document.ballot.player7.value = playerId;
			countVotes++;
      		break;
		case 8 :
			document.ballot.player8.value = playerId;
			countVotes++;
      		break;
		case 9 :
			document.ballot.player9.value = playerId;
			countVotes++;
      		break;
		case 10 :
			document.ballot.player10.value = playerId;
			countVotes++;
      		break;
   		default :
			break;
	} 

}

function setVoteCookie() {
  	var cookieString = ";";
	
	//Loop through all elements, looking for selected players
	//We must also fill out hidden field values for all player parameters
  	for( x = 0; x < (westCenterOffset + totalNumCenters); x++ )
  	{
  		if ( document.ballot.elements[x].checked ){ 
			cookieString += "" + x + ";";
			updatePlayerFormVar(playerId[x]);
		}
  	}
  
  if(cookieString.length > 0)
  	SetCookie ("AllStarVotes", cookieString, null, "/");
  
  //Determine if write in ballot cookie need to be erased
  //(User selected a write-in and then removed it upon review)
  if(GetCookie("AllStarVoteWriteInBallot") != null) {
  	DeleteCookie ("AllStarVoteWriteInBallot", "/");
	DeleteCookie ("AllStarVoteWriteInBallotText", "/");
	
  }
  
  if(document.ballot.writeinballot.selectedIndex > 0) {
	//Set cookie for Write In Vote
	SetCookie ("AllStarVoteWriteInBallot", document.ballot.writeinballot.options[document.ballot.writeinballot.selectedIndex].value, null, "/");
	SetCookie ("AllStarVoteWriteInBallotText", document.ballot.writeinballot.options[document.ballot.writeinballot.selectedIndex].text, null, "/");

  }
  
  return;
}

function updateWriteInBallot() {
	var writeInBallot = document.ballot.writeinballot;
	var optionCounter;
	var writeInCookie = GetCookie("AllStarVoteWriteInBallot");

	for (optionCounter = 0; optionCounter < writeInBallot.length;
 optionCounter++)
	{
		if(writeInCookie == writeInBallot.options[optionCounter].value) {
			writeInBallot.selectedIndex = optionCounter;
			return;
		}
	}
}

function getVote(voteId) {
	if(allStarVoteCookie.indexOf(";" + voteId + ";") > -1)
		return true;
	else
		return false;
}