// <![CDATA[

// -- begin settings --------------------------------------------------------------------------
var flashtime  = 2000;       // miliseconds to keep flash color on (2000 = 2 seconds);
var reloadTime = 3000;       // reload AJAX conditions every 5 seconds (= 5000 ms)
var maxupdates = 0;	         // Maxium Number of updates allowed (set to zero for unlimited)
                             // maxupdates * reloadTime / 1000 = number of seconds to update
var clientrawFile = 'weather/ajax/ajax-wind.php'; // location of clientraw.txt relative to this page on website
var ajaxLoaderInBody = false; // set to true if you have <body onload="ajaxLoader(..."
// -- end of settings -------------------------------------------------------------------------

// -- language settings -- you don't need to customize this area if you are using English -----




// -- end of language settings ----------------------------------------------------------

// --- you don't need to customize the stuff below, the actions are controlled by the 
//  settings above.  

var ie4=document.all;
var browser = navigator.appName;
var updates = 0;		// update counter for limit by maxupdates


function set_ajax_text( name, text ) 
{
	var element = document.getElementById(name);
	if (element ) 
	{ 
		element.innerHTML = text; 
	}
}

function set_ajax_table_background( name, imgName ) 
{
	var element = document.getElementById(name);
	if (element ) 
	{ 
		element.style.background = "url(" + imgName + ")"; 
	}
}

function set_ajax_table_title( name, text ) 
{
	var element = document.getElementById(name);
	if (element ) 
	{ 
		element.title = text; 
	}
}

// --- end of flash-green functions





// ------------------------------------------------------------------------------------------
//  main function.. read clientraw.txt and format <span class="ajax" id="ajax..."></span> areas
// ------------------------------------------------------------------------------------------
function ajaxLoader(url) 
{
	if (document.getElementById) 
	{
		var x = (window.ActiveXObject) 	? new ActiveXObject("Microsoft.XMLHTTP") 
										: new XMLHttpRequest(url);
	}
	
	if (x) 
	{ 
		// got something back
		x.onreadystatechange = function() 
		{
			try 
			{ 
				if (x.readyState == 4 && x.status == 200) 
				{ 
					// Mike Challis added fix to fix random error: NS_ERROR_NOT_AVAILABLE 
					var summary_text = x.responseText;
					var clientraw = x.responseText.split(' ');
					
					// now make sure we got the entire clientraw.txt  -- thanks to Johnnywx
					// valid clientraw.txt has '12345' at start and '!!' at end of record
					var wdpattern=/[A-Z]+ [0-9]+ [A-Z][a-z]+ [0-9]+ 20[0-9][0-9] [0-9:]+/;  

					// If we have a valid clientraw file AND updates is < maxupdates
					if ( wdpattern.test(x.responseText) && 
						( updates <= maxupdates || maxupdates > 0  ) ) 
					{
						if (maxupdates > 0 ) {updates++; } // increment counter if needed
						
						
						//WIND DIRECTION ...
						set_ajax_table_background("ajaxwindicon_bsc2", "http://www.burghfieldsailing.org/mod/menuman/img/wind_" + clientraw[0] + ".gif");
						
						//Windspeed ...
						var windspeed = clientraw[1];
						
						if (windspeed == 0 )
						{
							windspeed = 0.5;
						}
						
						set_ajax_text("ajaxwind_bsc2",windspeed);
						
						var updateTime =  clientraw[2] + " " + clientraw[3] + " " + clientraw[4] + " " + clientraw[5];
						summary_text = clientraw[0] + " " + clientraw[1] + " Kts (Updated " + updateTime + " BSC Time)"; 
						set_ajax_table_title("ajaxwindtable_bsc2", summary_text );
						
						set_ajax_text("ajaxtime_bsc",clientraw[5]);
						set_ajax_text("ajaxupdate_bsc2",clientraw[7]);
						
					} // END if(clientraw[0] = '12345' and '!!' at end)
					
				} // END if (x.readyState == 4 && x.status == 200)
				
			} // END try
			
			catch(e)
			{
			}  // Mike Challis added fix to fix random error: NS_ERROR_NOT_AVAILABLE
			
		} // END x.onreadystatechange = function() {
		
		x.open("GET", url, true);
		x.send(null);
		
		//  reset the flash colors, and restart the update unless maxupdate limit is reached
		
		setTimeout("reset_ajax_color('')",flashtime); // change text back to default color 
		
		if ( (maxupdates == 0) || (updates < maxupdates-1)) 
		{
			setTimeout("ajaxLoader(clientrawFile + '?' + new Date().getTime())", reloadTime); // get new data 
		}
	}
} // end ajaxLoader function

element = document.getElementById("ajaxcounter");
if (element) {
  window.setInterval("ajax_countup()", 1000); // run the counter for seconds since update
}

// invoke when first loaded on page
if (! ajaxLoaderInBody) 
{ 
	ajaxLoader(clientrawFile + '?' + new Date().getTime(), reloadTime); 
}

// ]]>

