// JavaScript Document
function addBookmark(title,url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url,"");
    } else if( document.all ) {
        window.external.AddFavorite( url, title);
    } else if( window.opera && window.print ) {
        return true;
    }
}
function capturaComida() {
	var todos = [];
     jQuery('div#tipoComida :checked').each(function() {
	   todos.push(jQuery(this).val());
     });
	 if (jQuery(todos).size() == 0)
		 jQuery('input#tipoComida').val(0);
	 else
	 	jQuery('input#tipoComida').val(todos.sort().join("-"));
}
function capturaRestaurante() {
	var todos = [];
     jQuery('div#tipoRestaurante :checked').each(function() {
	   todos.push(jQuery(this).val());
     });
	 if (jQuery(todos).size() == 0)
		 jQuery('input#tipoRestaurante').val(0);
	 else
	 	jQuery('input#tipoRestaurante').val(todos.sort().join("-"));
}
function showPopup(){
        $('#boletin').load('templates/formBoletin.php');
}
function hidePopup(){
	$('.bgOscuro').css('display','none');
	$('.formBoletinPopup').css('display','none');
}
function showPopupAmigo(url){
        $('#popAmigo').load('templates/formAmigo.php?url='+url);
}
function hidePopupAmigo(){
	$('.bgOscuro').css('display','none');
	$('.formPopupAmigo').css('display','none');
}
function enviarBoletin(formulario){
    var correcto=true;
    form = document.getElementById(formulario);
    var email = form.email.value;
    err = $('span#emailErrorPop');
    if (!validaEmail(email)) {
        err.html('¡Email incorrecto!');
        correcto = false;
    }else err.html('');
    if (correcto){
    jQuery.post('./procesos/envia-boletin.php',
        {email: email},
        function (result) {
            if (result.result == 'success') {
                jQuery('div#contentFormPop').html('<span>Se ha suscrito correctamente.</span>');
            }else if (result.result == 'error'){
                err.html('Este correo ya está suscrito.');
                /*jQuery('div#contentFormPop').html('<span>Este correo ya está suscrito.</span>');*/
            }
	},'json'
		);
    }
    return false;
}
function enviarContacto(formulario){
    var correcto=true;
    form = document.getElementById(formulario);
    //Nombre
    var nombre = form.nombreContacto.value;
    err = $('span#errorNombreContacto');
    if (nombre=="" || nombre =="undefined") {
        err.html('¡Introduce tu nombre!');
        correcto = false;
    }else err.html('');
    //Email
    var email = form.emailContacto.value;
    err = $('span#errorEmailContacto');
    if (!validaEmail(email)) {
        err.html('¡Email incorrecto!');
        correcto = false;
    }else err.html('');
    //Telefono
    var tel = form.telContacto.value;
    err = $('span#errorTelContacto');
    if (tel=="" || tel =="undefined") {
        err.html('¡Introduce tu teléfono!');
        correcto = false;
    }else err.html('');
    //Consulta
    var consulta = form.consultaContacto.value;
    err = $('span#errorConsultaContacto');
    if (consulta=="" || consulta=="undefined") {
        err.html('¡Introduce tu consulta!');
        correcto = false;
    }else err.html('');
    if (correcto){
    jQuery.post('./procesos/envia-contacto.php',
        {nombre:nombre, email:email, tel:tel, consulta:consulta},
        function (result) {
            if (result.result == 'success') {
                jQuery('div#formularioContacto').html('<span>Gracias por tu consulta,en breve te contactaremos.</span>');
            }
	},'json'
		);
    }
    return false;
}
function enviarAmigo(formulario){
    var correcto=true;
    form = document.getElementById(formulario);
    //Remite
    var remite = form.remite.value;
    err = $('span#remiteErrorPop');
    if (remite=="" || remite =="undefined") {
        err.html('¡Introduce tu nombre!');
        correcto = false;
    }else err.html('');
    //Remite Email
    var remiteEmail = form.remiteEmail.value;
    err = $('span#remiteEmailErrorPop');
    if (!validaEmail(remiteEmail)) {
        err.html('¡Email incorrecto!');
        correcto = false;
    }else err.html('');
    //Destinatario
    var dest = form.dest.value;
    err = $('span#destErrorPop');
    if (dest=="" || dest=="undefined") {
        err.html('¡Nombre incorrecto!');
        correcto = false;
    }else err.html('');
    //Destinatario Email
    var destEmail = form.destEmail.value;
    err = $('span#destEmailErrorPop');
    if (!validaEmail(destEmail)) {
        err.html('¡Email incorrecto!');
        correcto = false;
    }else err.html('');
    var url=form.url.value;
    if (correcto){
    jQuery.post('./procesos/envia-amigo.php',
        {remite:remite, remiteEmail: remiteEmail,dest:dest,destEmail:destEmail, url:url},
        function (result) {
            if (result.result == 'success') {
                jQuery('div#contentFormPopAmigo').html('<span>Email enviado correctamente.</span>');
            }
	},'json'
		);
    }
    return false;
}
function mostrarTelefono(idRest){
    jQuery.post('./procesos/muestra-tel.php',
        {idRest: idRest},
        function (result) {
            if (result.result == 'success') {
                jQuery('p#telefonoFicha').html('<span>Teléfono </span>'+result.telefono);
            }else if (result.result == 'error'){
                jQuery('p#telefonoFicha').html('<span>Teléfono </span>Sin teléfono');
            }
	},'json'
		);
    return false;
}
function enviaVoto(formulario){
    var correcto=true;
    
    var nombre = formulario.nombre.value;
    err = $('span#errorNombre');
    if (nombre=="" || nombre==undefined) {
        err.html('Introduce tu nombre.');
        correcto = false;
    }else err.html('');

    var email = formulario.email.value;
    err = $('span#errorMail');
    if (!validaEmail(email)) {
        err.html('Email incorrecto.');
        correcto = false;
    }else err.html('');

    var voto = formulario.voto.value;
    err = $('span#errorVoto');
    if (voto<1) {
        err.html('Selecciona una valoración.');
        correcto = false;
    }else err.html('');

    var texto = formulario.comentario.value;
    err = $('span#errorTexto');
    if (texto=="" || texto==undefined) {
        err.html('Introduce tu opinión.');
        correcto = false;
    }else err.html('');
    var idRestaurante = formulario.idRestaurante.value;
    if (correcto){
    jQuery.post('./procesos/guarda-voto.php',
        {idRestaurante:idRestaurante,nombre:nombre,email:email,voto:voto,texto:texto},
        function (result) {
            if (result.result == 'success') {
                jQuery('div#formularioComentario').html('<span>Gracias por su opinión.</span>');
            }else if (result.result == 'error'){
                jQuery('div#formularioComentario').html('<span>Se ha producido un error, inténtelo más tarde.</span>');
            }
	},'json'
		);
    }
    return false;
}
