$().ready(function() {
				   $("#errorchapata").hide();
	
	$("#formContactar").validate({
	  rules: {
	    fromnom: {
	      required: true
	    },
		frommail: {
	      required: true,
		  email: true
	    },
		message: {
	      required: true
	    },
		word: {
	      required: true
	    }
	  }
	});
		
		$("#enviamens").click( function(){	
				if($("#formContactar").valid()){
						$descripcio =$("#message").val();
						//cambia los saltos de línea por brs
						$descripcio = $descripcio.replace(/\n/g, "<br />");
	  					$descripcio = $descripcio.replace(/\n\n+/g, '<br /><br />');
						//llamada ajax a ajax_contacta.php
			         	$.ajax({
						url: "ajax_contacta.php",
						  data: "message="+$descripcio+"&fromnom="+$("#fromnom").val()+"&frommail="+$("#frommail").val()+"&fromtelefon="+$("#fromtelefon").val()+"&title="+$("#title").val()+"&listingid="+$("#listingid").val()+"&word="+$("#word").val()+"&userid="+$("#userid").val(),
						   success: function(msg){
						   	if(msg!=""){
						   		$(".loading").hide();
						   		if(msg==1){
									//si el capchka està mal
						   			$("#errorchapata").show();
						   		}else{
						     			$("#fenviar").html(msg);
						     		}
						     	}
						  }
					});
				}
			});
});
function new_freecap()
{
	// loads new freecap image
	if(document.getElementById)
	{
		// extract image name from image source (i.e. cut off ?randomness)
		thesrc = document.getElementById("freecap").src;
		thesrc = thesrc.substring(0,thesrc.lastIndexOf(".")+4);
		// add ?(random) to prevent caching
		document.getElementById("freecap").src = thesrc+"?"+Math.round(Math.random()*100000);
	} else {
		alert("Sorry, cannot autoreload freecap image\nSubmit the form and a new freecap will be loaded");
	}
}
