// JavaScript Document

// Global Functions
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}



/* ELEMENT CREATION*/ 

/* form */
function create_form(action,name,id,method) {
	var form = document.createElement("form");
	form.setAttribute("action",action);
	form.setAttribute("name",name);
	form.setAttribute("id",id);
	form.setAttribute("method",method);
	return form;
}

/* input */
function create_input(name,id,value,type) {
	var input = document.createElement("input");
	input.setAttribute("name",name);
	input.setAttribute("id",id);
	input.setAttribute("value",value);
	input.setAttribute("type",type);
	return input;	
}

/* select */
function create_select(name,id) {
	var select = document.createElement("select");
	select.setAttribute("name",name);
	select.setAttribute("id",id);
	return select;	
}

/* option */
function create_option(value,text) {
	var option = document.createElement("option");
	option.setAttribute("value",value);
	var option_text = document.createTextNode(text);
	option.appendChild(option_text);
	return option;	
}

/* button */
function create_button(name,id,text,type) {
	//Dynamically created buttons won't submit in IE - use input instead
	var button;
	if(is_ie()) {
		button = create_input(name,id,text,type);
	} else {
		var button = document.createElement("button");
		button.setAttribute("name",name);
		button.setAttribute("id",id);
		var button_text = document.createTextNode(text);
		button.appendChild(button_text);
		button.setAttribute("type",type);
	}
	return button;
}

/* Link */
function create_link(url,txt,c,id) {
	var new_link = document.createElement("a");
	new_link.setAttribute("href",url);
	new_link.setAttribute("id",id);
	new_link.className = c;
	var anchor_text = document.createTextNode(txt);
	new_link.appendChild(anchor_text);
	return new_link;
}

/* generic text mark up tags */
function create_tag(id,text,tagtype) {
	var tag = document.createElement(tagtype);
	tag.setAttribute("id",id);
	var tag_text = document.createTextNode(text);
	tag.appendChild(tag_text);
	return tag;
}

function create_label(text,lfor) {
	var label = document.createElement("label");
	label.setAttribute("for",lfor);
	var label_text = document.createTextNode(text);
	label.appendChild(label_text);
	return label;
}





//set up country drop down
function create_team_select() {
	//test for browser capabiities
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	//alert("hello");
	var c_form = create_form("","team","team","get");
	
	//h6 = document.getElementById("c_label");
	
	var ltext = "Select your team:";
	var lfor = "select_a_team";
	
	var label = create_label(ltext,lfor);
	
	c_form.appendChild(label);
	
	
	
	
	var c_select =  create_select("select_a_team","select_a_team");
	
	
	var football_teams = document.getElementById("football_teams");
	var a = football_teams.getElementsByTagName("a");
	var c_option = 	create_option("","Choose...");
	c_select.appendChild(c_option);
	//alert(a.length);
	for(i=0;i<a.length;i++) {
		c_option = create_option(a[i].getAttribute("href"),a[i].firstChild.nodeValue);
		//if (a[i].getAttribute("class") == "selected" || a[i].getAttribute("className") == "selected") c_option.setAttribute("selected","selected");
		c_select.appendChild(c_option);
	}
	
	c_select.onchange = function() {
		window.location = this.options[this.selectedIndex].value;
	}
	c_form.appendChild(c_select);
	var parent_node = football_teams.parentNode;
	parent_node.replaceChild(c_form,football_teams);
	
	//var container = country_list.parentNode;
	//container.removeChild(country_list);
	
	//country_language.removeChild(h6);
}



addLoadEvent(create_team_select);