var emptyOption = "<option value=''>Please select an option above first</option>";
var currentId = null;

Effect.Accordion = function (contentId) {
	var slideDown = 0.5;
	var slideUp = 0.5;
	contentId = $(contentId);
	if (currentId != contentId) {
			if (currentId == null) {
						new Effect.SlideDown(contentId, {duration: slideDown});
					} else {
					new Effect.SlideUp(currentId, {duration: slideUp});
					new Effect.SlideDown(contentId, {duration: slideDown});
			}
			currentId = contentId; 
	} else {
			new Effect.SlideUp(currentId, {duration: slideUp});
			currentId = null;
	}
};

function checkCoachRegistrationForm() {
	var validated = true;
	var errors = new Array();
	
	if ($('sport_id').value == '') {
		validated = false;
		$('sport_id').insert({'after': new Element('p', {'class': 'error', 'id': 'sport_id_error'})});
		$('sport_id_error').innerHTML= "You must choose a sport.";
		$('sport_id').activate();
		return false;
	}
	
	if ($('coach_type').value == 'college' || $('coach_type').value == 'highschool') {
		// do nothing
	} else {
		validated = false;
		$('coach_type').insert({'after': new Element('p', {'class': 'error', 'id': 'coach_type_error'})});
		$('coach_type_error').innerHTML= 'You must select "College" or "High School".';
		$('coach_type').activate();
		return false;
	}
	
	if ($('coach_type').value == 'college') {
		// college is required
		if ($('college_id') == undefined || $('college_id').value == '') {
			validated = false;
			$('college_id').insert({'after': new Element('p', {'class': 'error', 'id': 'college_id_error'})});
			$('college_id_error').innerHTML = 'You must select a college.';
			$('college_id').activate();
			return false;
		}
	} else if ($('coach_type').value == 'highschool') {
		// state, city and highschool are required.
		if ($('hs_state') == undefined || $('hs_state').value == '') {
			validated = false;
			$('hs_state').insert({'after': new Element('p', {'class': 'error', 'id': 'hs_state_error'})});
			$('hs_state_error').innerHTML = 'You must select a state.';
			$('hs_state').activate();
			return false;
		}
		
		if ($('hs_city') == undefined || $('hs_city').value == '') {
			validated = false;
			$('hs_city').insert({'after': new Element('p', {'class': 'error', 'id': 'hs_city_error'})});
			$('hs_city_error').innerHTML = 'You must select a city.';
			$('hs_city').activate();
			return false;
		}
		
		if ($('hs_id') == undefined || $('hs_id').value == '') {
			validated = false;
			$('hs_id').insert({'after': new Element('p', {'class': 'error', 'id': 'hs_id_error'})});
			$('hs_id_error').innerHTML = 'You must select a school.';
			$('hs_id').activate();
			return false;
		}
	}
	
	// Common fields that are required: first name, last name, email, password, password confirm, captcha
	
	if ($('signup_coach_name_first').value == undefined || $('signup_coach_name_first').value == '') {
		validated = false;
		$('signup_coach_name_first').insert({'after': new Element('p', {'class': 'error', 'id': 'signup_coach_name_first_error'})});
		$('signup_coach_name_first_error').innerHTML = 'First name is required.';
		$('signup_coach_name_first').activate();
		return false;
	} else {
		if ($('signup_coach_name_first_error') != undefined && $('signup_coach_name_first_error') != '') {
			$('signup_coach_name_first_error').remove();
		}
	}
	
	if ($('signup_coach_name_last').value == undefined || $('signup_coach_name_last').value == '') {
		validated = false;
		$('signup_coach_name_last').insert({'after': new Element('p', {'class': 'error', 'id': 'signup_coach_name_last_error'})});
		$('signup_coach_name_last_error').innerHTML = 'Last name is required.';
		$('signup_coach_name_last').activate();
		return false;
	} else {
		if ($('signup_coach_name_last_error') != undefined && $('signup_coach_name_last_error') != '') {
			$('signup_coach_name_last_error').remove();
		}
	}
	
	if ($('signup_coach_email').value == undefined || $('signup_coach_email').value == '') {
		validated = false;
		$('signup_coach_email').insert({'after': new Element('p', {'class': 'error', 'id': 'signup_coach_email_error'})});
		$('signup_coach_email_error').innerHTML = 'Email is required.';
		$('signup_coach_email').activate();
		return false;
	} else {
		if ($('signup_coach_email_error') != undefined && $('signup_coach_email_error') != '') {
			$('signup_coach_email_error').remove();
		}
	}
	
	if ($('signup_coach_email').value == undefined || !isRFC822ValidEmail($('signup_coach_email').value)) {
		validated = false;
		$('signup_coach_email').insert({'after': new Element('p', {'class': 'error', 'id': 'signup_coach_email_error'})});
		$('signup_coach_email_error').innerHTML = 'Email does not appear to be valid.';
		$('signup_coach_email').activate();
		return false;
	} else {
		if ($('signup_coach_email_error') != undefined && $('signup_coach_email_error') != '') {
			$('signup_coach_email_error').remove();
		}
	}
	
	//password, password confirm, captcha...
	
	if($('signup_coach_password').value.length < 4 || $('signup_coach_password').value.length > 30) {
		validated = false;
		$('signup_coach_password').insert({'after': new Element('p', {'class': 'error', 'id': 'signup_coach_password_error'})});
		$('signup_coach_password_error').innerHTML = 'Password must be between 4 and 30 characters.';
		$('signup_coach_password').activate();
		return false;
	} else {
		if ($('signup_coach_password_error') != undefined && $('signup_coach_password_error') != '') {
			$('signup_coach_password_error').remove();
		}
	}
	
	if($('signup_coach_password').value != $('signup_confirm_password').value) {
		validated = false;
		$('signup_coach_password').insert({'after': new Element('p', {'class': 'error', 'id': 'signup_coach_password_error'})});
		$('signup_coach_password_error').innerHTML = "Passwords don't match!";
		$('signup_coach_password').activate();
		return false;
	} else {
		if ($('signup_coach_password_error') != undefined && $('signup_coach_password_error') != '') {
			$('signup_coach_password_error').remove();
		}
	}

	if(validated == false) {
		return false;
	} else {
		return true;
	}
}

function getColleges(data) {
	var aj = new Ajax.Request(
		'/include/ajax-helper.php', {
			method: 'get',
			parameters: data,
			onComplete: updateCollegesSelect
		}
	);
}

function getHSStates(data) {
	var aj = new Ajax.Request(
		'/include/ajax-helper.php', {
			method: 'get',
			parameters: data,
			onComplete: updateHSStateSelect
		}
	);
}

function updateHSStateSelect(oReq) {
	$('hs_state').update(oReq.responseText);
}

function getHSCities(data) {
	var aj = new Ajax.Request(
		'/include/ajax-helper.php', {
			method: 'get',
			parameters: data,
			onComplete: updateHSCitiesSelect
		}
	);
}

function updateHSCitiesSelect(oReq) {
	$('hs_city').update(oReq.responseText);
}

function getHighSchools(data) {
	var aj = new Ajax.Request(
		'/include/ajax-helper.php', {
			method: 'get',
			parameters: data,
			onComplete: updateHighSchoolsSelect
		}
	);
}

function updateHighSchoolsSelect(oReq) {
	$('hs_id').update(oReq.responseText);
}

function sport_changed() {
	if ($('sport_id').value == '') {
		if($('hs_coach_form_fields').visible()) {
			new Effect.Accordion('hs_coach_form_fields');
		}
		if($('college_coach_form_fields').visible()) {
			new Effect.Accordion('college_coach_form_fields');
		}
	} else {
		activateMoreFields();
	}
	if ($('coach_type').value == 'college') {
		$('hs_id').disable();
		$('hs_id').update(emptyOption);
		$('hs_id').value = undefined;
	}
	if ($('sport_id_error') != undefined && $('sport_id') != '') {
		$('sport_id_error').remove();
	}
}

function coach_type_changed() {
	if ($('coach_type').value == '') {
		if($('hs_coach_form_fields').visible()) {
			new Effect.Accordion('hs_coach_form_fields');
		}
		if($('college_coach_form_fields').visible()) {
			new Effect.Accordion('college_coach_form_fields');
		}
	} else {
		if ($('coach_type_error') != undefined) { 
			$('coach_type_error').remove();
		}
		activateMoreFields();
	}
}

function activateMoreFields() {
	if ($('coach_type').value == 'college') {
		if($('hs_coach_form_fields').visible()) {
			new Effect.Accordion('hs_coach_form_fields');
		}
		if($('college_coach_form_fields').visible() == false) {
			new Effect.Accordion('college_coach_form_fields');
		}
		$('hs_id').disable();
		$('hs_id').value = undefined;
	}
	if ($('coach_type').value == 'highschool') {
		if ($('college_coach_form_fields') != undefined && $('college_coach_form_fields').visible()) {
			new Effect.Accordion('college_coach_form_fields');
		}
		if($('hs_coach_form_fields') == undefined || $('hs_coach_form_fields').visible() == false) {
			new Effect.Accordion('hs_coach_form_fields');
		}
		$('hs_city').disable();
		$('hs_city').update(emptyOption);
		
		$('hs_id').disable();
		$('hs_id').update(emptyOption);
		
		$('hs_state').enable();
		$('hs_state').activate();
		$('hs_state').update('');
		getHSStates({
			hs_state: $F('hs_state')
		});
	}
}

function hs_state_changed() {
	$('hs_city').enable();
	$('hs_city').activate();
	
	$('hs_id').disable();
	$('hs_id').update(emptyOption);
	
	getHSCities({
		hs_city: $F('hs_city'),
		hs_state: $('hs_state').value
	});
}

function hs_city_changed() {
	$('hs_id').enable();
	$('hs_id').activate();
	getHighSchools({
		hs_id: $F('hs_id'),
		hs_state: $('hs_state').value,
		hs_city: $('hs_city').value
	});
}

function hs_changed() {
	if ($('hs_id_error') != undefined && $('hs_id_error') != '') {
		$('hs_id_error').remove();
	}
}

// $Id$

/**
 * JavaScript function to check an email address conforms to RFC822 (http://www.ietf.org/rfc/rfc0822.txt)
 *
 * Version: 0.2
 * Author: Ross Kendall
 * Created: 2006-12-16
 * Updated: 2007-03-22
 *
 * Based on the PHP code by Cal Henderson
 * http://iamcal.com/publish/articles/php/parsing_email/
 */

/*

Portions copyright (C) 2006  Ross Kendall - http://rosskendall.com
Portions copyright (C) 1993-2005 Cal Henderson - http://iamcal.com

Licenced under Creative Commons _or_ GPL according to the terms below...

--

 Licensed under a Creative Commons Attribution-ShareAlike 2.5 License

 You are free:

    * to Share -- to copy, distribute, display, and perform the work
    * to Remix -- to make derivative works

 Under the following conditions:

    * Attribution. You must attribute the work in the manner specified by the author or licensor.
    * Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one.

    * For any reuse or distribution, you must make clear to others the license terms of this work.
    * Any of these conditions can be waived if you get permission from the copyright holder.

 http://creativecommons.org/licenses/by-sa/2.5/

--

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 http://www.gnu.org/copyleft/gpl.html

*/


function isRFC822ValidEmail(sEmail) {

  var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
  var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
  var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
  var sQuotedPair = '\\x5c[\\x00-\\x7f]';
  var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
  var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
  var sDomain_ref = sAtom;
  var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
  var sWord = '(' + sAtom + '|' + sQuotedString + ')';
  var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
  var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
  var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
  var sValidEmail = '^' + sAddrSpec + '$'; // as whole string
  
  var reValidEmail = new RegExp(sValidEmail);
  
  if (reValidEmail.test(sEmail)) {
    return true;
  }
  
  return false;
}

