$(document).ready(function() {
	
	// Active tab has lighter color and cannot be clicked
	$("#contact_nav li a.active_tab").css({
		"border" : "1px solid #3a3a3a", // If you change this, change the two in the two functions below as well
		"border-bottom" : "0px",
		"cursor" : "default"
	});
	
	$("#contact_nav li a").not(".active_tab").css({ 
		"border" : "0px",
		"cursor" : "pointer"
	});
	
	$("#second_contact_section").css({ "display" : "none" });
	
	$("#contact_nav li a").eq(0).click(function() { // First <a> tag (numbering starts at zero)
		class_test = $(this).attr("class");
		
		if(class_test == "active_tab") {
			return false;
		}
		
		else {
			$("#second_contact_section").hide();
			$("#first_contact_section").fadeIn("fast");
			
			$("#contact_nav li a").eq(1).removeClass("active_tab");
			$(this).addClass("active_tab");
			
			$("#contact_nav li a.active_tab").css({ // CSS has to be written again due to the event happening after initial styling
				"border" : "1px solid #3a3a3a",
				"border-bottom" : "0px",
				"cursor" : "default"
			});
	
			$("#contact_nav li a").not(".active_tab").css({ 
				"border" : "0px",
				"cursor" : "pointer"
			});
		}
		
		return false;
	});
	
	$("#contact_nav li a").eq(1).click(function() { // Second <a> tag
		class_test = $(this).attr("class");
		
		if(class_test == "active_tab") {
			return false;
		}
		
		else {
			$("#first_contact_section").hide();
			$("#second_contact_section").fadeIn("fast");
			
			$("#contact_nav li a").eq(0).removeClass("active_tab");
			$(this).addClass("active_tab");
			
			$("#contact_nav li a.active_tab").css({
				"border" : "1px solid #3a3a3a",
				"border-bottom" : "0px",
				"cursor" : "default"
			});
	
			$("#contact_nav li a").not(".active_tab").css({ 
				"border" : "0px",
				"cursor" : "pointer"
			});
		}
		
		return false;
	});
	
	// Contact Form Validation
	// Adapted from Trevor Davis' code tutorial: http://trevordavis.net/blog/tutorial/ajax-forms-with-jquery/

	$(".submit").click(function() {
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var senderNameVal = $(this).parent().children(".name").val();
		if(senderNameVal == '' || senderNameVal == ' ' || senderNameVal == 'Name...' || senderNameVal == 'Venue Name...') {
			$(this).parent().children(".name").css({ "border" : "1px solid #f00" });
			hasError = true;
		}
		
		var emailFromVal = $(this).parent().children(".emailFrom").val();
		if(emailFromVal == '') {
			$(this).parent().children(".emailFrom").css({ "border" : "1px solid #f00" });
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {	
			$(this).parent().children(".emailFrom").css({ "border" : "1px solid #f00" });
			hasError = true;
		}
		
		var subjectVal = $(this).parent().children(".subject").val();
		if(subjectVal == '' || subjectVal == ' ' || subjectVal == 'Subject...' || subjectVal == 'Location...') {
			$(this).parent().children(".subject").css({ "border" : "1px solid #f00" });
			hasError = true;
		}
		
		var messageVal = $(this).parent().children(".message").val();
		if(messageVal == '' || messageVal == ' ') {
			$(this).parent().children(".message").css({ "border" : "1px solid #f00" });
			hasError = true;
		}
		
		
		if(hasError == false) {	
			// Next four lines of code reverse the "red outline" error styling if there was any
			$(this).parent().children(".name, .emailFrom, .subject, .message").css({ "border" : "1px solid #ddd" });
			
			$(this).css({ 'opacity' : '.5', 'width' : 'auto' });
   						
			$(this).val('Message Sent!');
			
			emailType = $(this).parent().parent().attr('class'); // If you change the classes, change the values in the next if statement below
			
			template_path_contact = $("#template_path_contact").text(); // Used to get template_path which uses php code
			
			if(emailType == 'userEmail' || emailType == 'venueEmail') {
			
			$.post(template_path_contact + "/emailForm.php",
   				{ subject: subjectVal, message: messageVal, mailFrom: emailFromVal, from: senderNameVal + "<request@yoursite.com>", headers: "From: " + from + "\r\nReply-To: " + mailFrom },
   					function(data) {
   					});
			}
			
			else { // If you add a newsletter php form, put it inbetween here and remove the return false
				return false;
			}
		}
		
		return false;
	});
});
