//var response;
var request = null;            // 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.lankheet.eu/Bee/Responder.php";
//var url = "http://www.lankheet.eu/weather/codes/Responder.php";
//var url = "http://www.weerstationhaaksbergen.nl/weather/codes/Responder.php";
  var url = "codes/Responder.php";
var asynch = true;              // Make this an asynchronous request
var interval = 1000 * 30;       // Update the page at 30 second intervals
var Mozilla = false;

function getData()
{   

    //    
    // Do the first request, then    
    //     
    httpRequest();        
    
    //    
    // send the request at intervals    
    //    
    setInterval(httpRequest, interval);
}

// Wrapper function for constructing a Request object
function httpRequest()
{    
    //    
    // Only create the request object.    
    //    
    //Mozilla-based browsers    
    if(window.XMLHttpRequest)
    {        
		Mozilla = true;
        request = new XMLHttpRequest();    
    }     
    else if (window.ActiveXObject)    
    {        
		Mozilla = false;
		
        // 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;        
    
    //    
    // 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.    
    //    
    var urlToSend = url + "?key=ms" + new Date().getTime();    
    request.open(reqType, urlToSend, bool);      

	if ( Mozilla )
	{
		var doc = request.responseXML;         
		request.send(doc);
	}
	else
	{
		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;         
			if ( doc ) {
				displayDocInfo(doc);        
			}
        }         
        else         
        {           
//            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program. request.status = " + request.status);        
//            alert("Sitenaam dient volledig te zijn en vooraf te gaan door www. dus: www.weerstationhaaksbergen.nl");        
        }    
    }
    
    //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 = null;    
    var thisTag = null;  
    var localtime;  
	var Temperature = null;
    
    // If the root has children, i.e. if there's data elements    
    // under the root:    
	if ( 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                    
						if (nds[i].nodeName == "OutTemp" || 
							nds[i].nodeName == "Ch2Temp" || 
							nds[i].nodeName == "WindChill" ||
							nds[i].nodeName == "DewPoint" ||
							nds[i].nodeName == "OutsideHeatIndex" ||
							nds[i].nodeName == "Average10YearOutTemp" )
						{
							Temperature = parseFloat(nds[i].firstChild.nodeValue);

							if ( Temperature <= -10 )
							{
								thisTag.innerHTML = "<span style='color: #0000ff'>"+nds[i].firstChild.nodeValue;  // Blue
							}
							else if ( Temperature > -10  && Temperature < 0 )
							{
								thisTag.innerHTML = "<span style='color: #0895d6'>"+nds[i].firstChild.nodeValue;  // Light blue
							}
							else if ( Temperature >= 0 && Temperature < 20 )
							{
								if (nds[i].nodeName == "Average10YearOutTemp" )
								{
									thisTag.innerHTML = "<span style='color: #888888'>"+nds[i].firstChild.nodeValue;  // Gray
								}
								else
								{
									thisTag.innerHTML = "<span style='color: #000000'>"+nds[i].firstChild.nodeValue;  // Black
								}
							}
							else if ( Temperature >= 20 && Temperature < 30 )
							{
								thisTag.innerHTML = "<span style='color: #ff8001'>"+nds[i].firstChild.nodeValue;  // Orange
							}
							else
							{
								thisTag.innerHTML = "<span style='color: #ff0000'>"+nds[i].firstChild.nodeValue;  // Red
							}
						}
						else
						{
							thisTag.innerHTML = nds[i].firstChild.nodeValue;                
						}
					}                
					else                
					{                    
						// Set the tag value to an empty space                    
						thisTag.innerHTML = " ";                
					}            
				}        
			}        
	
			//        
			// We need to use the current time elsewhere so just get this nodes value        
			//  
			localtime = root.getElementsByTagName('StationTime')[0].firstChild.nodeValue;
			thisTag = document.getElementById('localtime');        
//        thisTag.innerHTML = localtime;    
		}
	}
 }
