var url = rewriteBase + "/ajax/tell-to-friend";
$(document).ready(function(){				
	$("#resultWindow").hide();
	$("#resultWindow").addClass('success-string');
	$("#tell-to-friend-link").click(function() {					
			$("#resultWindow").hide();
			clearErrorSpans()
			$("#tell-to-friend-box").slideToggle("slow");						
			return false;				
	});

	$("form#tellToFriendForm").submit(function(){

		clearErrorSpans();

		var hasErrors = false;	

		var sender_name = $.trim($("#vase-jmeno").val());
		if(sender_name == '') {
			$("form#tellToFriendForm span[@class=error-string]:eq(0)").text('Prosím zadejte Vaše jméno');
			hasErrors = true;
		}

		var sender_email = $.trim($("#vas-email").val());
		if(sender_email == '') {
			$("form#tellToFriendForm span[@class=error-string]:eq(1)").text('Prosím zadejte e-mail');
			hasErrors = true;
		} else if (!validateEmail(sender_email)) {
			$("form#tellToFriendForm span[@class=error-string]:eq(1)").text('Prosím zadejte validní e-mail');
			hasErrors = true;
		}
		
		var recipient_email = $.trim($("#email-prijemce").val());
		if(recipient_email == '') {
			$("form#tellToFriendForm span[@class=error-string]:eq(2)").text('Prosím zadejte e-mail');
			hasErrors = true;
		} else if (!validateEmail(recipient_email)) {
			$("form#tellToFriendForm span[@class=error-string]:eq(2)").text('Prosím zadejte validní e-mail');
			hasErrors = true;
		}
		
		/*
		var message = $.trim($("#text-zpravy").val());
		if(message == '') {
			$("form#tellToFriendForm span[@class=error-string]:eq(3)").text('Prosím zadejte text zprávy');
			hasErrors = true;
		}
		*/
		
		if(!hasErrors) {
			$("#loading").show();
			$.post(url,
				{
					sender_email: sender_email, 
					sender_name: sender_name,
					recipient_email: recipient_email,
					categoryItemId: categoryItemId
				}, function(xml) {
					showResult(xml);
				});
		}
		
		return false;
	});									
});

function clearErrorSpans() {
	$("span[@class=error-string]", "form#tellToFriendForm").each(function(i) {
		this.innerHTML = '';
	});	
}
				
function showResult(xml) {
	$("#loading").hide();
	
	if($("status",xml).text() == "1") {
		$("#resultWindow").show();					
		$("#resultWindow").html($("message",xml).text());							
		$("#messagewindow").empty();									
		$("input[@type=text]", "form#tellToFriendForm").each(function(i) {
			this.value = '';
		});	
		$("#tell-to-friend-box").slideToggle("slow");
	} else {
		$("errors/error",xml).each(function(i){
		   	var span = $("form#tellToFriendForm span[@class=error-string]").get(this.getAttribute('propertyIndex'));
		   	var otherErrors = '';

		   	if(span != null) {
		   		var newEl = document.createTextNode(this.firstChild.nodeValue);
		   		if(span.firstChild == null) span.appendChild(newEl);
		   		else span.replaceChild(newEl, pan.firstChild);
		   	} else {
		   		otherErrors += this.firstChild.nodeValue;
		   	}
		   	
		   	$("#messagewindow").html(otherErrors);
		});
	}
}
