$(function() {
  $('ul#reg_fields label.error').hide();
	$('#reg_form_message').hide();

  $('input.textfield').css({backgroundColor:"#FFFFFF"});
  $('input.textfield').focus(function(){
    $(this).css({backgroundColor:"#efefef"});
  });
  $('input.textfield').blur(function(){
    $(this).css({backgroundColor:"#eee"});
  });

  $(".btn").click(function() {
		// validate and process form
		// first hide any error messages
    $('.error').hide();

	  var fname = $("input#fname").val();
		if (fname == "") {
      $("label#fname_error").show();
      $("input#fname").focus();
      return false;
    }
	  var lname = $("input#lname").val();
		if (lname == "") {
      $("label#lname_error").show();
      $("input#lname").focus();
      return false;
    }
		var email = $("input#email").val();
		if (email == "") {
      $("label#email_error").show();
      $("input#email").focus();
      return false;
    } else if ((email.indexOf("@") == -1) || (email.lastIndexOf(".") == -1)) {
      $("label#email_error").text("Invalid email address");
      $("label#email_error").show();
      $("input#email").focus();
			return false;
		}
		var phone = $("input#phone").val();
		if (phone == "") {
      $("label#phone_error").show();
      $("input#phone").focus();
      return false;
    }
		var city = $("input#city").val();
		if (city == "") {
      $("label#city_error").show();
      $("input#city").focus();
      return false;
    }
		var state = $("input#state").val();
		if (state == "") {
      $("label#state_error").show();
      $("input#state").focus();
      return false;
    }
		var zipcode = $("input#zipcode").val();
		if (zipcode == "") {
      $("label#zipcode_error").show();
      $("input#zipcode").focus();
      return false;
    }
		var agegroup = $("#agegroup :selected").text();
		if (agegroup == "") {
      $("label#agegroup_error").show();
      $("input#agegroup").focus();
      return false;
    }
		var shirtsize = $("#shirtsize :selected").text();
		if (shirtsize == "") {
      $("label#shirtsize_error").show();
      $("input#shirtsize").focus();
      return false;
    }
		var inschool = $("#inschool :selected").text();
		if (inschool == "") {
      $("label#inschool_error").show();
      $("input#inschool").focus();
      return false;
    }
		var school_name = $("input#school_name").val();
		var occupation = $("input#occupation").val();
		if (occupation == "") {
      $("label#occupation_error").show();
      $("input#occupation").focus();
      return false;
    }
		var diet = $("input#diet").val();
		if (diet == "") {
      $("label#diet_error").show();
      $("input#diet").focus();
      return false;
    }
		var hosthotel = $("#hosthotel :selected").text();
		if (hosthotel == "") {
      $("label#hosthotel_error").show();
      $("input#hosthotel").focus();
      return false;
    }
		var anticipation = $("textarea#anticipation").val();
		if (anticipation == "") {
      $("label#anticipation_error").show();
      $("input#anticipation").focus();
      return false;
    }
		var referral_details = $("textarea#referral_details").val();
		if (referral_details == "") {
      $("label#referral_details_error").show();
      $("input#referral_details").focus();
      return false;
    }
		
		var dataString = 'fname='+ fname + '&lname=' + lname + '&email=' + email + '&phone=' + phone + '&city=' + city + '&state=' + state + '&zipcode=' + zipcode + '&agegroup=' + agegroup + '&shirtsize=' + shirtsize + '&inschool=' + inschool + '&school_name=' + school_name + '&occupation=' + occupation + '&diet=' + diet + '&hosthotel=' + hosthotel + '&anticipation=' + anticipation + '&referral_details=' + referral_details;

		//alert (dataString); return false;
		
		$.ajax({
      type: "POST",
      url: "bin/register.process.php",
      data: dataString,
      success: function() {
				$('#reg_form_panel').slideUp(200, function() {
					$('#reg_form_message').fadeIn(1700);
				});
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#fname").select().focus();
});

