function addLoadEvent(func) { 
    var oldonload = window.onload; 
    if (typeof window.onload != 'function') { 
        window.onload = func; 
    } else { 
        window.onload = function() { 
            if (oldonload) { 
                oldonload(); 
            } 
            func(); 
        } 
    } 
} 

// Functions for the error / msg / debugmsg boxes
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function hidediv(id) {
    //safe function to hide an element with a specified id
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'none';
    }
    else {
        if (document.layers) { // Netscape 4
            document.id.display = 'none';
        }
        else { // IE 4
            document.all.id.style.display = 'none';
        }
    }
}

function showdiv(id) {
    //safe function to show an element with a specified id
    if (document.getElementById) { // DOM3 = IE5, NS6
        document.getElementById(id).style.display = 'block';
    }
    else {
        if (document.layers) { // Netscape 4
            document.id.display = 'block';
        }
        else { // IE 4
            document.all.id.style.display = 'block';
        }
    }
}
/*
function loadCountryAreas(countryID) {
    var elSel = document.getElementById('areaDropdown');    
    if(elSel && caArray[countryID] && caArray[countryID].length) {
        for (var i = elSel.length - 1; i>=0; i--) {
            elSel.remove(i);
        }
        var opt     = document.createElement('option');
        opt.text    = 'Alle';
        opt.value   = 0;
        elSel.options.add(opt);
        for(var i in caArray[countryID][0]) {
            var io       = caArray[countryID][0][i];
            var opta     = document.createElement('option');
            opta.text    = io.name;
            opta.value   = io.ID;
            elSel.options.add(opta);
            if(caArray[countryID][io.ID]) {
                for(var j in caArray[countryID][io.ID]) {
                    var io2      = caArray[countryID][io.ID][j];
                    var optb     = document.createElement('option');
                    optb.text    = '     '+io2.name;
                    optb.value   = io2.ID;
                    elSel.options.add(optb);
                    optb.setAttribute('style', 'margin-left:1em');
                }
            }
        }
    } else if(elSel) {
        for (var i = elSel.length - 1; i>=0; i--) {
            elSel.remove(i);
        }
        var opt     = document.createElement('option');
        opt.text    = 'Niet beschikbaar';
        opt.value  = 0;
        elSel.options.add(opt);
    }
}
*/

function loadCountryAreas(countryID) {
    var elSel = document.getElementById('areaDropdown');    
    if(elSel && caArray[countryID] && caArray[countryID][0] && caArray[countryID][0].length) {
        // Remove the current areas
        for (var i = elSel.length - 1; i>=0; i--) {
            elSel.remove(i);
        }
        // Create 'all' option
        var opt     = document.createElement('option');
        opt.text    = 'Alle';
        opt.value   = 0;
        elSel.options.add(opt);
        // Create area options
        for(var i in caArray[countryID][0]) {
            var io       = caArray[countryID][0][i];
            var opta     = document.createElement('option');
            opta.text    = io.name;
            opta.value   = io.ID;
            elSel.options.add(opta);
        }
        var elRow = document.getElementById('subAreaRow');
        if(elRow)
            elRow.style.display = 'none';
    } else if(elSel) {
        // Remove the current areas
        for (var i = elSel.length - 1; i>=0; i--) {
            elSel.remove(i);
        }
        // Create 'not available' option
        var opt     = document.createElement('option');
        opt.text    = 'Niet beschikbaar';
        opt.value  = 0;
        elSel.options.add(opt);
        var elRow = document.getElementById('subAreaRow');
        if(elRow) {
            elRow.style.display = 'none';
            loadCountrySubArea(countryID, 0);
        }
    }
}

function loadCountrySubArea(countryID, areaID) {
    var elSel = document.getElementById('subAreaDropdown');
    if(areaID != 0 && elSel && caArray[countryID] && caArray[countryID][areaID] && caArray[countryID][areaID].length) {
        // Remove the current areas
        for (var i = elSel.length - 1; i>=0; i--) {
            elSel.remove(i);
        }
        // Create 'all' option
        var opt     = document.createElement('option');
        opt.text    = 'Alle';
        opt.value   = 0;
        elSel.options.add(opt);
        // Create area options
        for(var i in caArray[countryID][areaID]) {
            var io       = caArray[countryID][areaID][i];
            var opta     = document.createElement('option');
            opta.text    = io.name;
            opta.value   = io.ID;
            elSel.options.add(opta);
        }
        var elRow = document.getElementById('subAreaRow');
        if(elRow)
            elRow.style.display = '';    
    } else if(elSel) {
        // Remove the current areas
        for (var i = elSel.length - 1; i>=0; i--) {
            elSel.remove(i);
        }
        // Create 'not available' option
        var opt     = document.createElement('option');
        opt.text    = 'Niet beschikbaar';
        opt.value  = 0;
        elSel.options.add(opt);
        var elRow = document.getElementById('subAreaRow');
        if(elRow)
            elRow.style.display = 'none';
    }
}
