var host = "http://quotes.automotive.com/site.utils/DropDownJSONWsit";

var saveJson = "";

function loadXMLDoc (url, dropdownSet, controlIndex, baseUrl) 
{
	this.dropdownSet = dropdownSet;
	this.controlIndex = controlIndex;
	this.url = url;
	this.baseUrl = baseUrl;
	
	var self = this;
	
	if (true)
	{
		url += "callback=?";
		$.getJSON(url, function (data){  
			ProcessXML(self, data); 
		});
	}
}

function ProcessXML (self,jsonArr)
{	
	var controlIndex = self.controlIndex;

			if (self.dropdownSet.ConsistencyCheckRecursive (self.url, controlIndex) == false)
				return;

			var control = document.getElementById (self.dropdownSet.controlList[controlIndex]);
			control.length = 0;
		
			for (var i = 0; i < jsonArr.length; i++)
			{
//alert(jsonArr[i].name);
				if (self.dropdownSet.DropdownValueIsNull(jsonArr[i].name) || self.dropdownSet.DropdownValueIsNull(jsonArr[i].value))
					if (typeof jsonArr[i].value =='object'){
						jsonArr[i].value = "";
					}
					control.options[control.options.length] = new Option (jsonArr[i].name, jsonArr[i].value);
			}

	
			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;
			}
			
			
// call back function specific to the dropdown. 
			if ((typeof self.dropdownSet.callbackFunList == 'object' && self.dropdownSet.callbackFunList instanceof Array) ) {
				if (self.controlIndex < self.dropdownSet.callbackFunList.length) {
					if(typeof self.dropdownSet.callbackFunList[self.controlIndex] == 'function') {	
						self.dropdownSet.callbackFunList[self.controlIndex]();		// call the function
					}
				}
			}

			if(typeof self.dropdownSet.callback_once == 'function') {
				self.dropdownSet.callback_once();
				self.dropdownSet.callback_once = null;
			}
		
			if (self.dropdownSet.DropdownValueIsNull(control.value))
			{
				self.dropdownSet.hiddenControlList[controlIndex].value = control.value;
				self.dropdownSet.DropdownOnChange (controlIndex);
			}
}




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) && this.DropdownValueIsNull(control.value))
	{
		this.initValueList[index] = control.value;
		for (var i = 0; i < this.childList[index].length; i++) {
			this.PopulateDropdown (this.childList[index][i]);
		}
	}
}

function DropdownOnChangeCallbackOnce(index, callbackoncefun)
	{
		
		this.callback_once = callbackoncefun;
			
		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) && this.DropdownValueIsNull(control.value))
		{
			this.initValueList[index] = control.value;
			for (var i = 0; i < this.childList[index].length; i++)
				this.PopulateDropdown (this.childList[index][i]);
		}

	}

function setCallbackFun(index, callbackfun)
{
	if (!(typeof this.callbackFunList == 'object' && this.callbackFunList instanceof Array) ) {
		this.callbackFunList = new Array();
	}

	this.callbackFunList[index] = callbackfun;
}




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;
	
	this.callbackFunList = new Array();

	/* 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.DropdownOnChangeCallbackOnce = DropdownOnChangeCallbackOnce;	
	DropdownSet.prototype.setCallbackFun = setCallbackFun;

	DropdownSet.prototype.DropdownOnChange = DropdownOnChange;
	DropdownSet.prototype.GetQueryString = GetQueryString;
	DropdownSet.prototype.PopulateDropdown = PopulateDropdown;
	DropdownSet.prototype.InitControlRecursive = InitControlRecursive;
	DropdownSet.prototype.ConsistencyCheckRecursive = ConsistencyCheckRecursive;
	DropdownSet.prototype.GetParentQueryStringRecursive = GetParentQueryStringRecursive;
	DropdownSet.prototype.DropdownValueIsNull = function (value){return ((value != null) && (value != '') && (value != {}));}

	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.className = 'Input_Disabled';
		control.disabled = true;
	}
	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 '';
}
