var oldOnLoad = window.onload;
var buildMenu = function (){
	var tds = document.getElementsByTagName('td');
	
	for(var i = 0; i < tds.length; i++){
		var td = tds[i];
		
		if(td.className == "menuItem" || td.className == "menuItemSel"){
			//found a menu item
			td.onmouseover = function (){
				this.style.backgroundColor = "#A8C3F2";
				this.style["cursor"] = "pointer";
				this.style.color = "#000099";
			}
			
			td.onmouseout = function(){
				if(this.className != "menuItemSel"){
					this.style.backgroundColor = "#9dadb7";
					this.style["cursor"] = "default";
					this.style.color = "#014479";			
				}else{
					this.style.backgroundColor = "#004379";
					this.style["cursor"] = "default";
					this.style.color = "#FFFFFF";			
				}
			}
			
			var cont = true;
			if(td.getAttribute("rel")){
				var t = td.getAttribute("rel");
				if ((t.indexOf("menu_") != -1)  ||  t.toLowerCase() == "menu_") {
					td.onclick = function(){
						this.className = "menuItemSel";
						var tds = document.getElementsByTagName('td');
						
						for(var i = 0; i < tds.length; i++){
							var td = tds[i];
							
							if(td.className == "menuItemSel"){
								//found a menu item
								if(td != this){
									td.className = "menuItem";
									td.style.backgroundColor = "#9dadb7";
									td.style.color = "#014479";		
								}
								else{
									this.style.backgroundColor = "#004379";
									this.style.color = "#FFFFFF";									
								}
							}
						}
						menuAltClick(this.getAttribute("rel"));
					}
					cont = false;
				}	
			}
			
			
			if(cont){
				td.onclick = function(){
					this.className = "menuItemSel";
					var tds = document.getElementsByTagName('td');
					
					for(var i = 0; i < tds.length; i++){
						var td = tds[i];
						
						if(td.className == "menuItemSel"){
							//found a menu item
							if(td != this){
								td.className = "menuItem";
								td.style.backgroundColor = "#9dadb7";
								td.style.color = "#014479";		
							}
							else{
								this.style.backgroundColor = "#004379";
								this.style.color = "#FFFFFF";
								document.title = this.innerHTML;									
							}
						}
					}
					menuItemClick(this.id);
				}
			}
		}
	}
}

function extMenuItemClicked(clicked){
	//this.className = "menuItemSel";
	var tds = document.getElementsByTagName('td');
	
	for(var i = 0; i < tds.length; i++){
		var td = tds[i];
		
		if(td.className == "menuItemSel" || td.className == "menuItem"){
			//found a menu item
			if(td.id == clicked){
				td.className = "menuItemSel";
				td.style.backgroundColor = "#004379";
				td.style.color = "#FFFFFF";		
				document.title = td.innerHTML;
			}
			else{
				td.className = "menuItem";
				td.style.backgroundColor = "#9dadb7";
				td.style.color = "#014479";		
			}
		}
	}
	menuItemClick(clicked);
}

if(typeof oldOnLoad == 'function'){
	window.onload = function (){
		buildMenu();
		oldOnLoad();
	}
}
else{
	window.onload = function (){
		buildMenu();
	}
}

function unSelectAllMenu(){
	var tds = document.getElementsByTagName('td');
				
	for(var i = 0; i < tds.length; i++){
		var td = tds[i];
		
		if(td.className == "menuItemSel"){
			//found a menu item
			td.className = "menuItem";
			td.style.backgroundColor = "#9dadb7";
			td.style.color = "#014479";		
		}
	}
}

function menuItemClick(option){	
	var req = "option="+option;
	sendAjaxCommandUpdate(req, processResponse);
}

function menuAltClick(rel){
	setLocation(rel.substring(5));
}

function setLocation(loc){
	window.location = loc;
}

function processResponse(resp){
	document.getElementById('_CONTENT').innerHTML = resp;
}

var getNews = function (id){
	unSelectAllMenu();
	var req = "alt=_news&id="+id;
	sendAjaxCommandUpdate(req, processResponse);	
}

var switchSides = function (newSide){
	window.location = "index.php?p=1&_side="+newSide;
}

var displayVideoBox = function (id){
	var elem = document.getElementById(id);
	_displayVideoBox(elem)
	
	window.onscroll = function(){
		_displayVideoBox(elem);
	};
	window.onresize = function(){
		_displayVideoBox(elem);
	};
}

var _displayVideoBox = function (elem){
	var scrollPos = new getScrollPos();
	var pageSize = new getIboxPageSize();
	var emSizeWidth = getProperty(elem, "width");
	emSizeWidth = parseInt(emSizeWidth);
	var emSizeHeight = getProperty(elem, "height");
	emSizeHeight = parseInt(emSizeHeight);
		
	var x = Math.round(pageSize.width/2) - (emSizeWidth /2) + scrollPos.scrollX;
	var y = Math.round(pageSize.height/2) - (emSizeHeight /2) + scrollPos.scrollY;	
	elem.style.left = x+'px';
	elem.style.top = y+'px';	
	
	elem.style.display = 'block';
	document.getElementById('videoBD').style.height = pageSize.height+scrollPos.scrollY+'px';
	document.getElementById('videoBD').style.width = document.body.clientWidth;
	document.getElementById('videoBD').style.display = 'block';
}

var hideVideoBox = function (id){
	var div = document.getElementById(id);
	div.style.display = 'none';
	document.getElementById('videoBD').style.display = 'none';
	window.onscroll = null;
	window.onresize = null;
}

function getProperty(el, prop){
  if(document.defaultView != null){
  	return document.defaultView.getComputedStyle(el, "").getPropertyValue(prop);
  }else if(el.currentStyle){
     return el.currentStyle[prop];
  }

  return null;
}

function validateRequiredFields(name, msg){
	var requiredFields = getRequiredFields(name);
	
	for(var i = 0; i < requiredFields.length; i++){
		var id = requiredFields[i];
		var field = document.getElementById(id);
		if(!validateRequiredField(field, msg)){
			return false;
		}
	}
	
	return true;	
}

function getRequiredFields(name){
	var address = ["address_1", "city", "province", "postal_code"];
	var person = ["first_name", "last_name"]
	
	if(name == "athlete"){
		return address.concat(person);
	}
	
	if(name == "parent"){
		return address.concat(person).concat(["parent_relationship"]);
	}
	
	if(name == "medical"){
		return ["med_card", "med_card_province", "physician_name", "physician_phone", "emergency_contact_name", "emergency_contact_phone"];
	}
}

function validateRequiredField(field, msg){
	if(isEmpty(field)){
		field.style.backgroundColor = "yellow";
		alert(msg);
		return false;
	}
	
	return true;
}

function isEmpty(field){
	with(field){
		return(value == null || value == "");
	}
}

function validateForEmpty(field){
	with(field){
		if(value == null || value == ""){
			document.getElementById(name+'_blank').style.display = 'block';
			field.focus();
			return false;
		}
		document.getElementById(name+'_blank').style.display = 'none';
		return true;
	}
}

function validateForMatch(field1, field2){
	if(field1.value != field2.value){
		document.getElementById(field1.name+'_mismatch').style.display = 'block';
		field2.focus();
		return false;
	}
	document.getElementById(field1.name+'_mismatch').style.display = 'none';
	return true;
}

function validateAccountCreateForm(form){
	var validated = true;
	with(form){
		validated = validated && validateForEmpty(first_name);
		validated = validated && validateForEmpty(last_name);
		validated = validated && validateForEmpty(email);
		validated = validated && validateForMatch(email, email_repeat);
		validated = validated && validateForEmpty(password);
		validated = validated && validateForMatch(password, password_repeat);
	}
	return validated;
}

function useThisAddress(id){
	var li = document.getElementById(id);
	var addressID = getChildById(li, 'addressID').innerHTML;
	var address1 = getChildById(li, 'address1').innerHTML;
	var address2 = getChildById(li, 'address2').innerHTML;
	var city = getChildById(li, 'city').innerHTML;
	var province = getChildById(li, 'province').innerHTML;
	var postal_code = getChildById(li, 'postal_code').innerHTML;
	
	var regForm = document.getElementById('registrationForm');
	
	var checkBox = document.getElementById('useExistingAddress');
	checkBox.checked = true;
	checkBox.disabled = false;
	
	var mapped_vals = [];
	
	mapped_vals['addressID'] = addressID;
	mapped_vals['address_1'] = address1;
	mapped_vals['address_2'] = address2;
	mapped_vals['city'] = city;
	mapped_vals['province'] = province;
	mapped_vals['postal_code'] = postal_code;
	
	replaceInputValues(mapped_vals, false);
	
}

function useThisPhysician(id){
	var li = document.getElementById(id);
	var physicianID = getChildById(li, 'physicianID').innerHTML;
	var name = getChildById(li, 'name').innerHTML;
	var phone = getChildById(li, 'phone').innerHTML;

	
	var regForm = document.getElementById('registrationForm');
	
	var checkBox = document.getElementById('useExistingPhysician');
	checkBox.checked = true;
	checkBox.disabled = false;
	
	var mapped_vals = [];
	
	mapped_vals['physicianID'] = physicianID;
	mapped_vals['physician_name'] = name;
	mapped_vals['physician_phone'] = phone;
	
	replaceInputValues(mapped_vals, false);
}

function useThisEmergencyContact(id){
	var li = document.getElementById(id);
	var emergencyContactID = getChildById(li, 'emergencyContactID').innerHTML;
	var name = getChildById(li, 'name').innerHTML;
	var phone = getChildById(li, 'phone').innerHTML;

	
	var regForm = document.getElementById('registrationForm');
	
	var checkBox = document.getElementById('useExistingEmergencyContact');
	checkBox.checked = true;
	checkBox.disabled = false;
	
	var mapped_vals = [];
	
	mapped_vals['emergencyContactID'] = emergencyContactID;
	mapped_vals['emergency_contact_name'] = name;
	mapped_vals['emergency_contact_phone'] = phone;
	
	replaceInputValues(mapped_vals, false);
	
}

function useThisRegistration(id){
	var li = document.getElementById(id);

	var registrationID 	= getChildById(li, 'registrationID').innerHTML;
	var registrantID		= getChildById(li, 'registrantID').innerHTML;
	var first 					= getChildById(li, 'first').innerHTML;
	var last 						= getChildById(li, 'last').innerHTML;
	var bday 						= getChildById(li, 'bday').innerHTML;
	var email 					= getChildById(li, 'email').innerHTML;
	var home 						= getChildById(li, 'home').innerHTML;
	var chalet 					= getChildById(li, 'chalet').innerHTML;
	var mobile 					= getChildById(li, 'mobile').innerHTML;
	var addressID 			= getChildById(li, 'addressID').innerHTML;
	var address1 				= getChildById(li, 'address1').innerHTML;
	var address2 				= getChildById(li, 'address2').innerHTML;
	var city 						= getChildById(li, 'city').innerHTML;
	var province 				= getChildById(li, 'province').innerHTML;
	var postal 					= getChildById(li, 'postal').innerHTML;
	
	var regForm = document.getElementById('registrationForm');
	
	var checkBox = document.getElementById('useExistingAddress');
	checkBox.checked = true;
	checkBox.disabled = false;
	
	var mapped_vals = [];
	
	mapped_vals['addressID'] = addressID;
	mapped_vals['address_1'] = address1;
	mapped_vals['address_2'] = address2;
	mapped_vals['city'] = city;
	mapped_vals['province'] = province;
	mapped_vals['postal_code'] = postal;
	
	replaceInputValues(mapped_vals, false);
	
	mapped_vals = [];
	
	mapped_vals['registrationID'] = registrationID;
	mapped_vals['registrantID'] = registrantID;
	mapped_vals['first_name'] = first;
	mapped_vals['last_name'] = last;
	mapped_vals['email_address'] = email;
	mapped_vals['home_telephone'] = home;
	mapped_vals['chalet_telephone'] = chalet;
	mapped_vals['mobile_telephone'] = mobile;

	replaceInputValues(mapped_vals, false);	
	
	var date = bday.split("-");
	var year = date[0];
	var month = date[1];
	var day = date[2];
		
	updateDropDown("dob_year", year);
	updateDropDown("dob_month", month);
	updateDropDown("dob_day", day);
	
	var setActive = resetActive();
		
	var activeTag = document.getElementById(id+"active");
	
	activeTag.style.display = "block";
	
	setActive.value = id+"active";
}

function resetActive(){
	var setActive = document.getElementById("active");
	var activeValue = setActive.value;
	if(activeValue != "nothing"){
		activeTag = document.getElementById(activeValue).style.display= "none";
		setActive.value= "nothing";
	}
	
	return setActive;
}

function updateDropDown(id, selectedVal){
	var dd = document.getElementById(id);
	for(var i = 0; i < dd.options.length; i++){
		if(dd.options[i].value == selectedVal){
			dd.selectedIndex = i;
		}
	}
}

function resetDropDown(id){
	var dd = document.getElementById(id);
	dd.selectedIndex = 0;
}

var newAddress = function(){
	var checkBox = document.getElementById('useExistingAddress');
	var checked = checkBox.checked;
	if(!checked){
		checkBox.disabled = true;
		var mapped_vals = [];
		
		mapped_vals['addressID'] = "-1";
		mapped_vals['address_1'] = "";
		mapped_vals['address_2'] = "";
		mapped_vals['city'] = "";
		mapped_vals['province'] = "";
		mapped_vals['postal_code'] = "";
		
		replaceInputValues(mapped_vals, false);
	}
}

var newPhysician = function(){
	var checkBox = document.getElementById('useExistingPhysician');
	var checked = checkBox.checked;
	if(!checked){
		checkBox.disabled = true;
		var mapped_vals = [];
		
		mapped_vals['physicianID'] = "-1";
		mapped_vals['physician_name'] = "";
		mapped_vals['physician_phone'] = "";
		
		replaceInputValues(mapped_vals, false);
	}
}

var newEmergencyContact = function(){
	var checkBox = document.getElementById('useExistingEmergencyContact');
	var checked = checkBox.checked;
	if(!checked){
		checkBox.disabled = true;
		var mapped_vals = [];
		
		mapped_vals['emergencyContactID'] = "-1";
		mapped_vals['emergency_contact_name'] = "";
		mapped_vals['emergency_contact_phone'] = "";
		
		replaceInputValues(mapped_vals, false);
	}
}

var newRegistration = function(){
	document.getElementById('useExistingAddress').checked = false;
	newAddress();
	var mapped_vals = [];
	
	mapped_vals['registrationID'] = -1;
	mapped_vals['registrantID'] = -1;
	mapped_vals['first_name'] = "";
	mapped_vals['last_name'] = "";
	mapped_vals['email_address'] = "";
	mapped_vals['home_telephone'] = "";
	mapped_vals['chalet_telephone'] = "";
	mapped_vals['mobile_telephone'] = "";
	
	replaceInputValues(mapped_vals, false);
	resetActive();
	
	resetDropDown("dob_year");
	resetDropDown("dob_month");
	resetDropDown("dob_day");
	
}

function replaceInputValues(vals, disabled){
	var inputs = document.getElementsByTagName("input");
	for(var i = 0; i < inputs.length; i++){
		var input = inputs[i];
		var inputID = input.id;
		if(vals[inputID] != undefined){
			input.value = vals[inputID];
			if(input.type != "hidden")
				input.disabled = disabled;
		}
	}
}

function getChildById(elem, id){
	var children = elem.childNodes;
	for(var i = 0; i < children.length; i++){
		if(children.item(i).id == id){
			return children.item(i);
		}
	}
	
	return null;
}
