
function checkEmail(em){
	var regexp=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ //regular expression defining a 5 digit number
	if (em.search(regexp)==-1){return false;}else{return true;}
}


function showSendToFriend()
{
	$("#bttnDiscoverTasmania").css({display:"none"})
	$("#bttnSendToAFriend").css({display:"none"})
	$("#sideContent_default").css({display:"none"})
	
	$("#sideContent_sendToFriend").fadeIn(200);
	$("#sendToFriend").fadeIn(200);
	
	$("#bottomBody").append("<div id='sendToFriend_Send'><a href='#' title='SEND'>SEND</a></div>")
	$("#sendToFriend_Send").css({display:"none"})
	$("#sendToFriend_Send").fadeIn(200);
	
	$('#sendToFriend_Send a').click(function () {
		if (validate()) {
			$("#sendToFriend_Send").fadeOut(200);
			$("#sendToFriend").fadeOut(200);
			$("#sendToFriend_message").fadeIn(200);
			$("#sendToFriend_message").text("SENDING...");
//			alert("send page here - POTTIE DO YOUR MAGIC");

			var parametri1 = $("input").serialize();
			var parametri2 = $("textarea").serialize();
			var parametri = parametri1 +  '&' + parametri2;
//							data: $('#sideContent_sendToFriend #formSendToFriend').serialize() + '&action=send',
			
						$.ajax({
							url: '/s2f.asp',
							data: parametri,
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function (xhr) {
							//when its done with no error print this:
						    var response = xhr.responseText;
							if(response=='sent'){
								$("#sendToFriend_message").text("Thank you, your message has been sent.")
							}else{
								$("#sendToFriend_message").html("There was a problem with delivery.<br>Please try again later.<br>"+response)
							}
								setTimeout(function() { closeSendToFriend(); }, 2000);
							}
							//error: send2friend.error
						});			
			
			
			
			
			

			
		}
	});	
}
function closeSendToFriend()
{
	$("#sideContent_sendToFriend").css({display:"none"})
	$("#sendToFriend_Send").remove();
	$("#sendToFriend_message").fadeOut(200);
	$( "form" )[ 0 ].reset();
	$("#sideContent_default").fadeIn(200);
	$("#bttnDiscoverTasmania").fadeIn(200);
	$("#bttnSendToAFriend").fadeIn(200);
	
}	

function validate()
{
	var message = false;
	

	//your name
	if(!$("#sideContent_sendToFriend #yourName").val())
	{
		message = true;
		$("#sideContent_sendToFriend #yourName").css("background-color","#d90a08");
		$("#textYourName").animate({width: 320});
	}else{
		$("#sideContent_sendToFriend #yourName").css("background-color","#ccc6a1");
		$("#textYourName").animate({width: 81});
	}

	//your email
	if(!checkEmail($("#sideContent_sendToFriend #yourEmail").val()))
	{
		message = true;
		$("#sideContent_sendToFriend #yourEmail").css("background-color","#d90a08");
		$("#textYourEmail").animate({width: 320});
	}else{
		$("#sideContent_sendToFriend #yourEmail").css("background-color","#ccc6a1");
		$("#textYourEmail").animate({width: 81});
	}
	
	//friends name
	if(!$("#sideContent_sendToFriend #friendsName").val())
	{
		message = true;
		$("#sideContent_sendToFriend #friendsName").css("background-color","#d90a08");
		$("#textFriendsName").animate({width: 320});
	}else{
		$("#sideContent_sendToFriend #friendsName").css("background-color","#ccc6a1");
		$("#textFriendsName").animate({width: 100});
	}
	
	//friends email
	if(!checkEmail($("#sideContent_sendToFriend #friendsEmail").val()))
	{
		message = true;
		$("#sideContent_sendToFriend #friendsEmail").css("background-color","#d90a08");
		$("#textFriendsEmail").animate({width: 320});
	}else{
		$("#sideContent_sendToFriend #friendsEmail").css("background-color","#ccc6a1");
		$("#textFriendsEmail").animate({width: 100});
	}
	

	if (message) {
		return false;
	}else{
		return true;
	}
}

$(document).ready(function () {

	$('#bttnSendToAFriend a').click(function () {
		showSendToFriend();
	});
	
	$('#sendToFriendClose a').click(function () {
		closeSendToFriend();
	});

});	
