function somenteNumero (e) {
  var key = '';
  var strCheck = '0123456789';
  var whichCode = (window.Event) ? e.which : e.keyCode;
  if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
  key = String.fromCharCode(whichCode);
  if (strCheck.indexOf(key) == -1) return false;
  
}

function digitoucpf(valor) {
	if(valor!='') {
		document.cadastrar.cnpj.disabled = 'disabled';
		document.cadastrar.razao_s.disabled = 'disabled';
		document.cadastrar.nome_emp.disabled = 'disabled';
		document.cadastrar.tipo.value = 1;
	} else {
		document.cadastrar.cnpj.disabled = '';
		document.cadastrar.razao_s.disabled = '';
		document.cadastrar.nome_emp.disabled = '';
	}
}

function digitoucnpj(valor) {
	if(valor!='') {
		document.cadastrar.cpf.disabled = 'disabled';
		document.getElementById("nome_empresa").style.display = '';
		document.cadastrar.tipo.value = 2;
	} else {
		document.cadastrar.cpf.disabled='';
		document.getElementById("nome_empresa").style.display = 'none';
	}
}

function verificar() {
	var erro = true;
	var msg = "Os seguintes erros foram encontrados:\n";
	var dc = document.cadastrar;
	if((dc.cpf.value=='') && (dc.cnpj.value=='' || dc.razao_s.value=='' || dc.nome_emp.value=='')) {
		if(dc.cpf.value=='' && dc.tipo.value==1) {
			msg += "- Preencha seu CPF ou o CNPJ\n";
			if(erro){dc.cpf.focus();}
			erro = false;
		}
		if(dc.cnpj.value=='' && dc.tipo.value==2) {
			msg += "- Preencha o CNPJ ou seu CPF\n";
			if(erro){dc.cnpj.focus();}
			erro = false;
		}
		if(dc.razao_s.value=='' && dc.tipo.value==2) {
			msg += "- Preencha a razão social\n";
			if(erro){dc.razao_s.focus();}
			erro = false;
		}
		if(dc.nome_emp.value=='' && dc.tipo.value==2) {
			msg += "- Preencha o nome da empresa\n";
			if(erro){dc.nome_emp.focus();}
			erro = false;
		}
	}
	if(dc.nome.value=='') {
		msg += "- Preencha seu nome\n";
		if(erro){dc.nome.focus();}
		erro = false;
	}
	if(dc.email.value=='') {
		msg += "- Preencha seu e-mail\n";
		if(erro){dc.email.focus();}
		erro = false;
	} else if (dc.email.value.indexOf("@")<=1 || dc.email.value.indexOf(".")<=3) {
		msg += "- Preencha seu e-mail c\n";
		if(erro){dc.email.focus();}
		erro = false;
	}
	if(dc.endereco.value=='') {
		msg += "- Preencha seu endereço\n";
		if(erro){dc.endereco.focus();}
		erro = false;
	}
	if(dc.numero.value=='') {
		msg += "- Preencha seu número\n";
		if(erro){dc.numero.focus();}
		erro = false;
	}
	if(dc.cidade.value=='') {
		msg += "- Preencha sua cidade\n";
		if(erro){dc.cidade.focus();}
		erro = false;
	}
	if(dc.bairro.value=='') {
		msg += "- Preencha seu bairro\n";
		if(erro){dc.bairro.focus();}
		erro = false;
	}
	if(dc.telefone.value=='') {
		msg += "- Preencha seu telefone\n";
		if(erro){dc.telefone.focus();}
		erro = false;
	}
	if(dc.senha.value=='') {
		msg += "- Preencha sua senha\n";
		if(erro){dc.nome.focus();}
		erro = false;
	} else if(dc.r_senha.value=='') {
		msg += "- Preencha a repetição da senha\n";
		if(erro){dc.r_senha.focus();}
		erro = false;
	} else if(dc.senha.value!=dc.r_senha.value) {
		msg += "- A senha e a repetição da senha precisam estar identicas\n";
		if(erro){dc.senha.focus();}
		erro = false;
	}
	if(!erro){alert(msg);}else{dc.verifica.value=1625;}
	return erro;
}


var HttpReq = null;
var dest_combo = null;
function ajaxComboBox(url, comboBox){
    dest_combo = comboBox;
    var indice = document.getElementById('estados').selectedIndex;
    var sigla = document.getElementById('estados').options[indice].getAttribute('value');
    url = url + '?uf=' + sigla;
    if (document.getElementById) { //Verifica se o Browser suporta DHTML.
        if (window.XMLHttpRequest) {
            HttpReq = new XMLHttpRequest();
            HttpReq.onreadystatechange = XMLHttpRequestChange;
            HttpReq.open("GET", url, true);
            HttpReq.send(null);
        } else if (window.ActiveXObject) {
            HttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            if (HttpReq) {
                HttpReq.onreadystatechange = XMLHttpRequestChange;
                HttpReq.open("GET", url, true);
                HttpReq.send();
            }
        }
    }
}
function XMLHttpRequestChange() {
    if (HttpReq.readyState == 4 && HttpReq.status == 200){  //Verifica se o arquivo foi carregado com sucesso.
        var result = HttpReq.responseXML;
        var cidades = result.getElementsByTagName("nome");
        document.getElementById(dest_combo).innerHTML = "";
        for (var i = 0; i < cidades.length; i++) {
            new_opcao = create_opcao(cidades[i]);
            document.getElementById(dest_combo).appendChild(new_opcao);
        }
    }
}

function create_opcao(cidade) { //Cria um novo elemento OPTION.
    //return opcao.cloneNode(true);
    var new_opcao = document.createElement("option"); //Cria um OPTION.
    var texto = document.createTextNode(cidade.childNodes[0].data); //Cria um texto.
    new_opcao.setAttribute("value",cidade.getAttribute("id")); //Adiciona o atributo de valor a nova opção.
    new_opcao.appendChild(texto); //Adiciona o texto a OPTION.
    return new_opcao; // Retorna a nova OPTION.
}
