function WaiverFormOnSubmit( formReference ) {
	document.getElementById( "participantError" ).innerHTML = "";
	document.getElementById( "participantSignatureError" ).innerHTML = "";
	document.getElementById( "parentError" ).innerHTML = "";
	document.getElementById( "parentSignatureError" ).innerHTML = "";
	
	var error = false;
	var age = parseInt( formReference.age.value );
	if( age < 18 ) {
		if( formReference.parent.value.length == 0 ) {
			error = true;
			document.getElementById( "parentError" ).innerHTML = "&nbsp;(Required)";
		}
		if( formReference.parentSignature.value.length == 0 ) {
			error = true;
			document.getElementById( "parentSignatureError" ).innerHTML = "&nbsp;(Required)";
		}
	} else {
		if( formReference.participant.value.length == 0 ) {
			error = true;
			document.getElementById( "participantError" ).innerHTML = "&nbsp;(Required)";
		}
		if( formReference.participantSignature.value.length == 0 ) {
			error = true;
			document.getElementById( "participantSignatureError" ).innerHTML = "&nbsp;(Required)";
		}
	}
	
	if( error ) return false;
	
	var currentTime = new Date();
	formReference.clientSignatureTime.value = Math.floor( currentTime.getTime() / 1000 );
	
	return true;
}



function DecodeDate( monthElementId, dayElementId, yearElementId ) {
	var monthElement = document.getElementById( monthElementId );
	var dayElement = document.getElementById( dayElementId );
	var yearElement = document.getElementById( yearElementId );
	
	if( isNaN( monthElement.value ) || isNaN( dayElement.value ) || isNaN( yearElement.value ) ) return null;
	
	var monthValue = parseInt( monthElement.value, 10 ) - 1;
	var dayValue = parseInt( dayElement.value, 10 );
	var yearValue = parseInt( yearElement.value, 10 );
	
	var targetDate = new Date();
	targetDate.setFullYear( yearValue, monthValue, dayValue );
	
	if( targetDate.getFullYear() != yearValue || targetDate.getMonth() != monthValue || targetDate.getDate() != dayValue ) return null;
	
	return targetDate;
}


function PopulateBeltList() {
	var ageBreak = 17;
	
	var dateOfBirth = DecodeDate( "entryFormDateOfBirthMonth", "entryFormDateOfBirthDay", "entryFormDateOfBirthYear" );
	if( dateOfBirth === null ) return;
	var now = new Date();
	var age = now.getFullYear() - dateOfBirth.getFullYear() + ( dateOfBirth.getMonth() > now.getMonth() ? -1 : ( dateOfBirth.getMonth() == now.getMonth() && dateOfBirth.getDate() > now.getDate() ? -1 : 0 ) );
	if( age < 0 ) return;
	
	var beltElement = document.getElementById( "entryFormBelt" );
	if( beltElement.options.length > 0 ) {
		if( age < ageBreak && beltElement.options[ 2 ].value == "white-yellow" ) return;
		if( age >= ageBreak && beltElement.options[ 2 ].value == "green" ) return;
	}
	
	for( var index = beltElement.options.length - 1; index >= 0; index-- )
		beltElement.remove( index );
	
	var options = null;
	var labels = null;
	
	if( age < ageBreak ) {
		options = new Array( "white", "white-yellow", "yellow", "yellow-orange", "orange", "orange-green", "green", "green-blue", "blue", "blue-purple", "purple" );
	} else {
		options = new Array( "white", "green", "blue", "brown4", "brown3", "brown2", "brown1", "black" );
		labels = new Array( "White (6th kyu)", "Green (5th kyu)", "Blue (4th kyu)", "Brown (4th kyu)", "Brown (3rd kyu)", "Brown (2nd kyu)", "Brown (1st kyu)", "Black" );
	}
	
	var newOption = document.createElement( "option" );
	try { beltElement.add( newOption, null ); } catch( ex ) { beltElement.add( newOption ); }
		
	for( var index in options ) {
		newOption = document.createElement( "option" );
		newOption.value = options[ index ];
		newOption.text = ( labels === null ? options[ index ].substring( 0, 1 ).toUpperCase() + options[ index ].substring( 1 ) : labels[ index ] );
		try { beltElement.add( newOption, null ); } catch( ex ) { beltElement.add( newOption ); }
	}
}


function StartCountDown( tournamentId ) {
	setTimeout( "ContinueCountDown( " + tournamentId + " )", 1000 );
}

function ContinueCountDown( tournamentId ) {
	document.getElementById( "refreshCountdown" ).innerHTML = countdown;
	countdown--;
	
	if( countdown < 0 ) {
		window.location = "?i=" + tournamentId + "&a=r";
		return;
	}
	setTimeout( "ContinueCountDown( " + tournamentId + " )", 1000 );
}
