function ValidateNewMeetPerson(submitButton) {
	var formToValidate = submitButton.form
	var retVal = true
	var err = ""

	if (formToValidate.fname.value=="") {
		err+="- First name cannot be blank\n"
	}
	if (formToValidate.lname.value=="") {
		err+="- Last name cannot be blank\n"
	}
	if (IsVisible(formToValidate.level) && formToValidate.level.value=="") {
		err+="- Level cannot be blank\n"
	}	
	if ((formToValidate.usag_num.value != "") && 
			!isNaN(formToValidate.usag_num.value) &&
			(formToValidate.usag_num.value.length > 6)) {
		err+="- USAG numbers must be 6 or fewer digits\n"
	}
	if (IsVisible(formToValidate.comp_age) && 
			(formToValidate.comp_age.value=="" ||
			isNaN(formToValidate.comp_age.value))) {
		err+="- Competition age cannot be blank\n"
	}	
	if (IsVisible(formToValidate.birthdate) &&
			((formToValidate.birthdate) != null) &&
			(!check_date(formToValidate.birthdate))) {
		formToValidate.birthdate.value=""
		retVal = false
	}
	
	if (IsVisible(formToValidate.safety_expir) &&
			(formToValidate.safety_expir != null) && 
			(!check_date(formToValidate.safety_expir))) {
		formToValidate.safety_expir.value=""
		retVal = false
	}
	if (err != "") {
		alert("Please fix the following errors:\n"+err)
	}
	return ((err == "") && (retVal))
}

/*
	Calculates default competition age for gymnast from birthdate.
	Competition age is gymnast's age as of Sep. 1st of the competition year
	@birthdate - Format should be same as required by check_date, and validated before this function operates on it.
	@targetField - Form field to receive the calculated age.  Only filled if it is empty.
*/
function SetDefaultAge(birthdate, targetField, sex) {
	years = "";
	
	if (sex.toUpperCase() == 'M') {
		today = new Date()
		curMonth = today.getMonth()
		year = today.getFullYear()

		// Assume season is from Sep-April, so if we're executing this in April, we want a default
		// age based on Sep of previous year
		if (curMonth < 5) {
			year--
		}
		birthdate = (birthdate.split("-")).join("/") // remove "-"
		birthday = new Date(birthdate);

		refDate = new Date("09/01/"+year)
		years = refDate.getFullYear() - birthday.getFullYear()
		birthday.setYear( refDate.getFullYear() )
		// If birthday occurred after reference date, subtract 1
		if(birthday > refDate){
		   years-- 
		}
	} else {
		// Girls' age as of today
		today = new Date()
		birthdate = (birthdate.split("-")).join("/") // remove "-"
		birthday = new Date(birthdate);
		var ms = today.valueOf() - birthday.valueOf();
		var minutes = ms / 1000 / 60;
		var hours = minutes / 60;
		var days = hours / 24;
		var years = Math.floor(days/365.25);
	}

	if (!isNaN(years) && (targetField.value == "")) {
		targetField.value = years
	}
}


/*
	Sets the checked property of all checkboxes in a form
	to be the same as the given checkbox control.
*/
function checkBoxes(checkboxCtl)
{
	var parentForm = checkboxCtl.form;

	for (var i=0; i < parentForm.length;i++)
	{
		if (parentForm.elements[i].type == checkboxCtl.type) {
			parentForm.elements[i].checked = checkboxCtl.checked;
		}
	}
}

/*
	Changes check status of checkboxes whose name property begins with
	the string fom namePrefix
	@checkboxCtl - The control whose checked status the other checkboxes should match
	@namePrefix - Prefix of the controls whose checked status should be changed
*/
function checkAll(checkboxCtl, namePrefix) {
	var parentForm = this.form
	var checkboxes = document.getElementsByTagName("input")
	
	var results = ""
	var curName = ""
	for (var i=0; i<checkboxes.length; i++) {
		curCheckbox = checkboxes[i]
		if (curCheckbox.name.substr(0, namePrefix.length) == namePrefix) {
			curCheckbox.checked = checkboxCtl.checked
		}
	}
}

function ValidateForm(curForm) {
	var retVal = false
	var pwdControl = curForm.new_password
	var pwd = pwdControl.value
	var err = ""

	if (pwdControl) {
		if (pwdControl.value != curForm.confirm_password.value) {
			err += "- matches your confirmation password\n"
		}
		if (pwd.toLowerCase().indexOf("password") > -1) {
			err += "- does not contain the word 'password'\n"
		}
		if (pwd.length < 8) {
			err += "- is at least eight characters"
		}
		if (err.length > 0) {
			alert("Please enter a password that:\n"+err);
			pwdControl.select()
			pwdControl.focus()
		}
	}
	return (err.length==0)
}

/*
 This script and many more are available free online at
 The JavaScript Source!! http://javascript.internet.com
 Original:  Torsten Frey (tf@tfrey.de)
 Web Site:  http://www.tfrey.de
*/
<!-- Begin
function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var separator = "-";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
var yearAssumption = "";
var retVal;

	err = 0;
	DateValue = DateField.value;

	/* Delete all chars except 0..9 */
	for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
		  DateTemp = DateTemp + DateValue.substr(i,1);
	  }
	}
	DateValue = DateTemp;
	/* Always change date to 8 digits - string*/
	/* if year is entered as 2-digit / always assume 20xx */
	if (DateValue.length == 6) {
		if (DateValue.substr(4,2) < 50) {
			yearAssumption = '20';
		} else {
			yearAssumption = '19';
		}
		/*
		thisYear = new Date().getFullYear();
		if (thisYear < 2008) {
			yearAssumption = '19';
		} else {
			yearAssumption = '20';
		}
		*/
		DateValue = DateValue.substr(0,4) + yearAssumption + DateValue.substr(4,2);
	}
	if (DateValue.length != 8) {
		err = 19;
	}
	/* year is wrong if year = 0000 */
	year = DateValue.substr(4,4);
	if (year == 0) {
		err = 20;
	}
	/* Validation of month*/
	month = DateValue.substr(0,2);
	if ((month < 1) || (month > 12)) {
		err = 21;
	}
	/* Validation of day*/
	day = DateValue.substr(2,2);
	if (day < 1) {
	  err = 22;
	}
	/* Validation leap-year / february / day */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
		leap = 1;
	}
	if ((month == 2) && (leap == 1) && (day > 29)) {
		err = 23;
	}
	if ((month == 2) && (leap != 1) && (day > 28)) {
		err = 24;
	}
	/* Validation of other months */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
		err = 25;
	}
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
		err = 26;
	}
	/* if 00 is entered, no error, deleting the entry */
	if ((day == 0) && (month == 0) && (year == 00)) {
		err = 0 ; day = ""; month = ""; year = ""; separator = "";
	}
	/* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
	if (err == 0) {
		DateField.value = month + separator + day + separator + year;
		retVal = true;
	}
	/* Error-message if err != 0 */
	else {
		alert("Please enter "+field.name+" as: mm-dd-yyyy.");
		DateField.select();
		DateField.focus();
		retVal = false;
	}	
	return retVal;
}
//  End -->

function ShowAddClub(targetId) {
	var page_message = document.getElementById("pageMessage")
	if (page_message) {
		page_message.innerHTML = ""
	}
	var existingClubSection = document.getElementById("existingClub")
	existingClubSection.style.display = "none"

	var newClubSection = document.getElementById(targetId)
	if (newClubSection != null) {
		newClubSection.style.display = ""
	}
}

/*
 Changes login-form for password request
*/
function ShowRequestPassword() {
	document.login.btnSubmit.value = "Send password"
	document.login.txtPassword.name = "enteredEmail"
	//document.login.password.type = "text"
	var passwordBox = document.getElementById("txtPassword");
	changeInputType(passwordBox, "text");
	
	var label = document.getElementById("pwdLabel")
	if (label != null) {
		label.innerHTML = "Email password to:"
	}
	
	var pwdLink = document.getElementById("lkForgotPwd");
	if (pwdLink != null) {
		pwdLink.style.display = "none"
	}

	// change action
	var sFind = "";
	var sReplace = "";
	var curAction = document.login.action;
	// default.php?tpl=meetRegister1 & action=login & nextTpl=&nextAction=&testMode=true
	var variables = curAction.split("&");
	for (var i=0; i < variables.length; i++) {
		if (variables[i].toLowerCase().indexOf("action") > -1) {
			variables[i] = "action=requestPassword";
			break;
		}
	}

	var newAction = variables.join("&");	
	document.login.action = newAction;
}

/*
 change an input element's type, from text to password, or hidden to text
 IE doesn't make this easy. this function will:
 dynamically create a new element 
 copy the properties of the old element into the new element 
 set the type of the new element to the new type 
 then replace the old element with the new element
*/
function changeInputType(oldObject, oType) {
	try {
		document.getElementById(oldObject.id).type = 'text';
	} catch (err) {
		// for IE
		var newObject = document.createElement('input');
		newObject.type = oType;
		if(oldObject.size) newObject.size = oldObject.size;
		if(oldObject.value) newObject.value = oldObject.value;
		if(oldObject.name) newObject.name = oldObject.name;
		if(oldObject.id) newObject.id = oldObject.id;
		if(oldObject.className) newObject.className = oldObject.className;
		oldObject.parentNode.replaceChild(newObject,oldObject);
		return newObject;
	}
}

/*
 Changes action of form on login.tpl
 so password-reset request will be submitted.
*/
function PasswordResetRequest() {
	var sFind = "";
	var sReplace = "";
	var curAction = document.login.action;
	// default.php?tpl=meetRegister1 & action=login & nextTpl=&nextAction=&testMode=true
	var variables = curAction.split("&");
	for (var i=0; i < variables.length; i++) {
		if (variables[i].toLowerCase().indexOf("action") > -1) {
			variables[i] = "action=requestPasswordReset";
			break;
		}
	}

	var newAction = variables.join("&");	
	document.login.action = newAction;
}


/*
 When logged-in as admin, used by main.tpl to alter URL so submit changes active club
*/
function ChangeClub(clubId) {
	var page = new PageQuery(window.location.search); 

	keyIndex = page.getKeyIndex('clubId')
	if (keyIndex == false) {
		page.getKeyValuePairs().push("clubId="+clubId)
	} else {
		page.getKeyValuePairs()[keyIndex] = "clubId="+clubId
	}
	window.location.search = page.getKeyValuePairs().join("&")
}


function PageQuery(q) {
	if (q.length > 1) {
		this.q = q.substring(1, q.length);
	} else {
		this.q = null;
	}

	this.keyValuePairs = new Array();
	this.keys = new Array();
	this.values = new Array();

	if (q) {
		pairs = this.q.split("&")
		for(var i=0; i < pairs.length; i++) {
			this.keyValuePairs[i] = pairs[i];

			pairValue = pairs[i].split("=");
			this.keys[i] = unescape(pairValue[0])
			this.values[i] = unescape(pairValue[0])
		}
	}
	
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getKeys = function() { return this.key; }
	this.getValues = function() { return this.values; }
	
	this.getKeyIndex = function(keyToFind) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keys[j] == keyToFind) {
				return j;
			}
		}
		return false;		
	}
	
	this.getValue = function(keyToFind) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keys[j] == keyToFind) {
				return this.values[j];
			}
		}
		return false;
	}

	this.getLength = function() { return this.keyValuePairs.length; } 
	
	
}


function GetActiveStyle(element, sStyle) {
	var y;
	if (element.currentStyle) {
		y = element.currentStyle[sStyle];
	} else {
		try {
		y = document.defaultView.getComputedStyle(element,null).getPropertyValue(sStyle);
	  } catch(e) { }
	}
	return y;
}

function IsVisible(element) {
	if (null != element) {
		return GetActiveStyle(element, "visibility") == "visible"
	} else {
		return false;
	}
}


// Show or hide a div tag
function ShowContent(vThis)
{
	// http://www.javascriptjunkie.com
	// alert(vSibling.className + " " + vDef_Key);
	vParent = vThis.parentNode;
	vSibling = vParent.nextSibling;
	while (vSibling.nodeType==3) { // Fix for Mozilla/FireFox Empty Space becomes a TextNode or Something
		vSibling = vSibling.nextSibling;
	}

	if(vSibling.style.display == "none")
	{
		vThis.src="/images/collapse.gif";
		vThis.alt = "Hide";
		vSibling.style.display = "block";
	} else {
		vSibling.style.display = "none";
		vThis.src="/images/expand.gif";
		vThis.alt = "Show";
	}
	return;
}

