function FindACar()
{
	this.LocationType =
	{
		Default:	0,
		PopUp:		1,
		DealerPage: 2,
		OEMPage: 	3
	}

	this.Init = function(id)
	{
		this._clientId = id;
		this._type = this.LocationType.Default;
		this.OnBeforeNavigate = null;
		this.ZipWM = "Enter ZIP Code";
		this._txtZip = $get(this._clientId + "_txtZip");

		// Add handlers so we can call functions associated with this object.
		if ($get("ftcShowAdvanced") != null)
		{
			$addHandlers($get("ftcShowAdvanced"), {'click':this.ShowAdvanced}, this);
		}
		if ($get("ftcShowAdvanced2") != null)
		{
			$addHandlers($get("ftcShowAdvanced2"), {'click':this.ShowAdvanced}, this);
		}
		$addHandlers($get("ftcSearch"), {'click':this.Search}, this);
		$addHandlers(this._txtZip, {'focus':this.ZipOnFocus,'blur':this.ZipOnBlur}, this);
		$addHandlers($get(this._clientId + "_checkNew"), {'click':this.LotTypeOnClick}, this);
		$addHandlers($get(this._clientId + "_checkUsed"), {'click':this.LotTypeOnClick}, this);
		$addHandlers($get(this._clientId + "_checkCertified"), {'click':this.LotTypeOnClick}, this);
		$addHandlers($get(this._clientId + "_ddlMake"), {'change':this.MakeOnChange}, this);

		// Handler for key press to map "enter" to the search button.
		$addHandlers($get('carSearch'), {keypress:this.CheckKey}, this);

		this._delExecuteSearch = Function.createDelegate(this, this.ExecuteSearch);

		// Setup a method to cleanup
		var delDispose = Function.createDelegate(this, this.Dispose);
		Sys.Application.add_unload(delDispose);

		// call the zip on blur to get the watermark initialized.
		this.ZipOnBlur();
	}

	// Clean up after ourself
	this.Dispose = function()
	{
		if ($get("ftcShowAdvanced") != null)
		{
			$clearHandlers($get("ftcShowAdvanced"));
		}
		if ($get("ftcShowAdvanced2") != null)
		{
			$clearHandlers($get("ftcShowAdvanced2"));
		}
		$clearHandlers($get("ftcSearch"));
		$clearHandlers($get("carSearch"));
		$clearHandlers(this._txtZip);
		$clearHandlers($get(this._clientId + "_checkNew"));
		$clearHandlers($get(this._clientId + "_checkUsed"));
		$clearHandlers($get(this._clientId + "_checkCertified"));
		$clearHandlers($get(this._clientId + "_ddlMake"));

		this._delExecuteSearch = null;
	}

	this.CheckKey = function(evt)
	{
		// evt is a Sys.UI.DomEvent object

		if (mapKeyToAction(evt))
		{
			setEnterPress();
			this.Search();
			return false;
		}
	}

	// On focus for the zip input to do watermark processing.
	this.ZipOnFocus = function()
	{
		if(this.ZipWM == this._txtZip.value)
		{
			this._txtZip.value = "";
			removeCssClass(this._txtZip, "grey");
		}
	}

	// On blur for the zip input to do watermark processing.
	this.ZipOnBlur = function()
	{
		if("" == this._txtZip.value)
		{
			this._txtZip.value = this.ZipWM;
			addCssClass(this._txtZip, "grey");
		}
	}

	// The New/Used/Certified checkboxes have been clicked. Repopulate
	// the Make/Model drop downs.
	this.LotTypeOnClick = function()
	{
		// Call common CL java script function to populate the model list.
		PopulateMakes(this._clientId + "_ddlMake", this._clientId + "_ddlModel",
					$get(this._clientId + "_checkNew").checked,
					$get(this._clientId + "_checkUsed").checked,
					$get(this._clientId + "_checkCertified").checked,
					true);
	}

	this.MakeOnChange = function()
	{
		// Call common CL java script function to populate the model list.
		PopulateModels(this._clientId + "_ddlMake", this._clientId + "_ddlModel",
					$get(this._clientId + "_checkNew").checked,
					$get(this._clientId + "_checkUsed").checked,
					$get(this._clientId + "_checkCertified").checked,
					false);
	}

	// Search for this vehicle. Validate the fields and then search.
	this.Search = function()
	{
		if(this.Validate())
		{
			this.ExecuteSearch(this._txtZip.value);
		}
		else if (!this.DealerOnly())
		{
			if(this._type != this.LocationType.PopUp)
			{
				zipPromptVar.Open("Car", this._delExecuteSearch, this._txtZip.value);
			}
		}

		return false;
	}

	// Execute the search for this vehicle. No validation is done. It is assumed that has
	// already been done.
	this.ExecuteSearch = function(sZip)
	{
		_searchLF = new LinkFactory();
		_searchLF._path = (this.DealerOnly()) ? this._dealerPath : this._path;
		_searchLF._qstring = (this.DealerOnly()) ? this._dealerqstring : this._qstring;

		var makeDesc = "";
		var makeDelim = "__";

		// The new/used/certified lot type is a bit mask of the check boxes
		var lotType = 0;
		if ($get(this._clientId + "_checkNew").checked)
		{
			lotType += 1;
			if (this.DealerOnly()) { makeDesc += (((makeDesc == "") ? "" : makeDelim) + "New"); }
		}

		if ($get(this._clientId + "_checkUsed").checked)
		{
			lotType += 2;
			if (this.DealerOnly()) { makeDesc += (((makeDesc == "") ? "" : makeDelim) + "Used"); }
		}

		if ($get(this._clientId + "_checkCertified").checked)
		{
			lotType += 4;
			if (this.DealerOnly()) { makeDesc += (((makeDesc == "") ? "" : makeDelim) + "Certified"); }
		}

		if(0 == lotType)
		{
			lotType = 1;
			if (this.DealerOnly()) { makeDesc += (((makeDesc == "") ? "" : makeDelim) + "New"); }
		}

		if(0 < $get(this._clientId + "_ddlMake").selectedIndex)
		{
			_searchLF.ReplaceToken("xMAKEIDx", $get(this._clientId + "_ddlMake").value);
			makeDesc += (((makeDesc == "") ? "" : makeDelim) + $get(this._clientId + "_ddlMake").options[$get(this._clientId + "_ddlMake").selectedIndex].text);
		}
		else if (this.DealerOnly())
		{
			makeDesc += (((makeDesc == "") ? "" : makeDelim) + "Cars");
		}

		if (makeDesc != "")
		{
			_searchLF.ReplaceToken("xMAKEDESCx", makeDesc);
		}

		if(0 < $get(this._clientId + "_ddlModel").value.length)
		{
			_searchLF.ReplaceToken("xMODELx", $get(this._clientId + "_ddlModel").value);
		}

		_searchLF.ReplaceToken("xCATx", lotType);
		if (!this.DealerOnly())
		{
			_searchLF.ReplaceToken("xZIPx", sZip);
			_searchLF.ReplaceToken("xDISTANCEx", $get(this._clientId + "_ddlRange").value);
		}

		if(null != this.OnBeforeNavigate)
		{
			this.OnBeforeNavigate();
		}

		fnNavToSearchResults(_searchLF.FinalizeUrl());

		return false;
	}
	
	this.GetLotType = function()
	{
		// The new/used/certified lot type is a bit mask of the check boxes
		var lotType = 0;
		if ($get(this._clientId + "_checkNew").checked)
		{
			lotType += 1;
		}

		if ($get(this._clientId + "_checkUsed").checked)
		{
			lotType += 2;
		}

		if ($get(this._clientId + "_checkCertified").checked)
		{
			lotType += 4;
		}

		if(0 == lotType)
		{
			lotType = 1;
		}
		
		return lotType;
	}

	// Show the advanced search popup.
	this.ShowAdvanced = function()
	{
		try
		{
			if (this._type == this.LocationType.OEMPage)
			{
				// Call the AdvancedSearch.ascx.js function to open the
				// advanced search popup with default values selected.
				// showAdvancedSearchWithDefaults(makeId, type)
				showAdvancedSearchWithDefaults(
					$get(this._clientId + "_ddlMake").value,
					this.GetLotType()
				);
			}
			else
			{
				// Call the AdvancedSearch.ascx.js function to open the advanced search popup.
				showAdvancedSearch();
			}
		}
		catch(err)
		{}

		return false;
	}

	this.Validate = function()
	{
		valid = true;

		if (!this.DealerOnly())
		{
			if(0 == this._txtZip.value.length)
			{
				removeCssClass($get(this._clientId + "_reqZip"), "hide");
				this._txtZip.focus();
				valid = false;
			}
			else if(!ValidateZip(this._txtZip.value))
			{
				removeCssClass($get(this._clientId + "_reqZip"), "hide");
				this._txtZip.focus();
				valid = false;
			}
		}

		return valid;
	}

	this.DealerOnClick = function()
	{
		var checkDealerChecked = $get(this._clientId + "_checkDealer").checked;

		if (checkDealerChecked)
		{
			addCssClass($get('controlContainer2'), "hide");
		}
		else
		{
			removeCssClass($get('controlContainer2'), "hide");
		}

		$get(this._clientId + "_ddlRange").disabled = checkDealerChecked;
		$get(this._clientId + "_txtZip").disabled = checkDealerChecked;
	}

	this.DealerOnly = function()
	{
		return ($get(this._clientId + "_checkDealer") != null && $get(this._clientId + "_checkDealer").checked);
	}
}



