﻿function checkIndex()
{
    cEmail=document.getElementById("txtEmail").value;
    if ((cEmail.length==0) || !(compEmail(cEmail)))
    {
        alert("El email introducido no tiene un formato válido")
        return false;
    }
    return true;
}

function checkRegistro()
{
    bTodoOk=true;
    cMensaje="";
    cMatricula=document.getElementById("txtMatricula").value;
    if (cMatricula.length==0)
    {
        cMensaje=cMensaje + "Debes introducir una matrícula\n";
        bTodoOk=false;
    }
    cEmail=document.getElementById("txtEmailRegistro").value;
    if ((cEmail.length==0) || !(compEmail(cEmail)))
    {
        cMensaje=cMensaje + "El email introducido no tiene un formato válido";
        bTodoOk=false;
    }
    if (!bTodoOk)
    {
        alert(cMensaje);
    }
    return bTodoOk ;
}


function checkInicio()
{
    var bTodoOk = true;
    var cMensaje = "";
    var rd1 = document.getElementById("rdlistRecordatorio_0");
    var rd2 = document.getElementById("rdlistRecordatorio_1");
    var o_txtFecha = document.getElementById("txtFechaRevision");
    var o_txtKms = document.getElementById("txtKilometros");
    if (rd1) {
        if (!rd1.checked && !rd2.checked) {
            cMensaje = "No has marcado si quieres que te recordemos tu próximo mantenimiento";
            bTodoOk = false;
        }
    }
    if (rd1.checked)
    {
    if (o_txtFecha.value.length == 0) {
    if (cMensaje != "") cMensaje = cMensaje + "\n";
    cMensaje = cMensaje + "Debes introducir la fecha de tu última revisión";
    bTodoOk = false;
    }
    else if (!isDate(o_txtFecha.value)) {
    if (cMensaje != "") cMensaje = cMensaje + "\n";
    cMensaje = cMensaje + "La fecha introducida no es válida. Comprueba el formato y que no sea posterior a hoy.";
    bTodoOk = false;      
    }
    if (o_txtKms.value.length == 0) {
    if (cMensaje != "") cMensaje = cMensaje + "\n";
    cMensaje = cMensaje + "Debes introducir tu kilometraje actual";
    bTodoOk = false;
    }
    else if (isNaN(o_txtKms.value)) {
    if (cMensaje != "") cMensaje = cMensaje + "\n";
    cMensaje = cMensaje + "El kilometraje actual tiene que ser un número";
    bTodoOk = false;
    }
    }
    if (!bTodoOk) {
        alert(cMensaje);
    }
    return bTodoOk;
}

function showDatos(nRdValue) {
    if (nRdValue) {
        document.getElementById("trDatos").style.display = "block";
    }
    else {
        document.getElementById("trDatos").style.display = "none";
    }
}

function checkInvitar()
{
    cNombre=document.getElementById("txtNombre").value;
    cNombreAmigo=document.getElementById("txtNombreAmigo").value;
    cEmailAmigo=document.getElementById("txtEmailAmigo").value;
    bCheck=document.getElementById("chkAcepto");
    if (cNombre.length==0 || cNombreAmigo.length==0 || cEmailAmigo.length==0)
    {
        alert("Debes rellenar todos los campos para poder enviar la invitación");
        return false;
    }
    else if (!compEmail(cEmailAmigo))
    {
        alert("El email no tiene un formato válido");
        return false;
    }
    return true;
}

function compEmail (correo)
{
	var filter  = /^([a-zA-Z0-9_\.\-%%])+\@(([a-zA-Z0-9\-%%])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(correo))
		return true;
	else 
		return false;
}


function isDate(txtFecha) {
    var objDate;
    var mSeconds; 
    
    // la longitud tiene que ser de 10 caracteres (dd/mm/aaaa)
    if (txtFecha.length != 10) return false;

    var day = txtFecha.substring(0, 2) - 0;
    var month = txtFecha.substring(3, 5) - 1; // los meses empiezan desde 0 en JS
    var year = txtFecha.substring(6, 10) - 0;

    if (txtFecha.substring(2, 3) != '/') return false;
    if (txtFecha.substring(5, 6) != '/') return false;

    if (year < 1980 || year > 2050) return false;

    // se pasa milisegundos la fecha introducida
    mSeconds = (new Date(year, month, day)).getTime();

    // se crea un objeto Date asignandole los milisegundos
    objDate = new Date();
    objDate.setTime(mSeconds);

    // se comparan los valores del objeto Date con los introducidos
    // si no coinciden no son validos
    if (objDate.getFullYear() != year) return false;
    if (objDate.getMonth() != month) return false;
    if (objDate.getDate() != day) return false;
    //si la fecha es posterior a la actual, no es válida
    if (new Date().getTime() < mSeconds) return false;

    return true;
}
