function FORM_FUNCTIONS(the_form){
	return (VALIDATE_FORM(the_form));
}

//Start of form validation functions
function VALIDATE_FORM(the_form){
	if (CHECK_REQUIRED(the_form) && EMAIL(the_form.EMAIL_ADDRESS)){
		return true;
	} else {
    	return false;
	}
}

function TEST_REQUIRED_FIELDS_EXIST(required_field, array_no){
		if(!required_field)	alert("Required Field number: "+i+" does not exist.");
}

function GET_REQUIRED_ARRAY(the_form){
	var separator=",";//The separator used to seperate the different values in the hidden required field
	required_array=the_form.required.value.split(separator);//populates the required_array by splitting(exploding) the value of the hidden required field
	for(i=0;i<required_array.length;i++){
		required_array[i]=eval("document."+the_form.name+"."+required_array[i]);
		//alert("document."+the_form.name+"."+required_array[i]);
		//TEST_REQUIRED_FIELDS_EXIST(required_array[i], i);//Used this function to test if the fields in the hidden required field exist in the form, if they do not exist they will create errors when the form is processed on the server
	}
	return(required_array);
}

function CHECK_REQUIRED(the_form){

var required = GET_REQUIRED_ARRAY(the_form);
var alerts = new Array ("your First Name","your Last Name","your Email Address");
var field;//the position of the required field in the form array which is used to place the focus onto the field for the user to complete 
//if (required length != alerts.length){alert("Number of alerts not equal to number of required fields");}

	for (var i=0;i<required.length;i++){//loops through each of the fields in the the required array
		//alert (i);
		//alert (required[i]);
		//alert (required[i].value);
		if(required[i]){
			var value = required[i].value;
			if (NOT_EMPTY_BLANK(value)){//Checks if the value of each of the required fields is empty  
				if (i==(required.length-1)) {//If it reaches the end of the array and all the fields are filled the function returns true
					return true;
				}
			} else {
				alert ("Please enter " + alerts[i]);
				required[i].focus();
				return false;
			}				
		}else{
			//alert("Required form element: "+i+" does not exist");
			if (i==(required.length-1)) {//If it reaches the end of the array and all the fields are filled the function returns true
				return true;
			}
		}			
	}
}


function EMAIL(email_field){
	var str = email_field.value;
	var email_name = email_field.name.replace(/_+/g," ");//Replaces underscores in the field name with spaces
	if (NOT_EMPTY_BLANK(str)){//Checks if the value of each of the required fields is empty
		if (str.indexOf('@')==-1 || str.indexOf('.')==-1 || str.length<5){//checks if the character '@' and '.' are in the address in the email field and that the number of letters is greater than 5
			alert ("This email address is not in the correct format. Please re-enter your "+email_name);
			email_field.focus();
			return false;
		} else {
			return true;
		}
	} else {
		alert ("Please enter your "+email_name);
		email_field.focus();
		return false;
	}	
}

function NOT_EMPTY_BLANK(str) {
	if (str.length != 0){
		for (i = 0; i < str.length; i++) {
			if (str.charAt(i) != " "){
					return true;
			}
		}
	}
	else{
		return false;
	}
}

//Finish of form validation functions

function ShowHide_ChildrenLayers(selected_option) {

	if(selected_option.options[selected_option.selectedIndex].value)children_nos=selected_option.options[selected_option.selectedIndex].value
	Hide_ChildrenLayers();
	switch(children_nos) {
		case "10" :
			
		case "9" :
			
		case "8" :
			
		case "7" :
			
		case "6" :
			
		case "5" :
			
		case "4" :
			var theLayer4=document.getElementById("children4");
			theLayer4.style.display='';
		case "3" :
			var theLayer3=document.getElementById("children3");
			theLayer3.style.display='';
		case "2" :
			var theLayer2=document.getElementById("children2");
			theLayer2.style.display='';
			break;
	}
}

function Hide_ChildrenLayers() {
	var theLayer4=document.getElementById("children4")
	var theLayer3=document.getElementById("children3")
	var theLayer2=document.getElementById("children2")
	
	theLayer4.style.display='none';
	theLayer3.style.display='none';
	theLayer2.style.display='none';

	/*
	children2.style.display='none';
	children3.style.display='none';
	children4.style.display='none';
	children5.style.display='none';
	children6.style.display='none';
	children7.style.display='none';
	children8.style.display='none';
	children9.style.display='none';
	children10.style.display='none';
	*/
}


function GET_RADIO_VALUE(theRadio){
	for(var i=0;i<theRadio.length;i++){
		if (theRadio[i].checked) radioValue=theRadio[i].value
	}
	return radioValue;
}

function Populate_Camp_Weeks(this_form,venue_list){

	theVenue=venue_list.options[venue_list.selectedIndex].value;
	//alert(theVenue);
	
	theCamp=GET_RADIO_VALUE(this_form.camp);
	//alert(theCamp);
	
	weeks_array=Set_Weeks_Array(theVenue,theCamp);
	options_array=new Array();
	
	this_form.Week.options.length=0;
	
	//  /*
	if (document.all){
		for(i=0;i<weeks_array.length;i++){
			var new_option = document.createElement("Option")
			new_option.text=weeks_array[i];
			new_option.value=weeks_array[i];
			this_form.Week.options.add(new_option);
		}
	}else{
	//  */ 
		//alert(models_array.length);
		i=0;
		for(i=0;i<weeks_array.length;i++){
			options_array[i]= new Option(weeks_array[i], weeks_array[i],false,false);
			this_form.Week.options[i]= options_array[i];
		}
	}
	
}

function Populate_Class_Weeks(this_form,venue_list){

theVenue=venue_list.options[venue_list.selectedIndex].value;
//alert(theVenue);

	theDates=GET_RADIO_VALUE(this_form.term);
	//alert(theDates);
	
	class_array=Set_Class_Array(theVenue);
	options_array=new Array();
	
	this_form.Class.options.length=0;
	
	//  /*
	if (document.all){
		for(i=0;i<class_array.length;i++){
			var new_option = document.createElement("Option")
			new_option.text=class_array[i];
			new_option.value=class_array[i];
			this_form.Class.options.add(new_option);
		}
	}else{
	//  */ 
		//alert(models_array.length);
		i=0;
		for(i=0;i<class_array.length;i++){
			options_array[i]= new Option(class_array[i], class_array[i],false,false);
			this_form.Class.options[i]= options_array[i];
		}
	}
}

function Set_Weeks_Array(selected_option,theCamp){
	weeks_array="";
	if (selected_option=='Select Venue'||selected_option==''){
		weeks_array=new Array("Select A Venue");
	}else{
		campName=theCamp.toLowerCase();//
		campFullName=campName+"Camp";
		//alert ("camp arrayname = "+selected_option+"_"+campFullName);
		//alert (campFullName);
		campArray=eval(selected_option+"_"+campFullName);
		//alert (campArray);
		weeks_array = campArray;//Sets the weeks array to the value of a variable set in the lists.js file
	}	
		
	return weeks_array;
}

function Set_Class_Array(selected_option){

if (selected_option=='Select Venue'){
		weeks_array=new Array("Select A Venue");
	}else{
		classArray=eval(selected_option+"_class");
		//alert (classArray);
		weeks_array = classArray;//Sets the weeks array to the value of a variable set in the lists.js file
	}	
		
	return weeks_array;
}


function oldSet_Class_Array(selected_option){
//var models_array=new Array();

	switch(selected_option){
		case "Ballyroan" :
			weeks_array=Ballyroan_class;
			break;
		case "Cabra" :
			weeks_array=Cabra_class;
			break;
		case "Castleknock" :
			weeks_array=Castleknock_class;
			break;
		case "Clonmel" :
			weeks_array=Clonmel_class;
			break;
		case "Clontarf" :
			weeks_array=Clontarf_class;
			break;
		case "David Llyod Riverview" :
			weeks_array=David_Llyod_Riverview_class;
			break;
		case "Donnybrook" :
			weeks_array=Donnybrook_class;
			break;
		case "Greystones" :
			weeks_array=Greystones_class;
			break;
		case "Leixlip" :
			weeks_array=Leixlip_class;
			break;
		case "Leopardstown" :
			weeks_array=Leopardstown_class;
			break;
		case "Malahide" :
			weeks_array=Malahide_class;
			break;
		case "Naas" :
			weeks_array=Naas_class;
			break;
		case "Taney" :
			weeks_array=Taney_class;
			break;
		case "Terenure" :
			weeks_array=Terenure_class;
			break;
		case "Rathgar":
			weeks_array=Rathgar_class;
			break;
		case "Raheny":
			weeks_array=Raheny_class;
			break;
		default :
			weeks_array=new Array("Select A Venue");
	}
	//alert(weeks_array);
	return weeks_array;
	
}
