//<![CDATA[
	// When the page is ready
	$(document).ready(function() {
		$("#waiting").toggle();
		$('#container').width(180);
		$("#selectYear").bind("change", function(e){ 
			sendRequest(this);	
		});
		$("#startOver").bind("click", function(e){ 
			clearPage();	
			return false;
		});
	});

	// This function looks at the field that has changed, prepares the appropriate request and adds the 
	// results to the proper div
	function sendRequest(fieldPicked) {
		$("#waiting").toggle();
		$('.resultsCell').html("");
		switch($(fieldPicked).attr('NAME')) {
		case "selectYear":
			$('#searchmake').html("");
			$('#searchmodel').html("");
			$('#searchmake').load("search.php", {selectYear: $(fieldPicked).val()}, function() {
				$('#searchmake select:first').bind('change', function(e){
					sendRequest(this);
				});
				$("#waiting").toggle();
			});
			break;
		case "selectMake":
			$('#searchmodel').html("");
			$('#searchmodel').load("search.php", {selectYear: $('#selectYear').val(), selectMake: $('#searchmake select:first').val()}, function() {
				$('#searchmodel select:first').bind('change', function(e) {
					sendRequest(this);
				});
				$("#waiting").toggle();
			});
			break;
		case "selectModel":
			$('#results').load("search.php", {selectYear: $('#selectYear').val(), selectMake: $('#searchmake select:first').val(), selectModel: $('#searchmodel select:first').val()}, function() {
//				jQuery.each($('#results input'), function() {
//					$('#results').append($(this).attr('NAME') + "=" + $(this).val()+"<br />");
//				});
				$("#waiting").toggle();
				updateResults();
			});
			break;
		}
	}

	function updateResults() {
		jQuery.each($('#results input'), function() {
		//	alert($($(this).attr('NAME')).html());
			//alert("setting:"+$(this).attr('NAME')+" to: "+$(this).attr('VALUE'));
			$('#'+$(this).attr('NAME')).html($(this).attr('VALUE'));
			$('#'+$(this).attr('NAME')).addClass('resultsCell');
		});
	}

	function clearPage() {
		$('#searchmake').html("");
		$('#searchmodel').html("");	
		$('#selectYear').val("");		
		$('.resultsCell').html("");
	}
	//]]>


