//Ä caracter para forçar detecção UTF8

function abrirCorpo(campo, str) {
	doRequest(campo,str,true,false);
}

function abrirCorpoSinc(campo, str) {
	doRequest(campo,str,false, false);
}

function abrirJanela(w, h, title, str) {
	criarJanela(1*w, 1*h, title, str);
	doRequest('janela',str+'&janela=1',true,false);
}

function abrirJanelaSinc(w, h, title, str) {
	criarJanela(1*w, 1*h, title, str);
	doRequest('janela',str+'&janela=1',false,false);
}

function abrirCorpoParent(campo, str) {
	doRequest(campo,str,true,true);
}

function criarJanela(w, h, title, str) {
	box_janela = getID('janela');
	box_overlay = getID('overlay');

	box_janela.style.height = h+'px';
	box_janela.style.width = w+'px';

	if (!documentCompat)
		documentCompat = (document.compatMode && document.compatMode=='CSS1Compat') ? documentElement : document.body;
	
	padding = 3;
	
	box_janela.style.padding = padding+'px';
	box_janela.style.overflow = 'auto';
	box_janela.style.border = '';
	box_janela.style.color = '#0f0f0f';
	box_janela.style.background = '#ffffff';

	if (title != '') {
		show('titlebar');
		//getID('titulojanela').innerHTML = "<a onclick=\"abrirCorpo('janela','"+str+"');\">reload</a> " + title;
		getID('titulojanela').innerHTML = title;
		border = 2;
	}
	else {
		hide('titlebar');
		border = 0;
	}
	
	box_overlay.style.borderWidth = border+'px';
	
	if (isMSIE) {
		document.body.style.overflow = 'scroll';
		
		box_overlay.style.top = documentCompat.scrollTop + (documentCompat.clientHeight-h)/2;
		box_overlay.style.left = documentCompat.scrollLeft + (documentCompat.clientWidth-w)/2;
		
		box_overlay.style.height = (h+border*2)+'px';
		box_overlay.style.width = (w+border*2)+'px';
	}
	else {
		box_overlay.style.marginTop = -h/2 + 'px';
		box_overlay.style.marginLeft = -w/2 + 'px';
		
		//box_overlay.style.height = h+'px';
		box_overlay.style.width = (w+padding*2)+'px';
	}
	
	document.onkeydown = handleKeys;
	
	show('background');
	show('overlay');
}

function fecharJanela() {
	document.onkeydown = null;
	
	prevPic = 0;
	nextPic = 0;
	
	hide('background');
	hide('overlay');
	
	getID('titulojanela').innerHTML = '';
	getID('janela').innerHTML = '';
	
	if (isMSIE)
		document.body.style.overflow = 'auto';
}

function doRequest(campo, str, asincronia, isParent) {
	var xmlHttp = null;
	var recebido = false;

	if (!str) {
		getID(campo).innerHTML = "404 error";
		return false;
	}

	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
		if (xmlHttp.overrideMimeType)
			xmlHttp.overrideMimeType('text/html');
	}
	else if (window.ActiveXObject) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (xmlHttp) {
		var params = str+"&sid="+Math.random();

		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200 || xmlHttp.status == 0 || xmlHttp.status == null) {
					recebido = finalizarRequest(campo, xmlHttp);
				}
				else {
					alert('Erro de servidor.');
				}
			}
		};

		if (!isParent)
			getID(campo).innerHTML = "<div align='center'><img src='images/loading.gif'></div>";

		xmlHttp.open('POST', 'index.php', asincronia);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);

		if ((!asincronia) && (!recebido)) {
			recebido = finalizarRequest(campo, xmlHttp);
		}
	}
	else {
		alert("Seu navegador nao suporta javascript.");
		return false;
	}
}

function finalizarRequest(campo, xmlHttp) {
	getID(campo).innerHTML = xmlHttp.responseText;
	recebido = true;

	jscripts = getID(campo).getElementsByTagName('dd');
	for (i=0; i<jscripts.length; i++) {
		codigo = jscripts[i].innerHTML.replace(/&amp;/g,'&');
		if (window.execScript)
			window.execScript(codigo);
		else
			eval(codigo);
	}

	if (campo == 'menubar')
		showMenu();
	
	return true;
}