/*Funcao para abrir o objeto do ajax*/
function openAjax(){
	var ajax;
	  try {
		 ajax = new ActiveXObject("Microsoft.XMLHTTP");
	  } 
	  catch(e) {
		 try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		 }
		 catch(ex) {
			try {
			   ajax = new XMLHttpRequest();
			}
			catch(exc) {
			   alert("Esse browser n\xE3o tem recursos para uso do Ajax");
			   ajax = null;
			}
		 }
	  }
	  return ajax;
}
function exibirBgBody(){
	if (!document.getElementById('bgBody')) {
		var tagBody = document.getElementsByTagName('body').item(0);
		var sizesPage = getPageSize();
		var bgBody = document.createElement('div');
		bgBody.setAttribute('id', 'bgBody');
		bgBody.style.height = (arrayPageSize[1]+300) + 'px';
		bgBody.style.width = arrayPageSize[0] + 'px';
		if (!document.getElementById('bgBody')) {
			tagBody.insertBefore(bgBody, tagBody.firstChild);
		}
	}
}
function ajax_pg(elem, url, param) {
	var ajax = openAjax();
	url_open = url + '?' + param;
	ajax.open('GET', url_open);
	ajax.onreadystatechange = function(){
		var objRec = document.getElementById(elem);
		if(ajax.readyState == 1) abreImgLoading(true);
		if(ajax.readyState == 4) {
			if(ajax.status == 200) {
				abreImgLoading(false);
				objRec.innerHTML = ajax.responseText;
			}
		}
	}
	ajax.send(null);
}

function boxPg(){
	if (!document.getElementById('boxCad')){
		var objBody = document.getElementsByTagName('body').item(0);
		var sizesPage = getPageSize();
		var boxCad = document.createElement('div');
		boxCad.setAttribute('id', 'boxCad');
		var wPage = arrayPageSize[0]; // Largura total da p\xE1gina
		var hPage = arrayPageSize[1]; // tamanho total da p�gina
		/*boxCad.style.width = (wPage / 2) + 'px'; // metade da largura da p�gina*/
		var escala;
		if(navigator.appName.indexOf('Internet Explorer') != -1) escala = 0; else escala = 1;
		boxCad.style.height = (wPage / 2) + 'px';
		boxCad.style.marginTop = -(wPage / (6 + escala)) + 'px';
		boxCad.style.marginLeft = -(wPage / (5 + 0)) + 'px';
		objBody.insertBefore(boxCad, objBody.lastChild);
	}
}

function abreImgLoading(opt) {
	if (opt == true) {
		var refer = document.getElementById('bgBody');
		var referHeight = refer.offsetHeight;
		refer.style.textAlign = 'center';
		var img = document.createElement('img');
		img.setAttribute('src','images/loading.gif');
		img.setAttribute('id','loading');
		img.setAttribute('width','126');
		img.style.marginTop = (referHeight /2) + 'px';
		if (!document.getElementById('loading')) {
			refer.insertBefore(img, refer.firstChild);
		}
	} else if (opt == false) {
		var imgLoading = document.getElementById('loading');
		if (imgLoading) {
			imgLoading.parentNode.removeChild(imgLoading);
		}
	}
}
function removeDivs() {
	var bgBody = document.getElementById('bgBody');
	var boxCad = document.getElementById('boxCad');
	bgBody.parentNode.removeChild(bgBody);
	if (boxCad) {	
		boxCad.parentNode.removeChild(boxCad);
	}
}

function pgCenter(url) {
		exibirBgBody();
		boxPg();
		var ajax = openAjax();
		var recipiente = document.getElementById('boxCad');
		ajax.open('GET', url, true); 
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 1) {
				abreImgLoading(true);	
			} 
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					abreImgLoading(false);
					recipiente.innerHTML = ajax.responseText;
				} 
			}
		}
	ajax.send(null);
	return false;
}

function setaAtributo(obj, atrb, vlr) {
	document.getElementById(obj).setAttribute(atrb, vlr);
}
function copiaAtributo(objDest, atrb, objOrig){
	var valor = document.getElementById(objOrig).value;
	document.getElementById(objDest).setAttribute(atrb,valor );
}

/*Fun��es de Terceiros*/
//Retorna o 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) 

}
// JavaScript Document
