var HttpReq = null;
var dest_combo = null;
function ajaxComboBox(url, comboBox){
    dest_combo = comboBox;
    var indice = document.getElementById('loja').selectedIndex;
    var sigla = document.getElementById('loja').options[indice].getAttribute('value');
    url = url + '?loja=' + 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.
}

function passo_q() {
	fm = document.passo4;
	msg = "Você precisa preencher os seguintes campos:\n";
	erro = true;
	if(fm.placa.value=='') {
		msg+="- Placa do veículo\n";
		if(erro){fm.placa.focus();}
		erro = false;
	}
	if(fm.modelo.value=='') {
		msg+="- Modelo do veículo\n";
		if(erro){fm.modelo.focus();}
		erro = false;
	}
	if(fm.nome.value=='') {
		msg+="- Nome completo\n";
		if(erro){fm.nome.focus();}
		erro = false;
	}
	if(fm.email.value=='') {
		msg+="- E-mail\n";
		if(erro){fm.email.focus();}
		erro = false;
	}
	if(fm.telefoneFixo.value=='') {
		msg+="- Telefone fixo\n";
		if(erro){fm.telefoneFixo.focus();}
		erro = false;
	}
	if(fm.telefoneCelular.value=='') {
		msg+="- Telefone celular\n";
		if(erro){fm.telefoneCelular.focus();}
		erro = false;
	}
	if(!erro) {alert(msg);}
	return erro;
}

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 cancelar_oficina(codigo, cadastro, hora) {
	if(confirm('Deseja realmente cancelar esse agendamento?'))
	{
		window.location = '/!caminhoes/oficina/cancelar-agendamento/'+codigo+'/'+cadastro+'/'+hora;
	}
	else
	{
		return false;
	}
}

function ver_oficina() {
	fm = document.oficina;
	msg = "Os seguintes erros foram encontrados:\n";
	erro = true;
	if(fm.cpf.value=='') {
		msg += "- Preencha seu CPF ou CNPJ\n";
		if(erro){fm.cpf.focus();}
		erro = false;
	} else {
		_tamanho = fm.cpf.value.length;
		if( (_tamanho <= 11 && !verificarCPF(fm.cpf.value)) || (_tamanho > 11 && !verificarCNPJ(fm.cpf.value))) {
			if(_tamanho <= 11) {
				msg += "- Preencha um CPF válido\n";
			} else {
				msg += "- Preencha um CNPJ válido\n";
			}
			if(erro){fm.cpf.focus();}
			erro = false;
		}
	}
	if(!erro){alert(msg);}
	return erro;
}