function get_clean_url(url, param, clean) {
    if (clean == 1) {
        var retorna = url + param;
    } else {
        var retorna = url + "index.php?q=" + param;
    }
    return (retorna);
}



function filtro_editorial (url, p0, p1, p2, clean) {
    var editorial_id = document.getElementById("editorial").value; 
    if (editorial_id == "" ) {
        var param = p0 + "/" + p1 + "/" + p2 + "/pag_1";
    } else {
        var param = p0 + "/" + p1 + "/" + p2 + "/pag_1/edi_" + editorial_id;
    }    
    var new_location = get_clean_url(url, param, clean);
    window.location.href=new_location;
}


function anadir_pedido (url, p0, p1, p2, p3, clean) {
    var valor = document.getElementById("cantidad").value;   
    if (valor == 0 ) {
        return false;
    }    
    var param = p0 + "/" + p1 + "/" + p2 + "/add_" + p3 + "/" + valor;
    var new_location = get_clean_url(url, param, clean);
    window.location.href=new_location;
}






function ver_condiciones () {
    var pos = jQuery("#boton_comprar").offset();
    var width = jQuery("#panel_condiciones").width();
    var height = jQuery("#panel_condiciones").height();

    var width2 = jQuery("#boton_comprar").width();
    var height2 = jQuery("#boton_comprar").height();

    jQuery("#panel_condiciones").css( { "left": (pos.left - width + width2) + "px", "top": (pos.top - height + height2) + "px" } );
    jQuery("#panel_condiciones").show('fast');     
}

// Panel de condiciones de uso
function cierra_condiciones () {
    jQuery("#panel_condiciones").hide('fast');
}        
function acepta_condiciones () {
    cierra_condiciones ();
    abre_form();
}        

function abre_form () {
    var pos = jQuery("#boton_comprar").offset();
    var width = jQuery("#form_carrito").width();
    var height = jQuery("#form_carrito").height();

    var width2 = jQuery("#boton_comprar").width();
    var height2 = jQuery("#boton_comprar").height();

    jQuery("#form_carrito").css( { "left": (pos.left - width + width2) + "px", "top": (pos.top - height + height2) + "px" } );
    jQuery("#form_carrito").show('fast');
}
function cierra_form () {
    jQuery("#form_carrito").hide('fast');
}

function test_form () {
  // control de los campos del formulario , si todo es correcto levantamos el panel con las condiciones de uso
  
  var error = "";
  var txt = "";
// control datos personales      
  if (document.getElementById("telf").value == "") {
    error = "telf";
    txt = "Debe indicar un telefono de contacto";
  }
  if (emailValidator(document.getElementById("email").value) == false) {
    error = "email";
    txt = "Debe indicar un email de contacto correcto";
  }
  if (document.getElementById("pais").value == "") {
    error = "pais";
    txt = "Debe indicar el pais";
  }
  if (isNumeric(document.getElementById("cp").value) == false) {
    error = "cp";
    txt = "Debe indicar su codigo postal";
  }
  if (document.getElementById("poblacion").value == "") {
    error = "poblacion";
    txt = "Debe indicar la población";
  }
  if (document.getElementById("provincia").value == "") {
    error = "provincia";
    txt = "Debe indicar la provincia";
  }
  if (document.getElementById("direccion").value == "") {
    error = "direccion";
    txt = "Debe indicar una direccion";
  }
  if (document.getElementById("apellidos").value == "") {
    error = "apellidos";
    txt = "Debe indicar sus apellidos";
  }
  if (document.getElementById("nombre").value == "") {
    error = "nombre";
    txt = "Debe indicar su nombre";
  }
  
  if (error != "") {
    alert (txt);
    document.getElementById(error).focus();
    return false;
  } else {
    document.forms.form1.submit();
  }
}

function emailValidator(value){
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(value.match(emailExp)){
        return true;
    }else{
        return false;
    }
}
function isNumeric(value){
    var numericExpression = /^[0-9]+$/;
    if(value.match(numericExpression)){
        return true;
    }else{
        return false;
    }
}

// Control de parametros via javascript, quita el value que hay por defecto, que suele ser < algo >
// esta funcion es llamada desde un onfocus
function limpia(x)    {
    if (document.getElementById(x).value[0] == "-") {
        document.getElementById(x).value = "";
    }
    return true;
}
// inversa de la anterior, si el campo esta vacio, vuelve a poner lo que haya en el campo text, dejando algo como < descripcion >
// esta funcion se llama desde un onblur
function rellena(x, text)    {
    if (document.getElementById(x).value == "") {
        document.getElementById(x).value = "-" + text + "";
    }
    return true;
}

function cursor_loading() {
    jQuery("#loading").show(0);
}
function cursor_default() {
    jQuery("#loading").hide(0);
}


