//var host = "/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;
			
			if ((self.dropdownSet.urlList[controlIndex] == "JATOImage.aspx") ||
				(self.dropdownSet.urlList[controlIndex] == "NewCarImage.aspx"))
			{
				var control = document.getElementById (self.dropdownSet.controlList[controlIndex]);
				control.src = self.dropdownSet.initValueList[controlIndex] + response;
			}
			else
			{
				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;
						
					for (i = 0; i < control.length && control.selectedIndex == 0; i++)
					{
						tempControlValue = control.options[i].value;
						if ((tempControlValue != null) && (tempControlValue.indexOf ("|") >= 0))
							tempControlValue = tempControlValue.substring (0, tempControlValue.indexOf ("|"));
						if (tempControlValue == self.dropdownSet.initValueList[controlIndex])
							control.selectedIndex = i;
					}
				
					if (control.value != "")
					{
						self.dropdownSet.hiddenControlList[controlIndex].value = control.value;
						if ((self.dropdownSet.hiddenControlList[controlIndex].value != null) && (self.dropdownSet.hiddenControlList[controlIndex].value.indexOf ("|") >= 0))
							self.dropdownSet.hiddenControlList[controlIndex].value = self.dropdownSet.hiddenControlList[controlIndex].value.substring (0, self.dropdownSet.hiddenControlList[controlIndex].value.indexOf ("|"));
						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 ((hiddenControl.value != null) && (hiddenControl.value.indexOf("|") >= 0))
			hiddenControl.value = hiddenControl.value.substring (0, hiddenControl.value.indexOf ("|"));
	}				
	
	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, titleTagList, filterList, hostUrl, currentUrl, contentFilters)
{
	this.hostUrl = hostUrl
	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.titleTagList = titleTagList;
	this.filterList = filterList;
	this.currentUrl = currentUrl;
	this.contentFilters = contentFilters;
	
	/* 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.titleTagList[this.childList[i][j]]);
		this.InitControlRecursive (this.childList[i][j]);
	}
}

function InitControl (controlId, hiddenControlId, displayedTag, titleTag)
{
    control = document.getElementById (controlId);
	hiddenControl = document.getElementById (hiddenControlId);
	
	if ((control != null) && (control.options != null))
	{
		control.length = 0;	
		if ((titleTag != null) && (titleTag != ""))
		    control.options[control.options.length] = new Option (titleTag);
		else
		    control.options[control.options.length] = new Option ('Select ' + displayedTag, '');
		control.disabled = true;
	}
	if ((hiddenControl != null) && (control != null))
	{
		hiddenControl.value = control.value;
		if ((hiddenControl.value != null) && (hiddenControl.value.indexOf("|") >= 0))
			hiddenControl.value = hiddenControl.value.substring (0, hiddenControl.value.indexOf ("|"));
	}
}

function PopulateDropdown (i)
{
	var url = this.hostUrl + '/' + 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 + 'currentUrl=' + escape (this.currentUrl) + '&contentFilters=' + this.contentFilters + '&';
}

function GetIndividualQueryString (tag, value)
{
	if ((value != null) && (value != ""))
		return tag + '=' + escape (value) + '&';
	else return '';
}