// Special thanks to: Kevin Reed http://www.tnetweather.com/
// Kevin was the first to decode the clientraw in PHP
// Special thanks to: Pinto http://www.joske-online.be/
// Pinto wrote the basic AJAX code for this page!
// Cheerfully borrowed from Tom at CarterLake.org and adapted by
// Ken True - Saratoga-weather.org  21-May-2006
// --- added flash-green on data update functions - saratoga-weather.org  24-Nov-2006
// --- adapted for VWS use on data.csv by Ken True - saratoga-weather.org  3-Dec-2006
// -- begin settings
var flashcolor = '#467AA7'; // color to flash for changed observations
var flashtime  = '2000';    // miliseconds to keep flash color on (2000 = 2 seconds);
// -- end of settings

var ie4=document.all;
var browser = navigator.appName;

function get_ajax_tags ( ) {
// search all the span tags and return the list with class="ajax" in it
//
  if (ie4 && browser != "Opera") {
    var elem = document.body.getElementsByTagName('span');
	var lookfor = 'className';
  } else {
    var elem = document.getElementsByTagName('span');
	var lookfor = 'class';
  }
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute(lookfor);
          if(att == 'ajax') {
               arr[iarr] = elem[i];
               iarr++;
          }
     }

     return arr;
}

function reset_ajax_color( usecolor ) {
// reset all the <span class="ajax"...> styles to have no color override
      var elements = get_ajax_tags();
	  var numelements = elements.length;
	  for (var index=0;index!=numelements;index++) {
         var element = elements[index];
	     element.style.color=usecolor;

      }
}

function set_ajax_obs( name, value ) {
// store away the current value in both the doc and the span as lastobs="value"
// change color if value != lastobs

		var element = document.getElementById(name);
		if (! element ) { return; } // V1.04 -- don't set if missing the <span id=name> tag
		var lastobs = element.getAttribute("lastobs");
		element.setAttribute("lastobs",value);
		if (value != lastobs) {
          element.style.color=flashcolor;
		}
		element.innerHTML =  value;
}
function set_ajax_uom( name, onoroff ) {
// this function will set an ID= to visible or hidden by setting the style="display: "
// from 'inline' or 'none'

		var element = document.getElementById(name);
		if (! element ) { return; }
		if (onoroff) {
          element.style.display='inline';
		} else {
          element.style.display='none';
		}
}
// --- end of flash-green functions




function windDir ($winddir)
// Take wind direction value, return the
// text label based upon 16 point compass -- function by beeker425
//  see http://www.weather-watch.com/smf/index.php/topic,20097.0.html
{
   $windlabel = new Array("<img src='http://www.weatherbus.com/graphics/wind_direction/N.gif'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/NNE.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/NE.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/ENE.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/E.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/ESE.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/SE.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/SSE.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/S.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/SSW.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/SW.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/WSW.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/W.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/WNW.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/NW.gif' border='0' width='50' height='50'>", "<img src='http://www.weatherbus.com/graphics/wind_direction/NNW.gif' border='0' width='50' height='50'>");
   return $windlabel[Math.floor(((parseInt($winddir) + 11) / 22.5) % 16 )];
}





function temptherm ($temptherm)
// Take temp direction value, return the
// text label based upon 16 point compass -- function by beeker425
//  see http://www.weather-watch.com/smf/index.php/topic,20097.0.html
{
   $templabel = new Array("<img src='http://www.weatherbus.com/graphics/temp/10.png' border='0'>", "<img src='http://www.weatherbus.com/graphics/temp/11.png' border='0'>", "<img src='http://www.weatherbus.com/graphics/temp/12.png' border='0'>", "<img src='http://www.weatherbus.com/graphics/temp/13.png' border='0'>", "<img src='http://www.weatherbus.com/graphics/temp/14.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/15.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/17.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/17.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/18.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/19.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/20.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/21.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/22.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/23.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/24.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/25.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/26.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/27.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/28.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/29.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/30.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/31.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/32.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/33.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/34.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/35.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/36.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/37.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/38.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/39.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/40.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/41.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/42.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/43.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/44.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/45.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/46.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/47.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/48.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/49.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/50.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/51.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/52.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/53.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/54.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/55.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/56.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/57.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/58.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/59.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/60.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/61.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/62.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/63.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/64.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/65.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/66.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/67.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/68.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/69.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/70.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/71.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/72.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/73.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/74.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/75.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/76.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/77.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/78.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/79.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/80.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/81.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/82.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/83.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/84.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/85.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/86.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/87.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/88.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/89.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/90.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/91.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/92.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/93.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/94.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/95.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/96.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/97.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/98.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/99.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/100.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/101.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/102.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/103.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/104.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/105.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/106.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/107.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/108.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/109.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/110.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/111.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/112.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/113.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/114.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/115.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/116.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/117.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/118.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/119.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/120.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/121.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/122.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/123.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/124.png' border='0' width='48' height='160'>", "<img src='http://www.weatherbus.com/graphics/temp/125.png' border='0' width='48' height='160'>");
   return $templabel[Math.floor(((parseInt($temptherm) + 299) / 1) % 103 )];
}







// main AJAX routine .. load and format clientraw.txt
function ajaxLoader4(url) {
  if (document.getElementById) {
    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(url);
  }
  if (x) {
    x.onreadystatechange = function() {
      if (x.readyState == 4 && x.status == 200) {




//Thermometer Code

var temp = x.responseText.split(',')[9];
temp = temp * 1.0;
set_ajax_obs("temp",temp.toFixed(0));

//Thermometer Code

        val = temptherm(x.responseText.split(',')[9]);
// -----




	   if (temp > 0.0) {
 		set_ajax_obs("ajaxtempicon","<img src=http://www.weatherbus.com/graphics/temp/" + val + ".png width=\"12\" height=\"12\" alt=\"temp from" +
		val + "\" title=\"temp from " + val + "\" /> ");
		set_ajax_obs("ajaxtemptherm",val);
	   } else {
 		set_ajax_obs("ajaxtempicon","<img src=http://www.weatherbus.com/graphics/temp/" + val + ".png width=\"12\" height=\"12\" alt=\"temp from" +
		val + "\" title=\"temp from " + val + "\" /> ");
		set_ajax_obs("ajaxtemptherm",val);
	   }





//TEMPERATURE DATA
var temperature = x.responseText.split(',')[9];
temperature = temperature * 1.0;
set_ajax_obs("ajaxtemp",temperature.toFixed(1));

var temprate = x.responseText.split(',')[33];
set_ajax_obs("ajaxtemprate",temprate);


var temperature = x.responseText.split(',')[9];
temperature = temperature * 1.0;
set_ajax_obs("ajaxtemp",temperature.toFixed(1));


//PRESSURE DATA

var pressure = x.responseText.split(',')[10];
pressure = pressure * 1.0;
set_ajax_obs("ajaxbaro",pressure.toFixed(2));


var pressurerate = x.responseText.split(',')[38];
set_ajax_obs("ajaxpressurerate",pressurerate);







//WIND GUST DATA

var gust = x.responseText.split(',')[5];
gust = gust * 1.0;
set_ajax_obs("ajaxgust",gust.toFixed(0));

var gustrate = x.responseText.split(',')[31];
set_ajax_obs("ajaxgustrate",gustrate);

//WIND SPEED DATA

var wind = x.responseText.split(',')[4];
wind = wind * 1.0;
set_ajax_obs("wind",gust.toFixed(0));

var windrate = x.responseText.split(',')[32];
set_ajax_obs("ajaxwindrate",windrate);



//WIND DIRECTION DATA

        val = windDir(x.responseText.split(',')[3]);
// -----

       if (wind >= 0.1) {
		set_ajax_obs("ajaxwind",wind);
		set_ajax_uom("ajaxwinduom",true);
	   } else {
		set_ajax_obs("ajaxwind","0");
		set_ajax_uom("ajaxwinduom",false);
	   }

	   if (gust > 0.0) {
		set_ajax_obs("ajaxgust",gust);
		set_ajax_uom("ajaxgustuom",true);
	   } else {
		set_ajax_obs("ajaxgust","0");
		set_ajax_uom("ajaxgustuom",false);
	   }

	   if (gust > 0.0 || wind > 0.0) {
 		set_ajax_obs("ajaxwindicon","<img src=http://www.weatherbus.com/graphics/wind_direction/" + val + ".gif width=\"50\" height=\"50\" alt=\"Wind from" +
		val + "\" title=\"Wind from " + val + "\" /> ");
		set_ajax_obs("ajaxwinddir",val);
	   } else {
 		set_ajax_obs("ajaxwindicon","<img src=http://www.weatherbus.com/graphics/wind_direction/" + val + ".gif width=\"50\" height=\"50\" alt=\"Wind from" +
		val + "\" title=\"Wind from " + val + "\" /> ");
		set_ajax_obs("ajaxwinddir",val);
	   }


var winddirrate = x.responseText.split(',')[31];
set_ajax_obs("ajaxwinddirrate",winddirrate);

//RAIN DATA



rainmo = x.responseText.split(',')[39];
set_ajax_obs("ajaxrainmo",rainmo);
var rainyr = x.responseText.split(',')[11];
rainyr = rainyr * 1.0;
set_ajax_obs("ajaxrainyr",rainyr.toFixed(2));


//HUMIDITY DATA
var humidity2 = x.responseText.split(',')[7];
humidity2 = humidity2 * 1.0;
set_ajax_obs("ajaxhumidity2",humidity2.toFixed(0));


var humidityrate = x.responseText.split(',')[35];
set_ajax_obs("ajaxhumidityrate",humidityrate);



//DEW POINT DATA
var dew = x.responseText.split(',')[24];
dew = dew * 1.0;
set_ajax_obs("ajaxdew",dew.toFixed(1));

var dewrate = x.responseText.split(',')[52];
set_ajax_obs("ajaxdewrate",dewrate);



//
		element = document.getElementById("ajaxindicator");
		if (element) { // V1.04 set indicator if <span id="ajaxindicator" class="ajax"> exists
          element.style.color = flashcolor;
		}
   }
    }
    x.open("GET", url, true);
    x.send(null);

//get all of them every minute = 5000 milliseconds
//edit the location of your clienraw.txt twice!! (here and in the body onload)
	setTimeout("reset_ajax_color('')",flashtime); // change text back to default color
    setTimeout("ajaxLoader4('http://www.weatherbus.com/Data_Mesa/wflash.txt?'+new Date())", 3000); // get new data after 5 secs
  }
} // end ajaxLoader4 function




