/**
 * Returns the position on the screen of the element with the given id.
 * The returned value is an array with the first element the x coordinate
 * and the second element the y coordinate.
 */
function getPosition(elementID) {
    var x = 0;
    var y = 0;
    var element = document.getElementById(elementID);
    if (element != null) {
        if (element.offsetParent) {
            x = element.offsetLeft;
            y = element.offsetTop;
            while (element = element.offsetParent) {
                x += element.offsetLeft;
                y += element.offsetTop;
            }
        } else if (element.x && element.y) {
            x = element.x;
            y = element.y;
        }
    }
    return [x, y];
}

/**
 * Returns the XMLHttpRequest for the browser to perform AJAX related
 * operations.
 */
function getXMLHttpRequest() {
    var httpRequest = null;

    if (window.XMLHttpRequest) {
        // Mozilla, Safari, etc.
        httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        // IE.
        httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        // Not supported.
        alert("Your browser doesn't support AJAX!");
    }

    return httpRequest;
}

/**
 * Using AJAX makes a call to the specified url and when the response
 * is returned sets the html content of the layer identified by the 
 * layer id to the reponse's content.
 */
function ajaxObject(layer, url) {
    var that = this;
    var updating = false;
    this.callback = function() {}

    this.update = function(passData) {
        if (updating == true) {
            return false;
        }

        updating = true;
        var AJAX = null;
        if (window.XMLHttpRequest) {
            AJAX = new XMLHttpRequest();
        } else {
            AJAX = new ActiveXObject("Microsoft.XMLHTTP");
        }

        if (AJAX == null) {
            alert("Your browser doesn't support AJAX.");
            return false;
        } else {
            AJAX.onreadystatechange = function() {
                if (AJAX.readyState == 4 || AJAX.readyState == "complete") {
                    setHTML(LayerID, AJAX.responseText);
                    delete AJAX;
                    updating = false;
                    that.callback();
                }
            }

            var uri = urlCall;
            AJAX.open("GET", uri, true);
            AJAX.send(null);
            return true;
        }
    }

    // This area set up on constructor calls.
    var LayerID = document.getElementById(layer);
    var urlCall = url;
}

/**
 * Given a block of HTML content this function will find all the embedded
 * JavaScript SCRIPT tags and append them all to the page's head.
 *
 * This method was copied from 
 * http://www.modernmethod.com/sajax/forum/viewtopic.php?t=847
 */
function setHTML(div, content) {
    var search = content;
    var script;

    while (script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i)) {
        search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length);

        if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) {
            break;
        }

        block = search.substr(0, search.indexOf(RegExp.$1));
        search = search.substring(block.length + RegExp.$1.length);

        var oScript = document.createElement('script');
        oScript.text = block;
        document.getElementsByTagName("head").item(0).appendChild(oScript);
    }

    div.innerHTML = content;
}
