// JavaScript Document
function abreajax()
{ var ajax;
try
{
  ajax = new XMLHttpRequest(); //primeiro tentamos criar um objeto do tipo XMLHttpRequest()
}
catch(ee)//no entanto navegadores como IE não conhecem esse objeto
{
  try
  {
    ajax = new ActiveXObject("Msxml2.XMLHTTP"); //O IE conhece o ActiveXObject, mas o parâmetro depende da versão.
  }
  catch(e)
  {
    try
    {
      ajax = new ActiveXObject(" Microsoft.XMLHTTP");
    }
    catch(E)
    {
      ajax = false;//nesse caso o navegador não é compatível e nosso site não funcionará com AJAX :-(
    }
  }
}
return ajax;//retorna o objeto pronto.
}
<!-- Fecha ajax-->


//FUNÇÕES PARA GERAR A TELA EM CIMA DA TELA PRINCIPAL
//INICIO
function gE(ID) {
	return document.getElementById(ID);
}

function gEs(tag) {
	return document.getElementsByTagName(tag);
}	

function focusFechar2() {
	// Concede o focus ao campo nome do cadastro
	gE('nome').blur();
}


function validaRadio(){

var resposta;
var blnFlag = false;

radio = gEs("input");

for(i=0; i<radio.length; i++) // percorre os radios e verifica qual radio está marcado
	{

		if (radio[i].getAttribute("type") == "radio" && radio[i].checked == true) // se for input do tipo radio e estiver marcado
		{

		resposta = radio[i].value;
		blnFlag = true;
		}
	}
}




function focusFechar() {
	// Concede o focus ao campo nome do cadastro
	gE('email').blur();
}

function Ativar_form()
{
var news = gE('email').value;

	if(news != '') 
	{
		//Executa a função que cria o fundo da pagina
		exibirBgBody();
		//Cria uma div que conterá o retorno
		boxCad();
		//chama a função do ajax
		focusFechar();
		
		var ajax = abreajax();
		//declaramos o recepiente
		if(ajax)
		{
			var recipiente = gE('boxCad');
			//metodo e pagina a serem utilizados
			ajax.open('GET','ajax/grava_news_ajax.php?email='+news,true);
			//funçoes padrão do ajax
			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: formulario.php?ajax=true e coloca dentra da div definida na variável recipiente
						recipiente.innerHTML = ajax.responseText;
						// Chama a função que trabalha sobre os botões de Ok e Cancelar
						} // if-status->200
					} // if->readyState->4
			} // ajax->onreadystatechange
		// Envia a requisição
		ajax.send(null);
		}
		// Evita o reload da página
		return false;
	}
}


// Funções para o envio de formulário de contatos

function Envia_Form()
{
var nome = gE('nome').value;
var email = gE('email').value;
var mensagem = gE('mensagem').value;

var resposta;

radio = gEs("input");

for(i=0; i<radio.length; i++) // percorre os radios e verifica qual radio está marcado
	{

		if (radio[i].getAttribute("type") == "radio" && radio[i].checked == true) // se for input do tipo radio e estiver marcado
		{

		resposta = radio[i].value;
		}
	}


	if(nome != '') 
	{
	
		/*if(email.value.indexOf("@") == -1 || email.value.indexOf(".") == -1 || email.value == "" || email.value == null) {
			alert("Por favor, informe-nos um e-mail válido.");
			email.focus();
			return false;
			}*/
		
		
		//Executa a função que cria o fundo da pagina
		exibirBgBody();
		//Cria uma div que conterá o retorno
		boxCad();
		//chama a função do ajax
		focusFechar2();
		
		var ajax = abreajax();
		//declaramos o recepiente
		if(ajax)
		{
			var recipiente = gE('boxCad');
			//metodo e pagina a serem utilizados
			ajax.open('GET','ajax/grava_form_ajax.php?nome='+nome+'&email='+email+'&assunto='+resposta+'&mensagem='+mensagem,true);
			//funçoes padrão do ajax
			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: formulario.php?ajax=true e coloca dentra da div definida na variável recipiente
						recipiente.innerHTML = ajax.responseText;
						// Chama a função que trabalha sobre os botões de Ok e Cancelar
						} // if-status->200
					} // if->readyState->4
			} // ajax->onreadystatechange
		// Envia a requisição
		ajax.send(null);
		}
		// Evita o reload da página
		return false;
	}
}


// 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');
	// 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);
	}	
}

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



// 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/imgLoading.gif');
		// Setamos um atributo ID na img criada
		img.setAttribute('id','loading');
		// Definimos seu tamanho
		img.setAttribute('width','126');
		// Dizemos que o margin-top será a metada do tamanho da div
		img.style.marginTop = (referHeight /2) + '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);
		}
	}
}

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

function removerDivs2() {
	document.getElementById('nome').value = "";
	document.getElementById('email').value = "";
	document.getElementById('mensagem').value = "";
	var bgBody = gE('bgBody');
	var boxCad = gE('boxCad');
	bgBody.parentNode.removeChild(bgBody);
	if (boxCad) { // Por que ao clicar X (para deletar um registro) cria-se somente o encobridor e não o boxCad	
		boxCad.parentNode.removeChild(boxCad);
	}
}

function remover() {
	document.getElementById('assunto').value = "";
	document.getElementById('nome').value = "";
	document.getElementById('email').value = "";
	document.getElementById('mensagem').value = "";
}

/* Funções de terceiros */
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
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) 
}
//FIM

//AÇÕES PARA LIMPAR CAMPOS DO FORMULÁRIO DE CADASTRO.
function limpar_form(){
	document.getElementById('nome').value = "";
	document.getElementById('email').value = "";
	document.getElementById('empresa_cad').value = "";
	document.getElementById('cidade').value = "";
	document.getElementById('sigla').value = "";
	document.getElementById('ddd').value = "";
	document.getElementById('telefone').value = "";
	document.getElementById('opcao').value = "";
	document.getElementById('soft').value = "";
}


