$(document).ready(function() {
// places the alt value in a text field. takes it out when the field becomes active
	$("input[type=text],textarea,input[type=password]").each(function() {
		$(this).val($(this).attr("alt"))
		if ($(this).attr("alt")!="") {

			$(this).focus(function() {

				if ($(this).val()==$(this).attr("alt")) {

					$(this).val("");

				}

			}).blur(function() {

				if ($(this).val()=="") {

					$(this).val($(this).attr("alt"));

				}

			});

		}

	});
	
// makes the "specify other" input box appear when you select "Other" from the drop down
	$("#referred_other").hide();
	$("#referred_by").change(function() {
		var referredValue = $("#referred_by option:selected").val();
		if (referredValue == "other") {
				$("#referred_other").show();
			}
			else {
				$("#referred_other").hide();
			}
			
		});
// makes the "specify other" input box appear when you check off "Other"
	$("#specify_other_location").hide();
	$("span.checkbox").click( function() {
		if ($("#other_location_checkbox").is(":checked")) {
			$("#specify_other_location").show();
		}
		else {
			$("#specify_other_location").hide();
		}
		});
	$("#specify_other_pets").hide();
	$("span.checkbox").click( function() {
		if ($("#other_pets_checkbox").is(":checked")) {
			$("#specify_other_pets").show();
		}
		else {
			$("#specify_other_pets").hide();
		}
		});

// submits the form to the php script 
	$("#rental_submit").click(function() {

	// preparing the variables for the form submission
		
	// Referral variable
		if ($("input#referred_other").val() == "") {				// REFERRAL VARIABLE
			var referred_by = $("select#referred_by").val();		// this is used on the referral section. it
		}															// determines if the "other option" is checked.
		else {														// if it isn't set the variable with the 
			var referred_by = $("input#referred_other").val();		// value from the drop down box.
		}
		
	// Location variable
		var locationValues = [];				
		$('input[name=location]:checked').each(function() {		// LOCATION VARIABLE	
		    locationValues.push($(this).val());						// puts the value of each checkbox named "location"
		});															// into an array. Then joins them with a comma
		if ($("input#other_location_checkbox").is(":checked")) {	// seperating each array. 
			var locations = locationValues.join(", ") + $("input#specify_other_location").val();		
		}															// If statement checks to see if the other location
		else {														// box is checked and puts the value of the input
			var locations = locationValues.join(", ");				// that appears into the variable.
		}
	// Pet variables
		var petValues = [];
		$('input[name=pets]:checked').each(function() {
			petValues.push($(this).val());	
		});
		if ($("input#other_pets_checkbox").is(":checked")) {
			var pets = petValues.join(", ") + $("input#specify_other_pets").val();
		}
		else {
			var pets = petValues.join(", ");
		}
		
	// Employment variable
		var employmentValues = [];
		$('input[name=employment]:checked').each(function() {
			employmentValues.push($(this).val());	
		});
		var employment = employmentValues.join(", ");

		
	// basic variables, nothing special needed to set them.
		var name = $("#first_name").val() + " " + $("#last_name").val();
		var email = $("#email_address").val();
		var phone = $("#phone").val();
		var apt_size = $("#apt_size").val();
		var price_range = "$" + $("#minimum_price").val() + " - " + "$" + $("#maximum_price").val();
		var parking = $("select#parking").val();
		var number_of_people = $("#number_of_people").val();
		var number_of_spaces = $("#number_of_spaces").val();
		var amenities = $("#amenities").val();
		var deal_breaker = $("#deal_breaker").val();
		var credit_history = $("#credit_history").val();
		var co_signer = $("input[name=co_signer]:checked").val(); 
		var properties_viewed = $("#properties_viewed").val();

// Form validation
	// validate contact form on keyup and submit
	var rental_form = $("#rental_form");
		rental_form.validate({
			//set the rules for the field
			rules: {
				first_name: {
					required: true,
					minlength: 2,
				},
				last_name: {
					required: true,
					minlength: 2,
				},
				email: {
					required: true,
					email: true,
				},
				referred_by: {
					required: true,
				},
			},
			//set messages to appear inline
			messages: {
			first_name: "Please enter your first name",
			last_name: "Please enter your last name",
			email: "Please enter a valid email address",
			message: "Please enter your message",
			}
		});	
	
	// combining the variables into a string to submit in a POST request
		if (rental_form.valid() == true) {
			var dataString = 'name=' + name + '&email=' + email + '&referred_by=' + referred_by + '&phone=' + phone + '&apt_size=' + apt_size + '&locations=' + locations + '&price_range=' + price_range + '&number_of_people=' + number_of_people + '&pets=' + pets + '&parking=' + parking + '&number_of_spaces=' + number_of_spaces + '&amenities=' + amenities + '&deal_breaker=' + deal_breaker + '&employment=' + employment + '&credit_history=' +  credit_history + '&co_signer=' + co_signer + '&properties_viewed=' + properties_viewed;
			//alert(dataString); return false;
			
			$("#p.error").hide();
			$("#rental_form_wrapper").fadeIn(1500, function() {
				$(this).append("<p class='error'>Loading.</p>");
			})
			$.ajax({
				  type: "GET",
				  url: "http://eplacehomes.com/forms/includes/rental_app.php",
				  data: dataString,
				  success: function() {
					$('#rental_form_wrapper').animate({ scrollTop: $(".story").offset().top }, "fast").html("<div id='message'></div>");
					$('#message').html("<h2>Thank You!</h2>")
					.hide()
					.fadeIn("slow", function() {
					  $('#message').append("<h2>We will be in touch soon.</h2>");
					});
				  }
			});
		}
		else {
			$("p.error").hide();
				 $("#rental_form_wrapper").fadeIn(1500, function() {
				var me=$(this);
				err=document.createElement("p");
				$(err).addClass("error").text("Sorry, some fields were not completed properly. Please scroll up to check for any errors.").appendTo(me).hide().fadeIn("slow");
});
			}
				
		});
	
	

});