﻿function VerifTipoDocumento(source, args)
{
    var TipoDocumento, Documento, Validator, Valido;

    if (source.tagName == 'SELECT')
    {
        TipoDocumento = source;
        Documento = $get(ConstruirId(source.id, 'TxtDocumento', 1));
        Validator = $get(ConstruirId(source.id, 'RequiredDoc', 1));
        
        AsignarIdResFam(TipoDocumento, Documento, false);
    }
    else if (source.tagName == 'INPUT')
    {
        TipoDocumento = $get(ConstruirId(source.id, 'DdlTipoDocumento', 1));
        Documento = source;
        Validator = $get(ConstruirId(source.id, 'RequiredDoc', 1));
    }
    else
    {
        TipoDocumento = $get(ConstruirId(source.id, 'DdlTipoDocumento', 1));
        Documento = $get(ConstruirId(source.id, 'TxtDocumento', 1));
        Validator = source;
    }
    
    if (Documento.value != '')
    {
        Valido = ComprobarDocumento(TipoDocumento, Documento);
    }
    else
    {
        Valido = false;
    }
    
    if (args != null)
    {
        args.IsValid = Valido;        
    }
    else
    {
        Validator.isvalid = Valido;
    }
    
    if (Valido)
    {
        AsignarIdResFam(TipoDocumento, Documento, true);
    
        Validator.style.visibility = 'hidden';
    }
    else
    {
        Validator.style.visibility = 'visible';    
    }    
        
    return;
}

function ComprobarDocumento(TipoDocumento, Documento)
{
    var Valido;

    switch (TipoDocumento.value)
    {
        case 'NI':
            Valido = ValidarDNI(Documento.value);
            break; 
            
        case 'PP':
            Valido = ValidarPasaporte(Documento.value);
            break;
        
        case 'TR':
            Valido = ValidarNIE(Documento.value);
            break;
        
        default:
            Valido = ValidarDNICom(Documento.value);
            break;    
    }
        
    return Valido;
}

function ValidarDNI(Documento)
{
    var Valido;
    
    Documento = Documento.toUpperCase();
    
    var letras = ['T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X', 'B', 'N', 'J',
                  'Z', 'S', 'Q', 'V', 'H', 'L', 'C', 'K', 'E', 'T'];
                  
    if (/^\d{8}[A-Z]$/.test(Documento))
    {
        if(Documento.charAt(8) != letras[(Documento.substring(0, 8))%23]) 
        {
            Valido = false;
        }
        else
        {
            Valido = true;
        }
    }
    else
    {
        Valido = false;
    }
    
    return Valido;
}

function ValidarNIE(Documento)
{
    var Valido;
    
    Documento = Documento.toUpperCase();
    
    if (/^([X]|[Y])\d{7}[A-Z]$/.test(Documento))
    {
        var DocNIF;
        
        var Letra =  Documento.substring(0, 1);
        
        switch (Letra)
        {
            case 'X':
                DocNIF = '0';
                break;
                
            case 'Y':
                DocNIF = '1';
                break;
                
            case 'Z':
                DocNIF = '2';
                break;
        }
        
        DocNIF = DocNIF + Documento.substring(1);
    
        Valido = ValidarDNI(DocNIF);
    }
    else
    {
        Valido = false;
    }
    
    return Valido;
}

function ValidarPasaporte(Documento)
{
    var Valido = false;
        
    Documento = Documento.toUpperCase();
    
    if (/^([A-Z]|[0-9]){5,20}$/.test(Documento))
    {
        Valido = true;
    }
    
    return Valido;
}

function ValidarDNICom(Documento)
{
    var Valido = false;
    
    Documento = Documento.toUpperCase();
    
    if (/^([A-Z]|[0-9]){5,20}$/.test(Documento))
    {
        Valido = true;
    }
    
    return Valido;
}

function ValidarFechaNacResFam(Documento)
{
    var Resultado = false;
    
    if ($get(ConstruirId(Documento.id, 'UpFechaNacimiento', 5)).style.display != 'none')
    {
        Resultado = true;
    
        var DropDlAnoNac = $get(ConstruirId(Documento.id, 'DropDlAnoNac', 5));
        var DropDlMesNac = $get(ConstruirId(Documento.id, 'DropDlMesNac', 5));
        var DropDlDiaNa = $get(ConstruirId(Documento.id, 'DropDlDiaNac', 5));
        
        var anio = DropDlAnoNac.options[DropDlAnoNac.selectedIndex].value;
        var mes = DropDlMesNac.options[DropDlMesNac.selectedIndex].value;
        var dia = DropDlDiaNa.selectedIndex + 1;

        if (dia < 10)
        {
            dia = '0' + dia;
        }

        Documento.value = dia + '/' + mes + '/' + anio;
    }
    
    return Resultado;
}

function AsignarIdResFam(TipoDocumento, Documento, AsignarValor)
{
    var DatosResidente = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_DatosResidente', 2));
    var DatosFam = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_DatosFam', 2));

    if ((DatosResidente != null) || (DatosFam != null))
    {
        if (DatosResidente != null)
        {
            if (AsignarValor)
            {
                AsignarTiposResidentes(TipoDocumento, Documento);
            }
            else
            {
                IntroducirTiposResidentes(TipoDocumento);
            }
        }

        if (DatosFam != null)
        {
            if (AsignarValor)
            {
                AsignarTiposFamiliaNum(TipoDocumento, Documento);
            }
            else
            {
                IntroducirTiposFamiliaNum(TipoDocumento);
            }
        }
    }
}

function AsignarTiposResidentes(TipoDocumento, Documento)
{
    var TipoIdRes = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_DropDlTipoIDRes', 2));

    if (TipoDocumento.options[TipoDocumento.selectedIndex].value == 'NI')
    {
        if (TipoIdRes.options[TipoIdRes.selectedIndex].value == 'DN')
        {
            var TxtIdRes = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_TxtIDRes', 2));
            
            TxtIdRes.value = Documento.value;
        }
    }
    else
    {
        if (TipoDocumento.options[TipoDocumento.selectedIndex].value == 'TR')
        {
            if (TipoIdRes.options[TipoIdRes.selectedIndex].value == 'TR')
            {
                var TxtIdRes = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_TxtIDRes', 2));
                
                TxtIdRes.value = Documento.value;
            }
        }
    }
}

function AsignarTiposFamiliaNum(TipoDocumento, Documento)
{
    var TipoIdFam = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_DropDlTipoIDFam', 2));
    
    if (TipoDocumento.options[TipoDocumento.selectedIndex].value == 'NI')
    {
        if (TipoIdFam.options[TipoIdFam.selectedIndex].value == 'DN')
        {
            var TxtIdFam = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_TxtIDFam', 2));
            
            TxtIdFam.value = Documento.value;
        }
    }
    else
    {
        if (TipoDocumento.options[TipoDocumento.selectedIndex].value == 'TR')
        {
            if (TipoIdFam.options[TipoIdFam.selectedIndex].value == 'TR')
            {
                var TxtIdFam = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_TxtIDFam', 2));
                
                TxtIdFam.value = Documento.value;
            }
        }
    }
}

function IntroducirTiposResidentes(TipoDocumento)
{
    var TipoIdRes = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_DropDlTipoIDRes', 2));
    
    var TipoPax = $get(ConstruirId(TipoDocumento.id, 'LblTipoPasajero', 3)).innerText.trim();
    
    for (var i = (parseInt(TipoIdRes.options.length) - 1); i >= 0; i--)
    {
        TipoIdRes.remove(i);
    }
    
    var Ocurrencias;
    var DisCertificado = true;
    
    switch (TipoDocumento.options[TipoDocumento.selectedIndex].value)
    {
        case 'NI':
            if (TipoPax == 'Niño')
            {
                Ocurrencias = ['DNI-DN', 'Certificado Residencia-CR', 'Menor sin DNI-MR'];
            }
            else
            {
                Ocurrencias = ['DNI-DN', 'Certificado Residencia-CR', 'Diputado o Senador-GR', 'Menor sin DNI-MR'];
            }
            break;
            
        case 'NC':
        case 'PP':
            Ocurrencias = ['Certificado Registro Extranjero-CR'];
            DisCertificado = false;
            break;
            
        case 'TR':
            if (TipoPax == 'Niño')
            {
                Ocurrencias = ['Tarjeta Residencia-TR', 'Certificado Residencia-CR', 'Menor sin DNI-MR'];
            }
            else
            {
                Ocurrencias = ['Tarjeta Residencia-TR', 'Certificado Residencia-CR', 'Diputado o Senador-GR', 'Menor sin DNI-MR'];
            }
            break;
    }
    
    TipoIdRes.length = Ocurrencias.length;
    
    for (var j = 0; j < Ocurrencias.length; j++)
    {
        var opcion = Ocurrencias[j].split('-');
    
        TipoIdRes.options[j] = new Option(opcion[0], opcion[1]);
    }
    
    TipoIdRes.options[0].selected = true;
    
    var TipoSeleccionado = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_HiddenRes', 2));
    
    TipoSeleccionado.value = TipoIdRes.options[0].value;
        
    var Autorizacion = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_TextAutorizacion', 2));
    
    Autorizacion.disabled = DisCertificado;
    
    if (DisCertificado)
    {
        Autorizacion.style.backgroundColor = '#f7f7f7';
    }
    else
    {
        Autorizacion.style.backgroundColor = '#ffffff';
    }    
}

function IntroducirTiposFamiliaNum(TipoDocumento)
{
    var TipoIdFam = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_DropDlTipoIDFam', 2));
    
    for (var i = (parseInt(TipoIdFam.options.length) - 1); i >= 0; i--)
    {
        TipoIdFam.remove(i);
    }
    
    var Ocurrencias;
        
    switch (TipoDocumento.options[TipoDocumento.selectedIndex].value)
    {
        case 'NI':
            Ocurrencias = ['DNI-DN', 'Menor sin DNI-MR'];
            break;
            
        case 'NC':
        case 'PP':
            Ocurrencias = ['Certificado Registro Extranjero-TR'];
            break;
            
        case 'TR':
            Ocurrencias = ['Tarjeta Residencia-TR', 'Menor sin DNI-MR'];
            break;        
    }
    
    TipoIdFam.length = Ocurrencias.length;
    
    for (var j = 0; j < Ocurrencias.length; j++)
    {
        var opcion = Ocurrencias[j].split('-');
    
        TipoIdFam.options[j] = new Option(opcion[0], opcion[1]);
    }
    
    TipoIdFam.options[0].selected = true;
    
    var TipoSeleccionado = $get(ConstruirId(TipoDocumento.id, 'Datos_Pax_Residente1_HiddenFam', 2));
    
    TipoSeleccionado.value = TipoIdFam.options[0].value;
}

function VerificarIdentificacionResFam(source, args)
{
    var TipoDocumento, Documento, Validator;
    
    var TipoPax = source.id.substring(source.id.length - 3);
    
    if (source.tagName == 'SELECT')
    {
        TipoDocumento = source;
        Documento = $get(ConstruirId(source.id, 'TxtID' + TipoPax, 1));
        Validator = $get(ConstruirId(source.id, 'CstID' + TipoPax, 1));
        
        var TipoSeleccionado = $get(ConstruirId(source.id, 'Hidden' + TipoPax, 1));
        TipoSeleccionado.value = TipoDocumento.options[TipoDocumento.selectedIndex].value;
    }
    else if (source.tagName == 'INPUT')
    {
        TipoDocumento = $get(ConstruirId(source.id, 'DropDlTipoID' + TipoPax, 1));
        Documento = source;
        Validator = $get(ConstruirId(source.id, 'CstID' + TipoPax, 1));
    }
    else
    {
        TipoDocumento = $get(ConstruirId(source.id, 'DropDlTipoID' + TipoPax, 1));
        Documento = $get(ConstruirId(source.id, 'TxtID' + TipoPax, 1));
        Validator = source;
    }
    
    var TipoIDPax = $get(ConstruirId(source.id, 'UC_TipoDocumento1_DdlTipoDocumento', 5));
    
    if (TipoIDPax != null)
    {
        TipoIDPax = TipoIDPax.options[TipoIDPax.selectedIndex].value;
    }
        
    var Valido = ComprobarDocumentoResFam(TipoDocumento, Documento, TipoIDPax, TipoPax);
           
    if (args != null)
    {
        args.IsValid = Valido;
    }
    else
    {
        Validator.isvalid = Valido;
    }
    
    if (Valido)
    {
        Validator.style.visibility = 'hidden';
    }
    else
    {
        Validator.style.visibility = 'visible';
        
        Validator.title = ObtenerMensajeError(TipoIDPax, TipoDocumento.options[TipoDocumento.selectedIndex].value);
    }
    
    return;
}

function ComprobarDocumentoResFam(TipoDocumento, Documento, TipoIDPax, TipoPax)
{
    var Valido = false;
    
    var AutoPrint = false;

    if (TipoDocumento.options[TipoDocumento.selectedIndex].value == 'MR')
    {
        Valido = ValidarFechaNacResFam(Documento);
        
        AutoPrint = false;    
    }
    else
    {
        if (TipoIDPax == 'NI' || TipoDocumento.options[TipoDocumento.selectedIndex].value == 'DN')
        {
            Valido = ValidarDNI(Documento.value);
        }
        else
        {
            Valido = ValidarNIE(Documento.value);
        }
    
        if ((TipoDocumento.options[TipoDocumento.selectedIndex].value == 'CR') ||
            (TipoDocumento.options[TipoDocumento.selectedIndex].value == 'GR'))
        {
            AutoPrint = false;
        }
        else
        {
            AutoPrint = true;
        }
    }

    if (TipoPax == 'Res')
    {
        var Autorizacion = $get(ConstruirId(TipoDocumento.id, 'TextAutorizacion', 1));

        Autorizacion.disabled = AutoPrint;

        if (AutoPrint)
        {            
            Autorizacion.style.backgroundColor = '#f7f7f7';
        }
        else
        {
            Autorizacion.style.backgroundColor = '#ffffff';
        }
    }
    
    return Valido;
}

function ObtenerMensajeError(TipoIDPax, TipoDocumento)
{
    var Mensaje = '';

    if ((TipoDocumento == 'MR') || (TipoIDPax == 'MR'))
    {
        Mensaje = 'No se ha validado el pasajero como menor.';
    }
    else
    {
        if (TipoIDPax == 'NI' || TipoDocumento == 'DN')
        {
            Mensaje = 'DNI erroneo (formato NNNNNNNNA).';
        }
        else
        {
            Mensaje = 'Nie erroneo (formato ANNNNNNNA).';
        }
    }
    
    return Mensaje;
}

function VerificarAutorizacion(source, args)
{
    var TipoDocumento = $get(ConstruirId(source.id, 'DropDlTipoIDRes', 1));
    var TipoSeleccionado = TipoDocumento.options[TipoDocumento.selectedIndex].value;
    
    if ((TipoSeleccionado == 'CR') || (TipoSeleccionado == 'GR') || (TipoSeleccionado == 'MR'))
    {
        var Valido = false;
        
        var Documento, Validator;
        
        if (source.tagName == 'INPUT')
        {
            Documento = source.value.toUpperCase();
            Validator = $get(ConstruirId(source.id, 'CstAutorizacionRes', 1));
        }
        else
        {
            Documento = $get(ConstruirId(source.id, 'TextAutorizacion', 1)).value.toUpperCase();
            Validator = source;
        }        
    
        if (/^([A-Z]|[0-9]){9,20}$/.test(Documento))
        {
            Valido = true;
        }
        
        if (args != null)
        {
            args.IsValid = Valido;
        }
        else
        {
            Validator.isvalid = Valido;
        }
        
        if (Valido)
        {
            Validator.style.visibility = 'hidden';
        }
        else
        {        
            Validator.style.visibility = 'visible';
        }
    }
}