
//Função geral de retorno de erro na requisicao
var myErrorHandler = function(statusCode, statusMsg){
    alert('Status: ' + statusCode + ', ' + statusMsg);
}


Array.prototype.findIdx = function(value){
    for (var i=0; i < this.length; i++) {
        if (this[i] == value) {
            return i;
        }
    }
}


var select;

/*var retornoGeo = function(res){
	var lengthResposta = res.DATA.length;
	select.disabled = false;
    select.options.length = 0;
    if (lengthResposta != 0) {
        select.options[0] = new Option("Selecione", 0);
        for (var i = 0; i < lengthResposta; i++) {
			nome = res.DATA[i][res.COLUMNS.findIdx('GEONOME')];
			if(res.DATA[i][res.COLUMNS.findIdx('ABREVIACAO')] && res.DATA[i][res.COLUMNS.findIdx('ABREVIACAO')].length > 0){
				nome = 	res.DATA[i][res.COLUMNS.findIdx('ABREVIACAO')] + " - " + nome
			}
            select.options[i+1] = new Option(nome,res.DATA[i][res.COLUMNS.findIdx('GEOIDPK')]);
        }
    }
    else {
        select.options[0] = new Option("Nenhum registro encontrado.", 0);
    }
}*/


function buscarGeo(alvo, nivel, texto, pai){
	var ajax = new geoAjax();
	select = document.getElementById(alvo);
	select.options.length = 0;
	if(texto.length >= 3){
		select.disabled = true;
		select.options[0] = new Option("Processando.", 0);
		ajax.setCallbackHandler(retornoGeo);	
	    ajax.setErrorHandler(myErrorHandler);
	    ajax.buscar(nivel, escape(texto), pai);	
	}else{
		select.options[0] = new Option("Digite ao menos 3 caracteres.", 0);
	}
}



function buscarFilhos(alvo,pai,texto){
	var ajax = new geoAjax();
	select = document.getElementById(alvo);
	select.options.length = 0;
	if(pai != "" && pai != 0){
		select.options[0] = new Option("Processando.", 0);
		ajax.setCallbackHandler(retornoGeo);	
	    ajax.setErrorHandler(myErrorHandler);
	    ajax.filhos(pai);	
	}else{
		select.options[0] = new Option(texto, 0);
	}	
}



var retornoGeo = function(res){
	var lengthResposta = res.DATA.length;
	select.disabled = false;
    select.options.length = 0;
    if (lengthResposta != 0) {

        select.options[0] = new Option("Selecione", 0);
		
		controleTrabalho = 0;
		contador = 1;
		
        for (var i = 0; i < lengthResposta; i++) {
			if(controleTrabalho == 0 && res.DATA[i][res.COLUMNS.findIdx('GEOTRABALHO')] == 1) {
				select.options[contador] = new Option("----------------------------------------------------------------------",0);
				controleTrabalho = 1;
				contador++;
			}
			if(controleTrabalho == 1 && res.DATA[i][res.COLUMNS.findIdx('GEOTRABALHO')] == 0) {
				select.options[contador] = new Option("----------------------------------------------------------------------",0);
				controleTrabalho = 0;
				contador++;
			}
			nome = res.DATA[i][res.COLUMNS.findIdx('GEONOME')];
			if(res.DATA[i][res.COLUMNS.findIdx('ABREVIACAO')] && res.DATA[i][res.COLUMNS.findIdx('ABREVIACAO')].length > 0){
				nome = 	res.DATA[i][res.COLUMNS.findIdx('ABREVIACAO')] + " - " + nome
			}
            select.options[contador] = new Option(nome,res.DATA[i][res.COLUMNS.findIdx('GEOIDPK')]);
			contador++;
        }
    }
    else {
        select.options[0] = new Option("Nenhum registro encontrado.", 0);
    }
}

function limparGeo(alvo,texto){
	select = document.getElementById(alvo);
	select.options.length = 0;
	select.options[0] = new Option(texto, 0);
}
