debug = new Array();
debugc = 0;

function invUpdateLists(parentID, type) {

	//initialize default values
	var xmlQuery = '';
	var stack = '';
	
	debug[debugc++] = 'The variables intialized';
	
	if (typeof success == "undefined") {success = true;}
	
	if (typeof firstLoad == "undefined") {
		firstLoad = new Array();
		debug[debugc++] = 'Firstload undefined if statement';
	}
	
	if (typeof firstLoad[parentID] == "undefined") {
		firstLoad[parentID] = true;
		debug[debugc++] = 'Firstload ' + firstLoad[parentID] +' undefined if statement';
	} else {
		if(firstLoad[parentID] == true) {
			firstLoad[parentID] = false;
		}
	}
	
	// if this is the firstload for this search, then load default functions, that be loaded once.
	if(firstLoad[parentID] == true) {
		
		debug[debugc++] = 'Firstload for ' + parentID;

		$("#" + parentID + " .search-clear").click(function (e) {
			$("#" + parentID + " select").unbind("change");
			e.preventDefault();
			$("#" + parentID + ' select option:selected').removeAttr('selected');
			$("#" + parentID + ' select option:first-child').attr("selected", "selected");
			$("#" + parentID + " .search-stock-num").val("");
			$("#" + parentID + " select").removeAttr('disabled').css("color", "#000").removeClass("clear-option");
			$("#" + parentID + " .search-count").show();
						
			invUpdateLists(parentID);
		});

		// hide the other input fields in the search if search-stock has text
		var stockChange = function (event) {
			if($(this).val() != '') {
				$("#" + parentID + " select").attr('disabled', true).css("color", "#bbb");
				$("#" + parentID + " .search-count").hide();
			} else {
				$("#" + parentID + " select").removeAttr('disabled').css("color", "#000");
				$("#" + parentID + " .search-count").show();
			}
		};
		
		$("#" + parentID + " .search-stock-num").keyup(stockChange);
		$("#" + parentID + " .search-stock-num").change(stockChange);
		$("#" + parentID + " .search-stock-num").mouseenter(stockChange);
	}
		
	// -- Prevent multiple calls at once --
	$("#" + parentID + " select").unbind("change");
	$("#" + parentID + " .loading").text("Loading please wait...");
	
	// track selected items
	var selectArray = new Array();
	var tagNameList = '';
		
	// build the query, save current selection, and build the stack option
	$("#" + parentID + " select").each( function() {
		var selectName = $(this).attr("name");
		var selected = $(this).val();
		
		// default to any if value is blank. this is due to passing it to the inventory paramater.
		if(selected == null) {selected = '';}
		
		//save current selection	
		selectArray[selectName] = selected;
								
		// build the query paramters
		xmlQuery = xmlQuery + "&" + selectName + "=" + selected;
		
		switch($(this).attr('name')) {
			case 'make':
				selectName = 'make_name';
				break;
			case 'model':
				selectName = 'model_name';
				break;
			default:
				selectName = $(this).attr('name');
		}
		
		if(tagNameList == '') {
			tagNameList = selectName;
		} else {
			tagNameList = tagNameList + ',' + selectName;
		}
		
	});
		
	var fields = '&fields=' + tagNameList;
	
	// -- build the XML Query --
	xmlQuery = "search.php?" + xmlQuery + fields;
	
	// -- clean the query --
	xmlQuery = xmlQuery.replace("?&","?"); // Remove the extra &
	
	// -- fetch xml --
	$.get(xmlQuery, function(data){
		
		// -- empty lists --
		$("#" + parentID + " select").not("#" + parentID + " .search-type").not("#" + parentID + " .search-static").empty().each(function () {
			
			if($(this).hasClass("clear-option")){
				name = "(Clear) " + $(this).attr('title');
			}
			else{
				name = $(this).attr('title');
			}

			
			switch($(this).attr('name')) {
				case 'make':
					xmlTagName = 'make_name';
					break;
				case 'model':
					xmlTagName = 'model_name';
					break;
				default:
					xmlTagName = $(this).attr('name');
			}
			
			//xmlTagName = $(this).attr('name');
			
			$(this).append("<option value=''>" + name + "</option>");
			loadFormData(xmlTagName, this, data);
		});		
		
		$("#" + parentID + " .search-count").text(
			$(data).find("vehicles").attr('count')
		);
				
		// Select / Reselect the correct values
		$("#" + parentID + " select").each( function() {
			var selectName = $(this).attr("name");
			var noMatch = true;
						
			$(this).children().each( function() {
				val1 = $(this).val();
				val2 = selectArray[selectName];
								
				if(val1 == val2) {
					$(this).attr("selected", "selected");
					noMatch = false;
				}
			});
			
			// allows the list to get updated again, but keeps it from happening endlessly
			if(noMatch && success != false) {
				success = false;
			} else {
				success = true;
			}
			
		});

		// Add the change event back		
		$("#" + parentID + " select").change( function() {
			invUpdateLists(parentID, $(this).attr('name'));
			
			var firstOption = $(this).children(":first-child");
			var firstOptionHTML = firstOption.html();
			var testresult=firstOptionHTML.match("Clear");
			
			//add or remove 'clear-option class' to set up adding or removing the "Clear" before the field name	
				if (!testresult){
					$(this).addClass('clear-option');
				}
				else{
					$(this).removeClass('clear-option');
				}
//				if ($(this).hasClass('search-type')){
//					firstLoad[parentID] = true;
//					$(this).siblings().removeClass('clear-option');
//				}
//				if ($(this).hasClass('search-static')){
//					firstLoad[parentID] = true;
//					$(this).siblings().removeClass('clear-option');
//				}
//				if ($(this).hasClass('after-first-load')){
//					firstLoad[parentID] = true;
//					$(this).siblings().removeClass('clear-option');
//				}
		
		});
		$("#" + parentID + " .loading").text("");
		
		firstLoad[parentID] = false;
		
		// if the list update was not successfull then update again
		if (!success) {invUpdateLists(parentID, $(this).attr('name'));}

	}, "xml");
			
}

function loadFormData (xmlTagName, objectSelector, data) {
	var listoptions = '';
	
	$(data).find(xmlTagName).each( function() {
		var marker = $(this);
		if(marker.text()) { listoptions = listoptions + "<option value='" + encodeFormData(marker.text()) + "'>" + marker.text() + "</option>";}
	});
	$(objectSelector).append(listoptions);
}

function encodeFormData (data) {	
	data = data.replace("&", "%26");
	return data;
}