function isFormOK() {
	errorCount = 0;
	if ($("input#fName").val() == "") {$("input#fName").addClass('formError'); errorCount++;} else {$("input#fName").removeClass('formError');}
	if ($("input#email").val() == "") {$("input#email").addClass('formError'); errorCount++;} else {$("input#email").removeClass('formError');}
	if ($("input#tName").val() == "") {$("input#tName").addClass('formError'); errorCount++;} else {$("input#tName").removeClass('formError');}
	if ($("input#temail").val() == "") {$("input#temail").addClass('formError'); errorCount++;} else {$("input#temail").removeClass('formError');}
	if ($("input#securityCode").val() == "") {$("input#securityCode").addClass('formError'); errorCount++;} else {$("input#securityCode").removeClass('formError');}
	return (errorCount == 0) ? true : false;
}

function submitForm() {
	fromName = $("input#fName").val();
	fromEmail = $("input#email").val();
	toName = $("input#tName").val();
	toEmail = $("input#temail").val();
	code = $("input#securityCode").val();
	locat = location.href ;
	
	$.post("javascript/ajax/sendToFriend.php",{ fromName: fromName, fromEmail: fromEmail, toName: toName, toEmail: toEmail, code: code, locat: locat}, function(data){
		$('#sendToContent').html(data);
		$('#sendToHead').remove();
	});
}

$(document).ready(function(){
	prettyFriend.init();
	$('#ErrorNotice').hide();
	$('#submitButton').click(function(event) {
 		if (isFormOK()) {
 			$('#ErrorNotice').hide();
 			submitForm();
 		} else {
 			$('#ErrorNotice').show();
 		}
 		event.preventDefault();
	});  
});
	prettyFriend = {
		options : {
			'animationSpeed' : 'fast'
		},
		init : function(){
			$("#sendTo").bind('click',function() {
				prettyFriend.open(this); return false;
			});
			$(window).scroll(function(){ prettyFriend.centerPicture(); });
			$(window).resize(function(){ prettyFriend.centerPicture(); prettyFriend.resizeOverlay(); })
		},
		open : function(caller) {
			prettyFriend.caller = caller;
			prettyFriend.buildOverlay(prettyFriend.isSet);
			prettyFriend.centerPicture();
		},
		
		centerPicture : function(){
			if($('#sendToForm').size() > 0){
				var scrollPos = prettyFriend.getScroll();
				$('#sendToForm').css({
					'top': ($(window).height()/2) + scrollPos['scrollTop'] - 30 - ($('#sendToForm').height()/2),
					'left': ($(window).width()/2) + scrollPos['scrollLeft'] - ($('#sendToForm').width()/2)
				});
			};
		},
		
		buildOverlay : function(){
			backgroundDiv = "<div class='prettyFriendOverlay'></div>";
 			$('body').append(backgroundDiv);
			$('div.prettyFriendOverlay').css('height',$(document).height());
			
			$('div.prettyFriendOverlay').bind('click',function(){
				prettyFriend.close();
			});
			$('.sendcloser').bind('click',function(){
				prettyFriend.close();
				return false;
			});
			$('div.prettyFriendOverlay').css('opacity',0);
			$('div.prettyFriendOverlay').fadeTo(prettyPhoto.options['animationSpeed'],0.80, function(){
				$('#sendToForm').css('opacity',0);
				$('#sendToForm').show();
				$('#sendToForm').fadeTo(prettyPhoto.options['animationSpeed'],1);
			});
		},
		resize : function(width,height){			
			// Get the container size, to resize the holder to the right dimensions
			contentHeight = 30;
			containerHeight = contentHeight + 40;
			containerWidth = 196;
			
			var newWidth = width;
			var newHeight = height;
			
			if((containerWidth + width) > $(window).width() || (containerHeight + height) > $(window).height()) {
				// Get the original geometry and calculate scales
				var xscale=(width+containerWidth + 100)/$(window).width();
				var yscale=(height+containerHeight + 100)/$(window).height();
			
				// Recalculate new size with default ratio
				if (yscale>xscale){
					newWidth = Math.round(width * (1/yscale));
					newHeight = Math.round(height * (1/yscale));
				} else {
					newWidth = Math.round(width * (1/xscale));
					newHeight = Math.round(height * (1/xscale));
				};
			};

			// Get the container size, to resize the holder to the right dimensions
			containerHeight += newHeight;
			contentHeight += newHeight;
			containerWidth += newWidth;

			return {
				width:newWidth,
				height:newHeight,
				containerHeight:containerHeight,
				containerWidth:containerWidth,
				contentHeight:contentHeight
			};
		},
		resizeOverlay : function() {
			$('div.prettyFriendOverlay').css({
				'height':$(document).height(),
				'width':$(window).width()
			});
		},
		getScroll : function(){
			scrollTop = window.pageYOffset || document.documentElement.scrollTop || 0;
			scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || 0;
			return {scrollTop:scrollTop,scrollLeft:scrollLeft};
		},
		close : function(){
			$('#sendToForm').fadeTo(prettyFriend.options['animationSpeed'],0,function(){
				$('div.prettyFriendOverlay').fadeTo(prettyFriend.options['animationSpeed'],0, function(){
					$('div.prettyFriendOverlay').remove();
					$('#sendToForm').hide();
				});
			});	
		}
	}