function getDogs(event, source, message, target) {
    currMessage = strings[message];
    var search = document.getElementById(source);
    var url = "/scripts/searchdog.php?name="+escape(search.value);

    try {
        callBackFunc = function() { return processReqChange(target); };
        loadXMLDoc(url, callBackFunc);
    }
    catch(e) {
        var msg = (typeof e == "string") ? e : ((e.message) ? e.message : "Unknown Error");
        alert("Unable to get XML data:\n" + msg);
        return;
    }
}

// handle onreadystatechange event of req object
function processReqChange(elemName) {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            clearTopicList(elemName);
            buildTopicList(elemName);
         } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
         }
    }
}

// fill Topics select list with items from
// the current XML document
function buildTopicList(elemName) {
    var select = document.getElementById(elemName);
    var rows = req.responseXML.getElementsByTagName("dogs");
    rows = rows[0].getElementsByTagName("row");
    var total = req.responseXML.getElementsByTagName("total");
    hits = getElementTextNS("", "hits", total[0], 0);
    if (hits < 100) {
        max = hits;
    } else {
        max = 100;
    }
    message = currMessage + "(" + strings[3].replace("Y", max).replace("X", hits) +")";
    appendToSelect(select, 0, document.createTextNode(message));

    for (var i = 0; i < rows.length; i++) {
        appendToSelect(select,
            getElementTextNS("", "dog_id", rows[i], 0),
            document.createTextNode(getElementTextNS("", "reg_name_id", rows[i], 0)));
    }
}

// global flag
var isIE = false;

// global request and XML document objects
var req;


// retrieve XML document (reusable generic function);
// parameter is URL string (relative or complete) to
// an .xml file whose Content-Type is a valid XML
// type, such as text/xml; XML source must be from
// same domain as HTML file
function loadXMLDoc(url, callBackFunc) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = callBackFunc;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = callBackFunc;
            req.open("GET", url, true);
            req.send();
        }
    }
}


// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;
        }
    } else {
        return "n/a";
    }
}

// empty Topics select list content
function clearTopicList(elemName) {
    var select = document.getElementById(elemName);
    while (select.length > 0) {
        select.remove(0);
    }
}

// add item to select element the less
// elegant, but compatible way.
function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value;
    opt.appendChild(content);
    select.appendChild(opt);
}
