num = 0;
limite = 5;
function crear(obj, par1, par2) {
  num++;
  if(num <= limite){
    //Abilitar el buton de subir
    //document.getElementById('subir').disabled = false;
    //Guardar el total de fotos a subir
    document.getElementById('total').value = num;

    //Separador entre elementos
    separador = document.createElement('br');
    separador1 = document.createElement('br');
    separador2 = document.createElement('br');

    //Crear el div que lleva los input's
    fi = document.getElementById('fiel');
    contenedor = document.createElement('div');
    contenedor.id = 'div'+num;
    fi.appendChild(contenedor);
    //fi.insertBefore(contenedor,obj);

    if(par1 != ''){
      //Insertar el imput text "URL"
      contenedor.appendChild(document.createTextNode(par1));
      contenedor.appendChild(separador);
      elemento = document.createElement('input');
      elemento.type = 'text';
      elemento.name = 'url_'+num;
      elemento.id = 'url_'+num;
      contenedor.appendChild(elemento);
      contenedor.appendChild(separador1);
    }

    //Insertar el imput file
    contenedor.appendChild(document.createTextNode(par2));
    contenedor.appendChild(separador2);
    ele = document.createElement('input');
    ele.type = 'file';
    ele.name = 'archivo_'+num;
    contenedor.appendChild(ele);

    if(num == limite){
      //document.getElementById('crear').style.display = 'none';
      document.getElementById('crear_1').disabled = true;
    }
  }
}

function borrar(obj) {
  num--;
  if(num == 0){
    //Desabilitar el buton de subir
    document.getElementById('subir').disabled = true;
  }
  //Guardar el total de fotos a subir
  document.getElementById('total').value -= 1;
  //Quitar un elemento input file
  fi = document.getElementById('fiel');
  fi.removeChild(document.getElementById(obj));
  if(num < limite) {
    //document.getElementById('crear').style.display = 'block';
    document.getElementById('crear_1').disabled = false;
  }
}

function mostrar_contactar(elem) {
  elemento = document.getElementById(elem);
  if(elem == 'div_cita'){
    elemento.style.display = 'block';
    document.getElementById('div_info').style.display = 'none';
  }else if(elem == 'div_info'){
    elemento.style.display = 'block';
    document.getElementById('div_cita').style.display = 'none';
  }
}

//Función para validar el formulario
function validar_consulta_gratuita(f) {
  error = 0;
  texto = '';

  if (f.nombre.value.search(/\S/) == -1) {
    texto += "- Nombre\n";
    f.nombre.focus();
    error = 1;
  }
  if(f.apellidos.value.search(/\S/) == -1) {
    texto += "- Apellidos\n";
    f.apellidos.focus();
    error = 1;
  }
  if(f.telefono.value.search(/\S/) == -1) {
    texto += "- Teléfono\n";
    f.telefono.focus();
    error = 1;
  }else if(f.telefono.value.length != 9) {
    texto += "- Teléfono con formato incorrecto (9 digitos)\n";
    f.telefono.focus();
    error = 1;
  }
  if(f.email.value.search(/\S/) == -1) {
    texto += "- Correo\n";
    f.email.focus();
    error = 1;
  }else if (f.email.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-z]{2,}$/i) == -1) {
    texto += "- Correo con formato incorrecto\n";
    f.email.focus()
    error = 1;
  }
  if(f.clinica.value == 0) {
    texto += "- Clínica\n";
    f.clinica.focus();
    error = 1;
  }
  if(f.tratamiento.value == 0) {
    texto += "- Tratamiento\n";
    f.tratamiento.focus();
    error = 1;
  }
  if(f.acepto.checked == false) {
    texto += "- Aceptar condiciones\n";
    error = 1;
  }
  if(error == 1){
    alert("Por favor, revise los campos: \n\n"+texto);
    return false;
  }else{
    return true;
  }
}

function validar_info(f) {
  error = 0;
  texto = '';

  if (f.nombre.value.search(/\S/) == -1) {
    texto += "- Nombre\n";
    f.nombre.focus();
    error = 1;
  }
  if(f.apellidos.value.search(/\S/) == -1) {
    texto += "- Apellidos\n";
    f.apellidos.focus();
    error = 1;
  }
  if(f.telefono.value.search(/\S/) == -1) {
    texto += "- Teléfono\n";
    f.telefono.focus();
    error = 1;
  }else if(f.telefono.value.length != 9) {
    texto += "- Teléfono con formato incorrecto (9 digitos)\n";
    f.telefono.focus();
    error = 1;
  }
  if(f.email.value.search(/\S/) == -1) {
    texto += "- Correo\n";
    f.email.focus();
    error = 1;
  }else if (f.email.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-z]{2,}$/i) == -1) {
    texto += "- Correo con formato incorrecto\n";
    f.email.focus()
    error = 1;
  }
  if(f.clinica.value == 0) {
    texto += "- Clínica\n";
    f.clinica.focus();
    error = 1;
  }
  if(f.tratamiento.value == 0) {
    texto += "- Tratamiento\n";
    f.tratamiento.focus();
    error = 1;
  }
  if(f.acepto.checked == false) {
    texto += "- Aceptar condiciones\n";
    error = 1;
  }
  if(error == 1){
    alert("Por favor, revise los campos: \n\n"+texto);
    return false;
  }else{
    return true;
  }
}

function validar_form_sugerencias(f) {
  error = 0;
  texto = '';

  if (f.nombre.value.search(/\S/) == -1) {
    texto += "- Nombre\n";
    f.nombre.focus();
    error = 1;
  }
  if(f.email.value.search(/\S/) == -1) {
    texto += "- Correo\n";
    f.email.focus();
    error = 1;
  }else if (f.email.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-z]{2,}$/i) == -1) {
    texto += "- Correo con formato incorrecto\n";
    f.email.focus()
    error = 1;
  }
  if(f.sugerencia.value.search(/\S/) == -1) {
    texto += "- Descripción de tu sugerencia\n";
    f.sugerencia.focus();
    error = 1;
  }
  if(f.acepto.checked == false) {
    texto += "- Aceptar condiciones\n";
    error = 1;
  }
  if(error == 1){
    alert("Por favor, revise los campos: \n\n"+texto);
    return false;
  }else{
    return true;
  }
}

function validar_form_llamamos(f) {
  error = 0;
  texto = '';

  if (f.nombre.value.search(/\S/) == -1) {
    texto += "- Nombre\n";
    f.nombre.focus();
    error = 1;
  }
  if(f.apellidos.value.search(/\S/) == -1) {
    texto += "- Apellidos\n";
    f.apellidos.focus();
    error = 1;
  }
  if(f.telefono.value.search(/\S/) == -1) {
    texto += "- Teléfono\n";
    f.telefono.focus();
    error = 1;
  }else if(f.telefono.value.length != 9) {
    texto += "- Teléfono con formato incorrecto (9 digitos)\n";
    f.telefono.focus();
    error = 1;
  }
  if(f.email.value.search(/\S/) == -1) {
    texto += "- Correo\n";
    f.email.focus();
    error = 1;
  }else if (f.email.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-z]{2,}$/i) == -1) {
    texto += "- Correo con formato incorrecto\n";
    f.email.focus()
    error = 1;
  }
  if(f.hora.value == 0) {
    texto += "- Hora de contacto\n";
    f.hora.focus();
    error = 1;
  }
  if(f.tratamiento.value == 0) {
    texto += "- Tratamiento\n";
    f.tratamiento.focus();
    error = 1;
  }
  if(f.acepto.checked == false) {
    texto += "- Aceptar condiciones\n";
    error = 1;
  }
  if(error == 1){
    alert("Por favor, revise los campos: \n\n"+texto);
    return false;
  }else{
    return true;
  }
}


function validar_form_amigo(f) {
  error = 0;
  texto = '';

  if (f.nombre.value.search(/\S/) == -1) {
    texto += "- Tu Nombre\n";
    f.nombre.focus();
    error = 1;
  }
  if(f.email.value.search(/\S/) == -1) {
    texto += "- Tu Email\n";
    f.email.focus();
    error = 1;
  }else if (f.email.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-z]{2,}$/i) == -1) {
    texto += "- Email con formato incorrecto\n";
    f.email.focus()
    error = 1;
  }
  if (f.amigo_nombre.value.search(/\S/) == -1) {
    texto += "- Nombre de tu amigo/a\n";
    f.amigo_nombre.focus();
    error = 1;
  }
  if(f.amigo_email.value.search(/\S/) == -1) {
    texto += "- Email de tu amigo/a\n";
    f.amigo_email.focus();
    error = 1;
  }else if (f.amigo_email.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-z]{2,}$/i) == -1) {
    texto += "- Email de tu amigo/a con formato incorrecto\n";
    f.amigo_email.focus()
    error = 1;
  }
  if(f.acepto.checked == false) {
    texto += "- Aceptar condiciones\n";
    error = 1;
  }
  if(error == 1){
    alert("Por favor, revise los campos: \n\n"+texto);
    return false;
  }else{
    return true;
  }
}


function validar_form_reclamacion(f) {
  error = 0;
  texto = '';

  for (i = 0; i < 4; i++) {
    cuenta = false
    elemento = document.getElementsByName('queja[]')
    for (j = 0; j < elemento.length; j++) {
      cuenta = cuenta || elemento[j].checked
    }
  }
  if (!cuenta) {
    texto += "- Motivos de su reclamación / queja\n";
    error = 1;
  }

  if(f.clinica.value == 0) {
    texto += "- Clínica\n";
    f.clinica.focus();
    error = 1;
  }
  if (f.descripcion.value.search(/\S/) == -1) {
    texto += "- Descripción de su reclamación / queja\n";
    f.descripcion.focus();
    error = 1;
  }
  if (f.nombre.value.search(/\S/) == -1) {
    texto += "- Nombre y Apellidos\n";
    f.nombre.focus();
    error = 1;
  }
  if(f.tipo_via.value == 0) {
    texto += "- Tipo de vía\n";
    f.tipo_via.focus();
    error = 1;
  }
  if (f.nombre_via.value.search(/\S/) == -1) {
    texto += "- Nombre de la vía\n";
    f.nombre_via.focus();
    error = 1;
  }
  if(f.sin_numero.checked == false) {
    if (f.numero_via.value.search(/\S/) == -1) {
      texto += "- Número de la vía\n";
      f.numero_via.focus();
      error = 1;
    }
  }
  if(f.provincia.value == 0) {
    texto += "- Provincia\n";
    f.provincia.focus();
    error = 1;
  }
  if (f.ciudad.value.search(/\S/) == -1) {
    texto += "- Ciudad\n";
    f.ciudad.focus();
    error = 1;
  }
  if (f.cp.value.search(/\S/) == -1) {
    texto += "- Código postal\n";
    f.cp.focus();
    error = 1;
  }
  if(f.email.value.search(/\S/) == -1) {
    texto += "- Email\n";
    f.email.focus();
    error = 1;
  }else if (f.email.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-z]{2,}$/i) == -1) {
    texto += "- Email con formato incorrecto\n";
    f.email.focus()
    error = 1;
  }
  if(f.telefono.value.search(/\S/) == -1) {
    texto += "- Teléfono\n";
    f.telefono.focus();
    error = 1;
  }else if(f.telefono.value.length != 9) {
    texto += "- Teléfono con formato incorrecto (9 digitos)\n";
    f.telefono.focus();
    error = 1;
  }
  if(f.acepto.checked == false) {
    texto += "- Aceptar condiciones\n";
    error = 1;
  }
  if(error == 1){
    alert("Por favor, revise los campos: \n\n"+texto);
    return false;
  }else{
    return true;
  }
}


function validar_form_prensa(f) {
  error = 0;
  texto = '';

  if (f.nombre.value.search(/\S/) == -1) {
    texto += "- Tu Nombre\n";
    f.nombre.focus();
    error = 1;
  }
  if(f.email.value.search(/\S/) == -1) {
    texto += "- Tu Email\n";
    f.email.focus();
    error = 1;
  }else if (f.email.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-z]{2,}$/i) == -1) {
    texto += "- Email con formato incorrecto\n";
    f.email.focus()
    error = 1;
  }
  if (f.comentario.value.search(/\S/) == -1) {
    texto += "- Comentarios\n";
    f.comentario.focus();
    error = 1;
  }
  if(f.acepto.checked == false) {
    texto += "- Aceptar condiciones\n";
    error = 1;
  }
  if(error == 1){
    alert("Por favor, revise los campos: \n\n"+texto);
    return false;
  }else{
    return true;
  }
}



function soloNumeros(e){
  tecla = (document.all) ? e.keyCode : e.which;
  if((tecla < 48 || tecla > 57) && (tecla != 32 && tecla != 43 && tecla != 8 && tecla != 0)){
    return false;
  }
}
function noEmail(e){
  tecla = (document.all) ? e.keyCode : e.which;
  if(tecla == 64){
    return false;
  }
}

function abrir() {
  if(typeof(window.innerWidth) == 'number' ) {
    //No-IE
    ancho = window.innerWidth;
  } else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    //IE 6+ 'standards compliant mode'
    ancho = document.documentElement.clientWidth + 16;
  } else if(document.body && (document.body.clientWidth || document.body.clientHeight) ) {
    //IE 4 compatible
    ancho = document.body.clientWidth;
  }
  document.getElementById('filtro').style.display = 'block';
  document.getElementById('filtro').style.visibility = 'visible';
  document.getElementById('form_oculto').style.left = (ancho < 998) ? '748px' : (ancho / 2 + 268) + 'px'
  document.getElementById('form_oculto').style.display = 'block';
  document.getElementById('form_oculto').style.visibility = 'visible';
}

function cerrar() {
  document.getElementById('filtro').style.display = 'none';
  document.getElementById('filtro').style.visibility = 'hidden';
  document.getElementById('form_oculto').style.display = 'none';
  document.getElementById('form_oculto').style.visibility = 'hidden';
}
//Funciones socializacion
function ocultarObjeto(idObject)
{
	objeto=document.getElementById(idObject);
	objeto.style.display = "none";
}

function mostrarObjeto(idObject)
{
	objeto=document.getElementById(idObject);
	objeto.style.display = "block";
}

function switchObjeto(idObject)
{
	objeto=document.getElementById(idObject);
	if(objeto.style.display == "none"){ mostrarObjeto(idObject); }
	else {ocultarObjeto(idObject);}
}

function validar_chats_antiguos(f) {
  if (f.id_chat_anterior.value == 0) {
    alert('Por favor, seleccione un chat anterior');
    f.id_chat_anterior.focus();
    return false;
  }
  return true;
}

function validar_chat_pregunta(f) {
  var error = 0;
  var texto = '';

  if (f.nickname.value.search(/\S/) == -1) {
    texto += "<p style='font-size:12px; font-weight:bold; color:#d60518;'>* Por favor, escribe un nombre de usuario para que puedas identificar tu pregunta.</p>";
    f.nickname.focus();
    error = 1;
  }else if (f.nickname.value.search(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.[a-z]{2,}$/i) == 0) {
    texto += "<p style='font-size:12px; font-weight:bold; color:#d60518;'>* No debe introducir su email en el campo de nombre de usuario.</p>";
    f.nickname.focus()
    error = 1;
  }
  if(f.pregunta.value.search(/\S/) == -1) {
    texto += "<p style='font-size:12px; font-weight:bold; color:#d60518;'>* Por favor, escribe tu pregunta.</p>";
    f.pregunta.focus();
    error = 1;
  }
  if(error == 1){
    document.getElementById('error').innerHTML = texto;
    return false;
  }else{
    return true;
  }
}
function mostrar(i) {
  p = document.getElementById(i)
  if (p.style.display == 'none') {
    p.style.display = 'block'  } else {
    p.style.display = 'none'
  }
}

function mostrarFormacion() {
  if (document.getElementById('divFormacion').style.display == 'none') {
    document.getElementById('divFormacion').style.display='block';
    document.getElementById('imgFormacionBottom').src='imagenes/bottom_banners.gif';
    document.getElementById('divEnlaceFormacion').style.borderBottom='1px solid #00338D';

  }else {
    document.getElementById('divFormacion').style.display='none';
    document.getElementById('imgFormacionBottom').src='imagenes/bottom_banners2.gif';
    document.getElementById('divEnlaceFormacion').style.borderBottom='1px solid #FFFFFF';
  }
}
function mostrarActividad() {
  if (document.getElementById('divActividad').style.display == 'none') {
    document.getElementById('divActividad').style.display='block';
    document.getElementById('imgActividadBottom').src='imagenes/bottom_banners.gif';
    document.getElementById('divEnlaceActividad').style.borderBottom='1px solid #00338D';

  }else {
    document.getElementById('divActividad').style.display='none';
    document.getElementById('imgActividadBottom').src='imagenes/bottom_banners2.gif';
    document.getElementById('divEnlaceActividad').style.borderBottom='1px solid #FFFFFF';
  }
}

function calcularFinanciacion() {

  var resultado = 0;

  if($('#cuota').val() != 0 && $('#id_financiacion').val() != 0) {
    $('#resultadoFinanciacion').css('display', 'none');
    $('#resultadoFinanciacionLoading').css('display', 'block');

    $.ajax ({
      type: 'POST',
      dataType: "html",
      cache: false,
      contentType: "application/x-www-form-urlencoded; charset=iso-8859-1",
      data: $('#form_financiacion').serialize(),
      url: 'codigo/codigo.php',
      success: function(data) {

        resultado = data.split('&');

        $('#resultadoFinanciacionLoading').css('display', 'none');
        $('#resultadoFinanciacion').fadeIn(1000);

        $('#resultado_cuota').html(resultado[0]);
        $('#resultado_tin').html(resultado[1]);
        $('#resultado_tae').html(resultado[2]);
        $('#resultado_precio').html(resultado[3]);
        $('#resultado_importe_financiado').html(resultado[4]);

      },
      error: function(data) {
        alert("Error al calcular la financiación, intentelo de nuevo más tarde.");
      }
    });
  }else {
    $('#resultadoFinanciacion').fadeOut(1000);
  }

}
