$(document).ready(function() {
			/*
			* Contacta con el anunciante
			* /
			$("#email_contacta").attr( "value", "email@email.com" );
			$("#txtTelefono").attr( "value", "Teléfono" );
			$("#nombre_contacta").attr( "value", "Nombre" );
			$("#mensaje_contacta").html( "Escribe aquí tu mensaje..." );
			
			
			$("#email_contacta").focus(function () {//
				 if ($(this).attr( "value") == "email@email.com") 
				 	$(this).attr( "value", "" );
			}).blur(function(){
				if (typeof $(this).attr( "value") == "undefined") 
					$(this).attr( "value", "email@email.com" );
			});
			
			$("#txtTelefono").focus(function () {//
				 if ($(this).attr( "value") == "Teléfono") 
				 	$(this).attr( "value", "" );
			}).blur(function(){
				if (typeof $(this).attr( "value") == "undefined") 
					$(this).attr( "value", "Teléfono" );
			});	
			
			$("#nombre_contacta").focus(function () {//
				 if ($(this).attr( "value") == "Nombre") 
				 	$(this).attr( "value", "" );
			}).blur(function(){
				if (typeof $(this).attr( "value") == "undefined") 
					$(this).attr( "value", "Nombre" );
			});
						
			$("#mensaje_contacta").focus(function () {				
				 if ($(this).html() == "Escribe aquí tu mensaje...") 
				 	$(this).html( "" );
			}).blur(function(){
				if ( $(this).html() == "")  $(this).html( "Escribe aquí tu mensaje..." );
			});			
			/ *
			* Enviar a un amigo
			* /
			$("#txtAmigos").attr( "value", "email@email.com" );
			$("#txtEmail").attr( "value", "E-mail" );
			$("#txtNombre").attr( "value", "Nombre" );
			$("#txtMensaje").html( "Escribe aquí tu mensaje..." );		
			
			$("#txtAmigos").focus(function () {//
				 if ($(this).attr( "value") == "email@email.com") 
				 	$(this).attr( "value", "" );
			}).blur(function(){
				if (typeof $(this).attr( "value") == "undefined") 
					$(this).attr( "value", "email@email.com" );
			});
			
			$("#txtEmail").focus(function () {//
				 if ($(this).attr( "value") == "E-mail") 
				 	$(this).attr( "value", "" );
			}).blur(function(){
				if (typeof $(this).attr( "value") == "undefined") 
					$(this).attr( "value", "E-mail" );
			});	
			
			$("#txtNombre").focus(function () {//
				 if ($(this).attr( "value") == "Nombre") 
				 	$(this).attr( "value", "" );
			}).blur(function(){
				if (typeof $(this).attr( "value") == "undefined") 
					$(this).attr( "value", "Nombre" );
			});
						
			$("#txtMensaje").focus(function () {				
				 if ($(this).html() == "Escribe aquí tu mensaje...") 
				 	$(this).html( "" );
			}).blur(function(){
				if ( $(this).html() == "")  $(this).html( "Escribe aquí tu mensaje..." );
			});				
			/ *
			* Error anuncio
			
			$("#txtMensajeError").html( "Describe el error" );	
			$("#txtMensajeError").focus(function () {				
				 if ($(this).html() == "Describe el error") 
				 	$(this).html( "" );
			}).blur(function(){
				if ( $(this).html() == "")  $(this).html( "Describe el error" );
			});
			*/				
});//ready

function enviaError(){
	if ($("#error").val() == null || $("#error").val() == '')	{
		alert('Por favor, selecciona el motivo del error en el anuncio');
		return false;
	}
	if ($("#error").val() == 7 &&  ($("#txtMensajeError").val() == '' || $("#txtMensajeError").val() == 'Describe el error'	))	{
		alert('Por favor, escribe el motivo del error en este anuncio');
		return false;
	}	
	/*
	* Peticion
	*/	
	$.ajax({
			type: "POST",
			dataType: "json",
			data: "error=" + $("#error").val() + "&descripcion=" + $("#txtMensajeError").val() + "&" +  $("#frmError input[@type=hidden]").serialize(),
			url: "/ajax/ficha/envia.error.php" ,     
			success: function(data) {
				if(data.error == 0){
					alert(data.datos);
				}else{
					alert(data.error);
				}
			},
			error: function (xhr, desc, exceptionobj) {
				alert(desc);
			}

		});	
	
	
	
	
}

function enviaAmigo(){
	if ($("#txtNombre").val() == '' || $("#txtNombre").val() == 'Nombre' )	{
		alert('Por favor, rellena tu nombre');
		$("#txtNombre").focus();
		return false;
	}
	if ($("#txtEmail").val() =='' || $("#txtEmail").val() == 'E-mail' 	)	{
		alert('Por favor, rellena tu e-mail');
		$("#txtEmail").focus();
		return false;
	}
	if(!validarEmail($("#txtEmail").val())){
		alert('El e-mail que escribiste no es correcto');
		$("#txtEmail").focus();
		return false;
	}		
	if ($("#txtAmigos").val() == ''  || $("#txtAmigos").val() == 'email@email.com' )		{
		alert('Por favor, rellena los emails de destino');
		$("#txtAmigos").focus();
		return false;
	}
	if($("#txtAmigos").val().indexOf(",") != -1){//sdfsdfsd@dfsdfs.com
		emailStr = $("#txtAmigos").val().split(",");		
		for(i = 0; i < emailStr.length; i++){
			emailStr[i] = emailStr[i].replace(/^\s*|\s*$/g,"");
			if(!validarEmail(emailStr[i])){
				alert('El e-mail '+emailStr[i]+' no es correcto');
				$("#txtAmigos").focus();
				return false;
			}					
		}
	}else{
		if(!validarEmail($("#txtAmigos").val())){
			alert('El e-mail que escribiste no es correcto');
			$("#txtAmigos").focus();
			return false;
		}		
	}
	
	/*
	* Peticion
	*/	
	$.ajax({
			type: "POST",
			dataType: "json",
			data: $("#frmEnvioAmigo input[@type=text], #frmEnvioAmigo textarea, #frmEnvioAmigo input[@type=hidden] , #frmEnvioAmigo input[@type=checkbox] ").serialize()  ,
			url: "/ajax/ficha/envia.amigo.php" ,     
			success: function(data) {
				if(data.error == 0){
					alert(data.datos);
				}else{
					alert(data.error);
				}
			},
			error: function (xhr, desc, exceptionobj) {
				alert(desc);
			}

		});
}


function confirmaFormContacta(empresa){

	if (empresa == '4053') {
		
		if ($("#politica_privacidad").is(":checked") == false)	{
			alert('Es necesario aceptar la política de privacidad de Metrovacesa');
			$("#email_contacta").focus();
			return false;
		}	
		
		if ($("#cp_contacta").val() != '') {
			jcp = $("#cp_contacta").attr('value');
			if(jcp.length < 4 || jcp.length > 5) { 
				alert('El código postal no es correcto');
				$("#cp_contacta").focus();
				return false; 
			} else {
				 valor = parseInt(jcp);
				 if (isNaN(valor)) { 
					alert('El código postal no es correcto');
					$("#cp_contacta").focus();
					return false;
				 }
			}
		}
		else {
			alert('Por favor, indica tu código postal para que el anunciante se pueda poner en contacto contigo');
			$("#cp_contacta").focus();
			return false;
		}

		if ( ($("#email_contacta").val() == '') || ($("#email_contacta").val() == 'email@email.com') )	{
			alert('Por favor, indica tu email para que el anunciante se pueda poner en contacto contigo');
			$("#email_contacta").focus();
			return false;
		}
		
		if ( ($("#txtTelefono").val() == '') || ($("#txtTelefono").val() == 'Teléfono') ) {
			alert('Por favor, indica teléfono para que el anunciante se pueda poner en contacto contigo');
			$("#txtTelefono").focus();
			return false;
		}
		
		
	} else {			
		if (	($("#email_contacta").val() == '' && $("#txtTelefono").val() == '')	 || ($("#email_contacta").val() == 'email@email.com' && $("#txtTelefono").val() == 'Teléfono')		)	{
			alert('Por favor, indica tu email o teléfono para que el anunciante se pueda poner en contacto contigo');
			$("#email_contacta").focus();
			return false;
		}	
	}
	
	if ($("#nombre_contacta").val() == '' || $("#nombre_contacta").val() == 'Nombre')	{
		alert('Por favor, rellena tu nombre');
		$("#nombre_contacta").focus();
		return false;
	}
	if ($("#txtTelefono").val() != '' &&  $("#txtTelefono").val() != 'Teléfono')	{
		tlf = $("#txtTelefono").val();
		if(tlf.length < 9){	
			alert('El telefono no es correcto');
			$("#txtTelefono").focus();
			return false;
		}
	}	
	
	if ($("#promos_contacta").is(":checked") == true)	{
		if ($("#email_contacta").val() == '')		{
			alert('Para poder recibir información debes escribir tu e-mail');
			$("#email_contacta").focus();
			return false;
		}
	}	
	if($("#email_contacta").val() != "email@email.com" && $("#email_contacta").val() != ''  ){
		if(!validarEmail($("#email_contacta").val()) ){
			alert('El campo "E-mail" no es correcto');
			$("#email_contacta").focus();
			return false;
		}	
	}
	txtComentarioContacta = $("#mensaje_contacta").val();
	if(txtComentarioContacta.length > 1200){
		alert('Escribiste un mensaje demasiado largo.\nEl máximo número de caracteres es de 1200');
		$("#mensaje_contacta").focus();
		return false;		
	}
	
	$("#btnContactar").removeClass("boton_S");
	$("#btnContactar").css("float", "right");
	$("#btnContactar").html( '<img src="/recursos/imgs/loading.gif" style=" border:0px;"/>');	
	
		
	//return false;
}

//Función para abrir un pop-up
function FNpopup(p_url,p_id,p_ancho,p_alto,p_carac){
   
    car="width="+p_ancho+",height="+p_alto+",left="+((screen.width/2)-(parseInt(p_ancho)/2))+",top="+((screen.height/2)-(parseInt(p_alto)/2))+","+p_carac
	window.open(p_url,p_id,car)
}
