﻿function FechaNacimientoMesAnio(indice, fechaIda, aniosMin, aniosMax)
{
    var IdsDrops = ObtenerDropsPagina(indice, fechaIda, aniosMin, aniosMax);
    
    if ((IdsDrops.DropDlMesNac.selectedIndex >= IdsDrops.fechaDate.getMonth()) && 
        (IdsDrops.Anio == IdsDrops.fechaDate.getFullYear()))
    {
        IdsDrops.DropDlMesNac.selectedIndex = IdsDrops.fechaDate.getMonth();
        
        AsignarDias(IdsDrops);
         
        FechaNacimientoDiaProcess(IdsDrops);
    }
    else
    {
        AsignarDias(IdsDrops);
        
        VerificarFechaNac(IdsDrops);
    }    
}

function FechaNacimientoDia(indice, fechaIda, aniosMin, aniosMax)
{
    var IdsDrops = ObtenerDropsPagina(indice, fechaIda, aniosMin, aniosMax);
    
    FechaNacimientoDiaProcess(IdsDrops);
}

function FechaNacimientoDiaProcess(IdsDrops)
{
    if (IdsDrops.Anio == IdsDrops.fechaDate.getYear())
    {
        if (IdsDrops.DropDlMesNac.selectedIndex == IdsDrops.fechaDate.getMonth())
        {
            if (IdsDrops.DropDlDiaNa.options[IdsDrops.DropDlDiaNa.selectedIndex].text > IdsDrops.fechaDate.getDate())
            {
                IdsDrops.DropDlDiaNa.selectedIndex = IdsDrops.fechaDate.getDate() - 1;
            }
        }    
    }
    
    VerificarFechaNac(IdsDrops);
}

function ObtenerDropsPagina(indice, fechaIda, aniosMin, aniosMax)
{
    var indiceAux = ObtenerIndiceGrid(indice);
    
    var Id = 'ctl00_MainContentPlaceHolder_UC_Datos_PaxContactoInfoAdicional1_UC_Datos_Pasajeros1_DtLPasajeros_ctl';
        
    if (ObtPaginaActiva() == 'DisplayReserva')
    {
        Id = 'ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_DtLPasajeros_ctl';
    }
    
    return new IdsDrops(Id, indiceAux, fechaIda, aniosMin, aniosMax);
}

function ObtenerIndiceGrid(indice)
{
    indice = parseInt(indice);

    if (indice < 10)
    {
        indice = '0' + indice;
    }
    else
    {
        indice = indice;
    }
    
    return indice;
}

function IdsDrops(Id, indice, fechaIda, aniosMin, aniosMax)
{
    this.DropDlDiaNa = $get(Id + indice + '_DropDlDiaNac');
    this.DropDlMesNac = $get(Id + indice + '_DropDlMesNac');
    this.DropDlAnoNac = $get(Id + indice + '_DropDlAnoNac');
    this.ValidadorErrFecha = $get(Id + indice + '_ErrFecha');
    
    if (fechaIda == '')
    {
        var FechaAux = ObtenerMaxAniosPax(indice);
        
        this.fecha = FechaAux.FechaIda;
        this.AniosPaxMin = FechaAux.AniosMin;
        this.AniosPaxMax = FechaAux.AniosMax;
    }
    else
    {
        this.fecha = ObtenerFechaNum(fechaIda);
        
        if (aniosMin == '')
        {
            var AniosAux = ObtenerMaxAniosPax(indice);
            
            this.AniosPaxMin = AniosAux.AniosMin;
            this.AniosPaxMax = AniosAux.AniosMax;
        }
        else
        {
            this.AniosPaxMin = aniosMin;
            this.AniosPaxMax = aniosMax;
        }
    }
    
    this.fechaDate = new Date(this.fecha[2], (this.fecha[1] - 1), this.fecha[0]);
    
    this.Dia = this.DropDlDiaNa.options[this.DropDlDiaNa.selectedIndex].value;
    this.Mes = this.DropDlMesNac.options[this.DropDlMesNac.selectedIndex].value;
    this.Anio = this.DropDlAnoNac.options[this.DropDlAnoNac.selectedIndex].value;    
    this.MesText = this.DropDlMesNac.options[this.DropDlMesNac.selectedIndex].text;
}

function ObtenerMaxAniosPax(indice)
{
    var aniosMin, aniosMax, TipoPasajero, GuardaDias;
    
    if (ObtPaginaActiva() == 'DisplayReserva')
    {
        GuardaDias = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_FechaIdaVuelo');
        TipoPasajero = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_DtLPasajeros_ctl' + indice + '_LblTipoPasajero').innerHTML;
    }
    else
    {
        GuardaDias = $get('ctl00_MainContentPlaceHolder_UC_Datos_PaxContactoInfoAdicional1_UC_Datos_Pasajeros1_FechaIdaVuelo');
        TipoPasajero = $get('ctl00_MainContentPlaceHolder_UC_Datos_PaxContactoInfoAdicional1_UC_Datos_Pasajeros1_DtLPasajeros_ctl' + indice + '_LblTipoPasajero').innerHTML;
    }
    
    TipoPasajero = jsTrim(TipoPasajero);
    
    if (TipoPasajero == "Adulto")
    {
        aniosMin = 11;
        aniosMax = 14;
    }
    else if (TipoPasajero == "Niño")
    {
        aniosMin = 2;
        aniosMax = 11;
    }
    else
    {
        aniosMin = 0;
        aniosMax = 2;
    }
    
    return { FechaIda: ObtenerFechaNum(GuardaDias.value), AniosMin: aniosMin, AniosMax: aniosMax };
}

function ObtenerFechaNum(fechaIda)
{
    var fechaIdaAux = '';

    if (fechaIda != '')
    {
        //Formato de la fecha
        if (fechaIda.indexOf('/') == -1)
        {
            fechaIda = fechaIda.split(',');
            
            fechaIdaAux = new Array(3);
            fechaIdaAux[0] = fechaIda[2];
            fechaIdaAux[1] = fechaIda[1];
            fechaIdaAux[2] = fechaIda[0];    
        }
        else
        {
            fechaIdaAux = fechaIda.split('/');    
        }

        if (fechaIdaAux[1].toString().substring(0,1) == '0')
        {
            fechaIdaAux[1] = fechaIdaAux[1].substring(1,2);
        }
        
        if (fechaIdaAux[0].toString().substring(0,1) == '0')
        {
            fechaIdaAux[0] = fechaIdaAux[0].substring(1,2);
        }
        
        fechaIdaAux[1] = parseInt(fechaIdaAux[1]);
        fechaIdaAux[0] = parseInt(fechaIdaAux[0]);
    }
    
    return fechaIdaAux;
}

function AsignarDias(IdsDrops)
{  
    var Dias = 0, DiasAux = 0;
    var Longitud = 0;    

    switch (IdsDrops.MesText)
    {
         case 'Enero':
         case 'Marzo':
         case 'Mayo':
         case 'Julio':
         case 'Agosto':
         case 'Octubre':
         case 'Diciembre':
              Dias = 31;
              break;
              
         case 'Febrero':
              if ((IdsDrops.Mes % 4 == 0 && IdsDrops.Mes % 100 != 0) || IdsDrops.Mes % 400 == 0)
              {
                 Dias = 29;
              }
              else 
              {
                 Dias = 28;
              }
              break;
              
         default : 
              Dias = 30;
    }
        
    Longitud = parseInt(IdsDrops.DropDlDiaNa.options.length);
    DiasAux = Longitud - Dias;
    
    if (DiasAux > 0)
    {
        while(DiasAux > 0)
        {
            Longitud--;
            IdsDrops.DropDlDiaNa.remove(Longitud);
            DiasAux--;        
        }
    }
    else if (DiasAux < 0)
    {
        var LonTexto;
        Longitud += 1;
        
        while(DiasAux < 0)
        {
            if (Longitud < 10)
            {
                LonTexto = '0' + Longitud;
            }
            else
            {
                LonTexto = Longitud;
            }
        
            IdsDrops.DropDlDiaNa.options[Longitud - 1] = new Option(LonTexto,LonTexto);
            Longitud++;
            DiasAux++;        
        }        
    }
}

function VerificarFechaNac(IdsDrops)
{
    var Result = true;
    
    var Edad = parseInt(IdsDrops.fecha[2]) - IdsDrops.Anio - (IdsDrops.Mes > IdsDrops.fecha[1] ? 1 : (IdsDrops.Mes != IdsDrops.fecha[1]) ? 0 : (parseInt(IdsDrops.Dia) > IdsDrops.fecha[0]) ? 1 : 0);

    if (IdsDrops.AniosPaxMax == 2)
    {
        if (Edad < 0 || Edad >= 2)
        {
            Result = false;
        }
    }
    else
    {
        if (Edad < IdsDrops.AniosPaxMin || Edad > IdsDrops.AniosPaxMax)
        {
            Result = false;
        }
    }
       
    if (Result)
    {
        IdsDrops.ValidadorErrFecha.isvalid = true;
        IdsDrops.ValidadorErrFecha.style.display = 'none';
        IdsDrops.ValidadorErrFecha.style.visibility = 'hidden';
        
        VerificarSiResidenteFamiliaNum(IdsDrops);
    }
    else
    {
        IdsDrops.ValidadorErrFecha.isvalid = false;
        IdsDrops.ValidadorErrFecha.style.display = 'block';
        IdsDrops.ValidadorErrFecha.style.visibility = 'visible';    
    }
    
    return Result;
}

function VerificarSiResidenteFamiliaNum(IdsDrops)
{
    var DatosResidente = $get(ConstruirId(IdsDrops.DropDlDiaNa.id, 'UC_Datos_Pax_Residente1_DatosResidente', 1));
    var DatosFam = $get(ConstruirId(IdsDrops.DropDlDiaNa.id, 'UC_Datos_Pax_Residente1_DatosFam', 1));
    
    if ((DatosResidente != null) || (DatosFam != null))
    {
        var Fecha = IdsDrops.Dia + '/' + IdsDrops.Mes + '/' + IdsDrops.Anio;
        
        var DvFechas = $get(ConstruirId(IdsDrops.DropDlDiaNa.id, 'UpFechaNacimiento', 1));
    
        if (DatosResidente != null)
        {
            var TipoIdRes = $get(ConstruirId(DatosResidente.id, 'DropDlTipoIDRes', 1));
            
            if (TipoIdRes.options[TipoIdRes.selectedIndex].value == 'MR')
            {                
                var ValidatorRes = $get(ConstruirId(DatosResidente.id, 'CstIDRes', 1));
            
                if (DvFechas.style.display == 'none')
                {
                    ValidatorRes.isvalid = false;
                    ValidatorRes.style.visibility = 'visible';
                    ValidatorRes.title = ObtenerMensajeError('', TipoIdRes.options[TipoIdRes.selectedIndex].value);
                }
                else
                {
                    var TextoIdRes = $get(ConstruirId(DatosResidente.id, 'TxtIDRes', 1));                                
                    TextoIdRes.value = Fecha;
                                    
                    ValidatorRes.isvalid = true;
                    ValidatorRes.style.visibility = 'hidden';
                }
            }
        }
        
        if (DatosFam != null)
        {
            var TipoIdFam = $get(ConstruirId(DatosFam.id, 'DropDlTipoIDFam', 1));
            
            if (TipoIdFam.options[TipoIdFam.selectedIndex].value == 'MR')
            {
                var ValidatorFam = $get(ConstruirId(DatosFam.id, 'CstIDFam', 1));
            
                if (DvFechas.style.display == 'none')
                {
                    ValidatorFam.isvalid = false;
                    ValidatorFam.style.visibility = 'visible';
                    ValidatorFam.title = ObtenerMensajeError('', TipoIdFam.options[TipoIdFam.selectedIndex].value);
                }
                else
                {
                    var TextoIdFam = $get(ConstruirId(DatosFam.id, 'TxtIDFam', 1));
                    TextoIdFam.value = Fecha;
                    
                    ValidatorFam.isvalid = true;
                    ValidatorFam.style.visibility = 'hidden';
                }
            }
        }
    }
}

function VerificarFechaNacCustom(source, args)
{
    var Result = true;

    if ($get(ConstruirId(source.id, 'UpFechaNacimiento', 1)).style.display != 'none')
    {
        var indice = source.id.split('_');
        indice = indice[indice.length - 2];
        indice = indice.substring(3,5);
        
        var IdsDrops = ObtenerDropsPagina(indice, '', '', '');
               
        Result = VerificarFechaNac(IdsDrops);        
    }
    
    args.IsValid = Result;
}

function GuardarFechasResumenPasajeros(id)
{
    var GuardaDias = $get(ConstruirId(id, 'UC_Datos_Pasajeros1_Fechas', 1));
    
    GuardaDias.value = "";
    
    var DtlPax = $get(ConstruirId(id, 'UC_Datos_Pasajeros1_DtLPasajeros', 1));
    
    for (j = 0; j < DtlPax.children[0].children.length; j++)
    {
        var IdsDrops = ObtenerDropsPagina(j, '', '', '');
        
        if ($get(ConstruirId(IdsDrops.DropDlDiaNa.id, 'UpFechaNacimiento', 1)).style.display == 'none')
        {
            GuardaDias.value += ';';
        }
        else
        {
            GuardaDias.value += ';' + IdsDrops.Anio + ',' + IdsDrops.Mes + ',' + IdsDrops.Dia;
        }
    }
    
    GuardaDias.value = GuardaDias.value.substring(1);
}

function VerificarFechaCalendario(elem, ida) {

    var fechaSelec = elem.nextSibling.innerHTML;

    var fechaAnt, fechaComparar, fechaCheck, lblFecha, lblFechaCambio, fechaPresentacion, fechaCambioPresentacion;

    var prefijo = 'ctl00_MainContentPlaceHolder_UC_DispCalendario1_UC_Cal_OW1_';

    if (ida) {

        fechaAnt = $get(prefijo + 'HiddenFechaIda');

        fechaComparar = $get(prefijo + 'HiddenFechaVuelta');

        lblFecha = $get(prefijo + 'Label2');

        lblFechaCambio = $get(prefijo + 'lblVuelta');

        fechaPresentacion = $get(prefijo + 'HiddenFechaIdaPresentacion');

        fechaCambioPresentacion = $get(prefijo + 'HiddenFechaVueltaPresentacion');
    }
    else {

        fechaAnt = $get(prefijo + 'HiddenFechaVuelta');
        
        fechaComparar = $get(prefijo + 'HiddenFechaIda');

        lblFecha = $get(prefijo + 'lblVuelta');

        lblFechaCambio = $get(prefijo + 'Label2');

        fechaPresentacion = $get(prefijo + 'HiddenFechaVueltaPresentacion');

        fechaCambioPresentacion = $get(prefijo + 'HiddenFechaIdaPresentacion');
    }

    if (fechaSelec != fechaComparar.value) {

        var fechaSelecAux = fechaSelec.split(' ');

        var fechaCompararAux = fechaComparar.value.split(' ');

        if (fechaSelecAux[0] != fechaCompararAux[0]) {

            if (fechaSelecAux[1] == fechaCompararAux[1]) {

                if ((fechaSelecAux[0] > fechaCompararAux[0]) && ida) {

                    fechaCheck = AsignarCheck(elem, false);

                    ModifFechaCalendario(lblFechaCambio, fechaCambioPresentacion, fechaCheck, fechaComparar.value);

                    fechaComparar.value = fechaCheck;
                }
                else {

                    if ((fechaSelecAux[0] < fechaCompararAux[0]) && !ida) {

                        fechaCheck = AsignarCheck(elem, true);

                        ModifFechaCalendario(lblFechaCambio, fechaCambioPresentacion, fechaCheck, fechaComparar.value);

                        fechaComparar.value = fechaCheck;
                    }
                }
            }
            else {

                if ((fechaSelecAux[0] < fechaCompararAux[0]) && ida) {

                    fechaCheck = AsignarCheck(elem, false);

                    ModifFechaCalendario(lblFechaCambio, fechaCambioPresentacion, fechaCheck, fechaComparar.value);

                    fechaComparar.value = fechaCheck;
                }
                else {

                    if ((fechaSelecAux[0] > fechaCompararAux[0]) && !ida && CompararAnio(lblFecha, lblFechaCambio) &&
                        ObtenerNumeroMes(fechaSelecAux[1]) == 12) {

                        fechaCheck = AsignarCheck(elem, true);

                        ModifFechaCalendario(lblFechaCambio, fechaCambioPresentacion, fechaCheck, fechaComparar.value);

                        fechaComparar.value = fechaCheck;                        
                    }
                }
            }
        }
    }

    ModifFechaCalendario(lblFecha, fechaPresentacion, fechaSelec, fechaAnt.value);
    
    fechaAnt.value = fechaSelec;
}

function AsignarCheck(elem, ida)
{
    var fechaCheck;

    var idCheck = 'ctl00_MainContentPlaceHolder_UC_DispCalendario1_UC_Cal_OW1_';

    if (ida) {

        idCheck += 'gridCalendarioIda_';
    }
    else {

        idCheck += 'gridCalendarioVuelta_';
    }

    var idAux = elem.id.split('_');

    idCheck += idAux[idAux.length - 2] + '_';

    if (ida) {

        idCheck += 'RadioButton';
    }
    else {

        idCheck += 'RadioVuelta';
    }

    var numRadio = idAux[idAux.length - 1].substring(idAux[idAux.length - 1].length, idAux[idAux.length - 1].length - 1);

    var elemChek = $get(idCheck + numRadio);

    if (elemChek.disabled == false &&
        elem.nextSibling.innerHTML == elemChek.nextSibling.innerHTML) {

        fechaCheck = elem.nextSibling.innerHTML;

        elemChek.checked = true;
    }
    else {

        var fechaSeleccionada = elem.nextSibling.innerHTML.split(' ')[0];

        for (var i = 0; i < 7; i++) {

            var elemAux = $get(idCheck + i);

            if (elemAux.disabled == false) {
            
                var fechaAux = elemAux.nextSibling.innerHTML.split(' ')[0];

                if (ida) {

                    if (fechaAux <= fechaSeleccionada) {

                        fechaCheck = elemAux.nextSibling.innerHTML;

                        elemAux.checked = true;

                        break;
                    }
                }
                else {

                    if (fechaAux >= fechaSeleccionada) {

                        fechaCheck = elemAux.nextSibling.innerHTML;

                        elemAux.checked = true;

                        break;
                    }
                }
            }
        }
    }

    return fechaCheck;
}

function ModifFechaCalendario(lblFecha, fechaPresentacion, fechaSelec, fechaAnt) {

    var lblFechaAux = lblFecha.innerHTML.split(' ');    

    var fechaSelectAux = fechaSelec.split(' ');
    
    var fechaAntAux = fechaAnt.split(' ');
    
    fechaSelectAux[1] = ObtenerNumeroMes(fechaSelectAux[1]);
    
    fechaAntAux[1] = ObtenerNumeroMes(fechaAntAux[1]);

    var anio = lblFechaAux[0].split('/')[2];

    if (fechaSelectAux[1] == 12 || fechaAntAux[1] == 12) {
    
        var restaMes = fechaSelectAux[1] - fechaAntAux[1];

        if (restaMes > 0) {

            anio--;
        }
        else if (restaMes < 0) {

            anio++;
        }
    }

    var fechaResult = fechaSelectAux[0] + '/' + fechaSelectAux[1] + '/' + anio;

    fechaPresentacion.value = fechaResult;

    for (var i = 1; i < lblFechaAux.length; i++) {

        fechaResult += ' ' + lblFechaAux[i];
    }

    lblFecha.innerHTML = fechaResult;
}

function ObtenerNumeroMes(mesText) {

    var mesNum;

    switch (mesText.toUpperCase()) {

        case 'ENE':
            mesNum = '01';
            break;

        case 'FEB':
            mesNum = '02';
            break;

        case 'MAR':
            mesNum = '03';
            break;

        case 'ABR':
            mesNum = '04';
            break;

        case 'MAY':
            mesNum = '05';
            break;

        case 'JUN':
            mesNum = '06';
            break;

        case 'JUL':
            mesNum = '07';
            break;

        case 'AGO':
            mesNum = '08';
            break;

        case 'SEP':
            mesNum = '09';
            break;

        case 'OCT':
            mesNum = '10';
            break;

        case 'NOB':
            mesNum = '11';
            break;

        case 'DIC':
            mesNum = '12';
            break;
    }

    return mesNum;
}

function CompararAnio(lblFecha, lblFechaCambio) {

    var result = false;

    var lblFechaAux = lblFecha.innerHTML.split(' ');

    var lblFechaCambioAux = lblFechaCambio.innerHTML.split(' ');

    if (lblFechaAux[0].split('/')[2] == lblFechaCambioAux[0].split('/')[2]) {

        result = true;
    }

    return result;
}