$(document).ready(function() {
  
	// global variables
	var weekendCost = 100;
  
  $.fn.clearForm = function() {
		$("#total_cost").text("$0.00");
    $("#leaders_total").text("0");
    return this.each(function() {
  	var type = this.type, tag = this.tagName.toLowerCase();
  	if (tag == 'form')
  	  return $(':input',this).clearForm();
  	if (type == 'text' || type == 'password' || tag == 'textarea')
  	  this.value = '';
  	else if (type == 'checkbox' || type == 'radio')
  	  this.checked = false;
  	else if (tag == 'select')
  	  this.selectedIndex = -1;
    });
  };

  $.fn.addValues = function() {
		var totalStudents = 0;
		var totalLeaders = 0;
    var totalRegistrants = 0;
    $(".quantity").each(function() {
      var typeRegistrants = parseInt(this.value);
      if (isNaN(typeRegistrants)) typeRegistrants = 0;
      totalRegistrants += typeRegistrants;
    });
    $(".students").each(function() {
      var typeStudents = parseInt(this.value);
      if (isNaN(typeStudents)) typeStudents = 0;
      totalStudents += typeStudents;
    });
    $(".leaders").each(function() {
      var typeLeaders = parseInt(this.value);
      if (isNaN(typeLeaders)) typeLeaders = 0;
      totalLeaders += typeLeaders;
    });
		//if (totalRegistrants >= 5) weekendCost = 90;
		//else weekendCost = 99;
    var totalCost = totalRegistrants * weekendCost;
		totalCost = String(totalCost);
    $("#students_total").text(totalStudents).fadeIn("slow");
    $("#leaders_total").text(totalLeaders).fadeIn("slow");
    $("#total_overall").text(totalRegistrants).fadeIn("slow");
    if ( totalCost.length == 4 ) totalCost = totalCost.substr(-4, 1) + "," + totalCost.substr(-3);
    else if ( totalCost.length == 5 ) totalCost = totalCost.substr(-5, 2) + "," + totalCost.substr(-3);
    else if ( totalCost.length == 6 ) totalCost = totalCost.substr(-8, 2) + "," + totalCost.substr(-6, 3) + "," + totalCost.substr(-3);
    $("#total_cost").text("$" + totalCost + ".00").fadeIn("slow");
  };

  $(".resetButton input").click(function() {
    $('#registrationForm form').clearForm();
    return false;
  });
  
	$("#registrationForm form").validate({
		rules: {
			church_name: "required",
			main_contact_name: "required",
			main_contact_email: {
				required: true,
				email: true
			},
			students_male: "required",
			students_female: "required",
			leaders_male: "required",
			leaders_female: "required",
			weekend: "required",
			check_number: "required"
		},
		messages: {
			church_name: "&#8593; Please enter a church name",
			main_contact_name: "&#8593; Please enter a contact name",
			main_contact_email: {
			  required: "&#8593; Please enter an email address",
			  email: "&#8593; Please enter a valid email address"
      },
			students_male: "&#8593; Please enter # of male students",
			students_female: "&#8593; Please enter # of female students",
			leaders_male: "&#8593; Please enter # of male leaders",
			leaders_female: "&#8593; Please enter # of female leaders",
			weekend: "&#8593; Please choose a weekend",
			check_number: "&#8593; Please enter a valid check #"
		}
	});

  $(".quantity").keyup(function() {
    $(this).addValues();
  });
  
	$("#registrationForm form").addValues();

});