﻿function PassPickerSetup(isMatch, allowedFlightType, isStartup)
{
    if (isMatch != null)
    {
        document.getElementById("ResidentEligible").value = isMatch;
    }
    if (allowedFlightType != null)
    {
        document.getElementById("FlightType").value = allowedFlightType;
    }
    
    var paxAdt = document.getElementById(passpickerId[0]+"_PaxAdt");
    var paxChd = document.getElementById(passpickerId[0]+"_PaxChd");
    var paxInf = document.getElementById(passpickerId[0]+"_PaxInf");
    var paxSrc = document.getElementById(passpickerId[0]+"_PaxSrc");
    var paxYth = document.getElementById(passpickerId[0]+"_PaxYth");
    
    var promotionBox = null;
    
    
    try {
        if (promotionId.constructor.toString().indexOf("Array") == -1)
        {
            // no array
            promotionBox = document.getElementById(promotionId);
        }
        else
        {
            // is array
            promotionBox = document.getElementById(promotionId[0]);
        }
    } 
    catch(e) {}
    
    var resident = document.getElementById(passpickerId[0]+"_ResidentControl");
    if (resident != null)
    {
        var residentEnabled = document.getElementById("ResidentEligible").value == "true"
                        && document.getElementById("FlightType").value != "false" 
                        && (paxSrc == null || paxSrc.selectedIndex == 0) 
                        && (paxYth == null || paxYth.selectedIndex == 0);
        
        if (!residentEnabled && !isStartup)
        {
            resident.checked = false;
        }
        
        resident.disabled = !residentEnabled;
        resident.parentNode.disabled = !residentEnabled;
    }
        

    if ((paxAdt == null && paxChd == null && paxInf == null) || (paxSrc == null && paxYth == null))
    {
        return;
    }
    
    if (paxInf.selectedIndex == 0 && paxSrc.selectedIndex == 0 && 
        paxYth.selectedIndex == 0 && paxAdt.selectedIndex == 0 &&
        paxChd.selectedIndex == 0 && paxInf.selectedIndex == 0)
    {
            paxInf.disabled = paxSrc.disabled = paxYth.disabled = 
                paxAdt.disabled = paxChd.disabled = paxInf.disabled = false;
            
            if (promotionBox != null && !isStartup)
            {
                promotionBox.disabled = true;
            }
            
            return;
    }
    
    
    if (!paxAdt.disabled)
    {
        if (paxAdt.selectedIndex == 0 && paxChd.selectedIndex == 0 && paxInf.selectedIndex == 0)
        {
            if(paxYth.selectedIndex == 0)
            {
                paxAdt.disabled = paxChd.disabled = paxInf.disabled = paxYth.disabled = true;
                paxSrc.disabled = false;
            }
            if(paxSrc.selectedIndex == 0)
            {
                paxYth.disabled = false;
                paxAdt.disabled = paxChd.disabled = paxInf.disabled = paxSrc.disabled = true;
            }
        }
        else
        {
            paxInf.disabled = paxSrc.disabled = paxYth.disabled = true;
            paxAdt.disabled = paxChd.disabled = paxInf.disabled = false;
        }

        if (promotionBox != null && !isStartup) 
        {
            promotionBox.disabled = paxAdt.disabled || paxAdt.selectedIndex == 0 || resident.checked || document.getElementById("FlightType").value == "false";
        }
        
        return;
    }
    
    if (paxInf.selectedIndex == 0 && paxSrc.selectedIndex == 0 && paxYth.selectedIndex == 0 && !isStartup)
    {
        paxInf.disabled = paxSrc.disabled = paxYth.disabled = true;
        paxAdt.disabled = paxChd.disabled = paxInf.disabled = false;
        if (congressBox != null)
        {
            congressBox.disabled = true;
        }
    }            
}

function DisableIfNotNull(control, value)
{
    if (control != null)
    {
        alert(control);
    }
}

function GetIndexIfNotNull(control)
{
    return control == null ? 0 : control.selectedIndex;
}

