/* ----------------------------------------------------------------------------------------------------------------------------------------
/*
/*	This object handles member form validation - used on join_form.php and profile_edit.php
/*
/* ----------------------------------------------------------------------------------------------------------------------------------------*/
// create a namespace
YAHOO.namespace("login_form");
	
// define the namespace object
YAHOO.login_form.form_validation = function() {

	// shortcuts
	var $E = YAHOO.util.Event;
	var $D = YAHOO.util.Dom;
	var $ = $D.get;
	var form;
		
	return {
		init: function(){
			
			return true;	
		},
		
		validate_form: function(form_name){																		
			
			form = document.getElementById(form_name);
															
			// validate mandatory fields - all these have a 'mandatory' attribute						
			for(var i=0; i< form.elements.length; i++){
					
				$D.setStyle(form.elements[i],'border','0px');
				
				if(form.elements[i].getAttribute('mandatory')!=null){
															
					var field_label_field = document.getElementById(form.elements[i].id + '_label');
					
					if(field_label_field != null){
						var field_label = field_label_field.getAttribute('label');
						
						
					}																
					type_of_input = form.elements[i].type;
															
					switch(type_of_input){
						
						case 'text':
						
							if(isBlank(form.elements[i].value)){
								alert('Please enter a value for ' + field_label);
								form.elements[i].focus();
								return false;					
							}
							break;
							
						case 'password':
						
							if(isBlank(form.elements[i].value)){
								alert('Please enter a value for ' + field_label);
								form.elements[i].focus();
								return false;					
							}
							break;
							
						case 'select-one':
						
							if(form.elements[i].selectedIndex == 0){
																																
								switch(form.elements[i].id){
								
									case 'DateOfBirth_month' :
									
										alert('Please select the month of your date of birth ');										
										break;
										
									case 'DateOfBirth_day' :
									
										alert('Please select the day of your date of birth ');										
										break;
										
									case 'DateOfBirth_year' :
									
										alert('Please select the year of your date of birth ');										
										break;
										
									default :
										alert('Please select an option for ' + field_label);
										break;
									
								}
								form.elements[i].focus();
								$D.setStyle(form.elements[i],'border','1px solid red');
								return false;
								
								
							}												
							break;
						
					}
																				
				}
				
			}
		
			// validate Nickname as alphanumeric
			if(document.getElementById('NickName')){												
				if(!isAlphanumeric(document.getElementById('NickName').value)) {
					alert('Username can be composed of letters and numbers only');
					form.NickName.focus();
					return false;					
				}
			}
			
			// validate password / confirm password
			if(document.getElementById('Password')){
				if(document.getElementById('Password').value != document.getElementById('Password2').value) {
					alert('Passwords do not match');
					form.Password2.focus();
					return false;					
				}
			}
			
			// validate password / confirm password
			if(document.getElementById('Password1')){
				if(document.getElementById('Password1').value != document.getElementById('Password2').value) {
					alert('Passwords do not match');
					form.Password2.focus();
					return false;					
				}
			}
			
			
			// validate Player				
			if(document.getElementById('Do_you_Play_Polo') && document.getElementById('Do_you_Play_Polo').selectedIndex == 0 ){
				alert('Please choose yes or no.');
				form.Do_you_Play_Polo.focus();
				return false;				
			}
			
			if(document.getElementById('Do_you_Play_Polo').value == 'Yes' ){			
				if(!YAHOO.login_form.form_validation.validate_player()){
					return false;
				}
			}
																
			// validate coach umire groom indicator			
			if(document.getElementById('Are_you_a_coach_umpire_or_groom') && document.getElementById('Are_you_a_coach_umpire_or_groom').selectedIndex ==0){
				alert('Please state if you are a Coach, umpire or Groom');
				form.Are_you_a_coach_umpire_or_groom.focus();
				return false;
			}
			
			// if coach umpire or groom is yes, at least one of the checkboxes must be checked			
			if(document.getElementById('Are_you_a_coach_umpire_or_groom') && document.getElementById('Are_you_a_coach_umpire_or_groom').value == 'Yes'){
				
				if(
					(document.getElementById('Coach_Indicator_1') && document.getElementById('Coach_Indicator_1').checked != true)
					&&
					(document.getElementById('Umpire_Indicator_1') && document.getElementById('Umpire_Indicator_1').checked != true)
					&&
					(document.getElementById('Groom_Indicator_1') && document.getElementById('Groom_Indicator_1').checked != true)
				)
				{
					// at least one must be checked
					alert('Please select at least one option');
					form.Coach_Indicator_1.focus();
					return false;
				}
				
			}
			
			// validate Coach

			//alert('coach checked= ' + document.getElementById('Coach_Indicator_1').checked);
			
			if(document.getElementById('Coach_Indicator_1') && document.getElementById('Coach_Indicator_1').checked == true){
				if(!YAHOO.login_form.form_validation.validate_coach()){
					return false;
				}
			}
										
			// validate Umpire					
			if(document.getElementById('Umpire_Indicator_1') && document.getElementById('Umpire_Indicator_1').checked == true){
				if(!YAHOO.login_form.form_validation.validate_umpire()){
					return false;
				}
			}
						
			// validate Groom				
			if(document.getElementById('Groom_Indicator_1') && document.getElementById('Groom_Indicator_1').checked == true){
				if(!YAHOO.login_form.form_validation.validate_groom()){
					return false;
				}
			}
											
			// validate LBB indicator and fields
			if(document.getElementById('List_in_LBB_1') && document.getElementById('List_in_LBB_1').checked == true){
				
				if(!YAHOO.login_form.form_validation.validate_lbb()){
					return false;
				}								
			}
			
			
			return true;
		},
		
		validate_player: function(){
			
			// Arena / Outdoor (at least one must be checked)
			if(document.getElementById('Arena_Player_1')&&document.getElementById('Outdoor_Player_1')){
				
				if(document.getElementById('Arena_Player_1').checked != true
				&& document.getElementById('Outdoor_Player_1').checked != true
				){
					alert('Please choose at least one type of Polo Playing.');
					form.Arena_Player_1.focus();
					return false;
				}
				
				// validate arena handicap
				if(document.getElementById('Arena_Player_1').checked 
				&& document.getElementById('Arena_Handicap') 
				&& document.getElementById('Arena_Handicap').selectedIndex == 0 ){
					alert('Please select an Arena Handicap.');
					form.Arena_Handicap.focus();
					return false;
				}
				
				// validate outdoor handicap
				if(document.getElementById('Outdoor_Player_1').checked
				&& document.getElementById('Outdoor_Handicap') 
				&& document.getElementById('Outdoor_Handicap').selectedIndex == 0 ){
					alert('Please select an Outdoor Handicap.');
					form.Outdoor_Handicap.focus();
					return false;
				}
			}
			
			// vlaidate current playing country
			if(document.getElementById('Currently_Playing_Country') && document.getElementById('Currently_Playing_Country').selectedIndex == 0 ){
				alert('Please select which country you are currently playing polo in.');
				form.Currently_Playing_Country.focus();
				return false;
			}
			
			// validate club
			var club_selects = $D.getElementsByClassName('club_select');
			if(club_selects.length > 0 ){
												
				var club_chosen = false;
				
				for(var i=0; i<club_selects.length; i++){
					
					if(club_selects[i].selectedIndex > 0){
						club_chosen = true;
					}
				}
				if(club_chosen == false){
					
					if(document.getElementById('New_Club_Name') && isBlank(document.getElementById('New_Club_Name').value)){
						alert('Please select a Club, or if your club is not listed, enter its details in the fields provided.');
						form.New_Club_Name.focus();
						return false;
					}
					
				}
			}
														
			// New Club Name										
			if(document.getElementById('New_Club_Name') && !isBlank(document.getElementById('New_Club_Name').value)){
				
				// New Club Country
				if(document.getElementById('New_Club_Country') && document.getElementById('New_Club_Country').selectedIndex == 0 ){
					alert('You have chosen to enter a new Club.\n\nPlease select which country your club is in.');
					form.New_Club_Country.focus();
					return false;
				}
				
				// New Club Region
				if(document.getElementById('New_Club_Region') && document.getElementById('New_Club_Region').selectedIndex == 0 ){
					alert('You have chosen to enter a new Club.\n\nPlease select which region your club is in.');
					form.New_Club_Region.focus();
					return false;
				}
			}
			
			// New Club Country
			if(document.getElementById('New_Club_Country') && document.getElementById('New_Club_Country').selectedIndex > 0 ){
				
				// New Club Name										
				if(document.getElementById('New_Club_Name') && isBlank(document.getElementById('New_Club_Name').value)){
					alert('Please enter a Club name.');
					form.New_Club_Name.focus();
					return false;					
				}
				// New Club Region
				if(document.getElementById('New_Club_Region') && document.getElementById('New_Club_Region').selectedIndex == 0 ){
					alert('You have chosen to enter a new Club.\n\nPlease select which region your club is in.');
					form.New_Club_Region.focus();
					return false;
				}
								
			}
			
			// New Club Region
			if(document.getElementById('New_Club_Region') && document.getElementById('New_Club_Region').selectedIndex > 0 ){
				
				// New Club Name										
				if(document.getElementById('New_Club_Name') && isBlank(document.getElementById('New_Club_Name').value)){
					alert('Please enter a Club name.');
					form.New_Club_Name.focus();
					return false;
				}
				
				// New Club Country
				if(document.getElementById('New_Club_Country') && document.getElementById('New_Club_Country').selectedIndex == 0 ){
					alert('You have chosen to enter a new Club.\n\nPlease select which country your club is in.');
					form.New_Club_Country.focus();
					return false;
				}
			}
			
			return true;
			
		},
		
		validate_coach: function(){
						
			// Coach Qualification												
			if(document.getElementById('Coach_Qualification') && isBlank(document.getElementById('Coach_Qualification').value)){
				alert('Please enter your coaching qualification');
				form.Coach_Qualification.focus();
				return false;					
			}
			
			// Qualification Association
			if(document.getElementById('Coach_Qualification_Association') && document.getElementById('Coach_Qualification_Association').selectedIndex == 0 ){
				
				if(document.getElementById('New_Coach_Qualification_Association') && isBlank(document.getElementById('New_Coach_Qualification_Association').value)){
					alert('Please select which Association your qualification is for.\n\nIf the Association is not listed enter a new one');
					form.Coach_Qualification_Association.focus();
					return false;					
				}
				
				// New Association Country
				if(document.getElementById('New_Coach_Qualification_Association_Country') && document.getElementById('New_Coach_Qualification_Association_Country').selectedIndex == 0 ){
					alert('You have chosen to enter a new Association.\n\nPlease select which country your Association is in.');
					form.New_Coach_Qualification_Association_Country.focus();
					return false;
				}																						
			}
			
			// Current Coaching Country
			if(document.getElementById('Current_Coaching_Country') && document.getElementById('Current_Coaching_Country').selectedIndex == 0 ){
				alert('Please select which country you r are currenlty a coach in.');
				form.Current_Coaching_Country.focus();
				return false;
			}
			
			return true;
		},
		
		validate_umpire: function(){
			
			// Umpire Grade											
			if(document.getElementById('Umpire_Grade') && isBlank(document.getElementById('Umpire_Grade').value)){
				alert('Please enter your grade');
				form.Umpire_Grade.focus();
				return false;					
			}
			
			// Grade Association
			if(document.getElementById('Umpire_Grade_Association') && document.getElementById('Umpire_Grade_Association').selectedIndex == 0 ){
				
				if(document.getElementById('New_Umpire_Grade_Association') && isBlank(document.getElementById('New_Umpire_Grade_Association').value)){
					alert('Please select which Association your grade is for.');
					form.New_Umpire_Grade_Association.focus();
					return false;					
				}
				
				// New Association Country
				if(document.getElementById('New_Umpire_Grade_Association_Country') && document.getElementById('New_Umpire_Grade_Association_Country').selectedIndex == 0 ){
					alert('You have chosen to enter a new Association.\n\nPlease select which country your Association is in.');
					form.New_Umpire_Grade_Association_Country.focus();
					return false;
				}																						
			}
			
			// Current Umpiring Country
			if(document.getElementById('Current_Umpire_Country') && document.getElementById('Current_Umpire_Country').selectedIndex == 0 ){
				alert('Please select which country you r are currenlty an umpire in.');
				form.Current_Umpire_Country.focus();
				return false;
			}
			
			return true;
		},
		
		validate_groom: function(){
			
			// Current Groom Country
			if(document.getElementById('Current_Groom_Country') && document.getElementById('Current_Groom_Country').selectedIndex == 0 ){
				alert('Please select which country you are currenlty a Groom in.');
				form.Current_Groom_Country.focus();
				return false;
			}
			return true;
		},
		
		validate_lbb: function(){
			
			// lbb bio
			if(document.getElementById('Little_Black_Book_Bio') && isBlank(document.getElementById('Little_Black_Book_Bio').value)){
				alert('Please provide your current status and availability.');
				form.Little_Black_Book_Bio.focus();
				return false;
			}
			
			// lbb bio
			if(document.getElementById('Little_Black_Book_Tags') && isBlank(document.getElementById('Little_Black_Book_Tags').value)){
				alert('Please enter tags to accompany your entry.');
				form.Little_Black_Book_Tags.focus();
				return false;
			}
			return true;
		}
	}
	
}();

/* ----------------------------------------------------------------------------------------------------------------------------------------
/*
/*	This object handles the form controls, such as showing/hiding elements, initialisng dropdowns, creating elements etc
/*
/* ----------------------------------------------------------------------------------------------------------------------------------------*/
// define the namespace object
YAHOO.login_form.form_controls = function() {

	// shortcuts
	var $E = YAHOO.util.Event;
	var $D = YAHOO.util.Dom;
	var $ = $D.get;

	var player_indicator;
	var coach_umpire_groom_indicator;
	var coach_indicator;
	var umpire_indicator;
	var groom_indicator;
	
	var initialising_edit = false; 
	
	var additional_club_count = 0;
	
	return {
		
		init: function(){
				
			// we only need this functionality if we're on the join form or profile edit form
			//. detect these forms and if theyre not present (ie we're on the join_form.php page but AFTER a join has been done
			// then do not do anything
										
			if(document.jform || document.jform_edit){
			
				// intialise flags				
				YAHOO.login_form.form_controls.player_indicator = false;
				YAHOO.login_form.form_controls.coach_umpire_groom_indicator = false;
				YAHOO.login_form.form_controls.coach_indicator = false;
				YAHOO.login_form.form_controls.umpire_indicator = false;
				YAHOO.login_form.form_controls.groom_indicator = false;
				
				// ---------------------------------------------------
				/* Intialise EDIT FORM */
				// if on the edit form - set initial values
				// ---------------------------------------------------
				if(document.jform_edit){
					
					YAHOO.login_form.form_controls.initialising_edit = true;
					
					// Do you play polo
					if(document.getElementById('Do_you_Play_Polo').value == 'Yes'){
						YAHOO.login_form.form_controls.player_indicator = true;
					}
					
					// Coach Umpire or Groom check
					if(document.getElementById('Are_you_a_coach_umpire_or_groom').value == 'Yes'){
						YAHOO.login_form.form_controls.coach_umpire_groom_indicator = true;	
					}
					
					// Coach checkbox
					if(document.getElementById('Coach_Indicator_1').checked == true){
						YAHOO.login_form.form_controls.coach_indicator = true;		
					}
					
					// Umpire checkbox
					if(document.getElementById('Umpire_Indicator_1').checked == true){
						YAHOO.login_form.form_controls.umpire_indicator = true;		
					}
					
					// Groom checkbox
					if(document.getElementById('Groom_Indicator_1').checked == true){
						YAHOO.login_form.form_controls.groom_indicator = true;		
					}
																							
					// New coach association 
					if(document.getElementById('New_Coach_Qualification_Association').value != ''){
						
						document.getElementById('Coach_Qualification_Association').selectedIndex = 0;	
					}
					
					// New umpire association 					
					if(document.getElementById('New_Umpire_Grade_Association').value != ''){
						
						document.getElementById('Umpire_Grade_Association').selectedIndex = 0;	
					}
					
					
				}
				
				// ---------------------------------------
				//	Initialise Club(s) - for this we may need to add selects
				// 	We need to initialise on the edit form and ALSO the join form, to
				//	cater for when the form has posted but an error has been flagged
				//	and we need to recreate the form status
				// --------------------------------------
				// initialise multiple club fields - we get the values from hidden inputs
				
				/*if(document.jform_edit){*/
					
					// If the hidden field 'saveChanges' is YES then the profile edit page has
					// reloaded - either after a successfull save or an error
					// in this situatiuon, refresh the club dropdowns from the 
					// real post vars, not the hidden ones									
					var hidden_club_id_fields = $D.getElementsByClassName('hidden_club_id','input');
					
					//alert(hidden_club_id_fields.length + ' hidden club fields found!');
					
					if(hidden_club_id_fields.length > 0 ){
						// initialise first club - this select is already on the page
						var first_club_select = document.getElementById('Club_id_0');
														
						for(var i=0; i<first_club_select.options.length; i++){
																	
							if(first_club_select.options[i].value == hidden_club_id_fields[0].value){
								first_club_select.selectedIndex = i;
							}
						}
						// add listener to first select
						$E.addListener(first_club_select,'change', YAHOO.login_form.form_controls.change_hidden_club_field);
									
						// initialise all other club selects
						for(var i=1; i< hidden_club_id_fields.length; i++){
											
							YAHOO.login_form.form_controls.add_additional_club(null,hidden_club_id_fields[i].value);
							
							
						}
					}
				/*}*/
												
				// -----------------
				// Set some checkboxes to be checked by default
				if(document.jform){
					document.getElementById('I_would_like_news_and_information_sent_to_me_1').checked = true;
					document.getElementById('Allow_my_details_1').checked = true;
				}
				
				// Call functions that set other fields depending on certain form
				// settings - i.e. hide player fields if this is not a player
				YAHOO.login_form.form_controls.toggle_player_fields();
				YAHOO.login_form.form_controls.toggle_arena_handicap();
				YAHOO.login_form.form_controls.toggle_outdoor_handicap();				
				YAHOO.login_form.form_controls.toggle_coach_umpire_groom_indicators();
				YAHOO.login_form.form_controls.toggle_coach_fields();
				//YAHOO.login_form.form_controls.toggle_new_coach_qualification_association_enable();
				//YAHOO.login_form.form_controls.toggle_new_umpire_grade_association_enable();
				YAHOO.login_form.form_controls.toggle_umpire_fields();
				YAHOO.login_form.form_controls.toggle_groom_fields();
				YAHOO.login_form.form_controls.toggle_littleblackbook_fields();
				
				// set initial region & club regions			
				YAHOO.login_form.form_controls.populate_region_dropdown();
				YAHOO.login_form.form_controls.populate_coaching_region_dropdown();
				YAHOO.login_form.form_controls.populate_playing_region_dropdown();
				YAHOO.login_form.form_controls.populate_umpire_region_dropdown();
				YAHOO.login_form.form_controls.populate_groom_region_dropdown();
				
				YAHOO.login_form.form_controls.populate_new_club_region_dropdown();
									
				// Initialise the event listeners
				YAHOO.login_form.form_controls.initialise_event_listeners();
								
				// set the initialising flag to false - these prevents certain things happening in the handlers
				YAHOO.login_form.form_controls.initialising_edit = false;
			}
			
		},
		
		initialise_event_listeners: function(){
			
			// add event listeners to control show/hide				
				var oElement = document.getElementById('Do_you_Play_Polo');				
				$E.addListener(oElement,'change', YAHOO.login_form.form_controls.toggle_player_fields);
				
				var oElement = document.getElementById('New_Club_Country');				
				$E.addListener(oElement,'change', YAHOO.login_form.form_controls.populate_new_club_region_dropdown);
								
				var oElement = document.getElementById('Arena_Player_1');				
				$E.addListener(oElement,'click', YAHOO.login_form.form_controls.toggle_arena_handicap);
				
				var oElement = document.getElementById('Outdoor_Player_1');				
				$E.addListener(oElement,'click', YAHOO.login_form.form_controls.toggle_outdoor_handicap);
				
				/*var oElement = document.getElementById('Club_0');				
				$E.addListener(oElement,'change', YAHOO.login_form.form_controls.toggle_new_club_enable);*/
				
				var oElement = document.getElementById('Are_you_a_coach_umpire_or_groom');				
				$E.addListener(oElement,'change', YAHOO.login_form.form_controls.toggle_coach_umpire_groom_indicators);
	
				var oElement = document.getElementById('Coach_Indicator_1');				
				$E.addListener(oElement,'click', YAHOO.login_form.form_controls.toggle_coach_fields);
				
				var oElement = document.getElementById('Coach_Qualification_Association');				
				$E.addListener(oElement,'change', YAHOO.login_form.form_controls.toggle_new_coach_qualification_association_enable);
				
				var oElement = document.getElementById('Umpire_Indicator_1');				
				$E.addListener(oElement,'click', YAHOO.login_form.form_controls.toggle_umpire_fields);
				
				var oElement = document.getElementById('Umpire_Grade_Association');				
				$E.addListener(oElement,'change', YAHOO.login_form.form_controls.toggle_new_umpire_grade_association_enable);
				
				var oElement = document.getElementById('Groom_Indicator_1');				
				$E.addListener(oElement,'click', YAHOO.login_form.form_controls.toggle_groom_fields);
				
				var oElement = document.getElementById('List_in_LBB_1');				
				$E.addListener(oElement,'click', YAHOO.login_form.form_controls.toggle_littleblackbook_fields);
				
				
		},
		
		change_hidden_club_field: function(e){
			
			if(e){
				$E.stopEvent(e);
			}
				
			//alert(this.value);
			
			var id_number = this.id.substr(this.id.lastIndexOf('_')+1);
						
			if(hidden_club_id_field = document.getElementById('hidden_club_id_' + id_number)){	
				//alert(hidden_club_id_field);
				hidden_club_id_field.value = this.value;	
				//alert(hidden_club_id_field.id + ' - hidden field also updated!');
			}
			
		},
		
		populate_region_dropdown: function(){
		
			// user region
			if(document.getElementById('Region_hidden')){
				var initial_region_value = document.getElementById('Region_hidden').value;
			}else{
				var initial_region_value = 0;
			}
			
			YAHOO.s_common_polo.populate_region_drop_down('Country','Region',initial_region_value);
						
		},
		
		populate_new_club_region_dropdown: function(){
								
			// new club region
			if(document.getElementById('New_Club_Region_hidden')){
				var initial_region_value = document.getElementById('New_Club_Region_hidden').value;
			}else{
				var initial_region_value = 0;
			}
			
			YAHOO.s_common_polo.populate_region_drop_down('New_Club_Country','New_Club_Region',initial_region_value);
		},
		
		populate_playing_region_dropdown: function(){
		
			// user region
			if(document.getElementById('Currently_playing_Region_hidden')){
				var initial_region_value = document.getElementById('Currently_playing_Region_hidden').value;
			}else{
				var initial_region_value = 0;
			}
			
			YAHOO.s_common_polo.populate_region_drop_down('Currently_Playing_Country','Currently_playing_region',initial_region_value);
						
		},
		
		populate_coaching_region_dropdown: function(){
		
			// user region
			if(document.getElementById('Current_Coaching_Region_hidden')){
				var initial_region_value = document.getElementById('Current_Coaching_Region_hidden').value;
			}else{
				var initial_region_value = 0;
			}
			
			YAHOO.s_common_polo.populate_region_drop_down('Current_Coaching_Country','Current_Coaching_Region',initial_region_value);
						
		},
		
		populate_umpire_region_dropdown: function(){
		
			// user region
			if(document.getElementById('Current_Umpire_Region_hidden')){
				var initial_region_value = document.getElementById('Current_Umpire_Region_hidden').value;
			}else{
				var initial_region_value = 0;
			}
			
			YAHOO.s_common_polo.populate_region_drop_down('Current_Umpire_Country','Current_Umpire_Region',initial_region_value);
						
		},
		
		populate_groom_region_dropdown: function(){
		
			// user region
			if(document.getElementById('Current_Groom_Region_hidden')){
				var initial_region_value = document.getElementById('Current_Groom_Region_hidden').value;
			}else{
				var initial_region_value = 0;
			}
			
			YAHOO.s_common_polo.populate_region_drop_down('Current_Groom_Country','Current_Groom_Region',initial_region_value);
						
		},
															
		/* Player fields */
		
		// button to add additional club
		add_additional_club: function(e,initial_club_id){
									
			if(e){
				$E.stopEvent(e);
			}
					
			additional_club_count++;
																
			// clone element
			var club_div = document.getElementById('club_div_0');			
			var new_club_div = club_div.cloneNode(true);
												
			// define and set the id and name attributes of the div										
			new_club_div.setAttribute('id','club_div_' + additional_club_count);
			new_club_div.setAttribute('name','club_div_' + additional_club_count);
			
			// define and set the id and name attributes of the select
			var new_club_select = $D.getElementsByClassName('club_select','select',new_club_div)[0];
			new_club_select.setAttribute('id','Club_id_' + additional_club_count);
			new_club_select.setAttribute('name','Club_id_' + additional_club_count);
			
			// add a listener to adjust any hidden input at the same time - to keep track of changes for
			// edits!
			$E.addListener(new_club_select,'change', YAHOO.login_form.form_controls.change_hidden_club_field);
									
			// if we have been passed an initial value, then set it!			
			if(initial_club_id !== undefined){
												
				for(var i=0; i<new_club_select.options.length; i++){
															
					if(new_club_select.options[i].value == initial_club_id){
						new_club_select.selectedIndex = i;
					}
				}									
			}
			
			// define and set the id and name attributes of the delete link - then show it
			var new_club_delete = $D.getElementsByClassName('club_delete','a',new_club_div)[0];
			new_club_delete.setAttribute('id','club_delete_' + additional_club_count);
			new_club_delete.setAttribute('name','club_delete_' + additional_club_count);
			$D.setStyle(new_club_delete,'display','');
			
			// asign the delete event to the delete button											
			$E.addListener(new_club_delete,'click', YAHOO.login_form.form_controls.delete_additional_club);
													
			// add to form by appending to the DOM		
			document.getElementById('new_club_select_cell').appendChild(new_club_div);
			
																					
			
		},
		
		delete_additional_club: function(){
							
			// get the node to delete
			var id_number = this.id.substr(this.id.lastIndexOf('_')+1);
											
			// remove any events for the recipient - in this case just the delete button
			$E.removeListener(this.id, "click");
			$E.removeListener('club_select_' + id_number, "change");
			
			// remove the node from the DOM - the table TR		
			delete_node = document.getElementById('club_div_' + id_number);
			delete_node.parentNode.removeChild(delete_node);
			
			
			/* ALSO - delete any hidden inputs on the page for this club. - They are used when we first 
			/* load the edit profile and are needed to allow us to initialise the form with YUI
			/* but we can get into problems if they dont match at all times */
			if(hidden_node = document.getElementById('hidden_club_id_' + id_number)){
				//alert('hidden_club_id_' + id_number);
				hidden_node.parentNode.removeChild(hidden_node);
			}
		},
				
		toggle_new_club_enable: function(e){
			
			if(e){
				$E.stopEvent(e);
			}
			
			// get Club selects			
			var club_row = $D.getElementsByClassName('Club_row')[0];
			var club_select_array = $D.getElementsByClassName('club_select','select',club_row);
								
			var club_selected = false;
			for(var i=0; i<club_select_array.length; i++){				
				if(club_select_array[i].selectedIndex > 0){
					club_selected = true;
				}
			}
						
			if(club_selected == true){
				
				document.getElementById('New_Club_Name').disabled = true;
				document.getElementById('New_Club_Country').disabled = true;
				document.getElementById('New_Club_Region').disabled = true;
				
				// do not reset these fields when we're initialising the edit form
				if(YAHOO.login_form.form_controls.initialising_edit != true){ 
					document.getElementById('New_Club_Name').value = '';
					document.getElementById('New_Club_Country').selectedIndex = 0;
					document.getElementById('New_Club_Region').selectedIndex = 0;
				}
				
			}else{
				// do not reset these fields when we're initialising the edit form
				if(YAHOO.login_form.form_controls.initialising_edit != true){ 
					document.getElementById('New_Club_Name').disabled = false;
					document.getElementById('New_Club_Country').disabled = false;
					document.getElementById('New_Club_Region').disabled = false;				
					document.getElementById('New_Club_Name').value = '';
				}
			}
		},
		
		toggle_player_fields: function(e){
			
			if(e){
				$E.stopEvent(e);
			}
			
			var do_you_play_polo_field = document.getElementById('Do_you_Play_Polo');
			
									
			if(do_you_play_polo_field.value == 'No' || do_you_play_polo_field.selectedIndex ==0 || do_you_play_polo_field.value == 'Used_to_play' ){
												
				YAHOO.login_form.form_controls.player_indicator = false;
								
				YAHOO.login_form.form_controls.hide_player_fields();
												
				// hide non-player fields
				if(do_you_play_polo_field.value == 'No' || do_you_play_polo_field.value == 'Used_to_play' ){
					YAHOO.login_form.form_controls.show_non_player_fields();
				}else{
					YAHOO.login_form.form_controls.hide_non_player_fields();
					YAHOO.login_form.form_controls.reset_non_player_fields();
				}
				
				// reset the fields
				YAHOO.login_form.form_controls.reset_player_fields();
			}else{
				YAHOO.login_form.form_controls.show_player_fields();
				
				YAHOO.login_form.form_controls.player_indicator = true;

				// show non-player fields	
				YAHOO.login_form.form_controls.hide_non_player_fields();
				YAHOO.login_form.form_controls.reset_non_player_fields();
												
			}
			
			// show/hide LBB indicator
			YAHOO.login_form.form_controls.toggle_littleblackbook_indicator();
			
		},
		
		hide_player_fields: function(){
			
			var field_elements = $D.getElementsByClassName('player_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','none');
				
			}
		},
		
		reset_player_fields: function(){
			
			document.getElementById('Arena_Player_1').checked = false;
			document.getElementById('Arena_Handicap').selectedIndex = 0;
			document.getElementById('Outdoor_Player_1').checked = false;
			document.getElementById('Outdoor_Handicap').selectedIndex = 0;
			document.getElementById('Outdoor_Handicap').selectedIndex = 0;
			document.getElementById('Currently_Playing_Country').selectedIndex = 0;
			
			// reset all clubs								
			var club_select_array = $D.getElementsByClassName('club_select','select');
										
			for(var i=0; i<club_select_array.length; i++){				
				club_select_array[i].selectedIndex = 0;					
			}
												
			YAHOO.login_form.form_controls.toggle_new_club_enable();
			
												
		},
		
		show_player_fields: function(){
			
			var field_elements = $D.getElementsByClassName('player_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','');				
			}
		},
		
		hide_non_player_fields: function(){	
			
			var i_want_to_learn = $D.getElementsByClassName('I_want_to_Learn_row')[0];
			var i_want_to_watch = $D.getElementsByClassName('I_want_to_Watch_row')[0];																											
			$D.setStyle(i_want_to_learn,'display','none');
			$D.setStyle(i_want_to_watch,'display','none');
		},
		
		reset_non_player_fields: function(){
			
			document.getElementById('I_want_to_Learn_1').checked = false;
			document.getElementById('I_want_to_Watch_1').checked = false;
		},
		
		show_non_player_fields: function(){
			
			var i_want_to_learn = $D.getElementsByClassName('I_want_to_Learn_row')[0];
			var i_want_to_watch = $D.getElementsByClassName('I_want_to_Watch_row')[0];																											
			$D.setStyle(i_want_to_learn,'display','');
			$D.setStyle(i_want_to_watch,'display','');
		},
		
		toggle_arena_handicap: function(e){
					
			if(document.getElementById('Arena_Player_1').checked == true){
				
				document.getElementById('Arena_Handicap').disabled = false;												
			}else{
				document.getElementById('Arena_Handicap').disabled = true;				
			}
			
		},
		
		toggle_outdoor_handicap: function(e){
									
			if(document.getElementById('Outdoor_Player_1').checked == true){
				
				document.getElementById('Outdoor_Handicap').disabled = false;												
			}else{
				document.getElementById('Outdoor_Handicap').disabled = true;				
			}
			
		},
		
		// -----------------------------------------------------
		/* Coach, umpire or groom indicators (checkboxes) */
		// -----------------------------------------------------
		
		toggle_coach_umpire_groom_indicators: function(e){
									
			var coach_umpire_or_groom_field = document.getElementById('Are_you_a_coach_umpire_or_groom');
			
			
			if(coach_umpire_or_groom_field.value == 'No' || coach_umpire_or_groom_field.selectedIndex ==0 ){
			
				// reset the coach/umpire/groom checkboxes
				YAHOO.login_form.form_controls.reset_cug_indicators();
				
				// hide cug indicators
				YAHOO.login_form.form_controls.hide_cug_indicators();
				
				// set flag
				YAHOO.login_form.form_controls.coach_umpire_groom_indicator = false;
				
				// hide coach umpire & groom fields
				YAHOO.login_form.form_controls.toggle_coach_fields();
			
				YAHOO.login_form.form_controls.toggle_umpire_fields();
				
				YAHOO.login_form.form_controls.toggle_groom_fields();
												
							
			}else{
				YAHOO.login_form.form_controls.show_cug_indicators();
				
				YAHOO.login_form.form_controls.coach_umpire_groom_indicator = true;									
				
			}
			
			YAHOO.login_form.form_controls.toggle_littleblackbook_indicator();
		},
		
		reset_cug_indicators: function(){
			
			document.getElementById('Coach_Indicator_1').checked = false;
			document.getElementById('Umpire_Indicator_1').checked = false;
			document.getElementById('Groom_Indicator_1').checked = false;	
			
			YAHOO.login_form.form_controls.coach_indicator = false;
			YAHOO.login_form.form_controls.umpire_indicator = false;
			YAHOO.login_form.form_controls.groom_indicator = false;
		},
		
		hide_cug_indicators: function(){
			
			var coach_indicator = $D.getElementsByClassName('Coach_Indicator_row')[0];
			var umpire_indicator = $D.getElementsByClassName('Umpire_Indicator_row')[0];
			var groom_indicator = $D.getElementsByClassName('Groom_Indicator_row')[0];											
													
			$D.setStyle(coach_indicator,'display','none');
			$D.setStyle(umpire_indicator,'display','none');
			$D.setStyle(groom_indicator,'display','none');
		},
		
		show_cug_indicators: function(){
			
			var coach_indicator = $D.getElementsByClassName('Coach_Indicator_row')[0];
			var umpire_indicator = $D.getElementsByClassName('Umpire_Indicator_row')[0];
			var groom_indicator = $D.getElementsByClassName('Groom_Indicator_row')[0];
			
			$D.setStyle(coach_indicator,'display','');
			$D.setStyle(umpire_indicator,'display','');
			$D.setStyle(groom_indicator,'display','');
		},
		
		// ----------------------------
		/* Coach fields */
		// ----------------------------
		
		toggle_new_coach_qualification_association_enable: function(e){
			
			if(e){
				$E.stopEvent(e);
			}					
			
			if(document.getElementById('Coach_Qualification_Association').selectedIndex == 0 ){
				
				document.getElementById('New_Coach_Qualification_Association').disabled = false;
				document.getElementById('New_Coach_Qualification_Association_Country').disabled = false;
				
				// reset
				// do not reset these fields when we're initialising the edit form
				if(YAHOO.login_form.form_controls.initialising_edit != true){ 
					document.getElementById('New_Coach_Qualification_Association').value = '';
					document.getElementById('New_Coach_Qualification_Association_Country').selectedIndex = 0;
				}
				
																
			}else{
				document.getElementById('New_Coach_Qualification_Association').disabled = true;
				document.getElementById('New_Coach_Qualification_Association_Country').disabled = true;
				
				// reset
				// do not reset these fields when we're initialising the edit form
				if(YAHOO.login_form.form_controls.initialising_edit != true){ 
					document.getElementById('New_Coach_Qualification_Association').value = '';
					document.getElementById('New_Coach_Qualification_Association_Country').selectedIndex = 0;
				}
			}
		},
		
		toggle_coach_fields: function(e){
									
			if(document.getElementById('Coach_Indicator_1').checked == true){
				YAHOO.login_form.form_controls.show_coach_fields();
				YAHOO.login_form.form_controls.coach_indicator = true;
			}else{
				YAHOO.login_form.form_controls.hide_coach_fields();
				YAHOO.login_form.form_controls.coach_indicator = false;	
				
				// reset fields*/
				YAHOO.login_form.form_controls.reset_coach_fields();
			}
			YAHOO.login_form.form_controls.toggle_littleblackbook_indicator();
		},
		
		hide_coach_fields: function(){
			
			var field_elements = $D.getElementsByClassName('coach_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','none');				
			}
		},
		
		show_coach_fields: function(){
			
			var field_elements = $D.getElementsByClassName('coach_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','');				
			}
		},
		
		reset_coach_fields: function(){
			
			// do not reset these fields when we're initialising the edit form
			if(YAHOO.login_form.form_controls.initialising_edit != true){ 
				document.getElementById('Coach_Qualification').value = '';
				document.getElementById('Coach_Qualification_Association').selectedIndex = 0;
				document.getElementById('New_Coach_Qualification_Association').value = '';			
				document.getElementById('New_Coach_Qualification_Association_Country').selectedIndex = 0;
				document.getElementById('Current_Coaching_Country').selectedIndex = 0;
			}
		},
		
		// ----------------------------
		/* Umpire fields */
		// ----------------------------
		toggle_umpire_fields: function(e){
															
			if(document.getElementById('Umpire_Indicator_1').checked == true){
				YAHOO.login_form.form_controls.show_umpire_fields();
				YAHOO.login_form.form_controls.umpire_indicator = true;
								
			}else{
				YAHOO.login_form.form_controls.hide_umpire_fields();
				YAHOO.login_form.form_controls.umpire_indicator = false;
				
				// reset
				YAHOO.login_form.form_controls.reset_umpire_fields();
				
			}
			YAHOO.login_form.form_controls.toggle_littleblackbook_indicator();
			
		},
		
		hide_umpire_fields: function(){
			
			var field_elements = $D.getElementsByClassName('umpire_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','none');				
			}
		},
		
		show_umpire_fields: function(){
			
			var field_elements = $D.getElementsByClassName('umpire_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','');				
			}
		},
		
		reset_umpire_fields: function(){
			

			// we don't want to reset these fields when we're initialising the page 
			// during profile edit - it would overdide the setting of the actual value
			
			if(YAHOO.login_form.form_controls.initialising_edit != true){ 
				document.getElementById('Umpire_Grade').value = '';
				document.getElementById('Umpire_Grade_Association').selectedIndex = 0;
				document.getElementById('New_Umpire_Grade_Association').value = '';
				document.getElementById('New_Umpire_Grade_Association_Country').selectedIndex = 0;
				document.getElementById('Current_Umpire_Country').selectedIndex = 0;
			}
		},
		
		toggle_new_umpire_grade_association_enable: function(e){
			
			if(e){
				$E.stopEvent(e);
			}
									
			if(document.getElementById('Umpire_Grade_Association').selectedIndex == 0 ){
				
				document.getElementById('New_Umpire_Grade_Association').disabled = false;
				document.getElementById('New_Umpire_Grade_Association_Country').disabled = false;
				
				// reset - except if initialising the edit form
				if(YAHOO.login_form.form_controls.initialising_edit != true){ 	
					document.getElementById('New_Umpire_Grade_Association').value = '';
					document.getElementById('New_Umpire_Grade_Association_Country').selectedIndex = 0;
				}
												
			}else{
				document.getElementById('New_Umpire_Grade_Association').disabled = true;
				document.getElementById('New_Umpire_Grade_Association_Country').disabled = true;
				
				// reset - except if initialising the edit form
				if(YAHOO.login_form.form_controls.initialising_edit != true){ 			
					document.getElementById('New_Umpire_Grade_Association').value = '';
					document.getElementById('New_Umpire_Grade_Association_Country').selectedIndex = 0;
				}
			}
		},
		
		// ----------------------------
		/* Groom fields */
		// ----------------------------
		
		toggle_groom_fields: function(e){
																		
			if(document.getElementById('Groom_Indicator_1').checked == true){
				YAHOO.login_form.form_controls.show_groom_fields();
				YAHOO.login_form.form_controls.groom_indicator = true;
			}else{
				YAHOO.login_form.form_controls.hide_groom_fields();
				YAHOO.login_form.form_controls.groom_indicator = false;
				
				// reset
				YAHOO.login_form.form_controls.reset_groom_fields();
				
			}
			YAHOO.login_form.form_controls.toggle_littleblackbook_indicator();
		},
		
		hide_groom_fields: function(){
			
			var field_elements = $D.getElementsByClassName('groom_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','none');				
			}
		},
		
		show_groom_fields: function(){
			
			var field_elements = $D.getElementsByClassName('groom_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','');				
			}
		},
		
		reset_groom_fields: function(){
			
			document.getElementById('Current_Groom_Country').selectedIndex = 0;
		},
		
		/* Little Black Book fields and indicator checkbox */
		
		toggle_littleblackbook_indicator: function(){
			
			// show/hide the littleblackbook checkbox field depending on the
			// status of the player and coach/umpire/groom indicators
					 			
			if(YAHOO.login_form.form_controls.player_indicator == true 
			|| YAHOO.login_form.form_controls.coach_indicator == true
			|| YAHOO.login_form.form_controls.umpire_indicator == true
			|| YAHOO.login_form.form_controls.groom_indicator == true){
				YAHOO.login_form.form_controls.show_littleblackbook_indicator();
			}else{
				YAHOO.login_form.form_controls.hide_littleblackbook_indicator();							
			}
			
			YAHOO.login_form.form_controls.toggle_littleblackbook_fields();
			
		},
		
		show_littleblackbook_indicator: function(){
		
			var littleblackbook_indicator_field = $D.getElementsByClassName('List_in_LBB_row')[0];			
			$D.setStyle(littleblackbook_indicator_field,'display','');			
		},
		
		hide_littleblackbook_indicator: function(){
		
			var littleblackbook_indicator_field = $D.getElementsByClassName('List_in_LBB_row')[0];				
			$D.setStyle(littleblackbook_indicator_field,'display','none');	
			
			document.getElementById('List_in_LBB_1').checked = false;
		},
		
		toggle_littleblackbook_fields: function(e){
						
			var field = document.getElementById('List_in_LBB_1').checked;
						
			if(field){
				YAHOO.login_form.form_controls.show_littleblackbook_fields();
			}else{
				YAHOO.login_form.form_controls.hide_littleblackbook_fields();	
				
				// reset
				YAHOO.login_form.form_controls.reset_littleblackbook_fields();
			}
			
		},
		
		hide_littleblackbook_fields: function(){
					
			var field_elements = $D.getElementsByClassName('littleblackbook_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','none');				
			}
		},
		
		show_littleblackbook_fields: function(){
								
			var field_elements = $D.getElementsByClassName('littleblackbook_field','tr');
			
			for(var i=0; i<field_elements.length; i++){												
				$D.setStyle(field_elements[i],'display','');				
			}
		},
		
		reset_littleblackbook_fields: function(){
										
			document.getElementById('Little_Black_Book_Bio').value = '';
			document.getElementById('Little_Black_Book_Tags').value = '';
		}
				
	};
}();

// intialise page object once the page has loaded
YAHOO.util.Event.on(window, 'load', YAHOO.login_form.form_controls.init, YAHOO.login_form.form_controls, true );

YAHOO.util.Event.on(window, 'load', YAHOO.login_form.form_validation.init, YAHOO.login_form.form_validation, true );
