var host = "/site.utils/DropDownXML";

function loadXMLDoc (url, dropdownSet, controlIndex, baseUrl) 
{
	this.dropdownSet = dropdownSet;
	this.controlIndex = controlIndex;
	this.url = url;
	this.baseUrl = baseUrl;
	
	/* branch for native XMLHttpRequest object */
	if (window.XMLHttpRequest) 
	{
		this.req = new XMLHttpRequest ();		
		var self = this;
		this.req.onreadystatechange = function ()
		{
			ProcessXML (self);
		}
		this.req.open ("GET", url, true);
		this.req.send (null);
	} 
	/* branch for IE/Windows ActiveX version */
	else if (window.ActiveXObject) 
	{
		this.req = new ActiveXObject ("Microsoft.XMLHTTP");
		var self = this;
		if (this.req) 
		{
			this.req.onreadystatechange = function ()
			{
				ProcessXML (self);
			}
			this.req.open ("GET", url, true);
			this.req.send ();
		}
	}
}

function ProcessXML (self)
{
	var controlIndex = self.controlIndex;
	/* only if req shows "complete" */
	if (self.req.readyState == 4) 
	{
		/* only if "OK" */
		if (self.req.status == 200) 
		{
			if (self.dropdownSet.ConsistencyCheckRecursive (self.url, controlIndex) == false)
				return;
			
			var response = self.req.responseText;
			var tabbedLists = response.split ("\n");
			if (tabbedLists.length == 2)
			{
				var tags = tabbedLists[0].split ("\t");
				var valueTags = tabbedLists[1].split ("\t");
			}

			if (tags.length != valueTags.length)
				return;
				
			var control = document.getElementById (self.dropdownSet.controlList[controlIndex]);
			control.length = 0;
			
			for (var i = 0; i < tags.length; i++)
			{
				if ((tags[i] != null && tags[i] != "") || (valueTags[i] != null && valueTags[i] != ""))
					control.options[control.options.length] = new Option (tags[i], valueTags[i]);
			}
	
			control.disabled = false;
		    control.className = "Input_Text";
				
			for (i = 0; i < control.length && control.selectedIndex == 0; i++)
			{
				if (control.options[i].value == self.dropdownSet.initValueList[controlIndex])
					control.selectedIndex = i;
			}
		
			if (control.value != "")
			{
				self.dropdownSet.hiddenControlList[controlIndex].value = control.value;
				self.dropdownSet.DropdownOnChange (controlIndex);
			}
		} 
		else 
			alert("error: " + self.req.statusText);
	}
}

function DropdownOnChange (index)
{
	this.InitControlRecursive (index);
	control = document.getElementById (this.controlList[index]);
	hiddenControl = document.getElementById (this.hiddenControlList[index]);
	if ((hiddenControl != null) && (control != null))
		hiddenControl.value = control.value;
	
	if ((control != null) && (control.value != ''))
	{
		this.initValueList[index] = control.value;
		for (var i = 0; i < this.childList[index].length; i++)
			this.PopulateDropdown (this.childList[index][i]);
	}
}

function DropdownSet (controlList, hiddenControlList, childList, parentList, urlList, displayValueList, nameTagList, initValueList, filterList)
{
	this.controlList = controlList;
	this.hiddenControlList = hiddenControlList;
	this.childList = childList;
	this.parentList = parentList;
	this.urlList = urlList;
	this.displayValueList = displayValueList;
	this.nameTagList = nameTagList;
	this.initValueList = initValueList;
	this.filterList = filterList;
	
	/* for postback, if hiddencontrol is already fill with values, use that instead of init values */
	for (var i = 1; i < this.controlList.length; i++)
	{
		var hiddenControl = document.getElementById (hiddenControlList[i]);
		if ((hiddenControl != null) && (hiddenControl.value != ""))
			initValueList[i] = hiddenControl.value;
	}
	
	DropdownSet.prototype.DropdownOnChange = DropdownOnChange;
	DropdownSet.prototype.GetQueryString = GetQueryString;
	DropdownSet.prototype.PopulateDropdown = PopulateDropdown;
	DropdownSet.prototype.InitControlRecursive = InitControlRecursive;
	DropdownSet.prototype.ConsistencyCheckRecursive = ConsistencyCheckRecursive;
	DropdownSet.prototype.GetParentQueryStringRecursive = GetParentQueryStringRecursive;
	
	this.InitControlRecursive ('0');
	
	for (i = 0; i < this.childList[0].length; i++)
		this.PopulateDropdown (this.childList[0][i]);
}

function InitControlRecursive (i)
{
	for (var j = 0; j < this.childList[i].length; j++)
	{
		InitControl (this.controlList[this.childList[i][j]], this.hiddenControlList[this.childList[i][j]], this.displayValueList[this.childList[i][j]]);
		this.InitControlRecursive (this.childList[i][j]);
	}
}

function InitControl (controlId, hiddenControlId, displayedTag)
{
	control = document.getElementById (controlId);
	hiddenControl = document.getElementById (hiddenControlId);
	
	if (control != null)
	{
		control.length = 0;	
		control.options[control.options.length] = new Option ('Select ' + displayedTag, '');
		control.disabled = true;
		control.className = "Input_Text_Grey";
	}
	if ((hiddenControl != null) && (control != null))
		hiddenControl.value = control.value;
}

function PopulateDropdown (i)
{
	var url = host + '/' + this.urlList[i] + '?' + this.GetQueryString (i);
	new loadXMLDoc (url, this, i, this.urlList[i]);
}

function ConsistencyCheckRecursive (url, i)
{
	if ((this.parentList[i] != null) && (this.parentList[i] != "") && (this.parentList[i] != "0"))
	{
		if ((url.indexOf (this.nameTagList[this.parentList[i]] + "=") < 0) || (url.indexOf (GetIndividualQueryString (this.nameTagList[this.parentList[i]], document.getElementById (this.controlList[this.parentList[i]]).value)) < 0))
			return false;
		else
			return this.ConsistencyCheckRecursive (url, this.parentList[i]);
	}
	return true;
}

function GetParentQueryStringRecursive (i)
{
	if (this.parentList[i] == 0)
		return "";
	else
		return GetIndividualQueryString (this.nameTagList[this.parentList[i]], document.getElementById (this.controlList[this.parentList[i]]).value) + this.GetParentQueryStringRecursive (this.parentList[i]);
}

function GetQueryString (i)
{
	queryString = this.GetParentQueryStringRecursive (i);
	for (i = 0; i < this.filterList.length; i++)
		queryString += GetIndividualQueryString (this.filterList[i][0], this.filterList[i][1]);
		
	return queryString;
}

function GetIndividualQueryString (tag, value)
{
	if ((value != null) && (value != ""))
		return tag + '=' + value.replace ("?", "%3f").replace ("&", "%26") + '&';
	else return '';
}