function load_poblaciones(que)
{
	$("#poblacio").empty();
	$.get("poblaciones.php", {provincia: que}, function(data){
		try{
		$("elemento", data).each(function (j) {
			$("#poblacio").append("<option value='" + $(this).attr("item") + "'>" + $(this).attr("valor") + "</option>");
		} );
		}
		catch(ex){alert(ex);}
	});
}

function load_barrios(que)
{
	$("#barri").empty();
	$.get("barrios.php", {poblacio: que}, function(data){
		try{
		$("elemento", data).each(function (j) {
			$("#barri").append("<option value='" + $(this).attr("item") + "'>" + $(this).attr("valor") + "</option>");
		} );
		}
		catch(ex){alert(ex);}
	});
}

/*$(function() {
		$("#slider-range").slider({
			range: true,
			min: 0,
			max: 1000,
			values: [0, 1000],
			slide: function(event, ui) {
				$("#m2").html(ui.values[0] + ' - ' + ui.values[1]);
			}

		});
		$("#m2_val").val('$' + $("#slider-range").slider("values", 0) + ' - $' + $("#slider-range").slider("values", 1));
	});
*/

function validar()
{
	$("#mensaje").removeClass();
	$("#mensaje").addClass('msg_check');
	$("#mensaje").html("Comprovant dades...");

	if ($("#nom").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Nom</strong> és obligatori");
		document.frm_api.nom.focus();
		return 0;
	}
	
	if ($("#cognoms").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Cognoms</strong> és obligatori");
		document.frm_api.cognoms.focus();
		return 0;
	}
	
	if ($("#email").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Email</strong> és obligatori");
		document.frm_api.email.focus();
		return 0;
	}
	
	if ($("#tel").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Telèfon</strong> és obligatori");
		document.frm_api.tel.focus();
		return 0;
	}
	
	if (document.frm_api.politica.checked == false){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("Has de aceptar la <strong>Politica de Privacidad</strong> para continuar");
		return 0;
	}
	
	document.frm_api.submit();
}

function validar_paso1()
{
	$("#mensaje").removeClass();
	$("#mensaje").addClass('msg_check');
	$("#mensaje").html("Comprovant dades...");

	if ($("#nom").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Nom</strong> és obligatori");
		document.frm_venta.nom.focus();
		return 0;
	}
	
	if ($("#cognoms").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Cognoms</strong> és obligatori");
		document.frm_venta.cognoms.focus();
		return 0;
	}
	
	if ($("#tel").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>núm. de telèfon mòbil</strong> és obligatori");
		document.frm_venta.tel.focus();
		return 0;
	}

	if ($("#provincia").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Provincia</strong> és obligatori");
		document.frm_venta.provincia.focus();
		return 0;
	}

	if ($("#tipus").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Tipus de vivenda</strong> és obligatori");
		document.frm_venta.tipus.focus();
		return 0;
	}

	if (document.frm_venta.politica.checked == false){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("Has de aceptar la <strong>Politica de Privacidad</strong> para continuar");
		return 0;
	}
	
	document.frm_venta.submit();
	
}

function cargar_lista(opt)
{
	var lista = '';
	var lista1 = $('#lista1'), lista2 = $('#lista2');
	
	$("#lista1").empty();
	$.get("detalles.php", {caracter: opt}, function(data){
		try{
		$("elemento", data).each(function (j) {
			$("#lista1").append("<li>" + $(this).attr("valor") + "</li>");
		} );
		}
		catch(ex){alert(ex);}
		dragdrop();
	});
	
}

function dragdrop()
{
	var lista1 = $('#lista1'), lista2 = $('#lista2'), lista3 = $('#lista3');
	 // para ahorrar un poco de espacio voy a definir a las listas como variables
	// Activo draggable a la primera lista
	$('li',lista1).draggable({
		revert: 'invalid',
		helper: 'clone',
		cursor: 'move'
	});
	   // asigno droppable en la lista1 hacia la lista2
	lista3.droppable({
		accept: '#lista2 li',
		drop: function(ev, ui) {
					   // Al hacer drop se borra el elemento
			deleteLista2(ui.draggable);
		}
	});
	// Asigno draggable a la lista2
	$('li',lista2).draggable({
		revert: 'invalid',
		helper: 'clone',
		cursor: 'move'
	});
	   // Genero droppable para la segunda lista
	lista2.droppable({
		accept: '#lista1 > li',
		drop: function(ev, ui) {
			deleteLista1(ui.draggable);
		}
	});
}

// Genero el borrado de items con el evento drop
function deleteLista1($item) {
	var lista2 = $('#lista2');
	$item.fadeOut(function() {
				 // Agrego el item dropeado y lo hago aparecer
		$($item).appendTo(lista2).fadeIn();
	});
	$item.fadeIn();
}

function deleteLista2($item) {
	var papelera = $('#lista3');
	$item.fadeOut(function() {
					/// Agrego el item dropeado y lo hago aparecer
		$item.appendTo(papelera).fadeIn();
	});
}

function vaciar()
{
	$("#lista3").html('');
}

function email_ajax()
{
	var nom 			= $("#nom").val();
	var tel				= $("#tel").val();
	var email			= $("#email").val();
	var observaciones	= $("#observaciones").val();
	
	$("#mensaje").removeClass();
	$("#mensaje").addClass('msg_check');
	$("#mensaje").html("Comprovant dades...");

	if ($("#nom").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Nom</strong> és obligatori");
		document.frm_info_inmueble.nom.focus();
		return 0;
	}

	if ($("#tel").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>Telèfon</strong> és obligatori");
		document.frm_info_inmueble.tel.focus();
		return 0;
	}

	if ($("#email").val().length == 0){
		$("#mensaje").removeClass();
		$("#mensaje").addClass('msg_mal');
		$("#mensaje").html("El camp <strong>E-mail</strong> és obligatori");
		document.frm_info_inmueble.email.focus();
		return 0;
	}

	var param = 'nom='+nom+'&tel='+tel+'&email='+email+'&observaciones='+observaciones;
	$("#mensaje").html("Enviant l'e-mail al propietario");
	jQuery.ajax({
		type: 'GET',
		url: 'info_piso.php',
		data: param,
		success: function(data){
			$("#mensaje").removeClass();
			$("#mensaje").addClass('msg_ok');			
			$("#mensaje").html("E-mail enviado correctamente, en breve se pondran en contacto con usted.");
		}
	});
}

$(function() {		
	// initialize overlay trigger
	$("#a_campus").overlay({expose: "#000"});
});