/*
 *  Attribute Tree
 */
AttrTree = function()
{
    this.attributes = new Array();
}

AttrTree.prototype.getAttribute = function(attrEntry)
{
    for (var i = 0; i < this.attributes.length; i++) {
        if (attrEntry.equals(this.attributes[i])) {
            return this.attributes[i];
        }
    }
    return false;
}

/*
 *  Attribute Entry
 */
AttrEntry = function(inName, inValue, inCatEntryId)
{
    this.name = inName;
    this.value = inValue;
    this.catEntryId = inCatEntryId;
    this.subAttributes = new Array();
}

AttrEntry.prototype.toString = function()
{
    return	"name='" + this.name +  "'  " +
            "value='" + this.value +  "'  " +
            "catEntryId='" + this.catEntryId +  "'";
}

AttrEntry.prototype.equals = function(attrEntry)
{
    return this.name == attrEntry.name;
}

AttrEntry.prototype.contains = function(attrEntry)
{
    for (var i = 0; i < this.subAttributes.length; i++) {
        if (this.subAttributes[i].equals(attrEntry)) {
            return true;
        }
    }
    return false;
}

/**
 * If there are additional select boxes, populate them accordingly
 * @param dd The select box that triggered the onchange event
 */
function populateNext(dd, initialPopulate)
{
    // Prepare the next select box to populate
    var toPopulate = attrSelectBoxes[parseInt(dd.level)+1];

	// If the 'choose' option was selected,  reset sub attributes
	if (dd.selectedIndex == 0)
	{
        var selectBoxIndex = parseInt(dd.level);
		for (var i = selectBoxIndex+1; i < attrSelectBoxes.length; i++)
		{
			resetSelectBox(attrSelectBoxes[i]);
		}
	}

	// If an option was selected and there are more select boxes
	// attempt to populate the next select box with the selected attribute's sub attributes
	else if (dd.selectedIndex != 0 && toPopulate)
	{
		// Check to see if the new parent attribute contains the previously 
		// selected sub attribute, if it does, keep it selected
		if (toPopulate.selectedIndex != 0 && dd[dd.selectedIndex].attr.contains(toPopulate[toPopulate.selectedIndex].attr))
		{
			populateWithSelected(toPopulate, dd[dd.selectedIndex].attr);
		}

		// otherwise, repopulate the next attribute dropdown with
		// the newly selected parent attribute's sub attributes
		else
		{
			var attr = dd[dd.selectedIndex].attr;
			toPopulate.options.length = 1;
			for (var i = 0, j = 1; i < attr.subAttributes.length; i++, j++)
			{
				toPopulate[j] = new Option(attr.subAttributes[i].name, attr.subAttributes[i].value, false, false);
				toPopulate[j].attr = attr.subAttributes[i];
			}

			// Needed for Netscape 4.0 otherwise last option loaded will be selected
			toPopulate.selectedIndex = 0;

			// Enable the newly populated select box 
			toPopulate.disabled = false;
			
			if (initialPopulate)
			{
				singleOptionCheck(toPopulate);
			}
		}
	}
}

/**
 * Populates the select box and sets the selected option to the selected attribute entry passed in.
 * @param dd The select box to populate
 * @param selectedAttr The previously selected attribute entry
 */
function populateWithSelected(dd, selectedAttr)
{
	var prevSelectedIndex = 0;
	var prevSelected = dd[dd.selectedIndex].attr;
	dd.options.length = 1;
	for (var i = 0, j = 1; i < selectedAttr.subAttributes.length; i++, j++)
	{
		dd[j] = new Option(selectedAttr.subAttributes[i].name, selectedAttr.subAttributes[i].value, false, false);
		dd[j].attr = selectedAttr.subAttributes[i];
		if ( dd[j].attr.equals(prevSelected) )
		{
			prevSelectedIndex = i+1;
		}
	}
	dd.selectedIndex = prevSelectedIndex;

    // If this is not the last select box, populate the next select box
    if (parseInt(dd.level) < attrSelectBoxes.length)
    {
        populateNext(attrSelectBoxes[dd.level]);
    }
}

/**
 * If the select box contains a single option, select it.
 * @param dd The select box to check
 */
function singleOptionCheck(dd)
{
	if (dd.options.length == 2)
	{
		dd.selectedIndex = 1;
        // If this is not the last select box, populate the next select box
        if (parseInt(dd.level) < attrSelectBoxes.length)
        {
            populateNext(dd, true);
        }
	}
}

/**
 * Sets the selected index to the first option and removes the remaining options
 * @param dd The select box to reset
 */
function resetSelectBox(dd)
{
	dd.selectedIndex = 0;
	dd.options.length = 1;
	dd.disabled = true;
}
