﻿function bairros()
{
    var dlBairros = document.getElementById('ctl00_Pand_zoeken_cityarea');
    var arrBairro= new Array();
    var regions= new Array();

    var options = dlBairros.getElementsByTagName('option');
    for (var i = 0; i < options.length; i ++)
    {
        var value = options[i].value;
        var bairro = options[i].innerHTML;
        var region = options[i].getAttribute('region');
        if ((region == '' || region == null) && value != 0 && bairro != '')
        {
            region = 'Outro';
        }
        
        if (jQuery.inArray(region, regions) == - 1 && region != '')
        {
            regions.push(region);
        }
               
        bairro = bairro.split('(')[0].trim();
        bairro = bairro  + '(' + region + ')';
        if (value != 0)
        {
            if(arrBairro[bairro])
            {
               value = arrBairro[bairro] + value + "/"
            }
            else
            {
                value += "/"
            }
             arrBairro[bairro] = value;
        }
    }

    var items = document.getElementById('items');
    var sID = 0;
    //Verwijderen alle childnodes voor terug opbouwen
    while (items.hasChildNodes())
    {
        items.removeChild(items.firstChild);
    }
   
    //Opbouw verschillende regions
    var iRegion = 0;
    for(region in regions)
    {
        if (typeof regions[region] != 'function')
        {

            var divRegion = document.createElement('div');
            divRegion.id = 'divRegion' + iRegion + '';
            items.appendChild(divRegion);
            divRegion.setAttribute('class', 'divRegion');
            if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
            {   
                divRegion.setAttribute('className', 'divRegion');
            }
            
            if (regions.length > 0)
            {
                var divHeader = document.createElement('div');
                divHeader.setAttribute('class', 'divHeader');
                if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
                {   
                    divHeader.setAttribute('className', 'divHeader');
                }
                var header = document.createElement('h3');
                divHeader.appendChild(header);
                divRegion.appendChild(divHeader);
                header.setAttribute('class', 'headerBairros chkMain');
                if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
                {   
                    header.setAttribute('className', 'headerBairros chkMain');
                }
                
                //Checkbox for entire region
                var chkbox = document.createElement('input');  
                chkbox.value = "0"; 
                chkbox.id = 'chkRegion' + iRegion + '';
                chkbox.setAttribute("type", "checkbox");
                chkbox.setAttribute('class', 'chkRegion');
                if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
                {   
                    chkbox.setAttribute('className', 'chkRegion');
                }
                header.appendChild(chkbox);
                
                var span = document.createElement('span');
                span.innerHTML = regions[region];
                span.setAttribute('for', 'chkRegion' + iRegion + '');
                header.appendChild(span);
                //header.innerHTML = regions[region];
                
                var onclick;
                if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
                {
                    onclick = function onclick(){checkEntireRegion(this);};
                }
                else
                {
                    onclick = "checkEntireRegion(this);";
                }
                chkbox.setAttribute('onclick', onclick);
                
            }
            
            //Append all bairros to this region
            for(sBairro in arrBairro)
            {   
                if (typeof arrBairro[sBairro] != 'function' && sBairro.indexOf(regions[region]) != - 1)
                {
                    var div = document.createElement('div');
                    div.setAttribute('class', 'itemsDiv');
                    if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
                    {   
                        div.setAttribute('className', 'itemsDiv');
                    }
                    divRegion.appendChild(div);
                    var span = document.createElement('span');
                    span.setAttribute('class', 'chkRegion2');
                    if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
                    {   
                        span.setAttribute('className', 'chkRegion2');
                    }
                    div.appendChild(span);
                    
                    var chkbox = document.createElement('input');
                    var label = document.createElement('label');
                    chkbox.id = sID;
                    label.setAttribute("for", sID);
                    chkbox.setAttribute("type", "checkbox");
                    chkbox.value = arrBairro[sBairro];
                    
                    var bairro = sBairro.split('(')[0];
                    label.innerHTML = bairro;
                    span.appendChild(chkbox);
                    span.appendChild(label);      
                    
                    //overloop cookie om de checked te zetten van chkbox
                    var selectie = readCookie('Bairros');
                    if (selectie != '' && selectie != null)
                    {
                        if (selectie.indexOf(arrBairro[sBairro]) != -1)
                        {
                            chkbox.setAttribute('checked', true);
                        }
                    }
                    sID++;
                }
            }
            iRegion = iRegion + 1;
        }
    }
    
}

function popup()
{   
    bairros();
    $jq('#bairro_modal').dialog('open');
}

function controleerBairros()
{
    var sValues = '';
    $jq("#bairro_modal").find("input:checkbox").each(function() 
    {
        if(this.checked)
        {
            if (this.value != 0 && this.valueOf != "0")
            {
                sValues += $jq(this).val();   
            }
        }                       
    });
    createCookie('Bairros', sValues, 0.05);
}


//Load methos bairros
function loadBairros()
{
    var tdBairros = $jq('.pand_zoeken_ddl_cell')[3];
    var dlBairros = document.getElementById('ctl00_Pand_zoeken_cityarea');
    dlBairros.style.display = 'none';
    if (navigator.userAgent.indexOf('MSIE 6') != -1)
    {
        dlBairros.style.visibility = "hidden";
    }
    var img = document.createElement('img');
    img.id = 'imgBairros';
    tdBairros.appendChild(img);
    var dlCities = document.getElementById('ctl00_Pand_zoeken_city');
    var onchangeCities;
    if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
    {
        onchangeCities = function onchange(){resetImgBairro();criteriacity();setDropDownValue(this.value, 'city');setTimeout('statusBairros();', 3000);eraseCookie('Bairros');};
    }
    else
    {
        onchangeCities = dlCities.getAttribute('onchange') + "resetImgBairro();setTimeout('statusBairros();', 3000);eraseCookie('Bairros');"
    }
    dlCities.setAttribute('onchange', onchangeCities);
    statusBairros();
};

function statusBairros()
{
    var img = document.getElementById('imgBairros');
    if (document.getElementById('ctl00_Pand_zoeken_city').getElementsByTagName('option')[0].selected == true)
    {
        img.setAttribute('onclick', '');
        img.setAttribute('src', 'imgsite/bairro/' + lang + '/bairroDisabled.jpg');
    }
    else
    {
        if (document.getElementById('ctl00_Pand_zoeken_cityarea').getElementsByTagName('option').length > 2)
        {
            img.setAttribute('onclick', '');
            img.setAttribute('src', "imgsite/bairro/" + lang + "/bairroDisabled.jpg");
            if (navigator.userAgent.indexOf('MSIE 7') != -1 || navigator.userAgent.indexOf('MSIE 6') != -1)
            {
                var onclick = function onclick(){popup();};
                setTimeout("document.getElementById('imgBairros').setAttribute('src', 'imgsite/bairro/" + lang + "/bairroEnabled.jpg'); document.getElementById('imgBairros').setAttribute('onclick', " + onclick + ");", 1);
            }
            else
            {
                setTimeout("document.getElementById('imgBairros').setAttribute('src', 'imgsite/bairro/" + lang + "/bairroEnabled.jpg');document.getElementById('imgBairros').setAttribute('onclick', 'popup();');", 1);
            }
        }
        else
        {
            img.setAttribute('onclick', '');
            img.setAttribute('src', 'imgsite/bairro/' + lang + '/bairroDisabled.jpg');
        }
    } 
};

function resetImgBairro()
{
    var img = document.getElementById('imgBairros');
        img.setAttribute('onclick', '');
        img.setAttribute('src', 'imgsite/bairro/' + lang + '/bairroDisabled.jpg');
}


function checkEntireRegion(sender)
{
    $jq = jQuery.noConflict();
    var bChecked = sender.checked;   
    var id = sender.id.split("chkRegion")[1]; 
    $jq("#divRegion" + id).find("input:checkbox").each(function() 
    {
        this.checked = bChecked;
    });
}
