function createXMLHTTP() 
	{
		var ajax;
		try 
		{
			ajax = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
				ajax = new ActiveXObject("Msxml2.XMLHTTP");
				alert(ajax);
			}
			catch(ex) 
			{
				try 
				{
					ajax = new XMLHttpRequest();
				}
				catch(exc) 
				{
					 alert("Esse browser não tem recursos para uso do Ajax");
					 ajax = null;
				}
			}
			return ajax;
		}
	
	
		   var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
							    "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
							    "Microsoft.XMLHTTP"];
		   for (var i=0; i < arrSignatures.length; i++) 
		   {
				try 
				{
					var oRequest = new ActiveXObject(arrSignatures[i]);
					return oRequest;
				} 
				catch (oError) 
				{
			    }
		   }
		
			   throw new Error("MSXML is not installed on your system.");
	}

   
   function dadosCidade(valor) {
      //verifica se o browser tem suporte a ajax
         ajax = new createXMLHTTP();
     
	  //se tiver suporte ajax
	  if(ajax) {
	     //deixa apenas o elemento 1 no option, os outros são excluídos
		 document.form2.cidade.options.length = 1;
	     
		 idOpcao  = document.getElementById("opcoesCidade");
		 
	     ajax.open("POST", "js/cidade.php", true);
		 ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		 
		 ajax.onreadystatechange = function() {
            //enquanto estiver processando...emite a msg de carregando
			if(ajax.readyState == 1) {
			   idOpcao.innerHTML = "Carregando...!";   
	        }
			//após ser processado - chama função processXML que vai varrer os dados
            if(ajax.readyState == 4 ) {
			   if(ajax.responseXML) {
			      processXMLCidade(ajax.responseXML);
			   }
			   else {
			       //caso não seja um arquivo XML emite a mensagem abaixo
				   idOpcao.innerHTML = "--Primeiro selecione um UF--";
			   }
            }
         }
		 //passa o código do estado escolhido
	     var params = "uf="+valor;
         ajax.send(params);
      }
   }
   
   function processXMLCidade(obj){
      //pega a tag cidade
      var dataArray   = obj.getElementsByTagName("cidade");
	  //total de elementos contidos na tag cidade
	  if(dataArray.length > 0) {
	     //percorre o arquivo XML paara extrair os dados
         for(var i = 0 ; i < dataArray.length ; i++) {
            var item = dataArray[i];
			//contéudo dos campos no arquivo XML
			var codigo    =  item.getElementsByTagName("codigo")[0].firstChild.nodeValue;
			var descricao =  item.getElementsByTagName("descricao")[0].firstChild.nodeValue;
			
	        idOpcao.innerHTML = "Selecione uma das opções";
			
			//cria um novo option dinamicamente  
			var novo = document.createElement("option");
			    //atribui um ID a esse elemento
			    novo.setAttribute("id", "opcoes");
				//atribui um valor
			    novo.value = descricao;
				//atribui um texto
			    novo.text  = descricao;
				//finalmente adiciona o novo elemento
				document.form2.cidade.options.add(novo);
		 }
	  }
	  else {
	    //caso o XML volte vazio, printa a mensagem abaixo
		idOpcao.innerHTML = "--Primeiro selecione um UF--";
	  }	  
   }
// JavaScript Document
function validaCPF(cpf) 
 {
   erro = new String;
 
 	if (cpf.value.length == 11)
 	{	
 			cpf.value = cpf.value.replace('.', '');
 			cpf.value = cpf.value.replace('.', '');
 			cpf.value = cpf.value.replace('-', '');
 
 			var nonNumbers = /\D/;
 	
 			if (nonNumbers.test(cpf.value)) 
 			{
 					erro = "A verificacao de CPF suporta apenas números!"; 
 			}
 			else
 			{
 					if (cpf.value == "00000000000" || 
 							cpf.value == "11111111111" || 
 							cpf.value == "22222222222" || 
 							cpf.value == "33333333333" || 
 							cpf.value == "44444444444" || 
 							cpf.value == "55555555555" || 
 							cpf.value == "66666666666" || 
 							cpf.value == "77777777777" || 
 							cpf.value == "88888888888" || 
 							cpf.value == "99999999999") {
 							
 							erro = "Número de CPF inválido!"
 					}
 	
 					var a = [];
 					var b = new Number;
 					var c = 11;
 
 					for (i=0; i<11; i++){
 							a[i] = cpf.value.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.value.charAt(9) != a[9]) || (cpf.value.charAt(10) != a[10])) {
 						erro = "Número de CPF inválido.";
 					}
 			}
 	}
 	else
 	{
 		if(cpf.value.length == 0)
 			return false
 		else
 			erro = "Número de CPF inválido.";
 	}
 	if (erro.length > 0) {
 			alert(erro);
 			cpf.focus();
 			return false;
 	} 	
 	return true;	
 }
 
 //envento onkeyup
 function maskCPF(CPF) {
 	var evt = window.event;
 	kcode=evt.keyCode;
 	if (kcode == 8) return;
 	if (CPF.value.length == 3) { CPF.value = CPF.value + '.'; }
 	if (CPF.value.length == 7) { CPF.value = CPF.value + '.'; }
 	if (CPF.value.length == 11) { CPF.value = CPF.value + '-'; }
 }
 
 // evento onBlur
 function formataCPF(CPF)
 {
 	with (CPF)
 	{
 		value = value.substr(0, 3) + '.' + 
 				value.substr(3, 3) + '.' + 
 				value.substr(6, 3) + '-' +
 				value.substr(9, 2);
 	}
 }
 function retiraFormatacao(CPF)
 {
 	with (CPF)
 	{
 		value = value.replace (".","");
 		value = value.replace (".","");
 		value = value.replace ("-","");
 		value = value.replace ("/","");
 	}
 }
 
 
 // comparação de E-mails
 function comparaemail(){
	 
	var emailent=document.form2.email.value;
	var confemailent=document.form2.confemail.value;
		
	if(emailent != confemailent){
		alert('Os E-mails digitados não conferem!');
		document.form2.confemail.value="";
		document.form2.email.focus();
	}
 }
 
 // validação de e-mail	 
function checaremail(email){
if (email.value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) {
alert("Seu e-mail não é Válido");
email.focus();
email.value="";
return false;
}
return true;
}

 // comparação de Senhas
 function comparasenha(){
	 
	var senhaum=document.form2.senha.value;
	var senhadois=document.form2.resenha.value;
		
	if(senhaum != senhadois){
		alert('As Senhas digitadas não conferem!');
		document.form2.resenha.value="";
		document.form2.senha.value="";
		document.form2.senha.focus();
	}
 }
 // função de envio
 function verificanascimento(){
	 var ano=document.form2.ano.value;
	 var mes=document.form2.mes.value;
	 var dia=document.form2.dia.value;
	 Hr = new Date(); 
	 dd = Hr.getDate(); 
	 mm = Hr.getMonth() + 1; 
	 aa = Hr.getYear();
	 if((aa - ano)<19){
		 if((aa - ano)==18){
				if((mm - mes)<0){
					alert('Você precisa ter mais de 18 anos! Caso tenha Verifique se a data do sistema está correta.mes');
					document.form1.dia.focus();
		 			return false;		
				}else if((mm - mes)==0){
					if((dd - dia)<0){
						alert('Você precisa ter mais de 18 anos! Caso tenha Verifique se a data do sistema está correta.dia');
						document.form1.dia.focus();
		 				return false;
					}
				}
		}else{
		 		alert('Você precisa ter mais de 18 anos! Caso tenha Verifique se a data do sistema está correta.ano');
		 		document.form1.dia.focus();
		 		return false;
		}
	 }else{return true;}
 }
 function enviar(){
	 
	 var senhaum=document.form2.senha.value;
	 var senhadois=document.form2.resenha.value;
	 var emalconf=document.form2.confemail.value;
	 var emailum=document.form2.email.value;
	 var nome=document.form2.nome.value;
	 var cpf=document.form2.cpf.value;
	 
	 if(verificanascimento() == false){
		return;
	}else if(senhaum==""){
		 alert('Preencha o campo senha!');
		 return;
	 } else if(senhadois==""){
		 alert('Preencha o campo Repita a Senha!');
		 return;
	 }else if(emalconf==""){
		 alert('Preencha o campo Confirme o E-mai!');
		 return;
	 } else if(emailum==""){
		 alert('Preencha o campo E-mail!');
		 return;
	 } else if(nome==""){
		 alert('Preencha o campo Nome!');
		 return;
	 }else if(cpf==""){
		 alert('Preencha o campo CPF!');
		 return;
	 }else{
		 document.form2.submit();
	 }
 }
 
  function atualizar(){
	 
	 var emalconf=document.form2.confemail.value;
	 var emailum=document.form2.email.value;
	 var nome=document.form2.nome.value;
	 var cpf=document.form2.cpf.value;
	 
	 if(verificanascimento() == false){
		return;
	}else if(emalconf==""){
		 alert('Preencha o campo Confirme o E-mai!');
		 return;
	 } else if(emailum==""){
		 alert('Preencha o campo E-mail!');
		 return;
	 } else if(nome==""){
		 alert('Preencha o campo Nome!');
		 return;
	 }else if(cpf==""){
		 alert('Preencha o campo CPF!');
		 return;
	 }else{
		 document.form2.submit();
	 }
 }

 // formata campo mask
 function formatar(src, mask) {
var i = src.value.length;
var saida = mask.substring(0,1);
var texto = mask.substring(i)
if(texto.substring(0,1) != saida) {
src.value += texto.substring(0,1);
}
}
