//************************************************
//option class
//************************************************
function rddOption(sValue, sText, sParentValue, bPermanent)
{
	//attributes
	this.Value = sValue;
	this.Text = sText;
	this.ParentValue = sParentValue;
	this.Permanent = bPermanent;
	
	//methods
}

//************************************************
//DropDown class
//************************************************
function rddDropDown(sId, sParentId, sSelectedValue)
{
	//attributes
	this.Id = sId;
	this.ParentId = ((sId != sParentId) ? sParentId : null);
	this.Options = new Array();
	this.Filled = false;
	this.SelectedValue = sSelectedValue;
	
	//methods
	this.AddOption = rddDropDownAddOption;
	this.Initialize = rddDropDownInitialize;
	this.DropDownChanged = rddDropDownDropDownChanged;
	this.Fill = rddDropDownDropFill;
	this.GetSelectedValue = rddDropDownGetSelectedValue;
}

function rddDropDownAddOption(sValue, sText, sParentValue, bPermanent)
{
	this.Options[this.Options.length] = new rddOption(sValue, sText, sParentValue, bPermanent);
}

function rddDropDownInitialize(oManager)
{
	var oDdl = document.getElementById(this.Id);
	var oMe = this;
	if (oDdl)
	{
		if (oDdl.addEventListener)
		{
			oDdl.addEventListener("change", function() { oMe.DropDownChanged(oManager); }, false);
		}
		else
		{
			if (oDdl.attachEvent)
			{
				oDdl.attachEvent("onchange", function() { oMe.DropDownChanged(oManager); });
			}
		}
	}
}

function rddDropDownDropDownChanged(oManager)
{
	var aChildren = oManager.GetMyChildren(this.Id);
	var i = 0;
	for (i=0; i<aChildren.length; i++)
	{
		oManager.DropDowns[aChildren[i]].Filled = false;
	}
	for (i=0; i<aChildren.length; i++)
	{
		if (!oManager.DropDowns[aChildren[i]].Filled) oManager.DropDowns[aChildren[i]].Fill(oManager,true);
	}
	for (i=0; i<aChildren.length; i++)
	{
		oManager.DropDowns[aChildren[i]].DropDownChanged(oManager);
	}
}

function rddDropDownGetSelectedValue()
{
	var sReturn = null;
	var oDdl = document.getElementById(this.Id);
	if (oDdl)
	{
		sReturn = oDdl.value;
	}
	return sReturn;
}

function rddDropDownDropFill(oManager, bFillParents)
{
	var sParentValue = null;
	var iSelectedIndex = 0;
	if ((this.ParentId != null) && (bFillParents))
	{
		oParent = oManager.GetDropDownById(this.ParentId);
		if (oParent)
		{
			if (!oParent.Filled) oParent.Fill();
			sParentValue = oParent.GetSelectedValue();
		}
	}
	var i = 0;
	var oDdl = document.getElementById(this.Id);
	if (oDdl)
	{
		for (i=oDdl.options.length-1; i>=0; i--)
		{
			oDdl.options[i] = null;
		}
		for (i=0; i<this.Options.length; i++)
		{
			if (sParentValue != null)
			{
				if ((sParentValue == this.Options[i].ParentValue) || (this.Options[i].Permanent))
				{
					oDdl.options[oDdl.options.length] = new Option(this.Options[i].Text, this.Options[i].Value);
					if (this.SelectedValue == this.Options[i].Value) iSelectedIndex = oDdl.options.length - 1;
				}
			}
			else
			{
				oDdl.options[oDdl.options.length] = new Option(this.Options[i].Text, this.Options[i].Value);
				if (this.SelectedValue == this.Options[i].Value) iSelectedIndex = oDdl.options.length - 1;
			}
		}
		oDdl.options[iSelectedIndex].selected = true;
	}
	this.Filled = true;
}

//************************************************
//DropDownManager class
//************************************************
function ddrDropDownManager()
{
	//attributes
	this.DropDowns = new Array();
	
	//methods
	this.AddDropDown = ddrDropDownManagerAddDropDown;
	this.Initialize = ddrDropDownManagerInitialize;
	this.GetDropDownById = ddrDropDownManagerGetDropDownById;
	this.GetMyChildren = ddrDropDownManagerGetMyChildren;
}

function ddrDropDownManagerAddDropDown(sId, sParentId, sSelectedValue)
{
	this.DropDowns[this.DropDowns.length] = new rddDropDown(sId, sParentId, sSelectedValue);
	return this.DropDowns[this.DropDowns.length-1];
}

function ddrDropDownManagerInitialize()
{
	var i = 0;
	for (i=0; i<this.DropDowns.length; i++)
	{
		this.DropDowns[i].Initialize(this);
	}
	for (i=0; i<this.DropDowns.length; i++)
	{
		if (!this.DropDowns[i].Filled) this.DropDowns[i].Fill(this, true);
	}
}

function ddrDropDownManagerGetDropDownById(sId)
{
	var i = 0;
	var oDropDown = null;
	while ((i < this.DropDowns.length) && (oDropDown == null))
	{
		if (this.DropDowns[i].Id == sId) oDropDown = this.DropDowns[i];
		i++;
	}
	return oDropDown;
}

function ddrDropDownManagerGetMyChildren(sId)
{
	//returns array indexes
	var aReturn = new Array();
	var i = 0;
	for (i=0; i<this.DropDowns.length; i++)
	{
		if (this.DropDowns[i].ParentId == sId)
		{
			aReturn[aReturn.length] = i;
		}
	}
	return aReturn;
}

var oddrDropDownManager = new ddrDropDownManager();




