
function autoUsate_getModelsByBrandCode(brandNode, modelId) {
	var brand = $("option:selected", brandNode).val();
	
	$.ajax({
		async: true,
		url: "/ws/AutoUsate.cfc",
		type: "POST",
		data: {
			method: "GetModelByBrand",
			brand: brand
		},
		dataType: "json",
		success: function(result) {
			if (result.SUCCESS) {
				ret = result.DATA;
				
				var $model = $("#" + modelId);
				$model.html("");
				$('<option value="" selected="selected">Modello</option>').appendTo($model);
				$model.next().text("Modello");
				
				for (var i=0; i<ret.DATA.length; i++)
				{
					var modelValue = ret.DATA[i][0];
					
					$("<option>").
						attr("value", modelValue).
						text(modelValue).
						appendTo($model);
				}
			} else {
				autoUsate_manageError(result.DATA);
			}
		},
		error: function() {
			autoUsate_manageError("Server error.");
		}
	});
}


function autoUsate_getModelsByBrandCodeModel(modelSelect, brandNode, modelId) {
	var brand = $("option:selected", brandNode).val();
	
	$.ajax({
		async: true,
		url: "/ws/AutoUsate.cfc",
		type: "POST",
		data: {
			method: "GetModelByBrand",
			brand: brand
		},
		dataType: "json",
		success: function(result) {
			if (result.SUCCESS) {
				ret = result.DATA;
				
				var $model = $("#" + modelId);
				$model.html("");
				$('<option value="">Modello</option>').appendTo($model);
				$('<option value="'+modelSelect+'" selected="selected">'+modelSelect+'</option>').appendTo($model);
				$model.next().text("Modello");
				
				for (var i=0; i<ret.DATA.length; i++)
				{
					var modelValue = ret.DATA[i][0];
					
					$("<option>").
						attr("value", modelValue).
						text(modelValue).
						appendTo($model);
				}
			} else {
				autoUsate_manageError(result.DATA);
			}
		},
		error: function() {
			autoUsate_manageError("Server error.");
		}
	});
}

function autoUsate_manageError(message) {
	alert("An exception occurred in the script. Error message: " + message);
}

$(document).ready(function(){	
	if (!$.browser.opera) {
		$('select.select').each(function(){
			var title = $('option:selected',this).text();
			
			$(this)
				.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
				.after('<span class="select">' + title + '</span>')
				.change(function(){
					val = $('option:selected',this).text();
					$(this).next().text(val);
				})
		});
		$('select.select-medium').each(function(){
			var title = $('option:selected',this).text();
			
			$(this)
				.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
				.after('<span class="select-medium">' + title + '</span>')
				.change(function(){
					val = $('option:selected',this).text();
					$(this).next().text(val);
				})
		});
	};
});

