/* XMLHttpRequest */

var urlImages = 'http://imgs.sapo.pt/images/directorio/form/';

function getCatsHttp(id, conta, next) {

    if (window.XMLHttpRequest) 
        obj = new XMLHttpRequest();
    else if (window.ActiveXObject)
        obj = new ActiveXObject("Microsoft.XMLHTTP");

    if(id != '' && id != 0 && id) {
        obj.open("GET", 'treeviewactions.html?pk=' + id);
    }
    else {
        obj.open("GET", 'treeviewactions.html');
    }

    obj.onreadystatechange = function() {
        if (obj.readyState == 4 && obj.status == 200) {
            var output = obj.responseText;
            var myobj = eval('(' + output + ')');

            if(!id || !conta || !next)
            {
                addParentOptions(myobj);
            }
            else
            {
                if(myobj.length > 0)
                {
                    removeChilds(conta, next);
                    addChildOptions(myobj, conta, next);
                }
                else
                {
                    removeChilds(conta, next);
                    alert('A categoria nao tem subcategorias');
                }
            }
        }
    }
    obj.send(null);
}

function addParentOptions(myobj) {

    var conta = Number(document.getElementById("contaParents").value) + 1;
        
    divGeral = document.getElementById("catsSpace");
    
    divGrupo = document.createElement('div');
    divGrupo.setAttribute("id", 'divgrupo_'+conta);
    divGeral.appendChild(divGrupo);
    
    //divGrupo.appendChild(document.createElement('hr'));

    selectPai = document.createElement('select');
    selectPai.setAttribute("name", 'child['+conta+']');
    selectPai.setAttribute("id", 'select_pai_'+conta+'_'+conta);
    selectPai.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    divGrupo.appendChild(selectPai);

    for(var i = 0; i < myobj.length; i++)
    {
        filho = document.createElement('option');
        var text = '';
        for(var c=0; c < myobj[i].option.text.length; c++)
            text += String.fromCharCode(myobj[i].option.text[c]);
        filho.appendChild(document.createTextNode(text));
        filho.setAttribute("value", myobj[i].option.value);
        selectPai.appendChild(filho);
    }

    span1 = document.createElement('span');
    span1.setAttribute("id", 'span1_'+conta+'_0');
    span1.appendChild(document.createTextNode(' '));
    divGrupo.appendChild(span1); 
    
    botao_mais = document.createElement('img');
    botao_mais.setAttribute("src", urlImages+'mais.gif');
    botao_mais.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    if (window.ActiveXObject) {
        function myFunc1() {
            getCatsHttp(document.getElementById("select_pai_"+conta+"_"+conta).value, conta, (Number(conta) + 1));
        }
        botao_mais.onclick = myFunc1;
    }
    else
    {
        botao_mais.setAttribute("onClick", 'javascript:getCatsHttp(document.getElementById("select_pai_'+conta+'_'+conta+'").value, '+conta+', '+(Number(conta) + 1)+');'); // @params: parent, nextID
    }

    divGrupo.appendChild(botao_mais);
    
    span2 = document.createElement('span');
    span2.setAttribute("id", 'span2_'+conta+'_0');
    span2.appendChild(document.createTextNode(' '));
    divGrupo.appendChild(span2); 

    botao_menos = document.createElement('img');
    botao_menos.setAttribute("src", urlImages+'menos.gif');
    botao_menos.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    if (window.ActiveXObject) 
    {
        function myFunc2() {
            document.getElementById('catsSpace').removeChild(document.getElementById('divgrupo_'+conta));
        }
        botao_menos.onclick = myFunc2;
    }
    else 
    {
        botao_menos.setAttribute("onClick", "javascript:document.getElementById('catsSpace').removeChild(document.getElementById('divgrupo_"+conta+"'))");
    }
        
    divGrupo.appendChild(botao_menos);

    br = document.createElement('br');
    br.setAttribute("id", 'br_'+conta+'_0');
    divGrupo.appendChild(br);
    
    
    document.getElementById("contaParents").value = conta;
}


function addChildOptions(myobj, conta, next) {

    var divGrupo = document.getElementById("divgrupo_"+conta);
    
    span0=document.createElement('img');
    span0.setAttribute("id", 'span0_'+conta+'_'+next);
    span0.setAttribute("src", urlImages+'seta.gif');
    span0.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    divGrupo.appendChild(span0); 

    selectPai = document.createElement('select');
    selectPai.setAttribute("name", 'child['+conta+']');
    selectPai.setAttribute("id", 'select_pai_'+conta+'_'+next);
    selectPai.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    divGrupo.appendChild(selectPai);
    
    for(var i = 0; i < myobj.length; i++)
    {
        filho = document.createElement('option');
        var text = '';
        for(var c=0; c < myobj[i].option.text.length; c++)
            text += String.fromCharCode(myobj[i].option.text[c]);
        filho.appendChild(document.createTextNode(text));
        filho.setAttribute("value", myobj[i].option.value);
        selectPai.appendChild(filho);
    }
    span1= document.createElement('span');
    span1.setAttribute("id", 'span1_'+conta+'_'+next);
    span1.appendChild(document.createTextNode(' '));
    divGrupo.appendChild(span1);
    
    botao_mais = document.createElement('img');
    botao_mais.setAttribute("src", urlImages+'mais.gif');
    botao_mais.setAttribute("id", 'botao_mais_'+conta+'_'+next);
    botao_mais.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    if (window.ActiveXObject)
    {
        function myFunc3() {
            getCatsHttp(document.getElementById("select_pai_"+conta+"_"+next).value, conta, (Number(next) + 1));
        }
        botao_mais.onclick = myFunc3;
    }
    else
    {
        botao_mais.setAttribute("onClick", "javascript:getCatsHttp(document.getElementById('select_pai_"+conta+"_"+next+"').value,"+conta+", "+(Number(next) + 1)+");"); // @params: parent, nextID
    }
    divGrupo.appendChild(botao_mais);
    
    span2= document.createElement('span');
    span2.setAttribute("id", 'span2_'+conta+'_'+next);
    span2.appendChild(document.createTextNode(' '));
    divGrupo.appendChild(span2);

    botao_menos = document.createElement('img');
    botao_menos.setAttribute("src", urlImages+'menos.gif');
    botao_menos.setAttribute("id", 'botao_menos_'+conta+'_'+next);
    botao_menos.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    if (window.ActiveXObject) 
    {
        function myFunc4() {
            removeChilds(conta, next)
        }
        botao_menos.onclick = myFunc4;
    }
    else
    {
        botao_menos.setAttribute("onClick", "javascript:removeChilds("+conta+", "+next+")");
    }

    divGrupo.appendChild(botao_menos);
    
    br = document.createElement('br');
    br.setAttribute("id", 'br_'+conta+'_'+next);
    divGrupo.appendChild(br);
    
}


function removeChilds(conta, next) {

    var divGrupo = document.getElementById('divgrupo_'+conta);

    if(next > 0 && conta > 0)
    {
        for(var i=next; i < (Number(next) + 10); i++) // delete
        {
            if(document.getElementById('select_pai_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('select_pai_'+conta+'_'+i));
        
            if(document.getElementById('span0_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('span0_'+conta+'_'+i));
        
            if(document.getElementById('span1_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('span1_'+conta+'_'+i));
        
            if(document.getElementById('span2_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('span2_'+conta+'_'+i));
        
            if(document.getElementById('botao_mais_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('botao_mais_'+conta+'_'+i));
        
            if(document.getElementById('botao_menos_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('botao_menos_'+conta+'_'+i));
        
            if(document.getElementById('br_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('br_'+conta+'_'+i));
        }
    }
}

// #################################
// #################################### Geo
// #################################
    
function getGeoHttp(id, conta, next) {

    if (window.XMLHttpRequest) 
        obj = new XMLHttpRequest();
    else if (window.ActiveXObject)
        obj = new ActiveXObject("Microsoft.XMLHTTP");

    if(id != '' && id != 0 && id) {
        obj.open("GET", 'geoviewactions.html?pk=' + id);
    }
    else {
        obj.open("GET", 'geoviewactions.html');
    }

    obj.onreadystatechange = function() {
        if (obj.readyState == 4 && obj.status == 200) {
            var output = obj.responseText;
            var myobj = eval('(' + output + ')');

            if(!id || !conta || !next)
            {
                addGeoParentOptions(myobj);
            }
            else
            {
                if(myobj.length > 0)
                {
                    removeGeoChilds(conta, next);
                    addGeoChildOptions(myobj, conta, next);
                }
                else
                {
                    removeGeoChilds(conta, next);
                    alert('A Regiao nao tem subregiões');
                }
            }
        }
    }
    obj.send(null);
}

function addGeoParentOptions(myobj) {

    var conta = Number(document.getElementById("contaGeoParents").value) + 1;
        
    divGeral = document.getElementById("geoSpace");
    
    divGrupo = document.createElement('div');
    divGrupo.setAttribute("id", 'divgeogrupo_'+conta);
    divGeral.appendChild(divGrupo);
    
    //divGrupo.appendChild(document.createElement('hr'));

    selectPai = document.createElement('select');
    selectPai.setAttribute("name", 'geochild['+conta+']');
    selectPai.setAttribute("id", 'select_geopai_'+conta+'_'+conta);
    selectPai.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    divGrupo.appendChild(selectPai);

    for(var i = 0; i < myobj.length; i++)
    {
        filho = document.createElement('option');
        var text = '';
        for(var c=0; c < myobj[i].option.text.length; c++)
            text += String.fromCharCode(myobj[i].option.text[c]);
        filho.appendChild(document.createTextNode(text));
        filho.setAttribute("value", myobj[i].option.value);
        selectPai.appendChild(filho);
    }
    
    span1 = document.createElement('span');
    span1.setAttribute("id", 'geospan1_'+conta+'_0');
    span1.appendChild(document.createTextNode(' '));
    divGrupo.appendChild(span1); 
    
    botao_mais = document.createElement('img');
    botao_mais.setAttribute("src", urlImages+'mais.gif');
    botao_mais.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    if (window.ActiveXObject) {
        function myFunc1() {
            getGeoHttp(document.getElementById("select_geopai_"+conta+"_"+conta).value, conta, (Number(conta) + 1));
        }
        botao_mais.onclick = myFunc1;
    }
    else
    {
        botao_mais.setAttribute("onClick", 'javascript:getGeoHttp(document.getElementById("select_geopai_'+conta+'_'+conta+'").value, '+conta+', '+(Number(conta) + 1)+');'); // @params: parent, nextID
    }

    divGrupo.appendChild(botao_mais);
    
    span2 = document.createElement('span');
    span2.setAttribute("id", 'geospan2_'+conta+'_0');
    span2.appendChild(document.createTextNode(' '));
    divGrupo.appendChild(span2); 

    botao_menos = document.createElement('img');
    botao_menos.setAttribute("src", urlImages+'menos.gif');
    botao_menos.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    if (window.ActiveXObject) 
    {
        function myFunc2() {
            document.getElementById('geoSpace').removeChild(document.getElementById('divgeogrupo_'+conta));
        }
        botao_menos.onclick = myFunc2;
    }
    else 
    {
        botao_menos.setAttribute("onClick", "javascript:document.getElementById('geoSpace').removeChild(document.getElementById('divgeogrupo_"+conta+"'))");
    }
        
    divGrupo.appendChild(botao_menos);

    br = document.createElement('br');
    br.setAttribute("id", 'geobr_'+conta+'_0');
    divGrupo.appendChild(br);
    
    
    document.getElementById("contaGeoParents").value = conta;
}


function addGeoChildOptions(myobj, conta, next) {

    var divGrupo = document.getElementById("divgeogrupo_"+conta);
    
    span0=document.createElement('img');
    span0.setAttribute("id", 'geospan0_'+conta+'_'+next);
    span0.setAttribute("src", urlImages+'seta.gif');
    span0.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    divGrupo.appendChild(span0); 

    selectPai = document.createElement('select');
    selectPai.setAttribute("name", 'geochild['+conta+']');
    selectPai.setAttribute("id", 'select_geopai_'+conta+'_'+next);
    selectPai.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    divGrupo.appendChild(selectPai);

    for(var i = 0; i < myobj.length; i++)
    {
        filho = document.createElement('option');
        var text = '';
        for(var c=0; c < myobj[i].option.text.length; c++)
            text += String.fromCharCode(myobj[i].option.text[c]);
        filho.appendChild(document.createTextNode(text));
        filho.setAttribute("value", myobj[i].option.value);
        selectPai.appendChild(filho);
    }
    span1= document.createElement('span');
    span1.setAttribute("id", 'geospan1_'+conta+'_'+next);
    span1.appendChild(document.createTextNode(' '));
    divGrupo.appendChild(span1);
    
    botao_mais = document.createElement('img');
    botao_mais.setAttribute("src", urlImages+'mais.gif');
    botao_mais.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    if (window.ActiveXObject)
    {
        function myFunc3() {
            getGeoHttp(document.getElementById("select_geopai_"+conta+"_"+next).value, conta, (Number(next) + 1));
        }
        botao_mais.onclick = myFunc3;
    }
    else
    {
        botao_mais.setAttribute("onClick", "javascript:getGeoHttp(document.getElementById('select_geopai_"+conta+"_"+next+"').value,"+conta+", "+(Number(next) + 1)+");"); // @params: parent, nextID
    }
    divGrupo.appendChild(botao_mais);
    
    span2= document.createElement('span');
    span2.setAttribute("id", 'geospan2_'+conta+'_'+next);
    span2.appendChild(document.createTextNode(' '));
    divGrupo.appendChild(span2);

    botao_menos = document.createElement('img');
    botao_menos.setAttribute("src", urlImages+'menos.gif');
    botao_menos.setAttribute("style", 'line-height: 21px; vertical-align: middle;');
    if (window.ActiveXObject) 
    {
        function myFunc4() {
            removeChilds(conta, next)
        }
        botao_menos.onclick = myFunc4;
    }
    else
    {
        botao_menos.setAttribute("onClick", "javascript:removeGeoChilds("+conta+", "+next+")");
    }

    divGrupo.appendChild(botao_menos);
    
    br = document.createElement('br');
    br.setAttribute("id", 'geobr_'+conta+'_'+next);
    divGrupo.appendChild(br);
}


function removeGeoChilds(conta, next) {

    var divGrupo = document.getElementById('divgeogrupo_'+conta);

    if(next > 0 && conta > 0)
    {
        for(var i=next; i < (Number(next) + 10); i++) // del
        {
            if(document.getElementById('select_geopai_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('select_geopai_'+conta+'_'+i));
        
            if(document.getElementById('geospan0_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('geospan0_'+conta+'_'+i));
        
            if(document.getElementById('geospan1_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('geospan1_'+conta+'_'+i));
        
            if(document.getElementById('geospan2_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('geospan2_'+conta+'_'+i));
        
            if(document.getElementById('botao_geomais_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('botao_geomais_'+conta+'_'+i));
        
            if(document.getElementById('botao_geomenos_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('botao_geomenos_'+conta+'_'+i));
        
            if(document.getElementById('geobr_'+conta+'_'+i))
                divGrupo.removeChild(document.getElementById('geobr_'+conta+'_'+i));
        }
    }
}




