/*
	Sets the base url for dealer locator results. Sets the action plus brand id and desired count.
*/
function getDealerLocatorBaseUrl(form) {
	var BRANDID = document.getElementsByName('BRANDID')[0].value;
	var desiredCount =  document.getElementsByName('desiredCount')[0].value;
	var url = form.action + "?BRANDID="+ BRANDID + "&desiredCount=" + desiredCount;

	return url;
}

/*
Submits the search form, changing the search type to postal code
and validates the required parameters. In case there is a validation
error the form is not submitted.

Parameters:
 - form: The form to submit.

This function sets to blank the Zip code input when the input
contains the default text "Enter Zip Code".

Returns false when there is a validation error.
*/
function searchByZip(form)
{
	checkDefault(form.searchByPostalCodePostalCode);
	if(validateZip(form.searchByPostalCodePostalCode.value))
	{
		var zipcode = new zipcodeUtil(form.searchByPostalCodePostalCode.value);
		if(zipcode.isValid){
			var searchType = 'ByPostalCode';
			handlerCookie(form.searchByPostalCodePostalCode.value);
			var searchByPostalCodePostalCode =  document.getElementsByName('searchByPostalCodePostalCode')[0].value;
			var searchByPostalCodeProximity =  document.getElementsByName('searchByPostalCodeProximity')[0].value;
			var dealerType = "";
			var types = document.getElementsByName('dealerType');
			for (var i = 0; i < types.length; i++) {
				if (types[i].checked) {
					dealerType = types[i].value;
					break;
				}
			}

			var url = getDealerLocatorBaseUrl(form) + "&searchType=" + searchType
					+ "&searchByPostalCodePostalCode=" + searchByPostalCodePostalCode
					+ "&searchByPostalCodeProximity=" + searchByPostalCodeProximity
					+ "&dealerType=" + dealerType;
			/* This form of redirecting to results page instead of form.submit is needed in order to avoid conflict
	 		with low/high speed switching.*/
			window.location.href = url;

		}else{
			alert("Try Another Zip Code.");
			return false;
		}
	}
	else
		return false;
}


/*
Submits the search form, changing the search type to city/state search
and validates the required parameters. In case there is a validation
error the form is not submitted.

Parameters:
 - form: The form to submit.

This function sets to blank the city input when the input
contains the default text "Enter City Name".

Returns false when there is a validation error.
*/
function searchByCityState(form)
{
	checkDefault(form.searchByCityStateCityName);
	if (form.searchByCityStateCityName.value=="Enter City Name")
		form.searchByPostalCodePostalCode.value="";
	if(reqField(form.searchByCityStateCityName.value,"City Name") && reqField(form.searchByCityStateStateName.value,"State Name"))
	{
		var searchType = 'ByCityState';
		var searchByCityStateCityName =  document.getElementsByName('searchByCityStateCityName')[0].value;
		var searchByCityStateStateName =  document.getElementsByName('searchByCityStateStateName')[0].value;
		var searchByCityStateProximity =  document.getElementsByName('searchByCityStateProximity')[0].value;
		var dealerType = "";
		var types = document.getElementsByName('dealerType');
		for (var i = 0; i < types.length; i++) {
			if (types[i].checked) {
				dealerType = types[i].value;
				break;
			}
		}

		var url = getDealerLocatorBaseUrl(form) + "&searchType=" + searchType
					+ "&searchByCityStateCityName=" + searchByCityStateCityName
					+ "&searchByCityStateStateName=" + searchByCityStateStateName
					+ "&searchByCityStateProximity=" + searchByCityStateProximity
					+ "&dealerType=" + dealerType;
		/* This form of redirecting to results page instead of form.submit is needed in order to avoid conflict
	 		with low/high speed switching.*/
		window.location.href = url;
	}
	else
		return false;
}
/*
Submits the search form, changing the search type to vendor name search
and validates the required parameters. In case there is a validation
error the form is not submitted. The city name input is not required.

Parameters:
 - form: The form to submit.

This function sets to blank the city and vendor name inputs when those inputs
contain the default text "Enter City Name" or "Enter Dealer Name" respectively.

Returns false when there is a validation error.
*/
function searchByVendor(form)
{
	checkDefault(form.searchByVendorVendorName);
	checkDefault(form.searchByVendorCityName);
	if(reqField(form.searchByVendorVendorName.value,"Dealer Name") && reqField(form.searchByVendorStateName.value,"State Name") )
	{
		var searchType = 'ByVendor';

		var searchByVendorVendorName =  document.getElementsByName('searchByVendorVendorName')[0].value;
		var searchByVendorCityName =  document.getElementsByName('searchByVendorCityName')[0].value;
		var searchByVendorStateName =  document.getElementsByName('searchByVendorStateName')[0].value;

		var url = getDealerLocatorBaseUrl(form) + "&searchType=" + searchType
					+ "&searchByVendorVendorName=" + searchByVendorVendorName
					+ "&searchByVendorCityName="   + searchByVendorCityName
					+ "&searchByVendorStateName=" + searchByVendorStateName;
		/* This form of redirecting to results page instead of form.submit is needed in order to avoid conflict
	 		with low/high speed switching.*/
		window.location.href = url;

	}
	else
		return false;
}
/*
Checks the value of the objtext element. In case the value contains
the string "Enter" the value is set to "".

Parameters:
 - objtext: Text element.

*/
function checkDefault(objtext){
	if (objtext.value.indexOf("Enter ")!=-1)
		objtext.value="";
}

/*
Set the zipcode on the cookie.
If the cookie exists, update it
If the cookie don’ts exist create now one.
*/
function handlerCookie(zip){
	try{
		var loc = getDefaultLocation();
		if(loc!=null)
			if(loc.zipcode != zip)
				saveDefaultLocation(zip,null,null);
	}catch(e){}
}


function paramHandler(){
	try{
		var loc = getDefaultLocation();
		if(loc!=null)
			document.getElementById('ipZip').value = loc.zipcode;
	}catch(e){}
}

AttachEvent(window, "load", paramHandler);
function trackingDealerType(){
	var divisionObj = BRAND.toUpperCase();
	clickTrack({pageName: divisionObj+' | DIVISIONAL | DEALOC | ENTRY', prop1: 'DIVISIONAL', prop2:'DEALOC', prop3: 'DIVISIONAL | DEALOC', prop4: 'ENTRY', prop5:'DEALOC | ENTRY', prop6: 'DIVISIONAL | DEALOC | ENTRY', prop7:'ADVANCED SEARCH', prop8: 'ENTRY | ADVANCED SEARCH', prop9: 'DEALOC | ENTRY | ADVANCED SEARCH', prop10: 'DIVISIONAL | DEALOC | ENTRY | ADVANCED SEARCH',prop11:'TYPE OF DEALER',prop12:'ADVANCED SEARCH | TYPE OF DEALER',prop13:'ENTRY | ADVANCED SEARCH | TYPE OF DEALER',prop14:'DEALOC | ENTRY | ADVANCED SEARCH | TYPE OF DEALER',prop15:'DIVISIONAL | DEALOC | ENTRY | ADVANCED SEARCH | TYPE OF DEALER',prop17:'DEALER PREPARATION',prop18:'SHOPPING',prop24:'EN', prop25:divisionObj, prop26:(new Date()).getHours(), prop27: weekday[(new Date()).getDay()]});
}

function closeWindow(){
	UnTip();
}