var lstManufacturers = new List();
var lstModels = new List();
var lstBodyShape = new List();
var ajax = new AjaxRequest();


function load()
{
	manufacturerTypeSelected(document.getElementById("manufacturer_type").value);
}

function parseData(data, list)
{
	document.getElementById("resultDiv").innerHTML="";

	list.clear();
	var st = new StringTokenizer(data, ";");
	while (st.hasTokens())
	{
		var str = st.nextToken();
		str = (str).replace(/^\s*|\s*$/g,'');
		list.add(new Array(str.substr(0, str.indexOf(",")),
			str.substr(str.indexOf(",") + 1, str.length)));
	}
}

function manufacturerTypeSelected(manufacturer_type_id)
{
	var returnFunction = function()
	{
		if (ajax.isReady())
		{
			parseData(trim(ajax.getResponse()), lstManufacturers);
			refreshManufacturers();
		}
	};
	ajax.send("includes/ajax/manufacturer.jsp?manufacturer_type_id=" +manufacturer_type_id, "GET", returnFunction);
}


function manufacturerSelected(manufacturer_id)
{
	var manufacturer_type_id = document.getElementById("manufacturer_type").value;
	var returnFunction = function()
	{
		if (ajax.isReady())
		{
			parseData(trim(ajax.getResponse()), lstModels);
			refreshModels();
		}
	};
	ajax.send("includes/ajax/model.jsp?manufacturer_id=" +manufacturer_id +"&manufacturer_type_id=" +manufacturer_type_id, "GET", returnFunction);
}

function modelSelected(model_id)
{
	var manufacturer_type_id = document.getElementById("manufacturer_type").value;
	var manufacturer_id = document.getElementById("manufacturer").value;
	var returnFunction = function()
	{
		if (ajax.isReady())
		{
			parseData(trim(ajax.getResponse()), lstBodyShape);
			refreshBodyShape();
		}
	};
	ajax.send("includes/ajax/body_shape.jsp?model_id=" +model_id +"&manufacturer_type_id=" +manufacturer_type_id +"&manufacturer_id=" +manufacturer_id, "GET", returnFunction);
}

function bodyShapeSelected(bodyshape_id)
{
	var manufacturer_type_id = document.getElementById("manufacturer_type").value;
	var manufacturer_id = document.getElementById("manufacturer").value;
	var model_id = document.getElementById("model").value;
	
	var returnFunction = function()
	{
		if (ajax.isReady())
		{
			document.getElementById("resultDiv").innerHTML=trim(ajax.getResponse());
		}
	};
	ajax.send("includes/ajax/result.jsp?body_shape_id=" +bodyshape_id +"&model_id=" +model_id +"&manufacturer_type_id=" +manufacturer_type_id +"&manufacturer_id=" +manufacturer_id, "GET", returnFunction);
}




function refreshManufacturers()
{
	var manufacturers = document.getElementById("manufacturer");
	manufacturers.length = 0;
  manufacturers.options[0] = new Option("Please Select a Manufacturer", "-1");
		
	for (var i = 0; i < lstManufacturers.length(); i++)
	{
		manufacturers.options[i+1] = new Option(lstManufacturers.get(i)[1], lstManufacturers.get(i)[0]);
	}
		
	refreshBodyShape();
}


function refreshModels()
{
	var models = document.getElementById("model");
	models.length = 0;
  models.options[0] = new Option("Please Select a Model", "-1");
		
	for (var i = 0; i < lstModels.length(); i++)
	{
		models.options[i+1] = new Option(lstModels.get(i)[1], lstModels.get(i)[0]);
	}
	
	refreshBodyShape();
}

function refreshBodyShape()
{
	var bodyShape = document.getElementById("body_shape");
	bodyShape.length = 0;
    bodyShape.options[0] = new Option("Please Select a Body Shape", "-1");
	
	for (var i = 0; i < lstBodyShape.length(); i++)
	{
		bodyShape.options[i+1] = new Option(lstBodyShape.get(i)[1], lstBodyShape.get(i)[0]);
	}

}