/************************************************************

  MAVEN CREATIVE | www.wearemaven.com | copyright 2009 

  written/adapted by: Michael Sevilla | michael@wearemaven.com 

  This script requires jQuery and the jQuery Color plugin
  adapted from http://trevordavis.net

  I am sure there is a better way to write this.

***********************************************************/

$(document).ready(function(){			   
	//clear fields
		$('#firstN').val("");
		$('#lastN').val("");
		$('#company').val("");
		$('#email').val("");
		$('#phone').val("");
		$('#message').val("");
		
		//set fields
		$('#firstN').example(function() { return $(this).attr('title')});
		$('#lastN').example(function() { return $(this).attr('title')});
		$('#company').example(function() { return $(this).attr('title')});
		$('#email').example(function() { return $(this).attr('title')});
		$('#phone').example(function() { return $(this).attr('title')});
		$('#message').example(function() { return $(this).attr('title')});
		$('#referralName').example(function() { return $(this).attr('title')});
		$('#referralPhone').example(function() { return $(this).attr('title')});
		$('#referralEmail').example(function() { return $(this).attr('title')});
		
						   
	var startColor = "#000";
	var errorColor = "#ffd011";
	var animateSpeed = 200;
	
	
	var nrf=4; // number of required fields
	
	//set indiv. color vars in required fields array
	var rf=new Array();
	var i=0;
	for (i=0;i<=nrf-1;i++)
	{
		rf[i] =	startColor;
	} 
	
	//reset borders
	function rB(mVar, cc)
	{
		var cVar = rf[cc];
		$(mVar).css("border-color",cVar);
		$(mVar).animate({ borderColor: startColor }, animateSpeed);
		rf[cc] = startColor;
	};
	//set error borders
	function sB(sVar, sc)
	{
		var tVar = rf[sc];
		$(sVar).css("border-color",tVar);
		$(sVar).animate({ borderColor: errorColor }, animateSpeed);
		rf[sc] = errorColor;
	};
	
	$("#submit").click(function(){
		
		//non required fields
		
		var companyVal = $("#company").val();
		if(companyVal == 'Company Name' || companyVal == '')
		{
			companyVal = "No Company Entered";
		}
		
		var phoneVal = $("#phone").val();
		if(phoneVal == 'Phone Number' || phoneVal == '')
		{
			phoneVal = "No Phone Number Entered";
		}
		
		//required fields
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var fNameVal = $("#firstN").val();
		if(fNameVal == 'First Name' || fNameVal == '') 
		{
			sB("#fName", 0);
			hasError = true;
		}else{
			rB("#fName",0);
		}
		
		var lNameVal = $("#lastN").val();
		if(lNameVal == 'Last Name' || lNameVal == '') 
		{
			sB("#lName", 1);
			hasError = true;
		}else{
			rB("#lName",1);
		}
		
		var emailVal = $("#email").val();
		if(emailVal == 'Email Address' || emailVal == '') 
		{
			sB("#emailAd", 2);
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			sB("#emailAd", 2);
			hasError = true;
		}else{
			rB("#emailAd",2);
		}
		
		var phoneVal = $("#phone").val();
		if(phoneVal == 'Phone Number' || phoneVal == '') 
		{
			sB("#phoneN", 3);
			hasError = true;
		}else{
			rB("#phoneN",3);
		}
		
		var messageVal = $("#message").val();
		if(messageVal == 'Your Message Here' || messageVal == '') 
		{
			sB("#msg", 3);
			hasError = true;
		}else{
			rB("#msg",3);
		}
		
		if(hasError == false) 
		{
			$(this).hide();
			$("#sendEmail li.buttons").append('<div id="fLoader"><img src="http://www.kylekorverfoundation.com/blog/wp-content/themes/default/images/loader.gif" alt="Loading" id="loading" /></div>');
			
			$.post("sendemail.php",
   				{ firstN: fNameVal, lastN: lNameVal, email: emailVal, message: messageVal, phone: phoneVal, company: companyVal },
   					function(data){
						$("#contactForm").fadeOut("fast", function()									   
						{		
							$("#contactForm").fadeOut("fast");
							$("#thankyou").fadeIn("fast");
						});
   					}
				 );
		}				
		return false;
	});
});
