// Contact Form
$(document).ready(function(){
	
	/*----------------------------------------
		Variables
	-----------------------------------------*/
	var contactForm  = $("#contactForm");
	var sendComplete = $("#sendComplete");
	var sendBtn = $("#sendBtn");
	var name	= $("#name");
	var email	= $("#email");
	var msg		= $("#msg");
	
	var sending	 = true;
	var validate = new Array(false, false, false);
	
	
	/*----------------------------------------
		Reset Function
	-----------------------------------------*/
	sendComplete.hide();
	
	/*----------------------------------------
		Clear and Return Functions
	-----------------------------------------*/
	//Clear Input
	function clearInput(target, value){
		target.css({"color" : "#ea5226"});	
		var val = target.val();
		if(val == value){
			target.val("");	
		}
	}
	
	//Return Input
	function returnInput(target, value){
		var val = target.val();
		if(val == ""){
			target.val(value);
			target.css({"color" : "#d8d8d8"});
		}	
	}
	
	
	/*----------------------------------------
		Focus and Blue
	-----------------------------------------*/
	name.focus(function(){clearInput(name, "Name");});
	name.blur(function(){returnInput(name, "Name");});
	email.focus(function(){clearInput(email, "Email");});
	email.blur(function(){returnInput(email, "Email");});
	msg.focus(function(){clearInput(msg, "Message");});
	msg.blur(function(){returnInput(msg, "Message");});
	
	/*----------------------------------------
		Validate Functions
	-----------------------------------------*/
	//General
	function validateContact(target, defaultVal, arrayIndex, email){
		var targetVal = target.val();
		if(targetVal == "" || targetVal == defaultVal){
			if(email == true){
				validateEmail(target, targetVal, arrayIndex);
			}else{		
				validate[arrayIndex] = false;
				target.css({"border" : "1px solid #ea5226"});
			}

		}else{
			validate[arrayIndex] = true;	
			target.css({"border" : "1px solid #d8d8d8"});
		}
		
	}
	
	//Email
	function validateEmail(target, value, arrayIndex){
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if(!emailReg.test(value)){
			validate[arrayIndex] = false;
			target.css({"border" : "1px solid #ea5226"});		
		}else{
			validate[arrayIndex] = true;	
			target.css({"border" : "1px solid #d8d8d8"});
		}
	}
	
	/*----------------------------------------
		Send Button
	-----------------------------------------*/
	sendBtn.click(function(){
		validateContact(name, "Name", 0, false);
		validateContact(email, "Email", 1, true);
		validateContact(msg, "Message", 2, false);
		
		for(var i = 0; i < validate.length; i++){
			if(validate[i] == false){
				sending = false;	
			}
		}
		
		if(sending == true){
			var nameVal 	= name.val();
			var emailVal 	= email.val();
			var msgVal		= msg.val();
			
			$.post("contactSend.php", { contactSubmit: true, name: nameVal, email: emailVal, msg: msgVal }, function(data){ sendContact(data); });
		}
		
		return false;
		
	});
	
	function sendContact(d){
		if(d == 1){
			sendComplete.fadeIn(300);
		}
	}
	
	
});