/*
	Javascript to help dealer search page on MTF website
	
	04/02/2008 - AMB - Created
	
*/


var MtfDealerSearch = new Object();
MtfDealerSearch.BusinessTypeGroupname = "grpBusTypes";
MtfDealerSearch.BusinessTypeIdPrefix = "bt";
MtfDealerSearch.RegionIdPrefix = "rg";
MtfDealerSearch.PageName = "mtf-dealer-search.aspx";


MtfDealerSearch.SubmitSearch = function(sRegion) {

	//alert(MtfDealerSearch.GetSelectedBusTypes());
	var sBusTypes = MtfDealerSearch.GetSelectedBusTypes(true);
	
	sBusTypes = MtfDealerSearch.BusinessTypeIdPrefix + "=" + sBusTypes;
	
	var sRegionQs = MtfDealerSearch.RegionIdPrefix + "=";
	
	if (sRegion) {
		sRegionQs += sRegion;
	}
	
	
	window.location = MtfDealerSearch.PageName + "?" + sBusTypes + "&" + sRegionQs;

}


//Gets comma separated list of all business type ids for selected business types
MtfDealerSearch.GetSelectedBusTypes = function(ReturnAllIfNone) {

	var oAllTypes = document.getElementsByName(MtfDealerSearch.BusinessTypeGroupname);
	var sSelectedTypes = "";
	
	
	for (var x = 0; x < oAllTypes.length; x++) {
	
		if (oAllTypes[x].checked) {
			if (sSelectedTypes != "") {
				sSelectedTypes += ",";
			}
			sSelectedTypes += oAllTypes[x].id.replace(MtfDealerSearch.BusinessTypeIdPrefix, "");
		}
	
	}
	
	//If we want to return all the types if there are none then do so
	if (ReturnAllIfNone) {
		if (sSelectedTypes == "") {
			//Go through the items again but this time select them all
			for (var x = 0; x < oAllTypes.length; x++) {
			
				if (sSelectedTypes != "") {
					sSelectedTypes += ",";
				}
				sSelectedTypes += oAllTypes[x].id.replace(MtfDealerSearch.BusinessTypeIdPrefix, "");
			
			}
		}
	}
	
	return sSelectedTypes;

}
