/**
 * @author David
 */

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 = "http://www.tullig.net/cgi-bin/tullig_conditions_responder.rb";
// var url = "http://tullig-server/cgi-bin/tullig_conditions_responder.rb";
var asynch = true; // Make this an asynchronous request
var interval = 1000 * 30;	// Update the page at 30 second intervals

function getData()
{
	//
	// Do the first request, then
	// 
	httpRequest();
	
	//
	// send the request at intervals
	//
    setInterval(httpRequest, interval);
}

/* Wrapper function for constructing a Request object.
 Parameters:
  reqType: The HTTP request type such as GET or POST.
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not. */
function httpRequest()
{
    //
	// Only create the request object.
	//
	//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,url,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,url,bool){
    /* Specify the function that will handle the HTTP response */
    request.onreadystatechange = handleResponse;
	
	var urlToSend = url + "?key=ms" + new Date().getTime();
    request.open(reqType, urlToSend, 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)
{
	// Get the root of the XML document
	var root = doc.documentElement;
    var nds;
	var thisTag;
	// If the root has children, i.e. if there's data elements
	// under the root:
    if(root.hasChildNodes())  
	{
    	nds=root.childNodes;
		// alert("Root node has " + nds.length + " children");		
		// For each of the child nodes
        for (var i = 0; i < nds.length; i++)
		{
        	// Get the corresponding span tag from the HTML document, 
			// e.g. span id="curtemp" /span
			thisTag = document.getElementById(nds[i].nodeName);
			
			// If there's some data contained in this node then
			if (thisTag)
			{
	            if(nds[i].hasChildNodes)
				{
	            	// set the tyag value to the XML node value
					thisTag.innerHTML = nds[i].firstChild.nodeValue;
	            }
				else
				{
					// Set the tag value to an empty space
					thisTag.innerHTML = "&nbsp;";
				}
			}
        }
		//
		// We need to use the current time elsewhere so just get this nodes value
		//
		var localtime = root.getElementsByTagName('curtime')[0].firstChild.nodeValue;
		thisTag = document.getElementById('localtime');
		thisTag.innerHTML = localtime;
    }
    return;
}


