// -- begin settings
var flashcolor = '#00CC00'; // color to flash for changed observations
var flashtime  = '3000';    // miliseconds to keep flash color on (2000 = 2 seconds);
// -- end of settings

var ie4=document.all;
var browser = navigator.appName;
var counterSecs = 0;  // Tellercomponent
var lastajaxtime = 'unknown'; // Resetfunctie bij real time update

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';
}
}

// Teller fuctie
function ajax_countup() {
element = document.getElementById("ajaxteller");
if (element) {
element.innerHTML = counterSecs;
counterSecs++;
}
}
// --- 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("N <img src='logos/n.gif' border='0' title='Noord' style='cursor:pointer'>", "NNO <img src='logos/nne.gif' border='0' title='Noord/Noordoost' style='cursor:pointer'>", "NO <img src='logos/ne.gif' border='0' title='Noordoost' style='cursor:pointer'>", "ONO <img src='logos/ene.gif' border='0' title='Oost/Noordoost' style='cursor:pointer'>", "O <img src='logos/e.gif' border='0' title='Oost' style='cursor:pointer'>", "OZO <img src='logos/ese.gif' border='0' title='Oost/Zuidoost' style='cursor:pointer'>", "ZO <img src='logos/se.gif' border='0' title='Zuidoost' style='cursor:pointer'>", "ZZO <img src='logos/sse.gif' border='0' title='Zuid/Zuidoost' style='cursor:pointer'>", "Z <img src='logos/s.gif' border='0' title='Zuid' style='cursor:pointer'>", "ZZW <img src='logos/ssw.gif' border='0' title='Zuid/Zuidwest' style='cursor:pointer'>", "ZW <img src='logos/sw.gif' border='0' title='Zuidwest' style='cursor:pointer'>", "WZW <img src='logos/wsw.gif' border='0' title='West/Zuidwest' style='cursor:pointer'>", "W <img src='logos/w.gif' border='0' title='West' style='cursor:pointer'>", "WNW <img src='logos/wnw.gif' border='0' title='West/Noordwest' style='cursor:pointer'>", "NW <img src='logos/nw.gif' border='0' title='Noordwest' style='cursor:pointer'>", "NNW <img src='logos/nnw.gif' border='0' title='Noord/Noordwest' style='cursor:pointer'>");
$windlabel = new Array("N", "NNO", "NO", "ONO", "O", "OZO", "ZO", "ZZO", "Z", "ZZW", "ZW", "WZW", "W", "WNW", "NW", "NNW");
return $windlabel[Math.floor(((parseInt($winddir) + 11) / 22.5) % 16 )];
}

// main AJAX routine .. load and format clientraw.txt
function ajaxLoader(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) 
{

var d = new Date();
huidiguur = d.getHours();
huidigmin = d.getMinutes();

//Hier worden alle weersgegevens uitgelezen uit het bestand
//Datum

ajaxdateformat = x.responseText.split(',')[3] + '/' + x.responseText.split(',')[2] + '/' + x.responseText.split(',')[1];
set_ajax_obs("ajaxdate",ajaxdateformat);
//Tijd
ajaxtimeformat = ajax_format_time(x.responseText.split(',')[4], x.responseText.split(',')[5]);
set_ajax_obs("ajaxtijd",ajaxtimeformat);
if (lastajaxtime != ajaxtimeformat) 
{ 
counterSecs = 0; //reset tijd                   
lastajaxtime = ajaxtimeformat; // onthoud deze tijd
}

//Temp
var temp = x.responseText.split(',')[13];
set_ajax_obs("ajaxtemp",temp);
set_ajax_obs("ajaxvriezen",ajax_get_vriezen(temp));
var temprate = x.responseText.split(',')[33];
set_ajax_obs("ajaxtemprate",temprate);
set_ajax_obs("ajaxtemptendens",ajax_get_temptendens(temprate));

if (temp >= 15) {
                set_ajax_obs("ajaxheatidx", x.responseText.split(',')[30]);
                set_ajax_obs("ajaxwindchill", "---");
            }
            else 
            {
                set_ajax_obs("ajaxheatidx", "---");
                set_ajax_obs("ajaxwindchill", x.responseText.split(',')[28]);
            }

// heat index and wind-chill
//heatidx = x.responseText.split(',')[30];
//set_ajax_obs("ajaxheatidx",heatidx);
//windchill = x.responseText.split(',')[28];
//set_ajax_obs("ajaxwindchill",windchill);

//Pressure...
pressure = x.responseText.split(',')[14];
set_ajax_obs("ajaxbaro",pressure);
pressurerate = x.responseText.split(',')[35];
set_ajax_obs("ajaxbarotrend",ajax_set_barotrend(pressurerate));
set_ajax_obs("ajaxbarorate",pressurerate);

//Wind gust
gust = x.responseText.split(',')[8];
beaufortgust = ajax_get_beaufortgust(gust);
set_ajax_obs("ajaxbeaufortgust",beaufortgust);
windgevaargust = ajax_get_windgevaargust(gust);
set_ajax_obs("ajaxwindgevaargust",windgevaargust);
beaufortbanner = ajax_get_beaufortbanner(gust);
set_ajax_obs("ajaxbeaufortbanner",beaufortbanner);

//Windspeed ...
wind = x.responseText.split(',')[7];
beaufort = ajax_get_beaufort(wind);
set_ajax_obs("ajaxbeaufort",beaufort);
windgevaar = ajax_get_windgevaar(wind);
set_ajax_obs("ajaxwindgevaar",windgevaar);
wind = x.responseText.split(',')[7];
      
//WIND DIRECTION
windr = windDir(x.responseText.split(',')[9]);
set_ajax_obs("ajaxwind",wind);
set_ajax_obs("ajaxgust",gust);

if(typeof(windr)=="undefined")
{
set_ajax_obs("ajaxwinddir","---");
set_ajax_obs("ajaxwindicon","");
}
else
{
set_ajax_obs("ajaxwinddir",windr);
set_ajax_obs("ajaxwindicon","<img src=\"windpijldata2csv/"+windr+".png\" width=\"17\" height=\"17\" style=\"cursor:pointer\" alt=\""+windr+"\" title=\""+windr+"\">");
}

//Rain ...
rain = x.responseText.split(',')[16];
set_ajax_obs("ajaxrain",rain);
rainmo = x.responseText.split(',')[39];
set_ajax_obs("ajaxrainmo",rainmo);
rainyr = x.responseText.split(',')[15];
set_ajax_obs("ajaxrainyr",rainyr);
rainrate = x.responseText.split(',')[32];
set_ajax_obs("ajaxrainrate",rainrate);
set_ajax_obs("ajaxparaplu",ajax_get_paraplu(rainrate));
rainhour = x.responseText.split(',')[17];
set_ajax_obs("ajaxrainhour",rainhour);
 
//Humidity ...
humidity = x.responseText.split(',')[11];
set_ajax_obs("ajaxhumidity",humidity);
      
//Dewpoint ...
set_ajax_obs("ajaxdew",x.responseText.split(',')[31]);

// UV index actueel
set_ajax_obs("ajaxuvindex",x.responseText.split(',')[26]);
// Zonnestraling actueel
set_ajax_obs("ajaxsolar",x.responseText.split(',')[27]);

// temp op 10 cm
//aanzetten van juni t/m september
//if(huidiguur >=17 || huidiguur <9)
//{
//set_ajax_obs("ajaxtemp10cm",x.responseText.split(',')[19]);
//}
//else
//{
//set_ajax_obs("ajaxtemp10cm","---");
//}
set_ajax_obs("ajaxtemp10cm",x.responseText.split(',')[19]);

// Condens/bladnat
bladnat = x.responseText.split(',')[20];
set_ajax_obs("ajaxbladnat",ajax_get_bladnat(bladnat));

// Soil moisture
set_ajax_obs("ajaxsoil",x.responseText.split(',')[20]);

// Verdamping
set_ajax_obs("ajaxet",x.responseText.split(',')[25]);

//Online
huidiguurinsec = (huidiguur*3600);
huidigmininsec = (huidigmin*60);
huidigtijdinsec = (huidiguurinsec+huidigmininsec);

lastupdate = ajaxtimeformat;
lastupdatesplit = lastupdate.split(":");
lastupdateuur = lastupdatesplit[0];
lastupdatemin = lastupdatesplit[1];
lastupdateuurinsec = (lastupdateuur*3600);
lastupdatemininsec = (lastupdatemin*60);
lastupdatetijdinsec = (lastupdateuurinsec+lastupdatemininsec);
var online;
if ((huidigtijdinsec - lastupdatetijdinsec) >= 0 && (huidigtijdinsec - lastupdatetijdinsec) < 900)
{
online = "<img src='/img/online.png' border='0'>&nbsp;<font color=green>Online (up and running)</font>";
}
else
{
//online = "<img src='img/wissen.png' border='0'>&nbsp;<font color=#ff0000>Offline (storing)</font><br><br>Tijdelijk ziet u de meetgegevens van de vliegbasis Leeuwarden:<br><iframe src='weather/vliegbasis.php' width='490' height='110' scrolling='no' frameborder='0' marginwidth='0' marginheight='0'></iframe>";
online = "<img src='/img/offline.png' border='0'>&nbsp;<font color=#ff0000>Wegens storing of onderhoud, tijdelijk offline</font>";
}
set_ajax_obs("ajaxonline",online);
//Einde uitlezen weersgegevens

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.setRequestHeader("Cache-Control", "no-cache");
x.setRequestHeader("X_USERAGENT", "Weerstation Leeuwarden");
x.send(null);
//get all of them every minute = 10000 milliseconds
setTimeout("reset_ajax_color('')",flashtime); // change text back to default color 
setTimeout("ajaxLoader('/check_data2_csv.php?'+new Date().getTime())", 15000); // get new data after 15 secs
}
} 
// end ajaxLoader function

//Hier worden alle functies en berekeningen gedefinieerd

function ajax_format_time(hours,minutes) 
{
// convert 24hr time to 12hr time
if (minutes <= 9) minutes = "0" + minutes;
return(hours + ":" + minutes);
}

function ajax_get_beaufort ( wind ) 
{ // return a phrase for the beaufort scale based on wind MPH
  if (wind < 1 ) {return("0"); }
  if (wind < 6 ) {return("1"); }
  if (wind < 12 ) {return("2"); }
  if (wind < 20 ) {return("3"); }
  if (wind < 29 ) {return("4"); }
  if (wind < 39 ) {return("5"); }
  if (wind < 50 ) {return("6"); }
  if (wind < 62 ) {return("7"); }
  if (wind < 75 ) {return("8"); }
  if (wind < 89 ) {return("<font color=#ff0000>9</font>"); }
  if (wind < 102 ) {return("<font color=#ff0000>10</font>"); }
  if (wind < 117 ) {return("<font color=#ff0000>11</font>"); }
  if (wind > 116 ) {return("<font color=#ff0000>12</font>"); }
  return("---");
}

function ajax_get_beaufortbanner ( gust ) 
{
  if (gust < 1 ) {return("0"); }
  if (gust < 6 ) {return("1"); }
  if (gust < 12 ) {return("2"); }
  if (gust < 20 ) {return("3"); }
  if (gust < 29 ) {return("4"); }
  if (gust < 39 ) {return("5"); }
  if (gust < 50 ) {return("6"); }
  if (gust < 62 ) {return("7"); }
  if (gust < 75 ) {return("8"); }
  if (gust < 89 ) {return("<font color=#ff0000>9</font>"); }
  if (gust < 102 ) {return("<font color=#ff0000>10</font>"); }
  if (gust < 117 ) {return("<font color=#ff0000>11</font>"); }
  if (gust > 116 ) {return("<font color=#ff0000>12</font>"); }
  return("---");
}

function ajax_get_beaufortgust ( gust ) 
{ // return a phrase for the beaufort scale based on wind MPH
  if (gust < 1 ) {return("0"); }
  if (gust < 6 ) {return("1"); }
  if (gust < 12 ) {return("2"); }
  if (gust < 20 ) {return("3"); }
  if (gust < 29 ) {return("4"); }
  if (gust < 39 ) {return("5"); }
  if (gust < 50 ) {return("6"); }
  if (gust < 62 ) {return("7"); }
  if (gust < 75 ) {return("8"); }
  if (gust < 89 ) {return("<font color=#ff0000>9</font>"); }
  if (gust < 102 ) {return("<font color=#ff0000>10</font>"); }
  if (gust < 117 ) {return("<font color=#ff0000>11</font>"); }
  if (gust > 116 ) {return("<font color=#ff0000>12</font>"); }
  return("---");
}

function ajax_get_windgevaar ( wind ) 
{
  if (wind < 75 ) {return(""); }
  if (wind < 89 ) {return("<img src='logos/Warning7.gif' border='0' title='Gevaar! Stormschade mogelijk' style='cursor:pointer'>"); }
  if (wind < 102 ) {return("<img src='logos/Warning7.gif' border='0' title='Gevaar! Stormschade mogelijk' style='cursor:pointer'>"); }
  if (wind < 117 ) {return("<img src='logos/Warning7.gif' border='0' title='Gevaar! Stormschade mogelijk' style='cursor:pointer'>"); }
  if (wind > 116 ) {return("<img src='logos/Warning7.gif' border='0' title='Gevaar! Stormschade mogelijk' style='cursor:pointer'>"); }
  return("");
}

function ajax_get_windgevaargust ( gust ) 
{
  if (gust < 75 ) {return(""); }
  if (gust < 89 ) {return("<img src='logos/Warning7.gif' border='0' title='Gevaar! Stormschade mogelijk' style='cursor:pointer'>"); }
  if (gust < 102 ) {return("<img src='logos/Warning7.gif' border='0' title='Gevaar! Stormschade mogelijk' style='cursor:pointer'>"); }
  if (gust < 117 ) {return("<img src='logos/Warning7.gif' border='0' title='Gevaar! Stormschade mogelijk' style='cursor:pointer'>"); }
  if (gust > 116 ) {return("<img src='logos/Warning7.gif' border='0' title='Gevaar! Stormschade mogelijk' style='cursor:pointer'>"); }
  return("");
}

function ajax_set_barotrend(btrnd) 
{
   if ((btrnd >= -0.2) && (btrnd <= 0.2)) { return("<img src='logos/steady.gif' border='0' title='Trend: stabiel' style='cursor:pointer'>"); }
   if ((btrnd > 0.2) && (btrnd < 2)) { return("<img src='logos/rising.gif' border='0' title='Trend: stijgend' style='cursor:pointer'>"); }
   if (btrnd >= 2) { return("<img src='logos/rising.gif' border='0' title='Trend: snel stijgend' style='cursor:pointer'>"); }
   if ((btrnd < -0.2) && (btrnd > -2)) { return("<img src='logos/falling.gif' border='0' title='Trend: dalend' style='cursor:pointer'>"); }
   if (btrnd <= -2) { return("<img src='logos/falling.gif' border='0' title='Trend: snel dalend' style='cursor:pointer'>"); }
  return("");
}

function ajax_get_vriezen ( temp ) 
{ 
          if (temp < 0.0 ) {return("<img src='logos/Warning7.gif' border='0' title='Het vriest! Pas op, kans op gladheid' style='cursor:pointer'>"); }
          if (temp >= 0.0 ) {return(""); }
          return("");
}

function ajax_get_temptendens ( temprate ) 
{ 
          if (temprate < 0.0 ) {return("<img src='logos/falling.gif' border='0' title='Trend: dalend' style='cursor:pointer'>"); }
          if (temprate == 0.0 ) {return("<img src='logos/steady.gif' border='0' title='Trend: onveranderd' style='cursor:pointer'>"); }
          if (temprate > 0.0 ) {return("<img src='logos/rising.gif' border='0' title='Trend: stijgend' style='cursor:pointer'>"); }
          return("");
}

function ajax_get_bladnat ( bladnat ) 
{ 
if (bladnat == 0 ) {return("Droog " + bladnat); }
if (bladnat <= 5 ) {return("Bijna droog " + bladnat); }
if (bladnat <= 10 ) {return("Licht vochtig " + bladnat); }
if (bladnat <= 15 ) {return("Vochtig/nat " + bladnat); }
return("---");
}

function ajax_get_paraplu ( rainrate ) { 
if ((rainrate >= 0.0) && (rainrate < 0.2)){return(""); }
if (rainrate >= 0.2){return("<a href='http://www.buienradar.nl' target='_blank' title='Neerslagindicator. Klik hier voor de actuele neerslagbeelden'><img src='logos/regen.gif' border='0'></a>"); }
return("");
}

//Einde definieren alle functies

// Tel de seconden na de update
window.setInterval("ajax_countup()", 1000); // Tel de seconden sinds de laatste update
//ajaxLoader('data2.csv?'+new Date().getTime());
//-->
