var response;
var request = false; // The object to handle the request for data
var reqType = "GET"; // Make the request type a GET as opposed to a POST
var url;
var asynch = true; // Make this an asynchronous request
var interval = 1000*60/2;    // Update the page at 30 second intervals

function getData(url2) {
    url= url2;
    httpRequest();
    setInterval(httpRequest, interval);
}

/* Wrapper function for constructing a Request object.*/
function httpRequest() {
    //
    // Only create the request object.
    //
    request = null;
    //Mozilla-based browsers
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) {
        // IE browsers
        request = new ActiveXObject("Msxml2.XMLHTTP");
        if (! request) {
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    //the request could still be null if neither ActiveXObject
    //initializations succeeded
    if (request) {
        initReq(reqType, asynch); // Initialize the request
    }
    else {
        alert("Your browser does not permit the use of all " +
              "of this application's features!");
    }

}

/* Initialize a Request object that is already constructed */
function initReq(reqType, bool) {
    /* Specify the function that will handle the HTTP response */
    request.onreadystatechange = handleResponse;
    //
    // In order to prevent IE browsers from returning a cached version of
    // the XML file we append a unique value to the end of the URL. This is
    // ignored by the responder script; but ensures the page gets updated.
    //

    request.open(reqType, url+"?date=" + (new Date()).getTime(), bool);
    request.send();
    
}


//event handler for XMLHttpRequest

function handleResponse() {
    //    alert("readyState = " + request.readyState + "status = " + request.status);
    if (request.readyState == 4) {
        if (request.status == 200) {
            //
            // Get the XML document back from the request object
            // and display the results.
            //
            var doc = request.responseXML;
            displayDocInfo(doc);
        }
//        else {
//            alert("A problem occurred with communicating between the "
//                    + "XMLHttpRequest object and the server program.");
//            alert("request.status = " + request.status);
//        }
    }//end outer if
}

//
// Loop throught the XML document using the element names to
// identify the ids of the span tags in the HTML document.
//
function displayDocInfo(doc) {
    //    alert("got answer")
    // Get the root of the XML document
    var root = doc.documentElement;

    //
    // We need to use the current time elsewhere so just get this nodes value
    //
    updateData('windSpeed', root);
    updateData('windDirr', root);
    updateData('m1', root);
    updateData('m2', root);
    updateData('m3', root);

}

function updateData(tagName, root) {
    var newValue = root.getElementsByTagName(tagName)[0].firstChild.nodeValue;
    var thisTag = document.getElementById(tagName);
    thisTag.innerHTML = newValue;

}