// 
// Funções
//
	
	function Tecla(e)
	{	
		if (document.all) // Internet Explorer
			var tecla = e.keyCode;
		else
			var tecla = e.which;
		
		if (tecla > 47 && tecla < 58){
				return true;			
		}
		else
		{
			if (tecla == 8 || tecla == 0)
				return true;
			else
				return false;
		}
	
	}
	
	function Tecla2(e)
	{	
		if (document.all) // Internet Explorer
			var tecla = e.keyCode;
		else
			var tecla = e.which;
		if (
				(tecla > 47 && tecla < 58) ||
				(tecla >= 97 && tecla <= 122) ||
				(tecla >= 65 && tecla <= 90)
		)
		{ 
				return true;			
		}
		else
		{
			if (tecla == 8 || tecla == 0)
				return true;
			else
				return false;
		}
	}
	
	function Tecla3(e)
	{	
		if (document.all)
			var tecla = e.keyCode;
		else
			var tecla = e.which;
		
		if (
				(tecla > 47 && tecla < 58) ||
				(tecla >= 97 && tecla <= 122) ||
				(tecla >= 65 && tecla <= 90) || 
				 tecla == 46 || 
				 tecla == 64 ||
				 tecla == 45 || 
				 tecla == 95
		)
		{ 
				return true;			
		}
		else
		{
			if (tecla == 8 || tecla == 0)
				return true;
			else
				return false;
		}
	}
	
	function post_form(formulario)
	{
		var max = formulario.elements.length;
		var text = new String;
		for (var i=0; i<max; i++)
		{
			if (formulario.elements[i].type == "checkbox")
			{
				if (formulario.elements[i].checked)
					text += formulario.elements[i].name + "=" + escape(formulario.elements[i].value) + "&";
			}
			else
				text += formulario.elements[i].name + "=" + escape(formulario.elements[i].value) + "&";
		}
		return text;
	}
	
	// POST FORM opener
	
	function post_form_opener(formulario)
	{
		var max = formulario.elements.length;
		var text = new String;
		for (var i=0; i<max; i++)
		{
			if (formulario.elements[i].type == "checkbox")
			{
				if (formulario.elements[i].checked)
					text += formulario.elements[i].name + "=" + escape(formulario.elements[i].value) + "&";
			}
			else
				text += formulario.elements[i].name + "=" + escape(formulario.elements[i].value) + "&";
		}
		return text;
	}
	
	function post_xmlhttprequest(url, parametros, elementid)
	{
		var xmlhttp=false;
		 try 
		 {
		  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e) 
		 {
		  try 
		  {
		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		  } catch (E) 
		  {
		   xmlhttp = false;
		  }
		 }
		// Netscape
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
		{
		  xmlhttp = new XMLHttpRequest();
		}
		xmlhttp.open("POST",url,true);
		myText = parametros;
		xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // encoded formdata
		xmlhttp.setRequestHeader("Connection","close"); // Connection is to be closed after transfer
		xmlhttp.onreadystatechange=function() 
		{
		  if (xmlhttp.readyState==4) 
		  {
			document.getElementById(elementid).innerHTML = xmlhttp.responseText;
		  }
		}
		xmlhttp.send(myText);
	}
	
	function VerificaUsuario()
	{
		var xmlhttp=false;	
		try 
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (E) 
			{
				xmlhttp = false;
			}
		}
		// Netscape
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
		{
			xmlhttp = new XMLHttpRequest();
		}
		
		var tDate = new Date();
		
	
		var url="auxiliar.php?tipo=verifica_usuario&usuario=" + document.frmDados._usuario.value;
		xmlhttp.open("GET", url,true);
		 xmlhttp.onreadystatechange=function() 
		 {
		  if (xmlhttp.readyState==4) 
		  {
			//alert(xmlhttp.responseText);
			if(xmlhttp.responseText == 'indisponivel')
			{
				document.frmDados._usuario.value = '';
				alert('Nome de usuário (login) INDISPONÍVEL.\nTente mais uma vez com outro login.');	
				document.frmDados._usuario.focus();
			}
			else
			{
				alert('Login DISPONÍVEL!');
			}
		  }
		 }
		 xmlhttp.send(null);
	}	
	
	function VerificaCPF()
	{
		var xmlhttp=false;	
		try 
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (E) 
			{
				xmlhttp = false;
			}
		}
		// Netscape
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
		{
			xmlhttp = new XMLHttpRequest();
		}
		
		var tDate = new Date();
		
	
		var url="auxiliar.php?tipo=verifica_cpf&cpf=" + document.frmDados._cpf.value;
		xmlhttp.open("GET", url,true);
		 xmlhttp.onreadystatechange=function() 
		 {
		  if (xmlhttp.readyState==4) 
		  {
			if(xmlhttp.responseText == 'indisponivel')
			{
				document.frmDados._cpf.value = '';
				alert('O CPF escolhido já está cadastrado ou está incorreto.\nVerifique se você possui cadastro ou tente novamente com outro CPF.');	
				document.frmDados._cpf.focus();
			}
		  }
		 }
		 xmlhttp.send(null);
	}
	
	function VerificaLogin()
	{
		var xmlhttp=false;	
		try 
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (E) 
			{
				xmlhttp = false;
			}
		}
		// Netscape
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
		{
			xmlhttp = new XMLHttpRequest();
		}
		
		var tDate = new Date();
		
	
		var url="auxiliar.php?tipo=verifica_login&usuario=" + document.usu._usuario.value + '&senha=' + document.usu._senha.value;
		xmlhttp.open("GET", url,true);
		 xmlhttp.onreadystatechange=function() 
		 {
		  if (xmlhttp.readyState==4) 
		  {
			if(xmlhttp.responseText == 'incorreto')
			{
				document.usu._usuario.value = '';
				document.usu._senha.value = '';
				alert('As informações preenchidas estão incorretas.\nPor favor, verifique se o seu usuário e senha estão digitados corretamente e tente novamente.');	
				document.usu._usuario.focus();
			}
			else
				document.usu.submit();
		  }
		 }
		 xmlhttp.send(null);
	}
	
	function validaCPF(cpf) 
	{
		valor = true;
		erro = new String;
		var nonNumbers = /\D+/gi;
		cpf = cpf.replace(nonNumbers, '');
		
		if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
			  erro += "Numero de CPF invalido, tente novamente."
		}
		var a = [];
		var b = new Number;
		var c = 11;
		for (i=0; i<11; i++){
			a[i] = cpf.charAt(i);
			if (i < 9) b += (a[i] *  --c);
		}
		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		b = 0;
		c = 11;
		for (y=0; y<10; y++) b += (a[y] *  c--); 
		if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
			erro +="Número de CPF inválido, tente novamente.";
		}
		if (erro.length > 0){
			alert(erro);
			return false;
		}
		return true;
	}
	
	function FormataCNPJ_CPF(Formulario, Campo, TeclaPres)
	{
		var tecla = TeclaPres.keyCode;
		var strCampo;
		var vr;
		var tam;
		var TamanhoMaximo = 15;
							   
		eval("strCampo = document." + Formulario + "." + Campo);
		vr = strCampo.value;
		vr = vr.replace("/", "");
		vr = vr.replace("/", "");
		vr = vr.replace("/", "");
		vr = vr.replace(",", "");
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace("-", "");
		vr = vr.replace("-", "");
		vr = vr.replace("-", "");
		vr = vr.replace("-", "");
		vr = vr.replace("-", "");
		tam = vr.length;
	
		if (tam < TamanhoMaximo && tecla != 8){
			tam = vr.length + 1;
		}
		if (tecla == 8){
			tam = tam - 1;
		}
		if (tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105){
			if (tam <= 2){
				strCampo.value = vr;
			}
			if ((tam > 2) && (tam <= 6)){
				strCampo.value = vr.substr(0, tam - 2) + '-' + vr.substr(tam - 2, tam);
			}
			if ((tam >= 7) && (tam <= 9)){
				strCampo.value = vr.substr(0, tam - 6) + '.' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
			}
			if ((tam >= 10) && (tam <= 12)){
				strCampo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, 3) + '-' + vr.substr(9, 2);
			}
			if ((tam >= 13) && (tam <= 14)){
				strCampo.value = vr.substr(0, tam - 12) + '.' + vr.substr(tam - 12, 3) + '.' + vr.substr(tam - 9, 3) + '/' + vr.substr(tam - 6, 4) + '-' + vr.substr(tam - 2, tam);
			}
			/* if ((tam >= 15) && (tam <= 17)){
				strCampo.value = vr.substr(0, tam - 14) + '.' + vr.substr(tam - 14, 3) + '.' + vr.substr(tam - 11, 3) + '.' + vr.substr(tam - 8, 3) + '.' + vr.substr(tam - 5, 3) + '-' + vr.substr(tam - 2, tam);
			}*/
			if ((tam >= 15) && (tam <= 17)){
				strCampo.value = vr.substr(0, 3) + '.' + vr.substr(3, 3) + '.' + vr.substr(6, 3) + '/' + vr.substr(9, 4) + '-' + vr.substr(13, 2);
			}
			
		}
	}
	
	function formataFone(campo,keypress)
	{
		if (document.all) // Internet Explorer
			var tecla = keypress.keyCode;
		else
			var tecla = keypress.which;
		
		campo = eval (campo);
		
		separacao1 = "(";
		separacao2 = ")";
		separacao3 = "-";
		
		conjunto1 = 0;
		conjunto2 = 3;
		conjunto3 = 7;
		conjunto4 = 12;
		conjunto5 = 13;
		
		if (campo.value.length == conjunto1 && tecla != 8) 
			   campo.value = campo.value + separacao1;
		if (campo.value.length == conjunto2 && tecla != 8) 
			   campo.value = campo.value + separacao2;
		if (campo.value.length == conjunto3 && tecla != 8) 
			   campo.value = campo.value + separacao3;
		if (campo.value.length == conjunto4 && tecla != 8) 
			  campo.value = campo.value.substr(0,7)+campo.value.substr(8,1)+separacao3+campo.value.substr(9,3);
		if (campo.value.length == conjunto5 && tecla == 8) 
			  campo.value = campo.value.substr(0,7)+separacao3+campo.value.substr(7,1)+campo.value.substr(9,4);
	}
	
	function envia_dados_cadastro()
	{
		if(
			document.frmDados._nome.value != '' &&
			document.frmDados._email.value != '' &&
			document.frmDados._cpf.value != '' &&
			document.frmDados._usuario.value != '' &&
			document.frmDados._senha1.value != '' &&
			document.frmDados._senha2.value != '' &&
			document.frmDados._senha1.value == document.frmDados._senha2.value &&
			document.frmDados._descricao.value != '' &&
			validaCPF(document.frmDados._cpf.value)
		)
		{
			document.frmDados.action = '';
			document.frmDados._enviado.value = 1;
			document.frmDados.submit();
		}
	}