﻿var cboPilotName = null;
var txtPilotName = null;
var txtPilot = null;
var hdnSelectedMissionTypeID = null;

function ShowDatePicker(element_id)
{
    window.open("DatePicker.html?" + escape(element_id), "DatePicker", "top=200, left=200, width=310, height=280, directories=no, location=no, menubar=no, resizable=no, scrollbars=no, status=no, titlebar=no, toolbar=no");
}

function Pilot(id)
{
    this.ID = id;
    this.MissionTypes = new Array();
    
    this.HasMissionType = function(id)
    {
        for(var i = 0; i < this.MissionTypes.length; i++) {
            if(this.MissionTypes[i] == id)
                return true;
        }
        
        return false;
    }
}

function MissionType(id, external_load, button_id)
{
    this.ID = id;
    this.ExternalLoad = external_load;
    this.Button = document.getElementById(button_id);
    this.Categories = new Array();
    
    this.HasCategory = function(id)
    {
        for(var i = 0; i < this.Categories.length; i++) {
            if(this.Categories[i] == id)
                return true;
        }
        
        return false;
    }
}

function Category(id)
{
    this.ID = id;
    this.RiskFactors = new Array();
}

function RiskFactor(id, button_id)
{
    this.ID = id;
    this.Button = document.getElementById(button_id);
}

var Pilots = new Array();
var MissionTypes = new Array();
var Categories = new Array();

function GetPilotByID(id)
{
    for(var i = 0; i < Pilots.length; i++) {
        if(Pilots[i].ID == id)
            return Pilots[i];
    }
    
    return null;
}

function AddMissionTypeToPilot(pilot_id, mission_type_id)
{
    var pilot = GetPilotByID(pilot_id);
    if(!pilot) {
        pilot = new Pilot(pilot_id);
        Pilots.push(pilot);
    }

    pilot.MissionTypes.push(mission_type_id);
}

function GetMissionTypeByID(id)
{
    for(var i = 0; i < MissionTypes.length; i++) {
        if(MissionTypes[i].ID == id)
            return MissionTypes[i];
    }
    
    return null;
}

function AddCategoryToMissionType(mission_type_id, category_id)
{
    var type = GetMissionTypeByID(mission_type_id);
    if(!type)
        return;
        
    type.Categories.push(category_id);
}

function GetCategoryByID(id)
{
    for(var i = 0; i < Categories.length; i++) {
        if(Categories[i].ID == id)
            return Categories[i];
    }
    
    return null;
}

function AddRiskFactorToCategory(category_id, risk_factor)
{
    var category = GetCategoryByID(category_id);
    if(!category) {
        category = new Category(category_id);
        Categories.push(category);
    }
    
    category.RiskFactors.push(risk_factor);
}

// Called when the selected pilot is changed.
function SelectedPilotChanged() {


    var id = cboPilotName.options[cboPilotName.selectedIndex].value;
    var pilot = (cboPilotName.selectedIndex == 0) ? "" : cboPilotName.options[cboPilotName.selectedIndex].text;
   
    txtPilotName.value = pilot;
    if(txtPilot != null)
        txtPilot.value = pilot;

    if (cboPilotName.selectedIndex != 0) {
        var _cover = document.getElementById('noneSelectedCover');
        if (_cover) {
            _cover.parentNode.removeChild(_cover);
        }
        
    }else{
        var div = document.createElement("div");
        div.id = "noneSelectedCover";
        var _divTop = document.getElementById('cover').offsetTop;
        div.className = "selected";
        div.style.top = _divTop + "px";
        document.getElementById('cover').appendChild(div);

    } 
    
    // loop through each mission type and disable the ones
    // that the selected pilot does not have access to
    var tmp = GetPilotByID(id);
    for(var i = 0; i < MissionTypes.length; i++) {
        if(id.length > 0 && (tmp == null || tmp.HasMissionType(MissionTypes[i].ID) == false)) {
            GrayOutMissionType(MissionTypes[i].ID);
            if(MissionTypes[i].Button.className == "bulletSlct")
                MissionTypes[i].Button.click();

           
        } else
            UnGrayOutMissionType(MissionTypes[i].ID);

       
        
            
            
    }
}



var ExternalLoadMissionTypeSelected = false;

// Called when the selected mission type is changed.



function SelectedMissionTypeChanged()
{
    var type = GetMissionTypeByID(hdnSelectedMissionTypeID.value);
    ExternalLoadMissionTypeSelected = (type != null) ? type.ExternalLoad : false;
        
    // loop through each category and disable the ones
    // that are not applicable to the selected mission type
    for(var i = 0; i < Categories.length; i++) {
        if(type == null || type.HasCategory(Categories[i].ID)) {
            UnGrayOutCategory(Categories[i].ID);
        } else {
            GrayOutCategory(Categories[i].ID);
            
            // loop through and uncheck each risk factor in this category
            for(var j = 0; j < Categories[i].RiskFactors.length; j++) {
                var rf = Categories[i].RiskFactors[j];
                
                if(rf.Button.className == "bulletSlct")
                    rf.Button.click();
            }
        }
    }
}

var MissionTypeClickInProgress = false;

function MissionTypeClicked(button)
{
    if(MissionTypeClickInProgress)
        return;
    MissionTypeClickInProgress = true;
    
    var type = null;
    
    // get the mission type with the selected button and deselect others
    for(var i = 0; i < MissionTypes.length; i++) {
        if(MissionTypes[i].Button != button && MissionTypes[i].Button.className == "bulletSlct")
            MissionTypes[i].Button.click();
        else if(MissionTypes[i].Button == button)
            type = MissionTypes[i];
    }
    
    var old_value = hdnSelectedMissionTypeID.value;
    hdnSelectedMissionTypeID.value = (type != null && button.className == "bulletSlct") ? type.ID : "";
    
    if(hdnSelectedMissionTypeID.value != old_value)
        SelectedMissionTypeChanged();
        
    MissionTypeClickInProgress = false;
}
