function loadXMLDoc_c(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange_c;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange_c;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange_c() {
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here
            response  = req.responseXML.documentElement;
            if (response) {
                var result = new Array();
                for (i=0; i < response.getElementsByTagName('id').length; i++) {
                    result[i] = {'id': response.getElementsByTagName('id')[i].firstChild.data, 'name': response.getElementsByTagName('name')[i].firstChild.data, 'obj_name': response.getElementsByTagName('obj_name')[i].firstChild.data};
                }
                loadResult_c('',result);
            }
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function loadResult_c(url, result) { 

    if (result != '') {
        // Response mode
        for (i=0; i < result.length; i++) { 
            document.getElementById(result[i].obj_name).options[document.getElementById(result[i].obj_name).options.length] = new Option(result[i].name,result[i].id);
        }
    } else if (url != '') {
        // Input mode
        return (loadXMLDoc_c(url));
    }
}

function search_counties(host, val, form) {
        
    if (val != "") {
        document.getElementById('div_counties_results').className = "showThisOne";
        
        document.getElementById('counties_results').length = 0;
    
        url = host + '/search_counties.php?keyword=' + val;
        loadResult_c(url, '');    
        
    } else {
        document.getElementById('div_counties_results').className = "hideThisOne";
    }   
    
}

function addCounty(event, obj, max) {

    if ((event.type == "keypress" && event.keyCode=="13") || event.type == "dblclick") {
        if (obj.value != "") {
        
            var flag_id = false;
            for (i = 0; i < document.getElementById('extra_counties').options.length; i++)
                if (document.getElementById('extra_counties').options[i].value == obj.value)
                    flag_id = true;
        
            if (flag_id) {
                alert('This county was already inserted');
            } else {
                if (max) {
                    if (document.getElementById('extra_counties').options.length < max) {
                        document.getElementById('extra_counties').options[document.getElementById('extra_counties').options.length] = new Option(obj[obj.selectedIndex].text,obj.value);            
                    } else {
						alert('You cannot add more counties, please contact site manager.');
                    }
                } else {
                    document.getElementById('extra_counties').options[document.getElementById('extra_counties').options.length] = new Option(obj[obj.selectedIndex].text,obj.value);            
                }
            }
        }
    }
    
    document.getElementById('div_counties_results').className = "hideThisOne";
    
}