var thisRaceDayCount = 0;
var todaysDate = new Date();
var minBirthDate = new Date();
minBirthDate.setYear(todaysDate.getFullYear()-18);
minBirthDate.setHours(0);
minBirthDate.setMinutes(0);
minBirthDate.setSeconds(0);
minBirthDate.setMilliseconds(0);

function AdditionalFormValidation() {
	CheckAge();
	CheckAtLeastOneShiftChoosen();
	CheckRaceDayCount();
	CheckOverlappingShifts();
	CheckPasswordRequiredShifts();
}

function CheckAge() {
	var birthDate = new Date();
	birthDate.setFullYear($j('#birthYear').val());
	birthDate.setMonth($j('#birthMonth').val()-1);
	birthDate.setHours(0);
	birthDate.setMinutes(0);
	birthDate.setSeconds(0);
	birthDate.setMilliseconds(0);
	if(birthDate.valueOf() > minBirthDate.valueOf()) {
		frm.birthYear.throwError('You must be at least 18 years old to register online.');
	}
}

function CheckRaceDayCount() {
	var maxRaceDayShifts = 1;
	if($j('.shifts .raceDayShift .field input:checked').length > maxRaceDayShifts) {
		frm.volunteercategoryId.throwError('You can only select ' + maxRaceDayShifts + ' race day shift.');
	}
}

function CheckAtLeastOneShiftChoosen() {
	if(!$j('.shifts .field input:checked').length) {
		frm.volunteercategoryId.throwError('At least one Volunteer Shift is required.');
	}
}

function CheckOverlappingShifts() {
	$j('.shifts .field input:checked').each(function() {
		if(DoesShiftOverlap($j(this).attr("beginTime"), $j(this).attr("endTime"), $j(this).val())) {
			frm.volunteercategoryId.throwError('Overlapping shifts are not allowed.');
		}
	});
}

function DoesShiftOverlap(beginTime, endTime, categoryId) {
	var overlaps = false;
	var gracePeriodMinutes = 32; // shifts can overlap by this many minutes. from sarah horn on 8/14/09
	
	$j('.shifts .field input:checked').each(function() {
		if($j(this).val() != categoryId && 
										(
											IsValueBetween(parseInt(beginTime)+gracePeriodMinutes, parseInt($j(this).attr("beginTime")), parseInt($j(this).attr("endTime"))) 
											|| 
											IsValueBetween(parseInt(endTime)-gracePeriodMinutes, parseInt($j(this).attr("beginTime")), parseInt($j(this).attr("endTime")))
										)
		) {
			overlaps = true;
		}
	});

	return overlaps;
}

function IsValueBetween(value, start, end) {
	if(parseInt(value) > parseInt(start) && parseInt(value) < parseInt(end)) {
		return true;
	} else {
		return false;
	}
}

function CheckPasswordRequiredShifts() {
	var shiftId;
	var shiftPassword;
	var passwordFieldName;
	
	for(var i=0; i < $j('.shifts .passwordRequiredShift .field input').length; i++) {
		if($j('.shifts .passwordRequiredShift .field input')[i].checked) {
			passwordFieldName = $j('.shifts .passwordRequiredShift .shiftPassword input')[i].name;
			if(!$j('.shifts .passwordRequiredShift .shiftPassword input')[i].value.length) {
				frm[passwordFieldName].throwError("A Password is required for this shift.");
			} else {
				shiftId = $j('.shifts .passwordRequiredShift .field input')[i].value;
				shiftPassword = $j('.shifts .passwordRequiredShift .shiftPassword input')[i].value;
				$j.ajax({
					async: false,
					type: 'GET',
					url: 'index.cfm?fuseaction=volunteer.validateShiftPassword',
					data: 'shiftId='+shiftId+'&shiftPassword='+escape(shiftPassword)+'&x='+Math.floor(Math.random()*100000),
					success: function(resp) {
						if(resp == 0) {
							frm[passwordFieldName].throwError('The Password is incorrect for this shift.');
						}
					},
					error: function() {
						alert('password validation error: unable to connect');
					}
				});
			}
		}
	}
}

function checkEmail(email) {
	var _ajaxObject;
	var pars = 'email=' + email;
	var _ajaxUrl = 'index.cfm?fuseaction=volunteer.validateemail';
		_ajaxObject = new Ajax.Request(_ajaxUrl,{method: 'get', parameters: pars, onSuccess: _evaluateSuccess, onFailure: _evaluateFailure });
}

_evaluateSuccess = function(originalRequest) {
	var requestObj = originalRequest;
	originalRequest.responseText.evalScripts();
}

_evaluateFailure = function(originalRequest) {
	alert('failed');
}

_existingEmailPopup = function() {
	var confirmObj = confirm('You have already registered as a volunteer with this email address. If you are registering for someone else, click OK to continue.  Otherwise, click Cancel to return to the volunteer information page.');
	if (confirmObj == true) {

	} else {
		window.location = 'http://www.breastcancermarathon.com/support/volunteer/';
	}
}

