/**
 * This function validates the form
 */

function validate_form( thisform ) {
	
	with ( thisform ) {
		
		/**
		 * Workshop
		 */
		
		if ( validate_select ( workshop, "Please select a workshop." ) == false ) {
			
			workshop.focus();
			
			return false
			
		}		
		
		/**
		 * Training Date
		 */
		
		if ( validate_select ( training_date, "Please select a training date." ) == false ) {
			
			training_date.focus();
			
			return false
			
		}				
		
		// TEMPORARY VALIDATION FOR ACKNOWLEDGING ADDRESS CHANGE
		
		if ( document.getElementById("ack_address_change") != null ) {
			
			if ( document.getElementById("ack_address_change").checked == false ) {
			
				alert("Please acknowledge the address change for this session by clicking the checkbox.");
				return false;
			
			}
			
		}
		
			
		/**
		 * First name
		 */
		
		if ( validate_required ( fname, "Please enter your first name." ) == false ) {
			
			fname.focus();
			
			return false
			
		}	
		
		/**
		 * Last name
		 */
		 
		if ( validate_required ( lname, "Please enter your last name." ) == false ) {
			
			lname.focus();
			
			return false
			
		}			 
		
		/**
		 * Address 1
		 */
		 
		if ( validate_required ( address_1, "Please enter your address." ) == false ) {
			
			address_1.focus();
			
			return false
			
		}	
		
		/**
		 * City
		 */
		 
		if ( validate_required ( city, "Please enter your city." ) == false ) {
			
			city.focus();
			
			return false
			
		}
		
		/**
		 * State
		 */
		 
		if ( validate_required ( state, "Please enter your state." ) == false ) {
			
			state.focus();
			
			return false
			
		}
		
		/**
		 * Zip
		 */
		 
		if ( validate_required ( zip, "Please enter your zip code." ) == false ) {
			
			zip.focus();
			
			return false
			
		}		
		
		/**
		 * County
		 */
		 
		if ( validate_required ( county, "Please enter your county." ) == false ) {
			
			county.focus();
			
			return false
			
		}		
		
		/**
		 * Phone
		 */
		 
		if ( validate_required ( phone, "Please enter your phone number." ) == false ) {
			
			phone.focus();
			
			return false
			
		}				
		
		 /**
		  * E-mail
		  */
		
		if ( validate_email ( email, "Please enter a valid e-mail address." ) == false ) {
			
			email.focus();
		
			return false
		
		}
		
		/**
		 * Number of Guests
		 */
		
		if ( validate_select ( num_of_guests, "Please select your number of guests." ) == false ) {
			
			num_of_guests.focus();
			
			return false
			
		}		
		
		/**
		 * Child's name
		 */
		 
		if ( validate_required ( child_name, "Please enter the child's name." ) == false ) {
			
			child_name.focus();
			
			return false
			
		}
		
		/**
		 * Birthday
		 */
		 
		if ( validate_required ( birthday, "Please enter the child's birth date." ) == false ) {
			
			birthday.focus();
			
			return false
			
		}		
		
		/**
		 * Relationship
		 */
		 
		if ( validate_required ( relationship, "Please enter your relationship with the child." ) == false ) {
			
			relationship.focus();
			
			return false
			
		}	
		
		/**
		 * Disability
		 */
		 
		if ( validate_required ( disability, "Please enter the child's primary disability." ) == false ) {
			
			disability.focus();
			
			return false
			
		}
		
		/**
		 * Is Child Attending?
		 */
		 
		if ( validate_radio ( is_attending, "Please tell us if the child is attending the workshop." ) == false ) {
			
			is_attending.focus();
			
			return false
			
		}		
		
		/**
		 * Notification
		 */
		 
		if ( validate_required ( notification, "Please enter your preferred notification method." ) == false ) {
			
			notification.focus();
			
			return false
			
		}		
	
	}
	
} // end function validate_form

/**
 * This function checks for blank fields
 */

function validate_required( field, alerttxt ) {
	
	with ( field ) {
		
		if ( value == null || value == "" ) {
		
			alert ( alerttxt );
			
			return false
			
		} else { 
			
			return true
		
		}

	}
	
} // end function validate_required

/**
 * This function validates a select option
 */

function validate_select( field, alerttxt ) {
	
	with ( field ) {
		
		if ( value == null || value == "" || value < 0 ) {
		
			alert ( alerttxt );
			
			return false
			
		} else { 
			
			return true
		
		}

	}
	
} // end function validate_select

/**
 * This function validates a radio button
 */
 
function validate_radio( field, alerttxt ) {
	
	var cnt = -1;
	
    for ( var i = field.length - 1; i > -1; i-- ) {
		
        if ( field[i].checked ) {
			
			cnt = i; 
			
			i = -1;
		
		}
    
	}
	
	if ( cnt < 0 ) {
		
		alert( alerttxt );
		
		return false
		
	} else {
		
		return true
	
	}
		
} // end function validate_radio

/**
 * This function checks for a valid e-mail addres
 */
 
function validate_email( field, alerttxt ) {
	
	with ( field ) {
		
		apos = value.indexOf( "@" )
		
		dotpos = value.lastIndexOf( "." )
		
		if ( apos <1 || dotpos-apos < 2 ) { 
			
			alert( alerttxt );
			
			return false
		
		} else {
			
			return true
		
		}
		
	}
	
} // end function validate_email