/*********************************************************************************
 **                            PUBLICO                                          **
 *********************************************************************************/

$(document).ready(function(){
	$('#ddlOperacion').live('change', function(){
		var valorSelect=$(this).val();
		if(valorSelect==0){
			$('#labelPrecio').html('Precio:');
			
		}else if(valorSelect==1){
			$('#labelPrecio').html('Precio de alquiler:');
			
		}else{
			$('#labelPrecio').html('Precio de venta:');
		}
	});
	$('#ddlOperacion').live('keyup',function(){
		var valorSelect=$(this).val();
		if(valorSelect==0){
			$('#labelPrecio').html('Precio:');
			
		}else if(valorSelect==1){
			$('#labelPrecio').html('Precio de alquiler:');
			
		}else{
			$('#labelPrecio').html('Precio de venta:');
		}
	});
	
	$('input[name$="tipo_propiedad"]').click(function(){
		
		if($(this).val()=='1' || $(this).val()=='2'){
			$('#paxBox').removeClass('hidden');
                        $('#filtrosExtra').removeClass('hidden');
			
		}else if($(this).val()=='5'){
                    $('#filtrosExtra').removeClass('hidden');
                }else{
			$('#paxBox').addClass('hidden');
                        $('#filtrosExtra').addClass('hidden');
		}
		
	});
	
	
	
	
});
function postular_propiedad_get() {
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            type: 'POST',
            url: WEB_PATH + 'app/ajax/dialog_postular_propiedad_get.php',
            success: function(response) {
                document.sending = false;
                $('#contenido').html(response);
            }
        });
    }
}

function get_captcha() {
    $('#captcha').attr("src", WEB_PATH + 'app/captcha.php?'+Math.random());
}

function validar_propiedad(){
    $("#lblCaptcha").html('Ingrese el c�digo de seguridad:');
    if ($("#ddlTipo").val() == 0) {
        $("#mensajeError").html('Debe indicar el tipo de propiedad');
        return false;
    }
    if ($("#txtNombre").val().trim() == "") {
        $("#mensajeError").html('Debe ingresar el nombre de la propiedad');
        return false;
    }
    if ($("#ddlOperacion").val() == 0) {
        $("#mensajeError").html('Debe ingresar la operaci�n');
        return false;
    }
    if ($("#txtUbicacion").val().trim() == "") {
        $("#mensajeError").html('Debe ingresar la ubicaci�n');
        return false;
    }
    if ($("#txtPrecioVenta").val().trim() == "") {
        $("#mensajeError").html('Debe ingresar el precio');
        return false;
    }
    if ($("#txtContacto").val().trim() == "") {
        $("#mensajeError").html('Debe ingresar su nombre');
        return false;
    }
    if ($("#txtTelefono").val().trim() == "") {
        $("#mensajeError").html('Debe ingresar su tel�fono');
        return false;
    }
    if ($("#txteMail").val().trim() == "" || !is_email($("#txteMail").val().trim())) {
        $("#mensajeError").html('Debe ingresar un email v�lido');
        return false;
    }
    if ($("#captchatext").val().trim() == "") {
        $("#lblCaptcha").html('Verifique el c�digo de seguridad:');
        return false;
    }
    return true;
}

function enviar_postular_propiedad() {
    $("#mensajeError").html('Enviando...');
    if (validar_propiedad()) {
        if (!document.sending) {
            document.sending = true;
            $.ajax({
                data: {
                    tipo : $("#ddlTipo").val(),
                    nombre : $("#txtNombre").val(),
                    ubicacion : $("#txtUbicacion").val(),
                    operacion : $("#ddlOperacion").val(),
                    precio : $("#txtPrecioVenta").val(),
                    descripcion : $("#txtDescripcion").val(),
                    contacto : $("#txtContacto").val(),
                    telefono : $("#txtTelefono").val(),
                    localidad : $("#txtLocalidad").val(),
                    pais : $("#txtPais").val(),
                    email : $("#txteMail").val(),
                    comentario : $("#txtComentarios").val(),
                    captchatext : $("#captchatext").val(),
                    padron : $("#txtPadron").val(),
                    manzana : $("#txtManzana").val(),
                    txttipo : $("#ddlTipo option:selected").text(),
                    txtoperacion : $("#ddlOperacion option:selected").text()
                },
                type: 'POST',
                url: WEB_PATH + 'app/ajax/add_propiedad_postulada.php',
                success: function(response) {
                    document.sending = false;
                    if (response == "-1") {
                        $("#mensajeError").html('Ocurri� un error guardando su propiedad, por favor intente nuevamente.');
                    }else if (response == "0") {
                        $("#lblCaptcha").html('Verifique el c�digo de seguridad:');
                        $("#mensajeError").html('');
                    }else if (!isNaN(response)){
                        $('#hid_id_propiedad').val(response);
                        $("#frmFoto").submit();
                        $("#mensajeError").html('Datos guardados correctamente');
                    }
    
                }
            });
        }
    }

}

function borar_form(){
    $("#ddlTipo").val('1');
    $("#txtNombre").val('');
    $("#ddlOperacion").val('0');
    $("#txtUbicacion").val('');
    $("#txtPrecioVenta").val('');
    $("#txtContacto").val('');
    $("#txtDescripcion").val('');
    $("#txtPadron").val('');
    $("#txtManzana").val('');
    $("#hid_id_propiedad").val('');
    $("#fileFoto").val('');
    $("#txtContacto").val('');
    $("#txtTelefono").val('');
    $("#txtLocalidad").val('');
    $("#txtPais").val('');
    $("#txtComentarios").val('');
    $("#txtTelefono").val('');
    $("#lblCaptcha").val("Ingrese el c�digo de seguridad:");
    $("#txteMail").val('');
    $("#captchatext").val('');
}

/*********************************************************************************
 **                             ADMIN                                           **
 *********************************************************************************/

function buscar_propiedades_enviadas(pagina){
    contenido_div_error_admin(true);
    if (!document.sending) {
        document.sending = true;
        $.ajax({
            data: {
                tipo : 1,
                pagina : pagina
            },
            type: 'POST',
            url: WEB_PATH + '_admin/ajax/propiedades_enviadas_get.php',
            success: function(response) {
                contenido_div_error_admin(false);
                document.sending = false;
                $('#contenido').html(response);
            }
        });
    }
}

function eliminar_propiedad_postulada(id_propiedad){
    if (confirm("�Desea eliminar esta propiedad?")) {
        if (!document.sending) {
            document.sending = true;
            $.ajax({
                data: {
                    id_propiedad : id_propiedad
                },
                type: 'POST',
                url: WEB_PATH + '_admin/ajax/propiedad_postulada_del.php',
                success: function(response) {
                    document.sending = false;
                    if (response == 1) {
                        var divPropiedad = "#divEnviado" + id_propiedad;
                        $(divPropiedad).fadeOut(50, function () {
                            $(divPropiedad).remove();
                            buscar_propiedades_enviadas(-1);
                        });
                        
                    }
                }
            });
        }
    }
}
