// 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 = '#ff9302'; // 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

// main AJAX routine .. load and format clientraw.txt
function ajaxLoader3(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) {


//UPDATED TIME AND DATE
time2 = x.responseText.split(',')[276];
set_ajax_obs("ajaxtime2",time2) ;
date2 = x.responseText.split(',')[275];
set_ajax_obs("ajaxdate2",date2) ;






var rain = x.responseText.split(',')[254];
rain = rain * 1.0;
set_ajax_obs("ajaxrain",rain.toFixed(2));


var rainrate = x.responseText.split(',')[257];
rainrate = rainrate * 1.0;
set_ajax_obs("ajaxrainratehr",rainrate.toFixed(2));







//DO NOT EDIT BELOW THIS LINE

		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("ajaxLoader3('http://www.weatherbus.com/Data_Mesa/wflash2.txt?'+new Date())", 3000); // get new data after 5 secs
  }
} // end ajaxLoader3 function


function ajax_genarrow( nowTemp, yesterTemp, Legend, textUP, textDN) {
  var diff = nowTemp.toFixed(3) - yesterTemp.toFixed(3);
  var absDiff = Math.abs(diff);
  var diffStr = '' + diff.toFixed(1);  // sprintf("%01.0f",$diff);
  var absDiffStr = '' + absDiff.toFixed(1); // sprintf("%01.0f",$absDiff);
  var image = '';
  var msg = '';

  if (diff == 0) {
 // no change

    image = '&nbsp;';

  } else if (diff > 0) {
// today is greater
//    msg = textUP + " by " + diff.toFixed(1); // sprintf($textDN,$absDiff);
	msg = textUP.replace(/\%s/,absDiffStr);
    image = "<img src=\"/images/rising.gif\" alt=\"" + msg +
	"\" title=\""+ msg +
	"\" width=\"7\" height=\"8\" style=\"border: 0; margin: 1px 3px;\" />";
  } else {
// today is lesser
    msg = textDN.replace(/\%s/,absDiffStr); // sprintf($textDN,$absDiff);
//	msg = textDN.replace(/\%s/,absDiffStr);
    image = "<img src=\"/images/falling.gif\" alt=\"" + msg +
	"\" title=\""+ msg +
	"\" width=\"7\" height=\"8\" style=\"border: 0; margin: 1px 3px;\" />";

  }

   if (Legend) {
       return (diff + Legend + image);
	} else {
	   return image;
	}
} // end genarrow function

