// function to get the object by id
function GetThisFromID(id) 
{
    var el = (document.getElementById) ? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? getlayerRef(id,document): null;
    if (el)
        el.css = (el.style) ? el.style: el;
    return el;
}

// function to create a class of SelectArray (has two arrays)
function SelectArrayClass(displayArray, valueArray)
{
    this.displayArray = displayArray;
    this.valueArray = valueArray;
}

// a class with a main ID and an array of child ids
function TwoDRelationClass(mainID, array)
{
    this.arrayMainID = mainID;
    this.childIDsArray = array;
}

// a function that takes a selectID to the combo to be filled, SelectArrayClass to be filled
// a relationship array and the parent id selected
function fillRelatedItemsIntoSelect(selectID, SelectArrayClass, TwoDRelationObjectArray, mainIDSelected)
{
    var select = GetThisFromID(selectID);
    
    for (i = 0; i < TwoDRelationObjectArray.length; i++)
    {
        if (TwoDRelationObjectArray[i].arrayMainID == mainIDSelected)
        {
            for (x = 0; x < TwoDRelationObjectArray[i].childIDsArray.length; x++)
            {
                for (y = 0; y < SelectArrayClass.valueArray.length; y++)
                {
                    if (SelectArrayClass.valueArray[y] == TwoDRelationObjectArray[i].childIDsArray[x])
                    {
                        select.add(new Option(SelectArrayClass.displayArray[y],SelectArrayClass.valueArray[y]));
                        break;
                    }
                }
            }
            select.selectedIndex = 0;
            return;
        }
    }
}

// a simple function to fill a SelectArrayClass into a select dynnamically
function fillItemsIntoSelect(selectID, SelectArrayClass)
{
    var select = GetThisFromID(selectID);

    for (i = 0; i < SelectArrayClass.displayArray.length; i++)
        select.add(new Option(SelectArrayClass.displayArray[i],SelectArrayClass.valueArray[i]));
    
    select.selectedIndex = 0;
}

// a function to delete all the items of a select
function clearSelect(selectID)
{
    var select = GetThisFromID(selectID);
    select.options.length = 0;
}

// a function to get the new parent selected id and fill the children according to the id
function OnMasterDetailSelectChanged(masterSelect, detailSelect, masterArray, detailArray, relationArray)
{
    // 1. remove all the details from the select
    clearSelect(detailSelect);
    var masterSelectThis = GetThisFromID(masterSelect);
    
    fillRelatedItemsIntoSelect(detailSelect, detailArray, relationArray, masterSelectThis.value);
    var detailSelectThis = GetThisFromID(detailSelect);
    //detailSelectThis.selectedIndex = -1;
}

// not used for now    
function MasterChildInfoClass(masterChildInfoNodeClassArray)
{
    this.masterSelectArray = new SelectArrayClass(new Array(), new Array());
    this.TwoDRelationClassArray = new Array(); // of TwoDRelationClass
    
    for (i = 0; i < masterChildInfoNodeClassArray.length; i++)
    {
        var isInSelectList = 0;
        for (x = 0; x < this.masterSelectArray.valueArray.length; x++)
        {
            if (masterChildInfoNodeClassArray[i].masterID == this.masterSelectArray.valueArray[x])
            {
                isInSelectList = 1;
                break;
            }
        }
        if (isInSelectList == 0) // not in list
        {
            this.masterSelectArray.valueArray[this.masterSelectArray.valueArray.length] = masterChildInfoNodeClassArray[i].masterID;
            this.masterSelectArray.displayArray[this.masterSelectArray.displayArray.length] = masterChildInfoNodeClassArray[i].masterDisplay;
        }            
        
        var isInRelationList = 0;
        for (x = 0; x < this.TwoDRelationClassArray.length; x++)
        {
            if (masterChildInfoNodeClassArray[i].masterID == this.TwoDRelationClassArray[x].arrayMainID)
            {
                isInRelationList = 1;
                break;
            }
        }
        
        if (isInRelationList == 0) // not in list
        {
            var childIDsArray = new Array();
            for (y = 0; y < masterChildInfoNodeClassArray.length; y++)
            {
                if (masterChildInfoNodeClassArray[y].masterID == masterChildInfoNodeClassArray[i].masterID)
                    childIDsArray[childIDsArray.length] = masterChildInfoNodeClassArray[y].childID;
            }

            this.TwoDRelationClassArray[this.TwoDRelationClassArray.length] = 
                    new TwoDRelationClass(masterChildInfoNodeClassArray[i].masterID, childIDsArray);
        }
    }
}
