
var formShowing = false;


$(document).ready(function(){
	   
	
	$("#respond").click(function() {
		if(!formShowing){
			$("#response_message").html("");
			$("#response_form").show();
			$("#response_form_div").fadeIn();
			//$(this).hide();
			formShowing = true;
			$(this).html("Cancel Your Response");
			return false;
		} else {
			cancelForm();
		}
	});
	
	$("#clearbutton").click(function() {
		$(".response_input").clearFields();
		$("#response_message").html("");
		$("#response_name_warning").html("");
		$("#response_title_warning").html("");
		$("#response_feedback_warning").html("");
		$("#response_terms_warning").html("");
		return false;
	});
	

	var formOptions = {
			beforeSubmit: function() {
					$("#response_message").html('<img src="images/whiteLoader.gif" />');
					var thisIsValid = true;
					if ($("#response_name").val() == "") {
						$("#response_name_warning").html("Please enter a name/nickname.");
						thisIsValid = false;
					} else {
						$("#response_name_warning").html("");
					}
					if ($("#response_title").val() == "") {
						$("#response_title_warning").html("Please enter a title for your post.");
						thisIsValid = false;
					} else {
						$("#response_title_warning").html("");
					}
					if ($("#response_feedback").val() == "") {
						$("#response_feedback_warning").html("Please enter a response");
						thisIsValid = false;
					} else {
						$("#response_feedback_warning").html("");
					}
					if (!$("#response_terms").attr('checked')) {
						$("#response_terms_warning").html("You must agree to the terms by clicking the box");
						thisIsValid = false;					   
					} else {
						$("#response_terms_warning").html("");
					}
					if(!thisIsValid) {
						$("#response_message").html("<p>Please fill in all fields prior to submitting</p>");	
					}
					return thisIsValid;
				},
			success: function(responseText) {
        		$("#response_message").html(responseText);
				$(".response_input").clearFields();
				$("#response_form").hide();
				formShowing = false;
				$("#respond").html("Respond to this Artwork");
    		}
		};	
	
	$("#response_form").ajaxForm(formOptions);
});



