// Submit To Friend Form

var friendform = "";	// used to save form before it's changed via ajax so we can bring it back
						// when somone clicks the submit button again.
	
// Colorbox plugin options
var friendInlineObj =  {
		transition: "none",
		speed:0,
		opacity:0.6,
		width:"480",
		height:"420",
		inline:true, 
		href:"#submit-friend-form",
		close: "Close",
		scrollbars:false
	};
	
// setup the forms
$(document).ready(function(){
	friendform = ieInnerHTML(document.getElementById('submit-friend-form'));
	$(".submit-friend-inline").colorbox(
		friendInlineObj
	);
});	

$().bind('cbox_open', function(){
        drawSubmitFriendForm();
});	

function drawSubmitFriendForm() {
	$("#submit-friend-form").empty().append(friendform);
	$(".submit-friend-inline").colorbox(
		friendInlineObj
	);
}

// when form is loaded, we must attach our click
// events on our Labels so we can make them dissapear 
// and focus on the desired input.
$().bind('cbox_complete', function(){

	$("#lYourName").click(function() {
		$(this).hide();
		$("#yourName").focus();
	});
	$("#yourName").focus(function() {
		$("#lYourName").hide();
	});
	$("#yourName").blur(function() {
		if($("#yourName").val() == "") { $("#lYourName").show(); }
	});
	$("#lYourEmail").click(function() {
		$(this).hide();
		$("#yourEmail").focus();
	});
	$("#yourEmail").focus(function() {
		$("#lYourEmail").hide();
	});
	$("#yourEmail").blur(function() {
		if($("#yourEmail").val() == "") { $("#lYourEmail").show(); }
	});
	$("#lFriendName").click(function() {
		$(this).hide();
		$("#friendName").focus();
	});
	$("#friendName").focus(function() {
		$("#lFriendName").hide();
	});
	$("#friendName").blur(function() {
		if($("#friendName").val() == "") { $("#lFriendName").show(); }
	});
	$("#lFriendEmail").click(function() {
		$(this).hide();
		$("#friendEmail").focus();
	});
	$("#friendEmail").focus(function() {
		$("#lFriendEmail").hide();
	});
	$("#friendEmail").blur(function() {
		if($("#friendEmail").val() == "") { $("#lFriendEmail").show(); }
	});
	$("#lMessage").click(function() {
		$(this).hide();
		$("#yourMessage").focus();
	});
	$("#yourMessage").focus(function() {
		$("#lMessage").hide();
	});
	$("#yourMessage").blur(function() {
		if($("#yourMessage").val() == "") { $("#lMessage").show(); }
	});	
	
});	

// Submit Friend
function submitFriend() {
	$(".errorMsg").empty();
	var yourName = $("#yourName").val();
	var yourEmail = $("#yourEmail").val();
	var friendName = $("#friendName").val();
	var friendEmail = $("#friendEmail").val();
	var yourMessage = $("#yourMessage").val();
	
	var hasError = false;
	if(yourName == "") {hasError = true;}
	if(yourEmail == "" || !checkEmail(yourEmail)) { hasError = true; }	
	if(friendName == "") { hasError = true; }
	if(friendEmail == "" || !checkEmail(friendEmail)) { hasError = true; }	
	

	if(hasError) {
		$(".errorMsg").append("Please fill out the required fields");
		$(".errorMsg").show(200);

		return false;
	} else {
		
	$.post("/wp-content/themes/nobu/forms/send-friend.php", 
	{ yourName: yourName, yourEmail: yourEmail, friendName: friendName, friendEmail: friendEmail, yourMessage: yourMessage },
	function(data){
	 	$("#submit-friend-form").empty().append(data);
	
	  	// resize modal
		$(".submit-friend-inline").colorbox(
		{
			width:"480",
			height:"290",
			inline:true,
			href:"#submit-friend-form",
			scrollbars:false,
			open:true
		}
		);
	  
	  
	});	
	
	//
	return false;
	}
	
}