// 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");
	
	$('input[type="text"]').focus(function() 
	{
		$(this).removeClass("idleField");
		$(this).addClass("focusField");
		if($(this).val()==$(this).attr('title'))
		{
			$(this).val('');
		}
	});
	$('input[type="text"]').blur(function()
	{
		$(this).removeClass("focusField");
		$(this).addClass("idleField");
		if($(this).val()=='')
		{
			$(this).val($(this).attr('title'));
		}
	});
	
	$('textarea').focus(function() 
	{
		$(this).removeClass("idleField");
		$(this).addClass("focusField");
		if($(this).val()==$(this).attr('title'))
		{
			$(this).val('');
		}
	});
	$('textarea').blur(function()
	{
		$(this).removeClass("focusField");
		$(this).addClass("idleField");
		if($(this).val()=='')
		{
			$(this).val($(this).attr('title'));
		}
	});
	
	$("#firstName").blur(function()
	{
		if(this.value != this.defaultValue)
		{
			$("#fname-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');
			});
		}
	});
	
	$("#lastName").blur(function()
	{
		if(this.value != this.defaultValue)
		{
			$("#lname-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();
				});
			}
		}
	});
	
	$("#company").blur(function()
	{
		if(this.value != this.defaultValue)
		{
			$("#company-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');
			});
		}
	});
	
	$("#phone").blur(function()
	{
		if(this.value != this.defaultValue)
		{
			$("#phone-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');
			});
		}
	});
	$("#websiteURL").blur(function()
	{
		if(this.value != this.defaultValue)
		{
			$("#websiteURL-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');
			});
		}
	});
	
	$("#submit").click(function()
	{
		var ctr = 0;
		var firstEmpty = null;
		
		if($("#firstName").val()==$("#firstName").attr('title'))
		{
			if(firstEmpty == null)
			{
				firstEmpty = $("#firstName");
			}
			$("#fname-msg").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('First Name Required.').addClass('messageboxerror').fadeTo(900,1);
			});
			
			ctr++;
		}
		if($("#lastName").val()==$("#lastName").attr('title'))
		{
			if(firstEmpty == null)
			{
				firstEmpty = $("#lastName");
			}
			$("#lname-msg").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Last Name Required.').addClass('messageboxerror').fadeTo(900,1);
			});
			ctr++;
		}
		if($("#email").val()==$("#email").attr('title'))
		{
			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($("#phone").val()==$("#phone").attr('title'))
		{
			if(firstEmpty == null)
			{
				firstEmpty = $("#phone");
			}
			$("#phone-msg").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Phone Number Required.').addClass('messageboxerror').fadeTo(900,1);
			});
			ctr++;
		}
		if($("#company").val()==$("#company").attr('title'))
		{
			if(firstEmpty == null)
			{
				firstEmpty = $("#company");
			}
			$("#company-msg").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Company Name Required.').addClass('messageboxerror').fadeTo(900,1);
			});
			ctr++;
		}
		if($("#websiteURL").val()==$("#websiteURL").attr('title'))
		{
			if(firstEmpty == null)
			{
				firstEmpty = $("#websiteURL");
			}
			$("#websiteURL-msg").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Website URL Required.').addClass('messageboxerror').fadeTo(900,1);
			});
			ctr++;
		}
		if($("#keywords").val()==$("#keywords").attr('title'))
		{
			if(firstEmpty == null)
			{
				firstEmpty = $("#keywords");
			}
			$("#keywords-msg").fadeTo(200,0.1,function() //start fading the messagebox
			{ 
			  //add message and change the class of the box and start fading
			  $(this).html('Desired Keywords Required.').addClass('messageboxerror').fadeTo(900,1);
			});
			ctr++;
		}
		
		if(ctr == 0)
		{
			$.post("functions/submit-quote-request-form.php",{ firstName:$('#firstName').val(), lastName:$('#lastName').val(), email:$('#email').val(), phone:$('#phone').val(), company:$('#company').val(), websiteURL:$('#websiteURL').val(), keywords:$('#keywords').val()} ,function(data)
			{
				if(jQuery.trim(data)=='OK')
				{ 
					$("#thanks").show("slow");
					$("#contactForm").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;
	});
});
