var http_request = false;
function CriaAjax(){
	http_request = null;
	if (window.XMLHttpRequest) { 
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { 
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Sem suporte a Ajax!');
		return false;
	}
}

function CarregaModulos(url){
	document.getElementById('transparente').style.display = 'block';
	document.getElementById('Contem').style.display = 'block';
	document.getElementById('ExibeConteudo').style.display = 'block';
	scroll(0,0);
	CarregaUrl(url,'ExibeConteudo');
}

function CarregaPCH(url,pch){
	window.open('http://www.peixeenergia.com.br/modulos.php?PCH='+pch+'&novo=abre&url='+url,'_self');
	document.getElementById('transparente').style.display = 'block';
	document.getElementById('Contem').style.display = 'block';
	document.getElementById('ExibeConteudo').style.display = 'block';
	scroll(0,0);
	//CarregaUrl(url,'ExibeConteudo');
}

function VoltaNovos(){
	window.open('http://www.peixeenergia.com.br/admin.php?novos=abre','_self');
	document.getElementById('transparente').style.display = 'block';
	document.getElementById('Contem').style.display = 'block';
	document.getElementById('ExibeConteudo').style.display = 'block';
	scroll(0,0);
}

function CarregaUrl(url,div){
	CriaAjax();
	DirDiv = document.getElementById(div);
	DivTransparente = document;
	http_request.onreadystatechange = CarregaConteudo
	http_request.open('GET', url, true);
	http_request.send(null);
}

function CarregaUrlCadastro(url,div){
	CriaAjax();
	DirDiv = document.getElementById(div);
	DivTransparente = document;
	http_request.onreadystatechange = CarregaConteudoCadastro
	http_request.open('GET', url, true);
	http_request.send(null);
}

function CarregaUrlUpload(url,div){
	CriaAjax();
	DirDiv = parent.document.getElementById(div);
	DivTransparente = parent.document;
	http_request.onreadystatechange = CarregaConteudo
	http_request.open('GET', url, true);
	http_request.send(null);
}

function CarregaConteudoCadastro() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			DirDiv.innerHTML = http_request.responseText;
			//var height = DivTransparente.getElementById('DivLayout').scrollHeight + 50;
			//var height_doc = DivTransparente.body.scrollHeight;
			//alert('arquivo = '+height+'Documento = '+height_doc);
			//if (height <= height_doc){
			//    var height = height_doc;
			//}
			//DivTransparente.getElementById("transparente").style.height = height+'px';
		} else {
			DirDiv.innerHTML = http_request.responseText;
			//alert('Ocorreu um erro!');
		}
	} else {
		DirDiv.innerHTML = "<div style=\"text-align:center;\">Carregando...<br /><img src=\"imagens/carregando.gif\" alt=\"Carregando\" /></div>";
	}
}

function CarregaConteudo() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			DirDiv.innerHTML = http_request.responseText;
			var height = DivTransparente.getElementById('DivLayout').scrollHeight + 50;
			var height_doc = DivTransparente.body.scrollHeight;
			//alert('arquivo = '+height+'Documento = '+height_doc);
			if (height <= height_doc){
			    var height = height_doc;
			}
			DivTransparente.getElementById("transparente").style.height = height+'px';
		} else {
			DirDiv.innerHTML = http_request.responseText;
			//alert('Ocorreu um erro!');
		}
	} else {
		DirDiv.innerHTML = "<div style=\"text-align:center;\">Carregando...<br /><img src=\"imagens/carregando.gif\" alt=\"Carregando\" /></div>";
	}
}

function EnviaConteudo(url,div,localValidar,TestaElementos){
	DirDiv = document.getElementById(div);
	DivTransparente = document;
	CriaAjax();
	var texto = "";
	//pega todos os elementos input e textarea do form
	var FormInput = localValidar.getElementsByTagName("input");
	var FormTextarea = localValidar.getElementsByTagName("textarea");
	var FormSelect = localValidar.getElementsByTagName("select");
	//verifica campos obrigatorios (somente input)
	for (i=0;i<FormInput.length;i++){
		if (FormInput[i].name == TestaElementos[i]){
			if (FormInput[i].value == ""){
				alert('Os campos destacados são obrigatórios!');
				FormInput[i].focus();
				return false;
			}
		}
	}
	//atribui valores dos input
	for(i=0;i<FormInput.length;i++) {
		if (i == 0){
			texto += FormInput[i].name +"="+ FormInput[i].value;
		}else if (FormInput[i].name == "EnviarEmail"){
			texto += "&"+ FormInput[i].name +"="+ FormInput[i].checked;
		}else{
		   	texto += "&"+ FormInput[i].name +"="+ FormInput[i].value;
		}
	}
	//concatena valores dos textarea
	for (i=0;i<FormTextarea.length;i++){
		texto += "&"+ FormTextarea[i].name +"="+ FormTextarea[i].value;
	}
	//concatena valores dos select
	for (i=0;i<FormSelect.length;i++){
		texto += "&"+ FormSelect[i].name +"="+ FormSelect[i].value;
	}
	//envia o conteudo para gravar
	texto = encodeURI(texto);
	http_request.open("POST",url,true);
	http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=ISO-8859-1');
	http_request.send(texto);
	http_request.onreadystatechange = CarregaConteudo
}