var countEastC = 0;
var countEastG = 0;
var countEastF = 0;
var countWestC = 0;
var countWestG = 0;
var countWestF = 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 countFavEastForwards()
{
	numFavPlayers = 0;
  for( x = eastForwardOffset; x <(eastForwardOffset + totalNumForwardsEast); 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 )
  {
	showMaxForwardsAllowedAlert();
    return false;
  }
  else
    return true;
}

function countFavEastGuards()
{
  numFavPlayers = 0;
  for( x = eastGuardOffset; x < (totalNumGuardsEast + 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 )
  {
	showMaxGuardsAllowedAlert();
	return false;
  }
  else
    return true;
}

function countFavEastCenters()
{
  numFavPlayers = 0;

  for( x = eastCenterOffset; x < (totalNumCentersEast + eastCenterOffset); x++ )
//  for( x = eastCenterOffset; x < (totalNumCentersEast + 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 )
  {
	showMaxCentersAllowedAlert();
	countEastC--;
    return false;
  }
  else
    return true;
}

function countFavWestForwards()
{
  numFavPlayers = 0;
  for( x = westForwardOffset; x < (westForwardOffset + totalNumForwardsWest); 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 )
    {
		showMaxForwardsAllowedAlert();
		countWestF--;
    	return false;
  	}
  	else
   	 	return true;
}

function countFavWestGuards()
{
  numFavPlayers = 0;
  for( x = westGuardOffset; x < (totalNumGuardsWest + 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 )
  {
	showMaxGuardsAllowedAlert();
	countWestG--;
    return false;
  }
  else
    return true;
}

function countFavWestCenters()
{
  numFavPlayers = 0;
  
  for( x = westCenterOffset; x < (totalNumCentersWest + westCenterOffset); x++ )
//  for( x = westCenterOffset; x < (totalNumCentersWest + 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 )
  {
	showMaxCentersAllowedAlert();
    countWestC--;
	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
	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) 
	{
		showAtLeastOneVoteAlert();
		
		return false;
	}
	else
		return true;
}

function drawEasternForwards() {
	currentLocation = eastForwardOffset;

	targetLocation = eastForwardOffset + totalNumForwardsEast;
	
	validateString = "validateEastForwards()";
	checkboxString = "eastForward";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();

	playerLoop(currentLocation, targetLocation, linkColor );
}

function drawEasternGuards() {

	currentLocation = eastGuardOffset;
	targetLocation = eastGuardOffset + totalNumGuardsEast;

	validateString = "validateEastGuards()";
	checkboxString = "eastGuard";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();
	
	playerLoop(currentLocation, targetLocation, linkColor );
}

function drawEasternCenters() {

	currentLocation = eastCenterOffset;

	targetLocation = eastCenterOffset + totalNumCentersEast;
//	targetLocation = eastCenterOffset + totalNumCentersEast;
	
	
	validateString = "validateEastCenters()";
	checkboxString = "eastCenter";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();
	
	playerLoop(currentLocation, targetLocation, linkColor);
}

function drawWesternForwards() {
	currentLocation = westForwardOffset;
	targetLocation = westForwardOffset + totalNumForwardsWest;

	validateString = "validateWestForwards()";
	checkboxString = "westForward";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();
	
	playerLoop(currentLocation, targetLocation, linkColor );

}

function drawWesternGuards() {

	currentLocation = westGuardOffset;
	targetLocation = westGuardOffset + totalNumGuardsWest;

	validateString = "validateWestGuards()";
	checkboxString = "westGuard";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();
	
	playerLoop(currentLocation, targetLocation, linkColor );

}

function drawWesternCenters() {
	currentLocation = westCenterOffset;

	targetLocation = westCenterOffset + totalNumCentersWest;
//	targetLocation = westCenterOffset + totalNumCentersWest;
	
	
	validateString = "validateWestCenters()";
	checkboxString = "westCenter";
	linkColor = "playerLink";

	getHighlightText();
	getProfileText();
	
	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=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=\"210\" class=\""+linkColor+"\" nowrap>");

	document.write("<b>"+playerFname[x].substring(0,1) + ".&nbsp;" + playerLname[x] + "</b>,&nbsp;");
	
	document.write(playerTeamName[x]);

	document.write("</td><td width=\"90\" align=\"right\" class=\""+linkColor+"\">");
			
	document.write("<a class=\"" + linkColor + "\" href=\"javascript:open_window_ex('http://www.nba.com/dleague/playerfile/" + playerHomepage[x] + "/','',570,860)\">" + profile + "</a> ");	
		
	if (playerHighlightAvi[x]!='default')
	{ 
		var hname = playerHighlightAvi[x];
		
	//	document.write("<a class=\"" + linkColor + "\" href=\"javascript:nbaVideo(escape('http://broadband.nba.com/cc/playa.php?content=video&url=http://boss.streamos.com/wmedia/nba/asb/" + hname + "_asb.asx'),escape('blank'));\">" + highlight + "</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() {
	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/allstar2007/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 chose more than 2 forwards." );
}

function showMaxGuardsAllowedAlert()
{
	alert( "You are not permitted to chose more than 2 guards." );
}

function showMaxCentersAllowedAlert()
{
	alert( "You are not permitted to chose more than 1 center." );
}

function getProfileText()
{	
	profile='Stats';		
}


function getHighlightText()
{	
	highlight='Video';
}
