﻿function VerifPasajero(elem)
{
    var i, j;
    var Nodepende = true;
    var HidenDependencias = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_Dependencias');
    HidenDependencias = HidenDependencias.value.split('+')
    
    var dependencias = HidenDependencias[0].split(';');
    
    var IdElem = elem.id.split('_')[7];
    IdElem = parseInt(IdElem.substring(3,5));
    
    for (i = 0; i < dependencias.length; i++)
    {
        var Indices = dependencias[i].split('-');
        
        if (parseInt(Indices[0]) == parseInt(IdElem))
        {
            CheckearObj(Indices[0], Indices[1], elem.checked, HidenDependencias[1]);
            Nodepende = false;
            break;
        }
        else
        {
            if (parseInt(Indices[1]) == parseInt(IdElem))
            {
                CheckearObj(Indices[1], Indices[0], elem.checked, HidenDependencias[1]);
                Nodepende = false;
                break;
            }
        }
    }
    
    if (Nodepende)
    {
        ComprobarPaxSeleccionados(IdElem, '', elem.checked, HidenDependencias[1]);
    }
}

function CheckearObj(IndiceParent, IndiceChd, ParentChecked, NumTotalPax)
{
    var IndiceChdAux = ComprobarIndice(IndiceChd);

    var objdepen = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_DtLPasajeros_ctl' + IndiceChdAux + '_ChkSplit');
    objdepen.checked = ParentChecked;
    
    ComprobarPaxSeleccionados(IndiceParent, IndiceChd, ParentChecked, NumTotalPax);
}

function ComprobarPaxSeleccionados(IndiceParent, IndiceChd, ParentChecked, NumTotalPax)
{
    var PaxSeleccionados = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_PaxSlit');    
    
    if (ParentChecked)
    {
        if (ComprobarMaxPax(PaxSeleccionados.value.split(';'), IndiceParent, IndiceChd, NumTotalPax))
        {
            if (PaxSeleccionados.value == '')
            {
                if (IndiceChd == '')
                {
                    PaxSeleccionados.value = IndiceParent;
                }
                else
                {
                    PaxSeleccionados.value = IndiceParent + '-' + IndiceChd;
                }
            }
            else
            {
                if (IndiceChd == '')
                {
                    PaxSeleccionados.value += ';' + IndiceParent;
                }
                else
                {    
                    PaxSeleccionados.value += ';' + IndiceParent + '-' + IndiceChd;
                }
            }
        }
        else
        {
            ParentChecked = false;
        }
    }
    else
    {
        var i;
        var ListPax = PaxSeleccionados.value.split(';');
        var ListPaxSel = '';
        
        for (i = 0; i < ListPax.length; i++)
        {
            var Indices = ListPax[i].split('-');
            
            if ((parseInt(Indices[0]) != parseInt(IndiceParent)) && (parseInt(Indices[0]) != parseInt(IndiceChd)))
            {
                ListPaxSel += ListPax[i] + ';';
            }
        }
        
        if (ListPaxSel != '')
        {
            ListPaxSel = ListPaxSel.substring(0, ListPaxSel.length - 1);
        }
        
        PaxSeleccionados.value = ListPaxSel;
    }
    
    PintarFilas(IndiceParent, IndiceChd, ParentChecked);
}

function ComprobarMaxPax(ListPax, IndiceParent, IndiceChd, NumTotalPax)
{
    var i;
    var Resultado = true;
    var ListPaxSel = 0;
    var IndcicesAdd = 1;
        
    for (i = 0; i < ListPax.length; i++)
    {
        ListPaxSel += ListPax[i].split('-').length;
    }
    
    if (IndiceChd != '')
    {
        IndcicesAdd = 2;
    }
    
    if ((IndcicesAdd + ListPaxSel) == NumTotalPax)
    {
        Resultado = false;
    
        var objParent = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_DtLPasajeros_ctl' + ComprobarIndice(IndiceParent) + '_ChkSplit');
        objParent.checked = false;
    
        if (IndiceChd != '')
        {
            var objChd = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_DtLPasajeros_ctl' + ComprobarIndice(IndiceChd) + '_ChkSplit');
            objChd.checked = false;    
        }
        
        BuscarErrorCache("95000035");
    }
    
    return Resultado;
}

function ComprobarIndice(Indice)
{
    var IndiceAux = Indice;

    if (IndiceAux.length == undefined)
    {
        if (IndiceAux < 10)
        {
            IndiceAux = '0' + Indice;
        }    
    }
    else
    {
        if (IndiceAux.length == 1)
        {
            IndiceAux = '0' + Indice;
        }
    }
    
    return IndiceAux;
}

function PintarFilas(IndiceParent, IndiceChd, ParentChecked)
{
    var Color;
    
    if (ParentChecked)
    {
        Color = '#CCCCCC';
    }
    else
    {
        Color = '#D3DEEF';
    }
    
    var TblPasajeros = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_DtLPasajeros');
    
    TblPasajeros.children[0].children[parseInt(IndiceParent)].style.backgroundColor = Color;
    
    if (IndiceChd != '')
    {
        TblPasajeros.children[0].children[parseInt(IndiceChd)].style.backgroundColor = Color;    
    }
}

function ComprobarPaxSplit(NumAdultos)
{
    var Resultado = true;
    var PaxSeleccionados = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_PaxSlit');
    
    if (PaxSeleccionados.value == '')
    {
        Resultado = false;
        BuscarErrorCache("95000033");
    }
    else
    {
        var ComprobarPax = ComprobarSiAdultos(PaxSeleccionados.value.split(';'), NumAdultos);

        if (ComprobarPax == 1)
        {
            Resultado = false;
            BuscarErrorCache("95000037");
        }
        else
        {
            if (ComprobarPax == 2)
            {
                Resultado = false;
                BuscarErrorCache("95000036");
            }
        }
    }
    
    return Resultado;
}

function ComprobarSiAdultos(ListPax, NumAdultos)
{
    var Resultado = 0;
    var PrimerIndice = RecuperaPrimerIndice(ListPax, NumAdultos);
    
    if (PrimerIndice == 99)
    {
        Resultado = 1;
    }
    else
    {
        var TipoPax = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_DtLPasajeros_ctl' + ComprobarIndice(PrimerIndice) + '_LblTipoPasajero');
        
        if (TipoPax.innerHTML.trim() != 'Adulto')
        {
            Resultado = 2;
        }
    }
    
    return Resultado;
}

function RecuperaPrimerIndice(ListPax, NumAdultos)
{
    var ListPaxSel = '';
    var PrimerIndice = 99;

    for (i = 0; i < ListPax.length; i++)
    {
        var Indices = ListPax[i].split('-');
        
        ListPaxSel += Indices[0] + ';';
        
        if (parseInt(Indices[0]) < PrimerIndice)
        {
            PrimerIndice = parseInt(Indices[0]);
        }
        
        if (Indices.length > 1)
        {
            ListPaxSel += Indices[1] + ';';
        
            if (parseInt(Indices[1]) < PrimerIndice)
            {
                PrimerIndice = parseInt(Indices[1]);
            }
        }
    }
    
    ListPaxSel = ListPaxSel.substring(0, ListPaxSel.length - 1);
    
    PrimerIndice = ComprobarSiSoloCHD(ListPaxSel, PrimerIndice, NumAdultos);

    return PrimerIndice;
}

function ComprobarSiSoloCHD(ListPaxSel, PrimerIndice, NumAdultos)
{
    var i, j;
    var Indice = PrimerIndice;
    var ListaOrd = OrdenarListaSel(ListPaxSel);
    
    var TblPasajeros = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_DtLPasajeros');
    
    for (i = 0; i < TblPasajeros.children[0].children.length; i++)
    {
        var NoExiste = true;
        
        for (j = 0; j < ListaOrd.length; j++)
        {
            if (i == parseInt(ListaOrd[j]))
            {
                NoExiste = false;
                break;
            }
        }
        
        if (NoExiste)
        {
            if (i >= NumAdultos)
            {
                Indice = 99;
            }
            
            break;
        }        
    }
        
    return Indice;
}

function OrdenarListaSel(ListPaxSel)
{
    var i, j;
    var Listado = ListPaxSel.split(';');

    for (i = 0; i < Listado.length - 1; i++)
    {
        for (j = 0; j < Listado.length - i - 1; j++)
        {
            if (parseInt(Listado[j + 1]) < parseInt(Listado[j]))
            {
                var IndiceAux = Listado[j + 1];
                Listado[j + 1] = Listado[j];
                Listado[j] = IndiceAux;
            }
        }
    }

    return Listado;
}

function ElimPaxSeleccionados()
{
    var PaxSeleccionados = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_PaxSlit');
    PaxSeleccionados.value = '';
}

function GuardarFechaModifPasajero(indice)
{
    var GuardaDias = $get('ctl00_MainContentPlaceHolder_ctl01_UC_Modificar_Pasajero1_Fechas');
    GuardaDias.value = "";
    
    var IdsDrops = ObtenerDropsPagina(indice, '', '', '');
    
    GuardaDias.value += IdsDrops.Anio + ',' + IdsDrops.Mes + ',' + IdsDrops.Dia;
}