$(document).ready(function(){					

	$('#amount').format({precision: 2,allow_negative:false,autofix:true});
	$('table tr:even').addClass('alt');
	
	$('#row1').css('background-color', '#EDF0D1');
	$('#text1').css('color', '#000');
	$('#memorialName, #showRecurring').hide();
	$('#recipient').hide();
	
	
	$('.option_button').click(function() {
		
		$('.tableRowA > div').css("color", "#666");
		$('.tableRowB > div').css("color", "#666");
		$('.tableRowA').css("background-color", "#fff");
		$('.tableRowB').css("background-color", "#fff");
		
		$(this).parent("div").parent("div").css("background-color", "#EDF0D1");
		$(this).parent("div").siblings().css("color", "#000");
		get_member_cost();
	});
	
	function get_member_cost() {
		type_selected = $(".option_button:checked").val();
		if (type_selected == "Council of 100"){
			min_donation = council_100;
		}else if (type_selected == "Corporate Council"){
			min_donation = council_corporate;
		}else{
			min_donation = council_president;
		}
		$("#amount").val(min_donation);
		update_payments();
	}
	function update_payments(){
		current_donation = $('#amount').val();
		$("#recur_monthly").html((current_donation/12).toFixed(2));
		$("#recur_quarterly").html((current_donation/4).toFixed(2));
	}
	$("input[name='recurring']").click(function(){
		if ($(this).attr('id') == "recurring_yes") {
			$('#showRecurring').show();
		}else{
			$('#showRecurring').hide();
		}
	});
	$('#amount').blur(function(){
		update_payments();
	});
	$('.set_memorial').click(function() {
		if ($(this).attr('id') == "noMemorial")
		{
			$('#memorialName').hide();
			$('#name').val("");
		}
		else
		{
			$('#memorialName').show();
			$('#name').focus();
		}
	});
	
	$('#shippingChoice').click(function() {
		
		if ($(this).attr("checked"))
		{
			$('#recipient').show();
			$("#shipTo_firstName").focus();
		}
		else
		{
			$('#recipient').hide();
			$('#shipTo_firstName').val("");
			$('#shipTo_lastName').val("");
			$('#shipTo_street1').val("");
			$('#shipTo_street2').val("");
			$('#shipTo_city').val("");
			$('#shipTo_state').val("");
			$('#shipTo_postalCode').val("");
		}
		
	});
	
	
	$('#orderForm').submit(function() {
		
		var current_amount = $("#amount").val();
		
		if (isNaN(current_amount) || (current_amount < min_donation) )
		{
			alert('You must enter a donation amount of at least $' + min_donation + '.');
			$('#amount').focus();
			return false;
		}

		if (($('#aMemorial').attr("checked") == true) || ($('#anHonor').attr("checked") == true))
		{
			if ($('#name').val() == "")
			{
				alert('If you are donating in memory or honor of another person, you must enter their name.\n\nClick OK to try again.');
				$('#name').focus();
				return false;
			}
		}
		
		if ($('#shippingChoice').attr("checked") == true)
		{
			if ($('#shipTo_firstName').val() == "")
			{
				alert('You must enter the first name of the recipient.\n\nClick OK to try again.');
				$('#shipTo_firstName').focus;
				return false;
			}
			
			if ($('#shipTo_lastName').val() == "") {
				alert('You must enter the last name of the recipient.\n\nClick OK to try again.');
				('#shipTo_lastName').focus();
				return false;
			}
			
			if ($('#shipTo_street1').val() == "") {
				alert('You must enter the recipient\'s street address.\n\nClick OK to try again.');
				('#shipTo_street1').focus();
				return false;
			}

			if ($('#shipTo_city').val() == "") {
				alert('You must enter the recipient\'s city.\n\nClick OK to try again.');
				('#shipTo_city').focus();
				return false;
			}

			if ($('#shipTo_state').val() == "") {
				alert('You must enter the recipient\'s state.\n\nClick OK to try again.');
				('#shipTo_state').focus();
				return false;
			}

			if ($('#shipTo_postalCode').val() == "") {
				alert('You must enter the recipient\'s zip code.\n\nClick OK to try again.');
				('#shipTo_postalCode').focus();
				return false;
			}
		}

	});
	
	
	$('#billingForm').submit(function() {
		
		if ($('#firstName').val() == "") {
			alert('You must enter your first name.\n\nClick OK to try again.');
			$('#firstName').focus();
			return false;
		}

		if ($('#lastName').val() == "") {
			alert('You must enter your last name.\n\nClick OK to try again.');
			$('#lastName').focus();
			return false;
		}

		if ($('#street1').val() == "") {
			alert('You must enter your street address.\n\nClick OK to try again.');
			$('#street1').focus();
			return false;
		}

		if ($('#city').val() == "") {
			alert('You must enter the name of your city.\n\nClick OK to try again.');
			$('#city').focus();
			return false;
		}

		if ($('#state').val() == "") {
			alert('You must enter the abbreviation of your state.\n\nClick OK to try again.');
			$('#state').focus();
			return false;
		}

		if ($('#zip').val() == "") {
			alert('You must enter your zip code.\n\nClick OK to try again.');
			$('#zip').focus();
			return false;
		}

		if ($('#phone').val() == "") {
			alert('You must enter your phone number.\n\nClick OK to try again.');
			$('#phone').focus();
			return false;
		} else {
			// Validate the phone number format
			regex = /\s+/;
			var phoneNumber = $('#phone').val();
			if (phoneNumber.match(regex)) {
				alert('The phone number cannot contain any spaces.\n\nClick OK to try again.');
				$('#phone').focus();
				return false;
			}

			regex = /\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/;
			if (!phoneNumber.match(regex)) {
				alert('The phone number you entered was invalid.\n\nClick OK to try again.');
				$('#phone').focus();
				return false;
			}
		}

		if ($('#emailAddress').val() == "") {
			alert('You must enter your email address.\n\nClick OK to try again.');
			$('#emailAddress').focus();
			return false;
		} else {
			// Validate the email address
			var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			var emailAddress = $('#emailAddress').val();
			if (!emailAddress.match(regex)) {
				alert('The email address you entered was invalid.\n\nClick OK to try again.');
				$('#emailAddress').focus();
				return false;
			}
		}
		
	});
	
});