$(document).ready(function() { 
						   
	$('form#subscriberForm').submit(function() {
		
		$errors = new Array();
											 
		if ($('#name').val() == "") 
			$errors.push("Name is required");
			
		if ($('#email').val() == "") 
			$errors.push("Email is required");
		
		if (!$('#five_k').is(':checked') && !$('#ten_k').is(':checked') && !$('#half_marathon').is(':checked') && !$('#marathon').is(':checked') && !$('#triathlon').is(':checked') && !$('#other').is(':checked'))
			$errors.push("One of the races should be selected");
		
		if ($errors.length > 0) {
			$msg = "<ul>";
			for($x = 0; $x < $errors.length; $x++) {
				$msg += "<li>" + $errors[$x] + "</li>";
			}
			$msg += "</ul>";
			
			$.jGrowl('<div style="height: 10px"></div><div class="errors">'+$msg + "</div>", {
				theme: 	'error',
				header: 'ERROR',
				life:	5000,
				sticky: false
			});	
			
		} else {
			
			$five_k = 0; $ten_k = 0; $half_marathon = 0; $marathon = 0; $triathlon = 0; $other = 0;
			if ($('#five_k').is(':checked')) $five_k = 1;
			if ($('#ten_k').is(':checked')) $ten_k = 1;
			if ($('#half_marathon').is(':checked')) $half_marathon = 1;
			if ($('#marathon').is(':checked')) $marathon = 1;
			if ($('#triathlon').is(':checked')) $triathlon = 1;
			if ($('#other').is(':checked')) $other = 1;
			$.ajax({
				type: "POST",
				url: baseUrl + "/emailupdates/subscribe",
				data: "name="+ $('#name').val() + "&email=" + $('#email').val() + "&county=" + $('#county').val() + "&five_k=" + $five_k + "&ten_k=" + $ten_k + "&half_marathon=" + $half_marathon + "&marathon=" + $marathon + "&triathlon=" + $triathlon + "&other=" + $other,
				success: function(del) {
					switch(del) {
						case "INVALID_EMAIL" :
							$.jGrowl('<div style="height: 10px"></div>Invalid email address name', {
								theme: 	'error',
								header: 'ERROR',
								life:	5000,
								sticky: false
							});	
							break;
						case "ALREADY_IN_USE" :
							$.jGrowl('<div style="height: 10px"></div>You have already subscribed to our list', {
								theme: 	'error',
								header: 'ERROR',
								life:	5000,
								sticky: false
							});	
							break;
						case "SUCCESS" :
							changeLocation('/messages/sfthankyou');
					}
				}, 
				error : function(del) {
					alert("Error posting data!");
				}

	});
			
			
		}
		
		return false;
		
	});

});