var countC = 0;
var countG = 0;
var countF = 0;
var linkColor;

var cookieString = "";

var countVotes = 1;
var formTarget = "/webAction?actionId=asCastVote";

var currentUrl = document.URL;
var uIndex = currentUrl.indexOf("/asb/");
var version = currentUrl.substring(uIndex+5,uIndex+8);

var highlight = "";
var profile = "";


function countFavForwards()
{
  numFavPlayers = 0;
  for( x = westForwardOffset; x < (westForwardOffset + totalNumForwardsWest + totalNumForwardsEast); x++ )
  {
  	// needed for netscape
  	if ( document.ballot.elements[x].name.charAt(0) != "_" )
  	{
	    if ( document.ballot.elements[x].checked )
	      numFavPlayers++;
	  }
  }
  countF = numFavPlayers;
  return numFavPlayers;
}


function validateForwards()
{
	if ( countFavForwards() > maxNumberOfForwards )
    {
		showMaxForwardsAllowedAlert();
		countF--;
    	return false;
  	}
  	else
   	 	return true;
}

function countFavGuards()
{
  numFavPlayers = 0;
  for( x = westGuardOffset; x < (westGuardOffset + totalNumGuardsWest + totalNumGuardsEast); x++ )
  {
  	// needed for netscape
  	if ( document.ballot.elements[x].name.charAt(0) != "_" )
  	{
	    if ( document.ballot.elements[x].checked )
	      numFavPlayers++;
	 	}
  }
  countG = numFavPlayers;
  return numFavPlayers;
}


function validateGuards()
{
	if ( countFavGuards() > maxNumberOfGuards )
  {
	showMaxGuardsAllowedAlert();
	countG--;
    return false;
  }
  else
    return true;
}

function countFavCenters()
{
  numFavPlayers = 0;
  
  for( x = westCenterOffset; x < (westCenterOffset + totalNumCentersWest + totalNumCentersEast); x++ )
  {
  	// needed for netscape
  	if ( document.ballot.elements[x].name.charAt(0) != "_" )
  	{
	    if ( document.ballot.elements[x].checked ) {
			numFavPlayers++;
		}
	  }
  }
  countC = numFavPlayers;
  return numFavPlayers;
}


function validateCenters()
{
	if ( countFavCenters() > maxNumberOfCenters )
  {
	showMaxCentersAllowedAlert();
    countC--;
	return false;
  }
  else
    return true;
}

function clearAll()
{
	//Reset Checkboxes
    for( x = 0; x < document.ballot.elements.length; x++ )
  {
    document.ballot.elements[x].checked = false;
  }
  
  //Reset WriteIn Ballot
  document.ballot.writeinballot.selectedIndex = 0;
  
  document.ballot.hashcode.value = "";
}

function setSelections()
{
	if (atLeastOneVote())
	{
		setVoteCookie();
		return true;
	}
	else
		return false;
}

function validateForm()
{
	if (atLeastOneVote())
	{
		setVoteCookie();
		
		if(document.ballot.hashcode.value.length == 0) {
		
			showValidationCodeRequiredAlert();
		
			document.ballot.hashcode.focus();
			return false;
		}
		else {		
/*
			if (document.ballot.country.value.length <= 2) {
				showCountryAlert();
				document.ballot.country.focus();
				return false;
			}
*/		

//			SetCookie ("AllStarHashcodeDL", document.ballot.hashcode.value, null, "/");
		}
	
		return true;
	}
	else
		return false;
}

function atLeastOneVote()
{
	//Refresh counts
	countFavCenters();
	countFavGuards();
	countFavForwards();

	//Determine if at least one vote
	if(countC == 0 && countG == 0 && countF == 0) 
	{
		showAtLeastOneVoteAlert();
		
		return false;
	}
	else
		return true;
}

function drawEasternForwards() {
	currentLocation = eastForwardOffset;

	targetLocation = eastForwardOffset + totalNumForwardsEast;
	
	validateString = "validateForwards()";
	checkboxString = "eastForward";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();

	playerLoop(currentLocation, targetLocation, linkColor );
}

function drawEasternGuards() {

	currentLocation = eastGuardOffset;
	targetLocation = eastGuardOffset + totalNumGuardsEast;

	validateString = "validateGuards()";
	checkboxString = "eastGuard";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();
	
	playerLoop(currentLocation, targetLocation, linkColor );
}

function drawEasternCenters() {

	currentLocation = eastCenterOffset;

	targetLocation = eastCenterOffset + totalNumCentersEast;	
	
	validateString = "validateCenters()";
	checkboxString = "eastCenter";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();
	
	playerLoop(currentLocation, targetLocation, linkColor);
}

function drawWesternForwards() {
	currentLocation = westForwardOffset;
	targetLocation = westForwardOffset + totalNumForwardsWest;
	validateString = "validateForwards()";
	checkboxString = "westForward";
	linkColor = "playerLink";
	
	getHighlightText();
	getProfileText();
	
	playerLoop(currentLocation, targetLocation, linkColor );

}

function drawWesternGuards() {

	currentLocation = westGuardOffset;
	targetLocation = westGuardOffset + totalNumGuardsWest;

	validateString = "validateGuards()";
	checkboxString = "westGuard";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();
	
	playerLoop(currentLocation, targetLocation, linkColor );

}

function drawWesternCenters() {
	currentLocation = westCenterOffset;

	targetLocation = westCenterOffset + totalNumCentersWest;	
	
	validateString = "validateCenters()";
	checkboxString = "westCenter";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();
	
	playerLoop(currentLocation, targetLocation, linkColor);
}

function playerLoop(start, end, linkColor) 
{

  for( x = start; x < end; x++ ) 
  {
  	if(x % 2 == 0){
		document.write("<tr>");
	}
	 else{
	 	document.write("<tr class=\"even\">");
	}
	if(getVote(x)) {
		document.write("<td width=10><input type=\"checkbox\" onClick=\"return " + validateString + "\"  value=\"" + x + "\"  name=\"" + checkboxString + "\" checked=true></td>");
	}
	else {
		document.write("<td width=10><input type=\"checkbox\" onClick=\"return " + validateString + "\"  value=\"" + x + "\"  name=\"" + checkboxString + "\"></td>");
	}

	document.write("<td width=\"300\" class=\""+linkColor+"\" nowrap>");

//	document.write("<span class=\"pName\">"+playerFname[x].substring(0,1) + ".&nbsp;" + playerLname[x] + "</span>,&nbsp;");
	document.write("&nbsp;<a class=\"bpName\" href=\"javascript:open_window_ex('http://www.nba.com/dleague/playerfile/" + playerHomepage[x] + "/','',680,900)\">" + playerFname[x] + " " + playerLname[x] + "</a>,&nbsp;");
		
	document.write("<a class=\"" + linkColor + "\" href=\"javascript:open_window_ex('" + playerTeamHomepage[x] + "/','',680,900)\">" + playerTeamName[x] + "</a>");

	document.write("</td>");		
			
	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() {
	countVotes = 1;
	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 < (eastCenterOffset + totalNumCentersEast); x++ )
  	{
  		if ( document.ballot.elements[x].checked ){ 
			cookieString += x + "|";
			updatePlayerFormVar(playerId[x]);
		}
  	}
	
  	if(cookieString.length > 0)
  	{	
  		SetCookie ("AllStarVotesDL", cookieString.substring(0, cookieString.length-1), null, "/");
	}
  
  //Determine if write in ballot cookie needs to be erased
  //(User selected a write-in and then removed it upon review)
  if(GetCookie("AllStarVoteWriteInBallotDL") != null) {
  	DeleteCookie ("AllStarVoteWriteInBallotDL", "/");
	DeleteCookie ("AllStarVoteWriteInBallotTextDL", "/");
	
  }
  
  //if(document.ballot.writeinballot.selectedIndex > 0) {
	//Set cookie for Write In Vote
	//SetCookie ("AllStarVoteWriteInBallotDL", 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("AllStarVoteWriteInBallotDL");

	for (optionCounter = 0; optionCounter < writeInBallot.length;
 optionCounter++)
	{
		if(writeInCookie == writeInBallot.options[optionCounter].value) {
			writeInBallot.selectedIndex = optionCounter;
			return;
		}
	}
}

function getVote(playerIndex) {
	if(allStarVoteCookie2.indexOf("|" + playerIndex + "|") > -1)
		return true;
	else
		return false;
}


function openReviewPopupWithParam()
{
	var pls = "";
	if (cookieString!=null)
		pls = cookieString.substring(0, cookieString.length-1)
		
	initHeight = 138;
	
	popupHeight = 676;
	
	var reviewPage = 'http://www.nba.com/dleague/allstar2008/asb/review.html?players=' + pls;

	reviewPopup = window.open(reviewPage, 'asbReview', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=612,height=' + popupHeight);
	reviewPopup.focus();
}


function showAtLeastOneVoteAlert()
{
	alert( "You must vote for at least one player.");
}

function showCountryAlert()
{
	alert( "Please enter your country.");
}


function showValidationCodeRequiredAlert()
{
	alert("Please validate your vote by entering the appropriate code.");
}

function showMaxForwardsAllowedAlert()
{
	alert( "You are not permitted to choose more than 4 forwards." );
}

function showMaxGuardsAllowedAlert()
{
	alert( "You are not permitted to choose more than 4 guards." );
}

function showMaxCentersAllowedAlert()
{
	alert( "You are not permitted to choose more than 2 centers." );
}

function getProfileText()
{	
	profile='Stats';		
}


function getHighlightText()
{	
	highlight='Video';
}
