// JavaScript Document

	function isValidEmailAddress(emailAddress) 
	{
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}

$(document).ready(function()
{
	$('input[type="text"]').addClass("idleField");
	$('textarea').addClass("idleField");
	$('select').addClass("idleField");
	
	$("#name").blur(function()
	{
		if(this.value != this.defaultValue)
		{
			$("#name-msg").fadeTo(900,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('').removeClass('messageboxerror');
			  $(this).html('').removeClass('messagebox');
			});
		}
	});
	
	$("#email").blur(function()
	{
		var email = $("#email").val();
		if(this.value != this.defaultValue)
		{
			$("#email-msg").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
			if(isValidEmailAddress(email))
			{
				$("#email-msg").fadeTo(900,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('').removeClass('messageboxerror');
				  $(this).html('').removeClass('messagebox');
				});
			}
			else
			{
				$("#email-msg").fadeTo(200,0.1,function() //start fading the messagebox
				{ 
				  //add message and change the class of the box and start fading
				  $(this).html('Enter a Valid Email Address.').addClass('messageboxerror').fadeTo(900,1);
				  $("#email").focus();
				});
			}
		}
	});
	
	$("#submit").click(function()
	{
		var ctr = 0;
		var firstEmpty = null;
		
		var name = $("#name").val();
		var email     = $("#email").val();
		
		if(name.length == 0)
		{
			if(firstEmpty == null)
			{
				firstEmpty = $("#name");
			}
			$("#name-msg").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Name Required.').addClass('messageboxerror').fadeTo(900,1);
			});
			
			ctr++;
		}
		if(email.length == 0)
		{
			if(firstEmpty == null)
			{
				firstEmpty = $("#email");
			}
			$("#email-msg").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Email Address Required.').addClass('messageboxerror').fadeTo(900,1);
			});
			ctr++;
		}
		
		if(ctr == 0)
		{		
			var debug = $("#serviceRequested :selected").text();
			$.post("functions/submit-contact-form.php",{ name:$('#name').val(), email:$('#email').val(), phone:$('#phone').val(),  address:$('#address').val(), serviceRequested:$('#serviceRequested').val()} ,function(data)
			{
				if(jQuery.trim(data)=='OK')
				{ 
					$("#thanks").show("slow");
					$("#freeConsultForm").hide("slow");
				}  
				else  
				{  
					// If we get here there is a problem with the email form processing
					alert("Error Sending Mail.");
				}  
			});
		}
		else
		{
			firstEmpty.focus();
		}
		return false;
	});
});
