//##########################################################################################

//--> Begin Function :: SendTo
	function SendTo(ThisElement, Conference, Round, Game, Position){
		//get the league
		var League = ThisElement.getAttribute("league");
		
		if(League == "nhl"){
			//object for storing the seeds
			function Seed(id, seed, name){
				this.id = id;
				this.seed = seed;
				this.name = name;
			}

			//object for team we are moving and the team it should replace
			var NewTeam = new Seed(ThisElement.options[ThisElement.selectedIndex].getAttribute("value"), ThisElement.options[ThisElement.selectedIndex].getAttribute("seed"), ThisElement.options[ThisElement.selectedIndex].innerHTML);
			var OldTeam = new Seed("~", "", "");

			if(Conference == "east" && Round == "2"){
				//find the old team the new team will replace
				var EastRound1Series1Nodes = document.getElementById("team-e11").getElementsByTagName("option");
				var EastRound1Series2Nodes = document.getElementById("team-e12").getElementsByTagName("option");
				var EastRound1Series3Nodes = document.getElementById("team-e13").getElementsByTagName("option");
				var EastRound1Series4Nodes = document.getElementById("team-e14").getElementsByTagName("option");

				if(EastRound1Series1Nodes[0].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(EastRound1Series1Nodes[1].getAttribute("value"), EastRound1Series1Nodes[1].getAttribute("seed"), EastRound1Series1Nodes[1].innerHTML);
				}
				else if(EastRound1Series1Nodes[1].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(EastRound1Series1Nodes[0].getAttribute("value"), EastRound1Series1Nodes[0].getAttribute("seed"), EastRound1Series1Nodes[0].innerHTML);
				}
				else if(EastRound1Series2Nodes[0].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(EastRound1Series2Nodes[1].getAttribute("value"), EastRound1Series2Nodes[1].getAttribute("seed"), EastRound1Series2Nodes[1].innerHTML);
				}
				else if(EastRound1Series2Nodes[1].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(EastRound1Series2Nodes[0].getAttribute("value"), EastRound1Series2Nodes[0].getAttribute("seed"), EastRound1Series2Nodes[0].innerHTML);
				}
				else if(EastRound1Series3Nodes[0].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(EastRound1Series3Nodes[1].getAttribute("value"), EastRound1Series3Nodes[1].getAttribute("seed"), EastRound1Series3Nodes[1].innerHTML);
				}
				else if(EastRound1Series3Nodes[1].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(EastRound1Series3Nodes[0].getAttribute("value"), EastRound1Series3Nodes[0].getAttribute("seed"), EastRound1Series3Nodes[0].innerHTML);
				}
				else if(EastRound1Series4Nodes[0].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(EastRound1Series4Nodes[1].getAttribute("value"), EastRound1Series4Nodes[1].getAttribute("seed"), EastRound1Series4Nodes[1].innerHTML);
				}
				else if(EastRound1Series4Nodes[1].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(EastRound1Series4Nodes[0].getAttribute("value"), EastRound1Series4Nodes[0].getAttribute("seed"), EastRound1Series4Nodes[0].innerHTML);
				}
	
				//arrays for seeding
				var EastSeeds = new Array();
				var WestSeeds = new Array();
				
				//get the teams in the east, round 2
				var EastRound2Series1Nodes = document.getElementById("team-e21").getElementsByTagName("option");
				EastSeeds.push(new Seed(EastRound2Series1Nodes[0].getAttribute("value"), EastRound2Series1Nodes[0].getAttribute("seed"), EastRound2Series1Nodes[0].innerHTML));
				EastSeeds.push(new Seed(EastRound2Series1Nodes[1].getAttribute("value"), EastRound2Series1Nodes[1].getAttribute("seed"), EastRound2Series1Nodes[1].innerHTML));
				var EastRound2Series2Nodes = document.getElementById("team-e22").getElementsByTagName("option");
				EastSeeds.push(new Seed(EastRound2Series2Nodes[0].getAttribute("value"), EastRound2Series2Nodes[0].getAttribute("seed"), EastRound2Series2Nodes[0].innerHTML));
				EastSeeds.push(new Seed(EastRound2Series2Nodes[1].getAttribute("value"), EastRound2Series2Nodes[1].getAttribute("seed"), EastRound2Series2Nodes[1].innerHTML));
			
				//replace the old team with the new team
				var TeamReplaced = false;
				for(var i = 0; i < EastSeeds.length; i++){
					if(EastSeeds[i].id == OldTeam.id){
						EastSeeds[i] = NewTeam;
						TeamReplaced = true;
						break;
					}
				}

				//if no old team was replaced, put the new team in the first open slot
				if(!TeamReplaced){
					for(var i = 0; i < EastSeeds.length; i++){
						if(EastSeeds[i].name == ""){
							EastSeeds[i] = NewTeam;
							break;
						}
					}
				}
				
				//sort the teams in order of seed
				EastSeeds.sort(
					function(a,b) {
						return a.seed - b.seed;
					}
				);
				
				//the highest seeded team [0] and the lowest seeded team [3] will play in the first bracket
				EastRound2Series1Nodes[0].setAttribute("value", EastSeeds[0].id);
				EastRound2Series1Nodes[0].setAttribute("seed", EastSeeds[0].seed);
				EastRound2Series1Nodes[0].innerHTML = EastSeeds[0].name;
				EastRound2Series1Nodes[1].setAttribute("value", EastSeeds[3].id);
				EastRound2Series1Nodes[1].setAttribute("seed", EastSeeds[3].seed);
				EastRound2Series1Nodes[1].innerHTML = EastSeeds[3].name;
				
				//the two middle seeded teams [1] and [2] will play in the second bracket
				EastRound2Series2Nodes[0].setAttribute("value", EastSeeds[1].id);
				EastRound2Series2Nodes[0].setAttribute("seed", EastSeeds[1].seed);
				EastRound2Series2Nodes[0].innerHTML = EastSeeds[1].name;
				EastRound2Series2Nodes[1].setAttribute("value", EastSeeds[2].id);
				EastRound2Series2Nodes[1].setAttribute("seed", EastSeeds[2].seed);
				EastRound2Series2Nodes[1].innerHTML = EastSeeds[2].name;
			}
			else if(Conference == "west" && Round == "2"){
				//find the team the new team will replace
				var WestRound1Series1Nodes = document.getElementById("team-w11").getElementsByTagName("option");
				var WestRound1Series2Nodes = document.getElementById("team-w12").getElementsByTagName("option");
				var WestRound1Series3Nodes = document.getElementById("team-w13").getElementsByTagName("option");
				var WestRound1Series4Nodes = document.getElementById("team-w14").getElementsByTagName("option");
				if(WestRound1Series1Nodes[0].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(WestRound1Series1Nodes[1].getAttribute("value"), WestRound1Series1Nodes[1].getAttribute("seed"), WestRound1Series1Nodes[1].innerHTML);
				}
				else if(WestRound1Series1Nodes[1].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(WestRound1Series1Nodes[0].getAttribute("value"), WestRound1Series1Nodes[0].getAttribute("seed"), WestRound1Series1Nodes[0].innerHTML);
				}
				else if(WestRound1Series2Nodes[0].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(WestRound1Series2Nodes[1].getAttribute("value"), WestRound1Series2Nodes[1].getAttribute("seed"), WestRound1Series2Nodes[1].innerHTML);
				}
				else if(WestRound1Series2Nodes[1].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(WestRound1Series2Nodes[0].getAttribute("value"), WestRound1Series2Nodes[0].getAttribute("seed"), WestRound1Series2Nodes[0].innerHTML);
				}
				else if(WestRound1Series3Nodes[0].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(WestRound1Series3Nodes[1].getAttribute("value"), WestRound1Series3Nodes[1].getAttribute("seed"), WestRound1Series3Nodes[1].innerHTML);
				}
				else if(WestRound1Series3Nodes[1].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(WestRound1Series3Nodes[0].getAttribute("value"), WestRound1Series3Nodes[0].getAttribute("seed"), WestRound1Series3Nodes[0].innerHTML);
				}
				else if(WestRound1Series4Nodes[0].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(WestRound1Series4Nodes[1].getAttribute("value"), WestRound1Series4Nodes[1].getAttribute("seed"), WestRound1Series4Nodes[1].innerHTML);
				}
				else if(WestRound1Series4Nodes[1].getAttribute("value") == NewTeam.id){
					OldTeam = new Seed(WestRound1Series4Nodes[0].getAttribute("value"), WestRound1Series4Nodes[0].getAttribute("seed"), WestRound1Series4Nodes[0].innerHTML);
				}
	
				//arrays for seeding
				var WestSeeds = new Array();
				var WestSeeds = new Array();
				
				//get the teams in the west, round 2
				var WestRound2Series1Nodes = document.getElementById("team-w21").getElementsByTagName("option");
				WestSeeds.push(new Seed(WestRound2Series1Nodes[0].getAttribute("value"), WestRound2Series1Nodes[0].getAttribute("seed"), WestRound2Series1Nodes[0].innerHTML));
				WestSeeds.push(new Seed(WestRound2Series1Nodes[1].getAttribute("value"), WestRound2Series1Nodes[1].getAttribute("seed"), WestRound2Series1Nodes[1].innerHTML));
				var WestRound2Series2Nodes = document.getElementById("team-w22").getElementsByTagName("option");
				WestSeeds.push(new Seed(WestRound2Series2Nodes[0].getAttribute("value"), WestRound2Series2Nodes[0].getAttribute("seed"), WestRound2Series2Nodes[0].innerHTML));
				WestSeeds.push(new Seed(WestRound2Series2Nodes[1].getAttribute("value"), WestRound2Series2Nodes[1].getAttribute("seed"), WestRound2Series2Nodes[1].innerHTML));
			
				//replace the old team with the new team
				var TeamReplaced = false;
				for(var i = 0; i < WestSeeds.length; i++){
					if(WestSeeds[i].id == OldTeam.id){
						WestSeeds[i] = NewTeam;
						TeamReplaced = true;
						break;
					}
				}
				
				//if no old team was replaced, put the new team in the first open slot
				if(!TeamReplaced){
					for(var i = 0; i < WestSeeds.length; i++){
						if(WestSeeds[i].name == ""){
							WestSeeds[i] = NewTeam;
							break;
						}
					}
				}
				
				//sort the teams in order of seed
				WestSeeds.sort(
					function(a,b) {
						return a.seed - b.seed;
					}
				);
				
				//the highest seeded team [0] and the lowest seeded team [3] will play in the first bracket
				WestRound2Series1Nodes[0].setAttribute("value", WestSeeds[0].id);
				WestRound2Series1Nodes[0].setAttribute("seed", WestSeeds[0].seed);
				WestRound2Series1Nodes[0].innerHTML = WestSeeds[0].name;
				WestRound2Series1Nodes[1].setAttribute("value", WestSeeds[3].id);
				WestRound2Series1Nodes[1].setAttribute("seed", WestSeeds[3].seed);
				WestRound2Series1Nodes[1].innerHTML = WestSeeds[3].name;
				
				//the two middle seeded teams [1] and [2] will play in the second bracket
				WestRound2Series2Nodes[0].setAttribute("value", WestSeeds[1].id);
				WestRound2Series2Nodes[0].setAttribute("seed", WestSeeds[1].seed);
				WestRound2Series2Nodes[0].innerHTML = WestSeeds[1].name;
				WestRound2Series2Nodes[1].setAttribute("value", WestSeeds[2].id);
				WestRound2Series2Nodes[1].setAttribute("seed", WestSeeds[2].seed);
				WestRound2Series2Nodes[1].innerHTML = WestSeeds[2].name;
			}
			else{
				//find the element we are putting the winner into, and put their name there
				ToElement = SelectNode({"conference" : Conference, "round" : Round, "game" : Game});
				if(ToElement != null){
					ToElement.options[Position].text = ThisElement.options[ThisElement.selectedIndex].text;
					ToElement.options[Position].value = ThisElement.options[ThisElement.selectedIndex].value;
					ToElement.options[Position].setAttribute("seed", ThisElement.options[ThisElement.selectedIndex].getAttribute("seed"));
					ToElement.selectedIndex = -1;
				}
			}
			
			//wipe out all later round picks
				//get all the select elements on the page
				var Elements = document.getElementsByTagName("select");
				
				//loop through all of the select elements
				for (var i = 0; i < Elements.length; i++){
					//get element
					var Element = Elements[i];
					
					if(Element.getAttribute('round') >= Round && Element.getAttribute('conference') == Conference){
						Element.selectedIndex = -1;
					}
					if(Element.getAttribute('round') > Round && Element.getAttribute('conference') == Conference){
						Element.options[0].text = '';
						Element.options[0].value = '';
						Element.options[1].text = '';
						Element.options[1].value = '';
					}
					if(Element.getAttribute("conference") == "finals"){
						if(Conference == "east"){
							Element.options[0].text = '';
							Element.options[0].value = '';
						}
						if(Conference == "west"){
							Element.options[1].text = '';
							Element.options[1].value = '';
						}
						Element.selectedIndex = -1;
					}
				}
			//end wipe out all later round picks
			
/*			//check later rounds to be sure the old team was not chosen
			if(OldTeam.name != ""){
				//get all the elements on the page
				var Elements = document.getElementsByTagName("select");
				
				//loop through the entire document
				for (var i = 0; i < Elements.length; i++){
					//get element
					var Element = Elements[i];
		
					//if the round is greater than the round we just updated, remove the team from later rounds
					if(Element.name != null && Element.getAttribute('round') != null){
						if(Element.name.indexOf('team-') > -1){
							if(Element.getAttribute('round') >= Round && Element.getAttribute('conference') == Conference){
								Element.selectedIndex = -1;
							}
							if(Element.getAttribute('round') > Round){
								if((Element.options[0].value == OldTeam.id || Element.options[1].value == OldTeam.id) && Element.getAttribute('conference') == Conference){
									Element.options[0].text = '';
									Element.options[0].value = '';
									Element.options[1].text = '';
									Element.options[1].value = '';
								}
							}
						}
					}
				}

				if(Conference == "east"){
					document.getElementById("team-f41").getElementsByTagName("option")[0].value = "";
					document.getElementById("team-f41").getElementsByTagName("option")[0].innerHTML = "";
					document.getElementById("team-f41").selectedIndex = -1;
				}
				else{
					document.getElementById("team-f42").getElementsByTagName("option")[0].value = "";
					document.getElementById("team-f42").getElementsByTagName("option")[0].innerHTML = "";
					document.getElementById("team-f42").selectedIndex = -1;
				}
			} */
		}
		else{
			//find the element we are putting the winner into, and put their name there
			ToElement = SelectNode({"conference" : Conference, "round" : Round, "game" : Game});
			if(ToElement != null){
				ToElement.options[Position].text = ThisElement.options[ThisElement.selectedIndex].text;
				ToElement.options[Position].value = ThisElement.options[ThisElement.selectedIndex].value;
				ToElement.options[Position].setAttribute("seed", ThisElement.options[ThisElement.selectedIndex].getAttribute("seed"));
				ToElement.selectedIndex = -1;
			}
			
			//now, check all elements with a round higher than were we put this into
			//and remove the team it used to be from those rounds
			
			//get element number of team that was just 'unchosen'
			var originalIndex = (ThisElement.selectedIndex == 0 ? 1 : 0);
			var originalText = ThisElement.options[originalIndex].text;
	
			//get all the elements on the page
			var Elements = document.getElementsByTagName("*");
			
			//loop through the entire document
			for (var i = 0; i < Elements.length; i++){
				//get element
				var Element = Elements[i];
	
				//if the round is greater than the round we just updated, remove the team from later rounds
				if(Element.name != null && Element.getAttribute('round') != null){
					if(Element.name.indexOf('team-') > -1){
						if(Element.getAttribute('round') > Round){
							if(Element.options[0].test == originalText){
								Element.options[0].text = '';
								Element.options[0].value = '';
								Element.selectedIndex = -1;
							}
							if(Element.options[1].text == originalText){
								Element.options[1].text = '';
								Element.options[1].value = '';
								Element.selectedIndex = -1;
							}
						}
					}
				}
			}
		}
		
		return;
	}
//--> End Function :: SendTo

//##########################################################################################
	
//--> Begin Event :: FormCheck
	function FormCheck(Form){
		//ask the user if they are sure
		if(!confirm('Are you sure you want to submit your picks? \n\nWARNING: Your picks are final when you submit them and multiple or invalid picks will be disqualified.')){
			return false;
		}
		
		//initialize error list
		var ErrorList = "";
		
		//get all the elements on the page
		var Elements = document.getElementsByTagName("*");
		
		//loop through the entire document
		for (var i = 0; i < Elements.length; i++){
			//get element
			var Element = Elements[i];

			//check that there are two teams in each team box
			if(Element.name != null){
				if(Element.name.indexOf('team-') > -1){
					if(Element.options[0].text == '' || Element.options[1].text == ''){
						Conference = Element.getAttribute("Conference");
						Round = Element.getAttribute("Round");
						Game = Element.getAttribute("Game");
						ErrorList += "You must choose two teams for " + Conference + ", Round " + Round + ", Game " + Game + "\n";
					}
					//and that one of the teams was selected
					else if(Element.selectedIndex == -1){
						Conference = Element.getAttribute("Conference");
						Round = Element.getAttribute("Round");
						Game = Element.getAttribute("Game");
						ErrorList += "You must choose a winner for " + Conference + ", Round " + Round + ", Game " + Game + "\n";
					}
				}
			}
		}
		
		//check that a tie breaker has been entered
		var TieBreaker = document.getElementById('tiebreaker');
		if(TieBreaker.value == "" || TieBreaker.value == null){
			ErrorList += "You must enter a tiebreaker.\n";
		}
		//check that tiebreaker is numeric
		else if (TieBreaker.value == null || !TieBreaker.value.match(/^\d*$/)){
			ErrorList += "The value of the tiebreaker must be a number.\n";
		}

		//display errors, if necessary, and return to caller
		if(ErrorList == ""){
			return true;
		}
		else{
			alert(ErrorList);
			return false;
		}
	}
//--> End Function :: FormCheck

//##########################################################################################

//--> Begin Function :: SelectNode

	function SelectNode(Attributes){
		
		//get attribute array length
		var AttributeLength = 0;
		for (var Item in Attributes) {
			AttributeLength++;
		}
		
		var Elements = document.getElementsByTagName("*");
		
		//loop through the entire document
		for (var i = 0; i < Elements.length; i++){
			
			//get element
			var Element = Elements[i];
			var AttributeCount = 0;
			
			//locate node by getting attribute count
			for (var AttributeName in Attributes){
				if(Element.getAttribute(AttributeName) == Attributes[AttributeName]){
					AttributeCount++;
				}
			}
			
			//if attribute count is the same as the amount of identical attributes found
			//return the element
			if(AttributeCount == AttributeLength){
				return Element;
			}
		}
		return null;
	}
	
//--> End Function :: SelectNode

//##########################################################################################
