// JavaScript Document

//Jquery content switcher by Stormware Studios- 2009.

function showdiv(divname)
{
	$(divname).fadeIn();
}

function hidediv(divname)
{
	$(divname).hide();
}

function hidealldivs()
{
	hidediv("#about");
	hidediv("#home");
	hidediv("#gallery");
	hidediv("#contact");
}

$(document).ready(function()
{
	hidealldivs();
	showdiv("#home");
					  
	$("#homeButton").click(function()
	{
		hidealldivs();
		showdiv("#home");
	});
	
	$("#aboutButton").click(function()
	{
		hidealldivs();
		showdiv("#about");
	});
	
	$("#contactButton").click(function()
	{
		hidealldivs();
		showdiv("#contact");
	});
	
	
	$("#galleriesButton").click(function()
	{
		hidealldivs();
		showdiv("#gallery");
	});
	
	$("#formName").click(function()
	{
		var nameVal = $(this).val();
		{
			if (nameVal == "Name")
			{
				$(this).val('');
			}
		}
	});
	
	$("#formEmail").click(function()
	{
		var nameVal = $(this).val();
		{
			if (nameVal == "Email Address")
			{
				$(this).val('');
			}
		}
	});
	
	$("#formSubject").click(function()
	{
		var nameVal = $(this).val();
		{
			if (nameVal == "Subject")
			{
				$(this).val('');
			}
		}
	});
	
	$("#formMessage").click(function()
	{
		var nameVal = $(this).val();
		{
			if (nameVal == "Your message...")
			{
				$(this).val('');
			}
		}
	});
	
	
	
	$("#formSubmit").click(function()
	{
		hidediv(".error"); // Hide any tags of class .error

		var hasError = false; // Currently no errors
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; // Email regex
		var emailToVal = $("#formEmail").val(); // Value of the email field
		
		var nameVal = $("#formName").val(); // Value of the subject field
		if (nameVal == '') // If the subject is blank
		{
			$("#formName").after('<span class="error">*Required</span>'); // Tell the user to type something
			hasError = true; // There are errors
		}

		// Verify the email field is not empty
		if (emailToVal == '') // If the email field is empty
		{
			$("#formEmail").after('<span class="error">*Required</span>'); // Tell the user to type something
			hasError = true; // There are errors
		}
		
		// Verify the email field is not malformed
		else if (!emailReg.test(emailToVal)) // If the email address is malformed
		{
			$("#formEmail").after('<span class="error">*Invalid email format</span>'); // Tell the user it's malformed
			hasError = true; // There are errors
		}
		
		var subjectVal = $("#formSubject").val(); // Value of the subject field
		if (subjectVal == '') // If the subject is blank
		{
			$("#formSubject").after('<span class="error">*Required</span>'); // Tell the user to type something
			hasError = true; // There are errors
		}

		var messageVal = $("#formMessage").val(); // Value of the subject field
		if (messageVal == '') // If the subject is blank
		{
			$("#formMessage").after('<span class="error">*Required</span>'); // Tell the user to type something
			hasError = true; // There are errors
		}
		
		if(hasError == false)
		{
			hidediv("#formFields");
			showdiv("#formLoading");
			
			var formData = '';
			
			formData += "";
			
			$.ajax({
				type: "POST",
				url: "sendEmail.php",
				data: "name=John&location=Boston",
				success: function(msg)
				{
					hidediv("#formLoading");
					showdiv("#formResult");
				}
			});
			
			

			/*
			$.post("sendEmail.php",
			{
				emailTo: emailToVal, 
				emailFrom: emailFromVal, 
				subject: subjectVal,
				message: messageVal
			},
			
			function(data)
			{
				hidediv("#formLoading");
				showdiv("#formResult");
				//$("#sendEmail").slideUp("normal", function() {
				
				//	$("#sendEmail").before('<h1>Success</h1><p>Your email was sent.</p>');
				//});
			});		*/
		}

		return false;
	});
	
});						  


















































