function httpRequest(url, callback) {
    var httpObj = false;
    if (typeof XMLHttpRequest != 'undefined') {
        httpObj = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try{
            httpObj = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            try{
                httpObj = new ActiveXObject('iMicrosoft.XMLHTTP');
            } catch(e) {}
        }
    }
    if (!httpObj) return;

    httpObj.onreadystatechange = function() {
        if (httpObj.readyState == 4) { // when request is complete
            callback(httpObj.responseText);
        }
    };
    httpObj.open('GET', url, true);
    httpObj.send(null);
}

function XMLHRObj() {
	var obj;
	if (window.XMLHttpRequest) obj= new XMLHttpRequest(); 
	else if (window.ActiveXObject){
		try{
			obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
		}
		catch(er){
			try{
				obj= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(er){
				obj= false;
			}
		}
	}
	return obj;
}

function urlExists(url) {
	var req= new XMLHRObj();
	try {
		req.open("HEAD", url, false);
		req.send(null);		
		return req.status== 200 ? true : false;
	}
	catch (er) {
		return false;
	}
}

function fillSelectUf(JSON) {
    var selectMun = document.getElementById('mun');
   	
    // clear out existing options
    while (selectMun.options.length) {
        selectMun.options[0] = null;
    }
	
    // fill with new options from JSON array
    var data = eval(JSON);
    selectMun.options[0] = new Option('Selecione o município');
    for (var i=0;i < data.length;i++) {
        selectMun.options[selectMun.options.length] = new Option(data[i][1],data[i][0]);

    }
}

function fillSelectMun(JSON) {
    var selectZe = document.getElementById('ze');
   	
    // clear out existing options
    while (selectZe.options.length) {
        selectZe.options[0] = null;
    }
	
    // fill with new options from JSON array
    var data = eval(JSON);
    selectZe.options[0] = new Option('Zona');
    for (var i=0;i < data.length;i++) {
        selectZe.options[selectZe.options.length] = new Option(data[i][0],data[i][0]);
    }
}

function fillSelectZe(JSON) {
    var selectSe = document.getElementById('se');
    var selectZe = document.getElementById('ze');
    var selectedOption = selectZe.options[selectZe.selectedIndex].value;
   	
    // clear out existing options
    while (selectSe.options.length) {
        selectSe.options[0] = null;
    }
	
    // fill with new options from JSON array
    var data = eval(JSON);
    selectSe.options[0] = new Option('Seção');
    for (var i=0;i < data.length;i++) {
    	   if (selectedOption == data[i][0]){
    	       for (var x=0;x < data[i][1].length;x++) {
                 selectSe.options[selectSe.options.length] = new Option(data[i][1][x],data[i][1][x]);
    	       }
    	   }
    }
}

function onSelectChangeTurno() {
	  var ufs1t = ['AC','AL','AM','AP','BA','CE','ES','GO','MA','MG','MS','MT','PA','PB','PE','PI','PR','RJ','RN','RO','RR','RS','SC','SE','SP','TO'];
	  var ufs2t = ['AM','AP','BA','ES','GO','MA','MG','MT','PA','PB','PR','RJ','RS','SC','SP'];
	  var selectUf = document.getElementById('uf');
    var selectTur = document.getElementById('tur');
    var selectedTur = selectTur.options[selectTur.selectedIndex].value;
    
    while (selectUf.options.length) { selectUf.options[0] = null; }
    selectUf.options[selectUf.options.length] = new Option('UF','xx');
    
    if (selectedTur == "1"){
		    for (var i=0;i < ufs1t.length;i++) {
		        selectUf.options[selectUf.options.length] = new Option(ufs1t[i],(ufs1t[i]).toLowerCase());
		    }
    }
    else{
		    for (var i=0;i < ufs2t.length;i++) {
		        selectUf.options[selectUf.options.length] = new Option(ufs2t[i],(ufs2t[i]).toLowerCase());
		    }    	  
    }   
		clear();
}

function onSelectChangeUf() {
    var selectUf = document.getElementById('uf');
    var selectTur = document.getElementById('tur');
    var selectedTur = selectTur.options[selectTur.selectedIndex].value;
    var selectedOption = selectUf.options[selectUf.selectedIndex].value;
    if (selectedOption != "") {
    	  if (selectedTur == "2"){
        	  httpRequest('../../../../js/' + selectedOption + '/' + selectedOption + '2.js', fillSelectUf);
        }
        else{
        	  httpRequest('../../../../js/' + selectedOption + '/' + selectedOption + '.js', fillSelectUf);
        }
    } else {
        fillSelect('[]'); // empty array
        clearCombo('mun');
    }    			
		clearCombo('ze');
		clearCombo('se');
}

function onSelectChangeMun() {
    var selectMun = document.getElementById('mun');
    var selectedOption = selectMun.options[selectMun.selectedIndex].value;
    var selectUf = document.getElementById('uf');
    var selectedUf = selectUf.options[selectUf.selectedIndex].value;
    if (selectedOption != "") {
        httpRequest('../../../../js/' + selectedUf + '/' + selectedOption + '.js', fillSelectMun);
    } else {
        fillSelect('[]'); // empty array
        clearCombo('ze');
    }
		clearCombo('se');
}

function onSelectChangeZe() {
    var selectZe = document.getElementById('mun');
    var selectedOption = selectZe.options[selectZe.selectedIndex].value;
    var selectUf = document.getElementById('uf');
    var selectedUf = selectUf.options[selectUf.selectedIndex].value;    
    if (selectedOption != "") {
        httpRequest('../../../../js/' + selectedUf + '/' + selectedOption + '.js', fillSelectZe);
    } else {
        fillSelect('[]'); // empty array
        clearCombo('se');
    }
}

function onSelectChangeSe() {
    var selectTur = document.getElementById('tur');
    var selectedTur = selectTur.options[selectTur.selectedIndex].value;
    var nrTur = selectTur.selectedIndex + 1;	
    var selectUf = document.getElementById('uf');
    var selectedUf = selectUf.options[selectUf.selectedIndex].value;	
    var selectMun = document.getElementById('mun');
    var nrMun = selectMun.options[selectMun.selectedIndex].value;
    var selectZe = document.getElementById('ze');
    var nrZe = selectZe.options[selectZe.selectedIndex].value;
    var selectSe = document.getElementById('se');
    var nrSe = selectSe.options[selectSe.selectedIndex].value;
	  var txtURL = '../../../../' + selectedTur + '/' + selectedUf + '/' +  nrMun + '/' +  nrZe + '/' + selectedUf + 'o' +  nrTur + nrMun + nrZe + nrSe + '.html';

    if (urlExists(txtURL)){
       parent.mensagem.location = txtURL;
    }
    else{ 
    	  alert('O Boletim de Urna da seção informada não está disponível!');
    }
}

window.onload = function() {
    var selectTurno = document.getElementById('tur');
    selectTurno.onchange = onSelectChangeTurno;	
    var selectUf = document.getElementById('uf');
    selectUf.onchange = onSelectChangeUf;
    var selectMun = document.getElementById('mun');
    selectMun.onchange = onSelectChangeMun;
    var selectZe = document.getElementById('ze');
    selectZe.onchange = onSelectChangeZe;
    var selectSe = document.getElementById('se');
    selectSe.onchange = onSelectChangeSe;    
}

function clear() {
    var selectUf = document.getElementById('uf');
    var selectMun = document.getElementById('mun');
    var selectZe = document.getElementById('ze');
    var selectSe = document.getElementById('se');
    
    while (selectSe.options.length) { selectSe.options[0] = null; }
    while (selectZe.options.length) { selectZe.options[0] = null; }
    while (selectMun.options.length) { selectMun.options[0] = null; }
    
    selectUf.selectedIndex = 0;              
}

function clearCombo(idCombo) {
    var selectCombo = document.getElementById(idCombo); 
    while (selectCombo.options.length) { selectCombo.options[0] = null; }             
}





