var tmpBrand;
var tmpModel;

function findModelsForBrand()
{
	var sel = document.getElementById("caspd_brand");
	if (sel)
	{
		var selectedBrand = sel.options[sel.selectedIndex].value
		tmpBrand = selectedBrand;
		DropdownHelper.getCarBrandModels(selectedBrand, setModelOptions);
	}
}

function setModelOptions(modelArray)
{
	if (modelArray && modelArray.length > 0)
	{
		var sel = document.getElementById('caspd_model');
		sel.options.length = 0;
		
		var selectLabel = "Select ("+tmpBrand+")";
		sel.options[0] = new Option(selectLabel,"");
	
		for(var i = 0; modelArray && i < modelArray.length; i++)
		{
			var labelAndValue = modelArray[i].split(":");
			sel.options[i+1] = new Option(labelAndValue[0],labelAndValue[1]);
		}
	}
}

function saveSelectedModel()
{
	var selModel = document.getElementById('caspd_model');
	var model = selModel.options[selModel.selectedIndex].value;
	tmpModel = model;
}

function replaceSlash(str)
{
	str = str.replace("/", "_or_");

    if(str.indexOf("/") > -1)
    {
        str = replaceSlash(str);            
    }
    return str;
}

function replaceSpace(str)
{
	str = str.replace(" ", "_");

    if(str.indexOf(" ") > -1)
    {
        str = replaceSpace(str);            
    }
    return str;
}

function runSearch(folder)
{
	var brand = tmpBrand;
	var model = tmpModel;
	// sanitize brand for URL
	brand = replaceSlash(brand);
	brand = replaceSpace(brand);

	model = replaceSlash(model);
	model = replaceSpace(model);
	 
	// get model name and starting manufacturing date	
	var modelParts = model.split("@");
	window.location.href = '/'+folder+'/products/car_accessories/'+brand+'/'+modelParts[0]+'/'+modelParts[1]+'/index.html';
}