function ExecutarAjax(cAction,cParametros, idObj) {
	var xmlHttp;
	if (window.XMLHttpRequest){
		try{
			xmlHttp = new XMLHttpRequest();
		}catch(e){
			xmlHttp = false;
		}
	}else if(window.ActiveXObject){
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlHttp = ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlHttp = false;
			}
		}
	}

	var cRetorno = "";
	var obj = null;

	if (idObj != undefined) {
		obj = document.getElementById(idObj);	
	}

	if (!xmlHttp) {
		cRetorno = "Erro na criação do objeto Ajax!\nExperimente atualizar o seu navegador e teste novamente.";
		return cRetorno;
	}

	xmlHttp.open("POST", cAction, true);
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var resultadoAjax = xmlHttp.responseText;
				if (resultadoAjax != "") {
					if (obj == null) {
						eval(resultadoAjax);	
					}else{
						obj.innerHTML = resultadoAjax;
					}
					
				}
			}
		}
	}

	
	
	//xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=iso-8859-1');
	xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;');
	xmlHttp.setRequestHeader('Content-length', cParametros.length);
	xmlHttp.setRequestHeader('Cache-Control', 'no-cache'); 
	xmlHttp.setRequestHeader('Pragma', 'no-cache');
	xmlHttp.setRequestHeader('Connection', 'close'); 
	xmlHttp.send(cParametros);

	return true;
}

function RetornCampos() {
	var oElementos = document.formulario.elements;
	var cParams = "";
	var cSep = "";

	// Percorre todos os campos do formulario.
	for (var i=0; i<oElementos.length; i++) {

		// Verifica se não é do tido botão.
		if (oElementos[i].type != "button") {
			cParams += cSep + oElementos[i].name + "=" + oElementos[i].value;
			cSep = "&";
		}
	}

	return cParams;

}

function ExecutaForm(cAction, cPasta) {
	document.formulario.ACTION.value = cAction;
	document.formulario.PASTA.value = cPasta;
	document.formulario.action = "lib/action.php";
	document.formulario.submit();
}


function LimparCampos() {
	var oElems = document.formulario.elements;
	
	for (var i=0; i < oElems.length; i++) {
		if (oElems[i].type != "button") {
			if (oElems[i].type.indexOf("select") != -1) {
				oElems[i].value = "0";
			} else {
				oElems[i].value = "";
			}
		}
	}
}

function addInputFile(obj) {
	if ((obj == undefined) || (obj.tagName != "span")) {
		var oPai = document.getElementById("tdUpload");
	} else {
		var oPai = obj.parentNode;
	}

	var oDiv = document.createElement("DIV");
	if (document.all) {
		var oCheck = document.createElement('<input type="file" name="arquivo[]">');
	} else {
		var oCheck = document.createElement('INPUT');
	}
	var oSpan = document.createElement("SPAN");
	
	oSpan.innerHTML = "&nbsp;[Adicionar arquivo]";
	oSpan.style.cursor = "pointer";
	oSpan.onclick = addInputFile;

	
	if (!document.all) {
		oCheck.type = "file";
		oCheck.setAttribute("name","arquivo[]");
	}
	oCheck.size = "40";	

	oDiv.appendChild(oCheck);
	oDiv.appendChild(oSpan);
	oPai.appendChild(oDiv);
			
}

function editarNomeArquivo(oLinha) {
	var nIndice = oLinha.parentNode.rowIndex;
	var oEdit = document.getElementById("inputEdit");
	var oNomeAtual = document.formulario.NOMEATUAL;

	if (oEdit.parentNode == oLinha) { return false; }
	
	oNomeAtual.value = oLinha.innerHTML;
	
	oEdit.value = oLinha.innerHTML.substring(0, oLinha.innerHTML.length - 5);
	oLinha.innerHTML = "";

	oLinha.appendChild(oEdit);
	oEdit.style.visibility = "visible";
	
	oEdit.select();

}

function gravaEditNomeArquivo(e) {
	var oEdit = document.getElementById("inputEdit");
	var nIndice = oEdit.parentNode.parentNode.rowIndex;
	var oLinha = oEdit.parentNode;
	var oNomeAtual = document.formulario.NOMEATUAL;
	var oNovoNome = document.formulario.NOVONOME;
	
	if (oLinha.tagName != "TD") { return false; }
	if (window.all) { e = window.event; }
	if ((e.type == "keydown") && (e.keyCode!=13)) {	return false; }
	
	oEdit.style.visibility = "hidden";

	document.body.appendChild(oEdit);
	oLinha.innerHTML = oEdit.value + ".html";
	
	oNovoNome.value = oLinha.innerHTML;

	if (oNomeAtual.value != oNovoNome.value) {
		ExecutaForm('renomear', 'gerarfolder');
	}

}

function camposgerarfolder(cNome){
	var tabela = document.getElementById("tabelafolder");

	for (var i=0; i < tabela.rows.length; i++) {

		if (tabela.rows[i].getAttribute("name") == cNome) {
			if (tabela.rows[i].style.visibility == "hidden") {
				tabela.rows[i].style.visibility = "visible";
				tabela.rows[i].style.position = "static";
			} else {
				tabela.rows[i].style.visibility = "hidden";
				tabela.rows[i].style.position = "absolute";
			}
		}
	}
		
}

