

function montadoraPorTipo(ctx, buscando, elemTar, nomeTar, other) {
	var tipo = getEl('tipo').dom.options[getEl('tipo').dom.selectedIndex].value;
	var url = ctx + "/veiculosXml.montadoraPorTipo.mtw?buscando="+buscando+"&target="+nomeTar+"&other="+other+"&tipo="+tipo;
	Ajax.retrieve(url, elemTar);
}

function opcionaisPorTipo(ctx, idProduto, idVeiculo, from, elemTar) {
	var tipo = getEl('tipo').dom.options[getEl('tipo').dom.selectedIndex].value;
	var url = ctx + "/veiculosXml.opcionais.mtw?idProduto="+idProduto+"&tipo="+tipo+"&idVeiculo="+idVeiculo+"&from="+from;
	Ajax.retrieve(url, elemTar);
}

function selecionaMontadora(ctx, buscando, elemSrc, elemTar, nomeTar, tipoAgente, agente, other) {
	var tipo;
	try {
		tipo = getEl('tipo').dom.options[getEl('tipo').dom.selectedIndex].value;	
	} catch(e) { tipo = -1; }
	
	var src = getEl(elemSrc);
	var value = src.dom.options[src.dom.selectedIndex].value;
	var url = ctx + "/veiculosXml.montadora.mtw?buscando="+buscando+"&target="+nomeTar+"&other="+other+"&tipo="+tipo+"&idMontadora=" + value + (tipoAgente != -1 ? "&tipoAgente=" + getEl(tipoAgente).dom.value : "") 
				+ (agente != null ? "&idAgente=" + getEl(agente).dom.value : "");
	Ajax.retrieve(url, elemTar);

}

function selecionaCategoria(ctx, elemSrc, elemTar, id) {
	var updateMgr = getEl(elemTar).getUpdateManager();
	var src = getEl(elemSrc);
	var value = src.dom.options[src.dom.selectedIndex].value;
	var url = ctx + "/veiculosXml.categoria.mtw?id=" + id + "&idCategoria=" + value;
	updateMgr.update(url);
}


function mostrarImagem(ctx, elem, foto) {
	var updateMgr = getEl(elem).getUpdateManager();
	var url = ctx + "/public/agente/foto.jsp?foto="+foto;
	updateMgr.update(url);
}

function mostrarImagemVideo(ctx, elem, foto, hasVideo, nomeVideoHotSite) {
	var updateMgr = getEl(elem).getUpdateManager();
	var url = ctx + "/public/agentePersonalizado/foto.jsp?foto="+foto+"&hasVideo="+hasVideo+"&nomeVideoHotSite="+nomeVideoHotSite;
	updateMgr.update(url);
}

var Ajax = function() {
	var req, div;
	
	return {
		
		retrieve : function(url, idTarget) {
			div = document.getElementById(idTarget);
			this.getXhr(url, this.setContent);
		},
		
		getXhr : function(url, funcao) {
			if (window.XMLHttpRequest) { // Non-IE browsers
				req = new XMLHttpRequest();
				req.onreadystatechange = funcao;
				
				try {
					req.open("GET", url, true);
				} catch (e) {
					alert(e);
				}
				req.send(null);
			} else if (window.ActiveXObject) { // IE
				req = new ActiveXObject("Microsoft.XMLHTTP");
				if (req) {
					req.onreadystatechange = funcao;
					req.open("GET", url, true);
					req.send();
				}
			}
		},
	
		setContent : function() {
			if (req.readyState == 4) {
				if (req.status == 200) {
					div.innerHTML = req.responseText;
				} else {
					alert("Ocorreu um problema ao enviar os dados");
				}
			}	
		}
		
	}

}();