// JavaScript Document

	newOrUsed = "used";
	
	blRedirectToStockAfterAjax = false;
	
	
	$(document).ready ( function ()
	{
		
				
		$(".btnAddToCompare").click ( function ()
		{

			if ($(this).attr("stockID") != null)
				stockID = $(this).attr("stockID");

//			alert(stockID);
			
			$.ajax({
				url: 'ajax/setCompare.php',
				data: "stockID=" +  stockID ,
				cache: false,
			  
				success: function(data)
				{
					data = data.split("@@");
					
					
					
					alert(data[1]);
					
				},
				error: function(data)
				{
					alert("Error: " + data.status);
				}
			  
			});
			
			
			
		});

		
		
		$("#search_ul_category_used li").click( function ()
		{
			var parentSearch = $(this).parents(".searchLHS");
			
			
			if (parentSearch.length == 1) //If we are on compact left hand search (eg home page), now redirect to the stock page
			{
				blRedirectToStockAfterAjax = true;
			}
			
//			alert($(this).attr("rel"));
			$("#search_category_used").val($(this).attr("rel"));
			$(this).siblings().attr("class", "");
			$(this).attr("class", "selected");
			
			handleOnChange_vehicle_type("", this);
			
		});
		
		$(".searchLHS #btn_advanced_search_used").click (function ()
		{
//			alert("exp advanced redir");
			window.location.href = prefs_usedStockPage + "?advanced=true";
			return false;
		});

		$(".searchShortWide #btn_advanced_search_used").click (function ()
		{
			if ($(".searchShortWide .advanced").css("display") == "none")
			{
				$(".searchShortWide .advanced").slideDown("fast");
				advancedState = true;
			}
			else
			{
				$(".searchShortWide .advanced").slideUp("fast");
				advancedState = false;
			}
			
			$.ajax({
				url: 'ajax/setadvanced.php',
				data: "advanced=" + advancedState,
				cache: false,
			  
				success: function(data)
				{
//						alert("@" + data + "@");

				},
				error: function(data)
				{
					alert("Error: " + data.status);
				}
			  
			});				
			
			
			
			return false;

		});
		
		
	});
	
	
	
	function handleOnChange_ordering(callingObject)
	{
		var code = callingObject.value;
		
		window.location.href = "?ordering=" + code;
		//alert("ordering change");
		
		
		
	}

	function handleOnChange_vehicle_type(doWhatCode, callingObject)
	{
		
		//alert("g");
			/*
			alert("dis = " + document.getElementById("tax_free_car_search_new").style.display);	
		if (document.getElementById("tax_free_car_search_new").style.display == "block")
			newOrUsed = "new";
		else
			newOrUsed = "used";
		*/
	//	alert("change");
	
		isCommercial = 0;
		if ($("#isCommercialVan_" + newOrUsed).attr("checked"))
			isCommercial = 1;
		//alert(isCommercial);

		
		var makeChanged = "false";
		
		switch (doWhatCode)
		{
			case "selectIsCommercial":
	//			alert("change commercial");
				
				document.form1.make_used.selectedIndex = 0;
				
				makeChanged = "true";
			//	var isCommercial	= callingObject.value;
				
				$.ajax({
					url: 'ajax/carsearch.php',
					data: "action=lookup_makes&isCommercial=" + isCommercial + "&newOrUsed=" + newOrUsed,
					cache: false,
				  
					success: function(data)
					{
		//				alert("data is " + data);
						data = data.split("@@@");
						
						var cmbMake = $("#search_make_" + newOrUsed).get(0);
						cmbMake.options.length = 0; //Clear current combo options

						var cmbModel = $("#search_model_" + newOrUsed).get(0);
						cmbModel.options.length = 1; //Clear current combo options

						
						for (var i in data)
						{
							if (data[i] != "")
							{
								var opt = document.createElement('option');
								opt.text = data[i];
								opt.value = data[i];
								


								cmbMake.options.add(opt);
							}
						}
					},
					error: function(data)
					{
						alert("Error: " + data.status);
					}
				  
				});				
				
				
				
				
				break;
			
			case "selectModel": //Make has been changed so load appropriate models

				makeChanged = "true";
				var make_id	= callingObject.value;
				

//				alert("selecting model");
				
				$.ajax({
					url: 'ajax/carsearch.php',
					data: "action=lookup_models&make_name=" + make_id + "&newOrUsed=" + newOrUsed +"&isCommercial=" + isCommercial,
					cache: false,
				  
					success: function(data)
					{
		//				alert("lookup model = " + data);
						data = data.split("@@@");
//alert(newOrUsed);
						var model = document.getElementById("search_model_" + newOrUsed);
	
	

//						var model = $("#search_model_" + newOrUsed).get(0);
				//		alert(model);

						model.options.length = 1; //Clear current combo options


						for (var i in data)
						{
							if (data[i] != "")
							{
								var opt = document.createElement('option');
								opt.text = data[i];
								opt.value = data[i];

								model.options.add(opt);
							}
						}
					},
					error: function(data)
					{
						alert("Error: " + data.status);
					}
				  
				});


							
				break;
		}
		
		
		//Update # of results
		var div_result_count = document.getElementById("search_result_count_" + newOrUsed);
		var div_result_view = document.getElementById("search_result_view_" + newOrUsed);

//alert("make=" + document.form1.make.value + "&model=" + document.form1.search_model.value + "&min_price=" + document.form1.search_min_price.value + "&max_price=" + document.form1.search_max_price.value + "&category=" +  document.form1.category.value);

//		var value_make = (document.form1.elements["make_" + newOrUsed].value);
	//	var value_model = (document.form1.elements["search_model_" + newOrUsed].value);
//		var value_min_price = (document.form1.elements["search_min_price_" + newOrUsed].value);
	//	var value_max_price = (document.form1.elements["search_max_price_" + newOrUsed].value);
//		var value_category = (document.form1.elements["category_" + newOrUsed].value);


//alert("1");
		$.ajax({
			url: 'ajax/search_result_count.php',
			data: "ajaxChange=true&newOrUsed=" + newOrUsed +  "&makeChanged=" +  makeChanged + "&make_new=" + (document.form1.make_new.value) + "&make_used=" + (document.form1.make_used.value) + "&model_new=" + document.form1.search_model_new.value + "&model_used=" + document.form1.search_model_used.value + "&min_price_new=" + document.form1.search_min_price_new.value + "&min_price_used=" + document.form1.search_min_price_used.value + "&max_price_new=" + document.form1.search_max_price_new.value + "&max_price_used=" + document.form1.search_max_price_used.value + "&isCommercial=" + isCommercial  + "&mileage_used=" + document.form1.mileage_used.value + "&fueltype_used=" + document.form1.fueltype_used.value + "&transmission_used=" + document.form1.transmission_used.value + "&colour_used=" + document.form1.colour_used.value + "&age_used=" + document.form1.age_used.value + "&doors_used=" + document.form1.doors_used.value + "&category_used=" + document.form1.search_category_used.value,
			cache: false,
		  
			success: function(data)
			{
			//	alert("res count = " + data);
				var s = "";
				var carText = " car";
				
				if (isCommercial == 1)
					carText = " van";
				
				if (parseInt(data) != 1)
					s = "s";

				div_result_count.innerHTML = data + carText + s + " found";
				
				if (blRedirectToStockAfterAjax)
				{
					window.location.href = prefs_usedStockPage;
				}
				
			},
			error: function(data)
			{
				alert("Error: " + data.status);
			}
		  
		});
		


		


		
		div_result_count.style.display = "block";
		div_result_view.style.display = "block";

		
		
	}
