
//**************************************************************************************
//Função AJAX DE Envio via GET
//**************************************************************************************
function loadAjax(url,div) {
var req;

loadajax2(url);
function loadajax2(url){
 req = null;
if (window.XMLHttpRequest) {
 req = new XMLHttpRequest();
 req.onreadystatechange = processReqChange;
 req.open("GET", url, true); 
 req.send(null);

} else if (window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP.4.0");
} catch(e) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP.3.0");
} catch(e) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
req = false;
}
}
}
}
if (req) {
 req.onreadystatechange = processReqChange;
 req.open("GET", url, true);
 req.send();
}
}
}

function processReqChange(){
if(req.readyState == 1) {
	    if (div != "") {
          document.getElementById(div).innerHTML = "<div align='center'><img src='img/preload.gif' ></div>";
	    }
}

if (req.readyState == 4) {
if (req.status == 200) {
     if (div != "") {
       document.getElementById(div).innerHTML = req.responseText;
     }
} else {
alert("Houve um problema ao obter os dados:\n" + req.statusText);
}
}
}
}
//*******************************FIM*********************************************************


//Fecha o container que está por cima
function fechacontainer(usahab,divs)
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	if (usahab==1){
		HabDesabilitadiv(2,divs);
	}
	// disable keypress listener
	document.onkeypress = '';
}

// abre por ajax a pagina desejada
function ajaxinstancia(url,div,usahab,divs){
	page_request=false
	try{page_request = new XMLHttpRequest(); }
	catch(ee)
		{
		try{ page_request = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e)
		{
			try{page_request = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(E)
			{page_request = false; }
		}
	}
	bustcacheparameter=('&var='+new Date().getTime()+new Date().getTime())
	page_request.open('GET', url+bustcacheparameter, true)
	page_request.send(null)	
	page_request.onreadystatechange=function(){
	if (page_request.readyState == 4 ){
		document.getElementById(div).innerHTML="";
		var teste = page_request.responseText;
		teste = teste.replace('<META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use.">','');
		document.getElementById(div).innerHTML=teste;
		document.getElementById('fechar').onclick= function () {fechacontainer(usahab,divs); return false;}
		}
	}
}
//pega tamanho da pagina
function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
//pega tamanho da pagina
function getPageSize(){	
	var xScroll, yScroll;	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//'cria o container
function createcontainer(usahab,divs){	
	var objBody = document.getElementsByTagName("body").item(0);
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {fechacontainer(usahab,divs); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '90';	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);	
}
//faz aparecer o container
function showcontainer(postop,posleft){
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	
    //var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - 500) / 2);
	//var lightboxLeft = ((arrayPageSize[0] - 20 - 550) / 2);
	 var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - postop) / 2);
     var lightboxLeft = ((arrayPageSize[0] - 20 - posleft) / 2);
		
	objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
	objLightbox.style.display = 'block';
	objLightbox.innerHTML="<div align='center'><img src='img/loading.gif'></div>";
}
function submetepagina(formulario){
	//alert(formulario);
	formulario.submit();
	
	}
//desabilita os div
function HabDesabilitadiv(op,div){
	//desabilita
	if (op==1){
		try{document.getElementById(div).style.visibility="hidden"}
			catch(E)
			{}
		}
	//habilita
	if (op==2){
		try{document.getElementById(div).style.visibility="visible"}
			catch(E)
			{}
		}	
}

function chamaajax(pagina,usahab,divs,postop,posleft){
	//alert(usahab);
	if (usahab==1){
		HabDesabilitadiv(1,divs);
	}
	createcontainer(usahab,divs);
	showcontainer(postop,posleft);
	ajaxinstancia(pagina,'lightbox',usahab,divs)
}

