function limparBairro(form){
	form.bairros.length=0;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' deve conter um e-mail válido.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' deve conter apenas números.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' deve conter números entre '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' deve ser preenchido.\n'; }
  } if (errors) alert('Verifique as mensagens a seguir:\n'+errors);
  document.MM_returnValue = ((errors == '') && confirma());  
}

function verificanumero(){
    if ((window.event.keyCode < 48) | (window.event.keyCode > 57)){
        window.event.keyCode = 0
    }
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
                    return true; 
                }
    }else{
        return false;
        }
}

function confirma(tipo){
	

	var tamanhosenha = document.frmCadastrar.senha.value.length;	
	var achouendereco = document.getElementById("achouendereco").value;
    
	if (document.frmCadastrar.senha.value != document.frmCadastrar.confirmarsenha.value){
	    alert("As senhas não conferem!");
	    document.frmCadastrar.senha.value="";
	    document.frmCadastrar.confirmarsenha.value="";
	    document.frmCadastrar.senha.focus();
		return false;
	}
	//cadastro ou alteração
	if(document.getElementById("user_code") != undefined){
		if (document.frmCadastrar.user_code.value == ""){	
				alert('Digite o código exibido da imagem');	
				document.frmCadastrar.user_code.focus();
				return false;
		}
		if ((tamanhosenha < 5) || (tamanhosenha > 15)){
				alert('Sua senha deve ter no mímimo 5 e no máximo 15 caracteres.');
				return false;
		}		
	}	
	
	if (document.frmCadastrar.nome.value == ''){	
			alert('Por favor, digite o seu nome.');	
			return false;	
	}
	
	if (document.frmCadastrar.nascimento.value == ''){	
			alert('Por favor, digite a data de seu nascimento.');	
			return false;	
	}	
	
	if (document.frmCadastrar.cep.value == ''){	
			alert('Por favor, digite o seu cep.');	
			return false;	
	}		
		
	if (document.frmCadastrar.endereco.value == ''){	
			alert('Por favor, preencha o seu endereço .');	
			return false;	
	}		
	
	if (document.frmCadastrar.numero.value == ''){	
			alert('Por favor, preencha o número do seu endereço.');	
			return false;	
	}		
	
	if ((document.frmCadastrar.estados.options[0].selected == true) && (document.frmCadastrar.estados.options[0].value == 0)){	
			alert('Informe um estado.');	
			return false;
	}	
	
	if (document.frmCadastrar.cidades.options[0].selected == true){	
			if (achouendereco == 0){
				alert('Informe uma cidade.');	
				return false;
			}
	}	
	
	if (document.frmCadastrar.bairros.options[0].selected == true){
			if (achouendereco == 0){		
				alert('Informe um bairro.');	
				return false;
			}
	}	
	
	if (!checkMail(document.frmCadastrar.email.value)) {
			alert("Você deve digitar um e-mail válido.");
			return false;
	}		
	
	if (document.frmCadastrar.email.value == ''){	
			alert('Por favor, preencha seu e-mail.');	
			return false;	
	}	
	
	if (document.frmCadastrar.user_code.value == ''){	
			alert('Por favor, digite os caracteres que aparecem na imagem.');	
			return false;	
	}		
	
	if (document.frmCadastrar.cbaceitatermo.checked == false){	
			alert('Para prosseguir, você deve aceitar os termos de uso do site.');	
			return false;
	}		
	


}

function validalogin(){
    if ((window.event.keyCode < 48) | (window.event.keyCode > 57)){
        if ((window.event.keyCode < 97) | (window.event.keyCode > 122)){
            if(window.event.keyCode!=45 && window.event.keyCode!=46 && window.event.keyCode!=95)
                window.event.keyCode = 0;
				alert('Digite apenas letras minuscúlas e números.');
        }
    }
}

function sem_letras(){
    if ((window.event.keyCode < 48) | (window.event.keyCode > 57)){
        window.event.keyCode = 0;
    }
}

function MM_formtCep(e,src,mask) {
    if(window.event) { _TXT = e.keyCode; } 
    else if(e.which) { _TXT = e.which; }
    if(_TXT > 47 && _TXT < 58) { 
		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); } 
		    return true; } else { if (_TXT != 8) { return false; } 
		 else{
			 return true; }
    }
}

function validarData(campo) {
	if(campo.value!="__/__/____"){
	    var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	    var msgErro = 'Formato inválido de data. Preencha como dd/mm/yyyy';
	    if (!(campo.value.match(expReg)) && (campo.value!='')) {
	        alert(msgErro);
	        campo.focus();
	    } 
	}
}

function ajustar_data(input){
	if ((event.keyCode<48)||(event.keyCode>57)){
		event.returnValue = false; 
	} else { 
			if ((input.value.length==2)||(input.value.length==5))
			input.value=input.value + "/" ;
	}
}

function mostra(nome) {
	var nom = navigator.appName;
	if (nom == "Microsoft Internet Explorer"){
		document.getElementById(nome).style.left = event.clientX + 15;
		document.getElementById(nome).style.top = event.clientY + 15;
	}
	 
     document.getElementById(nome).style.visibility = 'visible';
}
function esconde(nome) {
     document.getElementById(nome).style.visibility = 'hidden';
}



function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
        xmlhttp = false;
        }
      }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
      } catch (e) {
      xmlhttp = false;
      }
    }
  return xmlhttp;
  }
var http = getHTTPObject();

function funcaowebservicecep() 
{
	http.open("GET", 'include/buscarendereco.inc.php?cep='+document.getElementById("cep").value, true);
	http.onreadystatechange = handleHttpResponse;
	http.send(null);

	var arr; //array com os dados retornados
	function handleHttpResponse() 
	{
		if (http.readyState == 4) 
		{
			var response = http.responseText;
			eval("var arr = "+response); //cria objeto com o resultado			
			document.frmCadastrar.estados.options[0].value = arr.id_estado;
			document.frmCadastrar.estados.options[0].text = arr.estado;

			document.frmCadastrar.cidades.options[0].value = arr.id_cidade;
			document.frmCadastrar.cidades.options[0].text = arr.cidade;	
		
			document.frmCadastrar.bairros.options[0].value = arr.id_bairro;
			document.frmCadastrar.bairros.options[0].text = arr.bairro;	
			
			document.getElementById("endereco").value = arr.rua;
			
			if (arr.bairro != "")
				document.getElementById("achouendereco").value = "1";
			else
				document.getElementById("achouendereco").value = "0";
		}
	}
}