// JavaScript Document
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
var threshold = 30; var fetchtime = 15000;
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function fetchdata() 
{
	var xmlhttp = null; var url = "req/getinfo.php?sid=all"; 
	if(window.ActiveXObject) {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");} else if(window.XMLHttpRequest) {xmlhttp = new XMLHttpRequest();}else{}			
	if(xmlhttp != null)
	{
		try
		{
			xmlhttp.open('GET', url, true);
			xmlhttp.onreadystatechange = function()
			{
				if(xmlhttp.readyState == 4)
				{					
					var xml = xmlhttp.responseXML; var info = xml.documentElement.getElementsByTagName("station");
					if(xml)
					{
						for (var i = 0; i < info.length; i++) 
						{
							var usid = info[i].getAttribute("usid"); var dsid = info[i].getAttribute("dsid"); var usstatus= info[i].getAttribute("usstatus"); var dsstatus= info[i].getAttribute("dsstatus"); 
							var rainid= info[i].getAttribute("rainid"); var rstatus= info[i].getAttribute("rstatus"); var stationid = info[i].getAttribute("id");
							var usdata = info[i].getAttribute("usdata"); var dsdata = info[i].getAttribute("dsdata"); var zusdata = info[i].getAttribute("zusdata"); var zdsdata = info[i].getAttribute("zdsdata"); 
							var raindt = ""; var wlvdt = ""; var mzusdata = info[i].getAttribute("mzusdata"); var mzdsdata = info[i].getAttribute("mzdsdata"); var rdata = info[i].getAttribute("rdata");						
							var usdate = info[i].getAttribute("usdate"); var ustime = info[i].getAttribute("ustime"); var dsdate = info[i].getAttribute("dsdate"); var dstime = info[i].getAttribute("dstime");
							var rdate = info[i].getAttribute("rdate"); var rtime = info[i].getAttribute("rtime");  var gdata = info[i].getAttribute("gdata"); gdata = gdata.replace(/,/gi,"|"); var tgdata = gdata.split("|");
							var r1ddata = info[i].getAttribute("r1ddata"); var intrdata = info[i].getAttribute("intrdata"); var qdata = info[i].getAttribute("qdata");
							if(usdate != "") {wlvdt = usdate + " " + ustime;}else {if(dsdate != "") {wlvdt = dsdate + " " + dstime;}} if(rdate != "") {raindt = rdate + " " + rtime;}
							if(tgdata == "") {tgdata[0] = "&nbsp;"; tgdata[1] = "&nbsp;"; tgdata[2] = "&nbsp;"; tgdata[3] = "&nbsp;"; tgdata[4] = "&nbsp;";}
							
							if(rainid != "")
							{
								if((usstatus > threshold) && (dsstatus > threshold) && (rstatus > threshold)) 
								{
									// red status
									var oObj = document.getElementById("img_" + stationid); if(oObj) {oObj.src= "images/red.png";}
									var oObj = document.getElementById("img_status_" + stationid); if(oObj) {oObj.src= "images/offline.gif";}
								}else if((usstatus > threshold) || (dsstatus > threshold) || (rstatus > threshold)) 
								{
									// yellow status
									var oObj = document.getElementById("img_" + stationid); if(oObj) {oObj.src= "images/yellow.png";}
									var oObj = document.getElementById("img_status_" + stationid); if(oObj) {oObj.src= "images/warning.gif";}
								}else
								{
									// green status
									var oObj = document.getElementById("img_" + stationid); if(oObj) {oObj.src= "images/green.png";}
									var oObj = document.getElementById("img_status_" + stationid); if(oObj) {oObj.src= "images/online.gif";}
								}
								if(usdata == "") {zusdata = "-9999";} if(dsdata == "") {zdsdata = "-9999";} if(rdata == "") {rdata = "-9999";}
								var tag = "rain_15m_" + stationid; var tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=rdata;}
								var tag = "rain_1d_" + stationid; var tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=r1ddata;}
								var tag = "rain_int_" + stationid; var tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=intrdata;}
								
								tag = "wlv_us_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=zusdata;} 
								tag = "wlv_ds_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=zdsdata;}
								tag = "wlv_mus_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=mzusdata;} 
								tag = "wlv_mds_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=mzdsdata;}
								
								tag = "rain_dt_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=raindt;}
								tag = "wlv_dt_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=wlvdt;}
								
								tag = "q_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=qdata;}
								
								tag = "gif_us_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=zusdata;}
								tag = "gif_ds_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=zdsdata;}
								
								tag = "gif_g1_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[0];}								
								tag = "gif_g2_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[1];}								
								tag = "gif_g3_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[2];}								
								tag = "gif_g4_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[3];}								
								tag = "gif_g5_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[4];}								
								
								tag = "xcross_" + stationid + "_1"; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=zusdata + "&nbsp; m.(msl)";}								
								tag = "xcross_" + stationid + "_2"; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=zdsdata + "&nbsp; m.(msl)";}								
							}else
							{
								if((usstatus > threshold) && (dsstatus > threshold)) 
								{
									// red status
									var oObj = document.getElementById("img_" + stationid); if(oObj) {oObj.src= "images/red.png";}
									var oObj = document.getElementById("img_status_" + stationid); if(oObj) {oObj.src= "images/offline.gif";}
								}else if((usstatus > threshold) || (dsstatus > threshold)) 
								{
									// yellow status
									var oObj = document.getElementById("img_" + stationid); if(oObj) {oObj.src= "images/yellow.png";}
									var oObj = document.getElementById("img_status_" + stationid); if(oObj) {oObj.src= "images/warning.gif";}
								}else
								{
									// green status
									var oObj = document.getElementById("img_" + stationid); if(oObj) {oObj.src= "images/green.png";}
									var oObj = document.getElementById("img_status_" + stationid); if(oObj) {oObj.src= "images/online.gif";}
								}
								if(usdata == "") {zusdata = "-9999";} if(dsdata == "") {zdsdata = "-9999";} if(rdata == "") {rdata = "-9999";}
								var tag = "wlv_us_" + stationid; var tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=zusdata;} 
								tag = "wlv_ds_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=zdsdata;}
								tag = "wlv_mus_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=mzusdata;} 
								tag = "wlv_mds_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=mzdsdata;}
								
								tag = "rain_dt_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML="";}
								tag = "wlv_dt_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=wlvdt;}

								tag = "q_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.value=qdata;}
								
								tag = "gif_us_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=zusdata;}
								tag = "gif_ds_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=zdsdata;} 
								
								tag = "gif_g1_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[0];}								
								tag = "gif_g2_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[1];}								
								tag = "gif_g3_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[2];}								
								tag = "gif_g4_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[3];}								
								tag = "gif_g5_" + stationid; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=tgdata[4];}								
								
								tag = "xcross_" + stationid + "_1"; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=zusdata + "&nbsp; m.(msl)";}								
								tag = "xcross_" + stationid + "_2"; tagObj = document.getElementById(tag); if(tagObj) {tagObj.innerHTML=zdsdata + "&nbsp; m.(msl)";}								
							}
						}
					}
				}
			}
			xmlhttp.send(null);
		}catch(e) {}
	}
	setTimeout("fetchdata()",fetchtime);
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function sprintf()
{
	if (!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
				   if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			  else if (pPad) pad = pPad;
			var justifyRight = true;
				   if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
				   if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
				   if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
				   if (pType == 'b') subst = parseInt(param).toString(2);
			  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			  else if (pType == 'u') subst = Math.abs(param);
			  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			  else if (pType == 'o') subst = parseInt(param).toString(8);
			  else if (pType == 's') subst = param;
			  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------------

