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

  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
		$('#YourName').val("");
		$('#FriendName').val("");
		$('#emailF').val("");
		
		//set fields
		$('#YourName').example(function() { return $(this).attr('title')});
		$('#FriendName').example(function() { return $(this).attr('title')});
		$('#emailF').example(function() { return $(this).attr('title')});
		
						   
	var startColor = "#2a2a2a";
	var errorColor = "#00a4d3";
	var animateSpeed = 200;
	
	
	var nrf=3; // 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(){
		
		//required fields
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var fNameVal = $("#YourName").val();
		if(fNameVal == 'Your Name' || fNameVal == '') 
		{
			sB("#YName", 0);
			hasError = true;
		}else{
			rB("#YName",0);
		}
		
		var lNameVal = $("#FriendName").val();
		if(lNameVal == 'Friends Name' || lNameVal == '') 
		{
			sB("#FName", 1);
			hasError = true;
		}else{
			rB("#FName",1);
		}
		
		var emailVal = $("#emailF").val();
		if(emailVal == 'Friends Email' || emailVal == '') 
		{
			sB("#emailAd", 2);
			hasError = true;
		} else if(!emailReg.test(emailVal)) {
			sB("#emailAd", 2);
			hasError = true;
		}else{
			rB("#emailAd",2);
		}
		
		
		if(hasError == false) 
		{
			$(this).hide();
			$("#sendEmail li.buttons").append('<div id="fLoader"><img src="../images/loader.gif" alt="Loading" id="loading" /></div>');
			
			$.post("sendemail.php",
   				{ YourName: fNameVal, FriendName: lNameVal, emailF: emailVal },
   					function(data){
						$("#contactForm").fadeOut("fast", function() 
						{				
							$("#thankyou").fadeIn("fast");										
						});
   					}
				 );
		}				
		return false;
	});
});
