//Metódos de link na tabela
// Manipulação de mouse
// Metódo de abertura do ajax

// Esta função instancia o objeto XMLHttpRequest
function openAjax() {
	var ajax;
	try {
		ajax = new XMLHttpRequest();
	} catch(ee) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				ajax = false;
			}
		}
	}
	return ajax;
}



// Métodos e ações em JavaScript

//Pega o tamanho da página
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) 

}

// Utilizado para evitar de digitar: document.getElementById toda hora, tornando o processo mais prático
function gE(ID) {
	return document.getElementById(ID);
}

// Utilizado para evitar de digitar: document.getElementsByTagName toda hora, tornando o processo mais prático
function gEs(tag) {
	return document.getElementsByTagName(tag);
}


// Utilizado para criar o fundo sobre a página (wiewport), body.
function exibirBgBody() {
	// Seleciona a tag body. item(0) por que só existe uma tag body
	var tagBody = gEs('body').item(0);
	// Pega os tamanhos atuais da página, como largura, altura, ...
	var sizesPage = getPageSize();
	// Vamos criar uma tag div
	var bgBody = document.createElement('div');
	// Setar o atributo ID a div criada
	bgBody.setAttribute('id','bgBody');
	//bgBody.setAttribute('onclick','alert("oi")');
	bgBody.onclick= function(){
    removerDivs();}
	// Essa div terá o tamanho exato da página
	bgBody.style.height = arrayPageSize[1] + 'px';
	// Essa div terá a largura exata da página
	bgBody.style.width = arrayPageSize[0] + 'px';
	// Evita criar a div novamente
	//if (!gE('bgBody')) {
		tagBody.insertBefore(bgBody, tagBody.firstChild);
	//}	
}

// Esta função elimina da página o fundo criado sobre o body e o boxCad;
function removerDivs() {
	var bgBody = gE('bgBody');
	var box0 = gE('box0');
	bgBody.parentNode.removeChild(bgBody);
	if (box0) { // Por que ao clicar X (para deletar um registro) cria-se somente o encobridor e não o boxCad	
		box0.parentNode.removeChild(box0);
	}
}

// Cria a div denominada como box0, a qual conterá o formulário de cadastro
function box0() {
	// Cria um 'container' que comportará o formulário de cadastro.
	var objBody = gEs('body').item(0);
	//var sizesPage = getPageSize();
	var box0 = document.createElement('div');
	box0.setAttribute('id','box0');
	//var wPage = arrayPageSize[0]; // Largura total da página
	//var hPage = arrayPageSize[1]; // tamanho total da página
	//box0.style.width = (wPage / 2) + 'px'; // metade da largura da página
	//box0.style.height = (wPage / 2) + 'px'; // metada da altura da página
	//box0.style.marginLeft = (arrayPageSize[0] / 2 - 400) + 'px'; // 1 quarto da altura
	objBody.insertBefore(box0, objBody.lastChild);
}

// Utilizado para criar o efeito de loading
function loading(opt) {
	if (opt == true) {
		// A tag que receberá a img de loading
		var refer = gE('bgBody');
		// O tamanho da referida tag
		//var referHeight = refer.offsetHeight;
		// Dizemos que os elementos dentro dela será alinhado ao centro
		//refer.style.textAlign = 'center';
		// Criamos uma imagem, img.
		var img = document.createElement('img');
		// Informamos o caminho da img
		img.setAttribute('src','imagens/fixo/meio/ajax_load.gif');
		// Setamos um atributo ID na img criada
		img.setAttribute('id','loading');
		// Dizemos que o margin-top será a metade do tamanho da div
		img.style.marginTop = 30 + '%';
		img.style.width = 126 + 'px';
		img.style.height = 15 + 'px';
		// Evita que seja criada duas ou mais img de loading
		if (!document.getElementById('loading')) {
			// Insere a img na tag informada na variável refer
			refer.insertBefore(img, refer.firstChild);
		}
	} else if (opt == false) {
		// Referenciamos a img de login através de seu ID
		var imgLoading = gE('loading');
		// Removemos a img de loading
		if (imgLoading) {
			imgLoading.parentNode.removeChild(imgLoading);
		}
	}
}
//---------------------------------------------------------------------------------------------------------------------------------
// Inserir pagina pedida com ajax -------------------------------------------------------------------------------------------------
function insertpage(oque, onde, remove) {
		// Inicia o Ajax, através da variável Ajax
		var ajax = openAjax();
		// Executa a função que cria o fundo sobre página
		exibirBgBody();	
		box0();	
		var remover = remove; 
		// A tag bgBody conterá o formulário de cadastro
		var recipiente = gE(onde);
		// Informamos o método e a página que será requisitada
		ajax.open('GET', oque, true); 
		// bla
		ajax.onreadystatechange = function() {
			if (ajax.readyState == 1) {
				// Cria o efeito de loading
				loading(true);	
			} // if->readyState->1
			if (ajax.readyState == 4) {
				if (ajax.status == 200) {
					// Remove o efeito de loading
					loading(false);
					// Pega o conteúdo - HTML - da página requisitada e coloca dentra da div definida na variável recipiente
					recipiente.innerHTML = ajax.responseText;
				} // if-status->200
			} // if->readyState->4
		} // ajax->onreadystatechange
		// Envia a requisição
		ajax.send(null); 
		if (remover == 'sim'){
		removerDivs();
		}
		// Evita o reload da página
		return false;
}
function openmenu(f){
	if(gE(f)){gE(f).style.display='block';}
}
function closemenu(f){
	if(gE(f)){gE(f).style.display='none';}
}
function trocaico(f){
	if(gE(f)){
		if(gE(f).className=='menos'){
		gE(f).className='mais';
		} else {
		gE(f).className='menos';
		}
	}
}
function openurl(caminho){
	window.location=caminho;
}
