var TRACK_ENDECAPARAMSEARCH = true;

function OnListChange(lsList, listId, subListId, subListName)
{
	var list = document.getElementById(listId);
	var subList = document.getElementById(subListId);
	if (list == null || subList == null)
	{
		if (location.search.indexof("debug=true") > 0)
			alert("List or SubList is null");
		return;
	}
	// Find the selected parent list item and remember the Text and ID
	var listID = list.options[list.selectedIndex].value;
	var subListString = new String(list.options[list.selectedIndex].text);

	// Reset the subList
	subList.options.length = 0;

	// If the selected Make isn't the default
	if (listID != "")
	{
		// Remove any COUNT ie. (34)
		j = subListString.indexOf("(");
		if (j > 0)
			subList.options[subList.options.length] = new Option("(all " + subListString.substr(0, j-1)+ " " + subListName + ")", "");			
		else
			subList.options[subList.options.length] = new Option("(all " + subListString + " " + subListName + ")", "");

		// populate subList with Text/ID from array
		for (i=0; i<lsList[listID].length; i+=2)
		{
			if (typeof(lsList[listID][i]) == "string")
			{				
				subList.options[subList.options.length] = new Option(lsList[listID][i+1], lsList[listID][i]);
			}
		}
	}
	// The selected Make is the default, add the default models Text/ID
	else
	{		
		subList.options[subList.options.length] = new Option("(all " + subListName + ")", "");
	}
	
}
function BuildSearchUrl(searchInterface, basePath, keywords)
{
	var url = location.protocol + "//" + location.hostname + basePath;
	var ns = "";
	for (i=3; i<BuildSearchUrl.arguments.length; i++)
	{
		var n = BuildSearchUrl.arguments[i];
		if (n.length > 0)
			ns += n + " ";
	}
	if (ns.length > 0)
	{
		url += "?N=" + ns.trim();
	}
	if (keywords != "")
	{
		if (ns.length > 0) 
			url += "&";
		else
			url += "?";
		url += "Ntx=mode+matchallpartial&Ntk=" + searchInterface + "&Ntt=" + escape(keywords) + "&Dx=mode+matchany&D=" + escape(keywords);
	}
//alert(url);
	return url;
}

function BuildEditorialSearchUrl(searchInterface, basePath, keywords)
{
	var url = location.protocol + "//" + location.hostname + basePath;
	var ns = "";
	for (i=3; i<BuildEditorialSearchUrl.arguments.length; i++)
	{
		var n = BuildEditorialSearchUrl.arguments[i];
		if (n.length > 0)
			ns += n + " ";
	}
	if (ns.length > 0)
	{
		url += "?N=" + ns.trim();
	}
	if (keywords != "")
	{
		if (ns.length > 0) 
			url += "&";
		else
			url += "?";
		url += "Ntt=" + escape(keywords) + "&Ntx=&Dx=&Ntk=Default&D=" + escape(keywords);
	}
//alert(url);
	return url;
}

function UsedCarSearch_OnSearchClick(control, dimensionFilterIDs)
{

	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	
	var make = window.document.getElementById(parentid+"cboMake");
	make = (make == null) ? window.document.getElementById(parentid+"cboMakeAjax").value : make.value;	
	var model = window.document.getElementById(parentid+"cboModel");
	model = (model == null) ? window.document.getElementById(parentid+"cboModelAjax").value : model.value;

	var loc = window.document.getElementById(parentid+"cboLocation").value;
	var p1 = window.document.getElementById(parentid+"cboPriceFrom").value;
	var p2 = window.document.getElementById(parentid+"cboPriceTo").value;
	var keywords = window.document.getElementById(parentid+"txtKeywords").value.trim();
	var advanced = (window.document.getElementById(parentid+"cboBodyType") != null);
	
	var mtaNValue = "";
	var chkMTADealerId = window.document.getElementById(parentid+"chkMTADealer");
	if (chkMTADealerId != null)
	{
		if (chkMTADealerId.checked == true)
			mtaNValue = "615";
	}

	var iFinanceDealer = "";
	var chkIFinance = window.document.getElementById(parentid+"chkFinanceDealer");
	if (chkIFinance != null)
	{
		if (chkIFinance.checked == true)
			iFinanceDealer = "919";
	}
	
	var targetUrl = "";
	if (advanced)
	{
		var body = window.document.getElementById(parentid+"cboBodyType").value;
		var trans = window.document.getElementById(parentid+"cboTransmission").value;
		var y1 = window.document.getElementById(parentid+"cboYearFrom").value;
		var y2 = window.document.getElementById(parentid+"cboYearTo").value;
		if (make == "" && loc == "" && keywords == "" && p1 == "" && p2 == "" && 
			body == "" && trans == "" && y1 == "" && y2 == "")
		{
			alert("Please select one or more criteria to begin your search.");
		}
		else
		{		
			targetUrl = BuildSearchUrl("CarAll", "/used-cars/search.aspx", keywords, "279", mtaNValue, iFinanceDealer, make, model, loc, p1, p2, body, trans, y1, y2, dimensionFilterIDs);
		}
	}
	else
	{
		if (make == "" && loc == "" && keywords == "" && p1 == "" && p2 == "")
		{
			alert("Please select one or more criteria to begin your search.");
		}
		else
		{
			targetUrl = BuildSearchUrl("CarAll", "/used-cars/search.aspx", keywords, "279", mtaNValue, iFinanceDealer, make, model, loc, p1, p2, dimensionFilterIDs);
		}
	}
	
	if (targetUrl.length > 0)
	{
		if (iFinanceDealer.length > 0)
			targetUrl += "&Finance=true";
		location.href = targetUrl;
	}
	return false;
}
function NewCarSearch_OnSearchClick(control,nValues)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	
	var make = window.document.getElementById(parentid+"cboMake");
	make = (make == null) ? window.document.getElementById(parentid+"cboMakeAjax").value : make.value;	
	var model = window.document.getElementById(parentid+"cboModel");
	model = (model == null) ? window.document.getElementById(parentid+"cboModelAjax").value : model.value;
			
	var p1 = window.document.getElementById(parentid+"cboPriceFrom").value;
	var p2 = window.document.getElementById(parentid+"cboPriceTo").value;
	var keywords = window.document.getElementById(parentid+"txtKeywords").value.trim();
	var advanced = (window.document.getElementById(parentid+"cboBodyType") != null);
			
	if (advanced)
	{
		var body = window.document.getElementById(parentid+"cboBodyType").value;
		var trans = window.document.getElementById(parentid+"cboTransmission").value;
		if (make == "" && keywords == "" && p1 == "" && p2 == "" && body == "" && trans == "")
		{
			alert("Please select one or more criteria to begin your search.");
		}
		else
		{		
			location.href = BuildSearchUrl("CarAll", "/new-cars/search.aspx", keywords, nValues, make, model, p1, p2, body, trans);
		}
	}
	else
	{	
		if (make == "" && keywords == "" && p1 == "" && p2 == "")
		{
			alert("Please select one or more criteria to begin your search.");
		}
		else
		{
			location.href = BuildSearchUrl("CarAll", "/new-cars/search.aspx", keywords, nValues, make, model, p1, p2);
		}
	}
		
	return false;
}
function CarReviewSearch_OnSearchClick(control)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	var make = window.document.getElementById(parentid+"cboMake").value;
	var category = window.document.getElementById(parentid+"cboCategory").value;
	var keywords = window.document.getElementById(parentid+"txtKeywords").value.trim();
		
	if (make == "" && category == "" && keywords == "")
	{
		alert("Please select one or more criteria to begin your search.");
	}
	else
	{
		location.href = BuildEditorialSearchUrl("CarAll", "/news-reviews-advice/results.aspx", keywords, "2981+4294967281", make, category);
	}
	return false;
}
function CarDealerSearch_OnSearchClick(searchInterface, control, dimensionFilterIDs)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	var franchiseELE = window.document.getElementById(parentid+"cboFranchiseCarDealerSearch");
	var franchise = "";
	if(franchiseELE != null)
		franchise = franchiseELE.value;
	var loc = window.document.getElementById(parentid+"cboLocation").value;
	var keywords = window.document.getElementById(parentid+"txtKeywords").value.trim();
		
	if (franchise == "" && loc == "" && keywords == "")
	{
		alert("Please select one or more criteria to begin your search.");
	}
	else
	{
		location.href = BuildSearchUrl(searchInterface, "/car-dealers/search.aspx", keywords, "277", franchise, loc, dimensionFilterIDs);
	}
	return false;
}
function CarValuationSearch_OnSearchClick(control)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);

	var make = window.document.getElementById(parentid+"cboMake");
	make = (make == null) ? window.document.getElementById(parentid+"cboMakeAjax").value : make.value;	
	var model = window.document.getElementById(parentid+"cboModel");
	model = (model == null) ? window.document.getElementById(parentid+"cboModelAjax").value : model.value;
		
	if (make == "")
	{
		alert("Please select one or more criteria to begin your search.");
	}
	else
	{
		location.href = BuildSearchUrl("CarAll", "/car-valuations/search.aspx", "", "280", make, model);
	}
	return false;
}
function MSNUsedCarSearch_OnSearchClick(control)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	var make = window.document.getElementById(parentid+"cboMake");
	make = (make == null) ? window.document.getElementById(parentid+"cboMakeAjax").value : make.value;	
	var model = window.document.getElementById(parentid+"cboModel");
	model = (model == null) ? window.document.getElementById(parentid+"cboModelAjax").value : model.value;
	var loc = window.document.getElementById(parentid+"cboLocation").value;
	var p1 = window.document.getElementById(parentid+"cboPriceFrom").value;
	var p2 = window.document.getElementById(parentid+"cboPriceTo").value;
	var keywords = window.document.getElementById(parentid+"txtKeywords").value.trim();
	if (keywords == "anything else eg: Automatic Hatch")
		keywords = "";
	if (make == "" && loc == "" && keywords == "" && p1 == "" && p2 == "")
	{
		alert("Please select one or more criteria to begin your search.");
	}
	else
	{
		var url = BuildSearchUrl("CarAll", "/used-cars/search.aspx", keywords, "279", make, model, loc, p1, p2);
		var wnd = window.open(url, "_blank"); wnd.focus();
	}
	return false;
}
function NineMSNUsedCarSearch_OnSearchClick(control)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	var make = window.document.getElementById(parentid+"cboMake");
	make = (make == null) ? window.document.getElementById(parentid+"cboMakeAjax").value : make.value;	
	var model = window.document.getElementById(parentid+"cboModel");
	model = (model == null) ? window.document.getElementById(parentid+"cboModelAjax").value : model.value;
	var loc = window.document.getElementById(parentid+"cboLocation").value;		
	if (make == "" && loc == "")
	{
		alert("Please select one or more criteria to begin your search.");
	}
	else
	{
		var url = BuildSearchUrl("CarAll", "/used-cars/search.aspx", "", "279", make, model, loc, "", "");
		url += "&refid=abbsearchused";
		var wnd = window.open(url, "_blank"); wnd.focus();
	}
	return false;
}
function UsedCarSearchNZ_OnSearchClick(control)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	var make = window.document.getElementById(parentid+"cboMake");
	make = (make == null) ? window.document.getElementById(parentid+"cboMakeAjax").value : make.value;	
	var model = window.document.getElementById(parentid+"cboModel");
	model = (model == null) ? window.document.getElementById(parentid+"cboModelAjax").value : model.value;
	var loc = window.document.getElementById(parentid+"cboLocation").value;
	var keywords = window.document.getElementById(parentid+"txtKeywords").value.trim();
	if (keywords == "anything else")
		keywords = "";
	if (make == "" && loc == "" && keywords == "")
	{
		alert("Please select one or more criteria to begin your search.");
	}
	else
	{
		var url = BuildSearchUrl("CarAll", "/used-cars/search.aspx", keywords, "279", make, model, loc);
		window.open(url, "_top");
	}
	return false;
}
function NewCarSearchNZ_OnSearchClick(control)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	var make = window.document.getElementById(parentid+"cboMake");
	make = (make == null) ? window.document.getElementById(parentid+"cboMakeAjax").value : make.value;	
	var model = window.document.getElementById(parentid+"cboModel");
	model = (model == null) ? window.document.getElementById(parentid+"cboModelAjax").value : model.value;
	var keywords = window.document.getElementById(parentid+"txtKeywords").value.trim();
	if (keywords == "anything else")
		keywords = "";
	if (make == "" && keywords == "")
	{
		alert("Please select one or more criteria to begin your search.");
	}
	else
	{
		var url = BuildSearchUrl("CarAll", "/new-cars/search.aspx", keywords, "281", make, model);
		window.open(url, "_top");
	}
	return false;
}

function CarReviewSearchNZ_OnSearchClick(control)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	var make = window.document.getElementById(parentid+"cboMake").value;
	var category = window.document.getElementById(parentid+"cboCategory").value;
	var keywords = window.document.getElementById(parentid+"txtKeywords").value.trim();
	if (keywords == "anything else")
		keywords = "";		
	if (make == "" && category == "" && keywords == "")
	{
		alert("Please select one or more criteria to begin your search.");
	}
	else
	{
		var url = BuildSearchUrl("CarAll", "/car-reviews/search.aspx", keywords, "278", make, category);
		window.open(url, "_top");
	}
	return false;
}

function BoatDealerSearch_OnSearchClick(control)
{
	var parentid = control.id.substring(0, control.id.lastIndexOf("_")+1);
	var brand = window.document.getElementById(parentid+"cboBrand").value;
	var loc = window.document.getElementById(parentid+"cboLocation").value;
	var keywords = window.document.getElementById(parentid+"txtKeywords").value.trim();
		
	if (brand == "" && loc == "" && keywords == "")
	{
		alert("Please select one or more criteria to begin your search.");
		return false;
	}

	return true;
}