var evts = null;

var initAplicacao = null;
var initResults = null;
var aplicacaoIsPopulated = false;
var resultsIsPopulated = false;

function initAjax() {
	evts = new Array(
		//          0         1        2         3           4       5
		//new Array("source", "event", "target", "function", "page", new Array("paramName1", "paramName2"))
		new Array("produto", "change", "montadoraDiv", "produtoHandler", "listMontadoras.jsp", new Array("produto")),
		new Array("montadora", "change", "aplicacaoDiv", "montadoraHandler", "listAplicacoes.jsp", new Array("produto", "montadora")),
		new Array("aplicacao", "change", "resultsDiv", "aplicacaoHandler", "results4.jsp", new Array("produto", "montadora", "aplicacao")),
		new Array("search", "click", "resultsDiv", "searchHandler", "results4.jsp", new Array("search")),
		//new Array("searchByApplication", "click", "resultsDiv", "searchHandler", "results2.jsp", new Array("searchByApplication")),
		new Array("searchByApplication", "keyup", "appResDiv", "searchHandler", "searchByApplicationLike.jsp", new Array("searchByApplication")),
		new Array("searchByApplicationLike", "click", "resultsDiv", "searchHandler", "results4.jsp", new Array("searchByApplicationLike")),
		new Array("showMedidas", "click", "medidasDiv", "searchHandler", "medidas.jsp", new Array("searchByApplicationLike")),
		new Array("lancamentos", "click", "resultsDiv", "searchHandler", "results4.jsp", new Array("lancamentos"))
	);
	initAplicacao = $("aplicacaoDiv").innerHTML;
	initResults = $("resultsDiv").innerHTML;
}

function searchHandler(field) {
	mainHandler(field);
	aplicacaoIsPopulated = true;
}

function produtoHandler() {
	mainHandler("produto");
	if (aplicacaoIsPopulated) {
		$("aplicacaoDiv").innerHTML = initAplicacao;
		aplicacaoIsPopulated = false;
	}
	if (resultsIsPopulated) {
		$("resultsDiv").innerHTML = initResults;
		resultsIsPopulated = false;
	}
}

function montadoraHandler() {
	mainHandler("montadora");
	aplicacaoIsPopulated = true;
	if (resultsIsPopulated) {
		$("resultsDiv").innerHTML = initResults;
		resultsIsPopulated = false;
	}
}

function aplicacaoHandler() {
	mainHandler("aplicacao");
	resultsIsPopulated = true;
}

function medidasHandler(produto, classificacao, roscaHaste, roscaCopo, medida) {
	var id = "showMedidas";
	var idx = indexOf(evts, id);
	var target = $(evts[idx][2]);
	var page = evts[idx][4];
	//var params = mountParams(evts[idx][5]);
	var params = "produto=" + escape(produto) + "&classificacao=" + escape(classificacao) + "&roscaHaste=" + escape(roscaHaste) + "&roscaCopo=" + roscaCopo + "&medida=" + escape(medida);
	var url = page + "?" + params;
	$("conversaoDiv").style.visibility = "hidden";
	$("alertaDiv").style.visibility = "hidden";
	if (id != "appResDiv") $("appResDiv").style.visibility = "hidden";
	$("ondeComprarDiv").style.visibility = "hidden";
	doHttpPost(url, target);
	centralizeDiv("medidasDiv");
	$("medidasDiv").style.visibility = "visible";
}

function mainHandler(id) {
	var idx = indexOf(evts, id);
	var target = $(evts[idx][2]);
	var page = evts[idx][4];
	var params = mountParams(evts[idx][5]);
	var url = page + "?" + params;
	$("conversaoDiv").style.visibility = "hidden";
	$("alertaDiv").style.visibility = "hidden";
	$("medidasDiv").style.visibility = "hidden";
	if (id != "appResDiv") $("appResDiv").style.visibility = "hidden";
	$("ondeComprarDiv").style.visibility = "hidden";
	doHttpPost(url, target);
}

function indexOf(array, value) {
	for (var i = 0; i < array.length; i++)
		if (array[i][0] == value) return i;
	alert("Não encontrado o valor '" + value + "' no array especificado");
	return -1;
}

function mountParams(params) {
	var mounted = "";
	for (var i = 0; i < params.length; i++)
		mounted += params[i] + "=" + escape($F(params[i])) + "&";
	return mounted;
}

function $(id) {
	return document.getElementById(id);
}

function $F(id) {
	return $(id).value;
}

function doHttpPost(url, targetObj) {
	showStatusBar();
	var xmlhttp;
	try{
	    xmlhttp = new XMLHttpRequest();
	} catch(e1){
	    try{
	        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch(e2){
	        try{
	            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch(e3){
	            xmlhttp = false;
	        }
	    }
	}
	if (netscape) xmlhttp.overrideMimeType('text/xml');
	var texto = null;
	xmlhttp.open("POST", url, true);
	xmlhttp.onreadystatechange = function() {
	    if (xmlhttp.readyState == 4){
			texto = xmlhttp.responseText;
            texto = texto.replace(/\+/g, " ");
            texto = unescape(texto);
			targetObj.innerHTML = texto;
			hideStatusBar();
	    }
	}
	xmlhttp.send(null);
	return texto;
}

function setHtmlCorpo(url) {
	//showStatusBar();
	var xmlhttp;
	try{
	    xmlhttp = new XMLHttpRequest();
	} catch(e1){
	    try{
	        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch(e2){
	        try{
	            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch(e3){
	            xmlhttp = false;
	        }
	    }
	}
	if (netscape) xmlhttp.overrideMimeType('text/xml');
	var texto = null;
	xmlhttp.open("POST", url, true);
	xmlhttp.onreadystatechange = function() {
	    if (xmlhttp.readyState == 4){
			texto = xmlhttp.responseText;
            texto = texto.replace(/\+/g, " ");
            texto = unescape(texto);
			//targetObj.innerText = texto;
			$("htmlCorpo").value = texto;
			//hideStatusBar();
	    }
	}
	xmlhttp.send(null);
	return texto;
}
