$(document).ready(function()
{
	// Placeholder variable-arrays for default input and textarea values...
	var input = [];
	var txt = [];

	// iterate through each required input and save default values
	$('input.required').each(function(index) 
	{
		input[index] = $(this).val();
  	});
	
	// iterate through each required textarea and save default values
	$('textarea.required').each(function(index) 
	{
		txt[index] = $(this).val();
  	});
	
	// Clear input when clicked on
	$('input.required').focus(function()
	{
		var index = $(this).index();		
		if( $(this).val() == input[index] ) 
		{
			$(this).val('');
		}
	});
	
	// Reset input to default text (if it hasn't been edited)
	$('input.required').blur(function()
	{
		var index = $(this).index();
		if( $(this).val() == '' )
		{
			$(this).val(input[index]);
		}
	});
	
	// Clear textarea when clicked on
	$('textarea.required').focus(function()
	{
		var index = $(this).index();
		if( $(this).val() == txt[index] ) 
		{
			$(this).val('');
		}
	});
	
	// Reset input to default text (if it hasn't been edited)
	$('textarea.required').blur(function()
	{
		var index = $(this).index();
		if( $(this).val() == '' )
		{
			$(this).val(txt[index]);
		}
	});
	
	// Submit button is clicked:	
	$(".form-submit").click(function()
	{
		// Form validation check variable
		var valid = true;
		
		// iterate through the inputs and check to make sure it doesn't equal either
		// the default value or that it's not blank
		$('input.required').each(function(index) 
		{
			if ( ($(this).val() == "") || ( $(this).val() == input[index]) ) 
			{
				valid = false;
			} else if ($(this).hasClass('email') && !checkemail($(this).val()) ) 
			{
				valid = false;
			} 
		});
		
		// iterate through the textareas and check to make sure it doesn't equal either
		// the default value or that it's not blank
		$('textarea.required').each(function(index)
		{
			if ( ($(this).val() == "")  || ( $(this).val() == txt[index]) ) 
			{
				valid = false;
			}
		});
		
		// if the form input is not valid, throw an error...
		if (!valid) 
		{
			$('span.status-text').text('**Please check all fields and click "SUBMIT"');
		} else
		{
			// otherwise, let's call the ajax function and POST the variables to the "action" 
			// attribute in the form (mail.php at the time this was coded, 5/16/2011)
			$.post( $('form.anket-form').attr('action'), $('form.anket-form').serialize(),
			function()
			{
				// Success! Display thank you message
				$('span.status-text').text('Thank you - your message has been sent!');
				alert('Thank you - your message has been sent!');								
			});			
		}
		
		// Function used to validate that the email entered is valid...
		function checkemail(e)
		{
			var emailfilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			return emailfilter.test(e);
		}
	
	// no need to submit the form since we used AJAX to do it... return FALSE! :-)
	return false;	
	
	});
	
	
});
