function windDir ($winddir){$windlabel = new Array("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW");  
 return $windlabel[Math.floor(((parseInt($winddir) + 11) / 22.5) % 16 )];
}

function set_ajax_obsL( name, value ) {               
		var element = document.getElementById(name);
		if (! element ) { return; }   
		var lastobs = element.getAttribute("lastobs");
		element.setAttribute("lastobs",value);
		if (unescape(value) != unescape(lastobs)) {
		  element.innerHTML =  value;
		}
}

var updates = 0;   
var reloadTime = 30000;       // reload AJAX conditions every 5 seconds (= 5000 ms)
var maxupdates = 30;	         // Maxium Number of updates allowed (set to zero for unlimited)
var updates = 0;	         // Maxium Number of updates allowed (set to zero for unlimited)
var nextmin = 0;

bgcolor = new Array("#61C770", "#CED156", "#D2A055", "#C45539");

function TRACLoader(url) {  
  if (document.getElementById) {
    var w = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(url);
  }
	if (w) { // got something back
    w.onreadystatechange = function() {
    try { if (w.readyState == 4 && w.status == 200) { 
		
//alert(w.responseText);
		
	check = w.responseText.split(' ')[2] + w.responseText.split(' ')[3];
	check = check.replace(/^\s*|\s*$/g,'');
	if(check == 'Nothunderstorms') {
	set_ajax_obsL("STORMS", "<font color=\"#00FF00\">" + txtNost + "</font>");
	set_ajax_obsL("NEARBY", "<font color=\"#00FF00\">" + txtInac + "</font>");
	set_ajax_obsL("STORM", "");

	} else {

		storms = w.responseText.split('\n')[2];
		value = storms.split(' ')[1]*1;
		set_ajax_obsL("STORMS", "<font color=\"#EE2C2C\">" + txtTrac + " " + value + " " + txtStor + "</font>");

		// Reading the storms
		var stormlist=new Array();
		var p = 1;
		for (var i = 1; i <= value; i++) {
		stormlist[i-1] = w.responseText.split('-------------------------------------------------------')[p];
		p++;
		p++;
		}

		// Process the each storm

		var o = 0;
		var se = '';
		var table=new Array();
		var nearby = 'off';

		var trends = new Array("No", "Weakening", "Intensifying", "Undetermined");
		var intensitys = Array("Weak", "Moderate", "Strong", "Severe");

		table+= se + '<table width="815" border= "0" cellspacing="0" cellpadding="0" style="padding-bottom:0px; border: 1px solid #555; background: #000;"><tr style="background-color:#333; height: 18px;"><td width="63"><div align="center" class="nexstyle11">ID</div></td><td width="63"><div align="center" class="nexstyle11">' + txtLast + '</div></td><td width="63"><div align="center" class="nexstyle11">' + txtBear + '</div></td><td width="63"><div align="right" class="nexstyle11"style="padding-right:5px;">' + txtDist + '</div></td><td width="92"><div align="center"class="nexstyle11">' + txtInte + '</div></td><td width="93"><div align="center" class="nexstyle11">' + txtTren+ '</div></td><td width="63"><div align="right" class="nexstyle11" style="padding-right:5px;">' + txtRate + '</div></td><td width="66"><div align="right" class="nexstyle11" style="padding-right:5px;">' + txtPeak + '</div></td><td width="66"><div align="right" class="nexstyle11" style="padding-right:5px;">CG</div></td><td width="66"><div align="right" class="nexstyle11" style="padding-right:5px;">CC</div></td><td width="60"><div align="right" class="nexstyle11" style="padding-right:5px;">' + txtTota + '</div></td><td width="60"><div align="center" class="nexstyle11">' + txtDete + '</div></td></tr>';

		for (var i = 0; i <= stormlist.length-1; i++) {
		part = '';
		part= stormlist[o];
		o++;

		//parts = part.split('\n')[13];

		ids = part.split('\n')[1];
		id = ids.split(' ')[2];
		detec = ids.split(' ')[4];

		locs = part.split('\n')[3];
		bear = windDir(locs.split(' ')[3]);
		dist = locs.split(' ')[6];

		lac = part.split('\n')[5];
		lastact = lac.split(' ')[8];

		clad = part.split('\n')[6];
		clae = clad.split(' ')[14];

		tre = part.split('\n')[7];
		trend = tre.split(' ')[14];

		cur = part.split('\n')[9];
		curate = cur.split(' ')[11];
		curate = curate.replace(/ute/,'');

		peak = part.split('\n')[10];
		prate = peak.split(' ')[14];
		prate = prate.replace(/ute/,'');

		tot = part.split('\n')[12];
		totst = tot.split(' ')[8];

		cgs = part.split('\n')[13];
		cg = cgs.split(' ')[11];
		cg = cg.replace(/%/gm,'');
		cg = (cg*100/100).toFixed(0);

		ccs = part.split('\n')[14];
		cc = ccs.split(' ')[13];
		cc = cc.replace(/%/gm,'');
		cc = (cc*100/100).toFixed(0);

		for (var u=0; u < trends.length; u++) {
		if (trends[u] == trend) {
		v = u;
		}
		}

		tt = trendtxt[v];

		for (var k=0; k < intensitys.length; k++) {
		if (intensitys[k] == clae) {
		b = k;
		}
		}

		jj = intensitytxt[b];
		tcolor = bgcolor[b];

		if(dist < 50) {
		nearby = 'on';
		}

		//table+= id + " " + lastact + " " + bear + " " + dist + " " + clae + " " + trend + " " + curate + " " + prate + " " + cg + " " + cc + " " + totst + " " + detec + "<br/>";

		table+= se + '<tr class="tracreporttr"><td width="63" class="tracreport"><div class="nexstyle1" align="left" style="padding-left:7px;">' + id + '</span></td><td width="63" class="tracreport"><div align="center" class="nexstyle1">' + lastact + '</div></td><td width="63" class="tracreport"><div align="center" class="nexstyle1">' + bear + '</div></td><td width="63" class="tracreport"><div align="right" class="nexstyle1"style="padding-right:5px;">' + dist + ' km</div></td><td width="92" class="tracreport"><div align="center" class="nexstyle1"><font color="' + tcolor + '">' + jj + '</font></div></td><td width="93" class="tracreport"><div align="center" class="nexstyle1">' + tt + '</div></td><td width="63" class="tracreport"><div align="right" class="nexstyle1"style="padding-right:5px;">' + curate + '</div></td><td width="66" class="tracreport"><div align="right" class="nexstyle1"style="padding-right:5px;">' + prate + '</div></td><td width="66" class="tracreport"><div align="right" class="nexstyle1">' + cg + '%</div></td><td width="66" class="tracreport"><div align="right" class="nexstyle1">' + cc + '%</div></td><td width="60" class="tracreport"><div align="right" class="nexstyle1" style="padding-right:10px;">' + totst + '</div></td><td width="60" class="tracreport"><div align="center" class="nexstyle1">' + detec + '</div></td></tr>';

		}

		table+= se + "</table><br/>";

		set_ajax_obsL("STORM", table);

		if(nearby == "on") {
		set_ajax_obsL("NEARBY", "<font color=\"#EE2C2C\">" + txtActi + "</font>");
		} else {
		set_ajax_obsL("NEARBY", "<font color=\"#00FF00\">" + txtInac + "</font>");
		}

	} // End if storms

    } // END if (x.readyState == 4 && x.status == 200)
	} // END try
	catch(e){}  // Mike Challis added fix to fix random error: NS_ERROR_NOT_AVAILABLE
    } // x.onreadystatechange = function()
    w.open("GET", url, true);
    w.send(null);
  }  // end of 'got something back'

} // end TRACLoader function


//TRACLoader('TRACRep.txt?' + new Date().getTime());



function ajaxLoader(url) {  
  if (document.getElementById) {
    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(url);
  }
	if (x && ( updates <= maxupdates || maxupdates > 0  )) { // got something back
    x.onreadystatechange = function() {
    try { if (x.readyState == 4 && x.status == 200) { 
		if (maxupdates > 0 ) {updates++; } // increment counter if needed



//alert(x.responseText)		
		
		rate = x.responseText.split(',')[24];
		clrate = x.responseText.split(',')[26];

		if(rate < 30) {
		set_ajax_obsL("RATE", rate);
		} else {
		set_ajax_obsL("RATE", "<font color=\"#EE2C2C\">" + rate + "</font>");
		}

		if(clrate < 30) {
		set_ajax_obsL("CLOSERATE", clrate);
		} else {
		set_ajax_obsL("CLOSERATE", "<font color=\"#EE2C2C\">" + clrate + "</font>");
		}
/*
			if((x.responseText.split(',')[11] == 1) && (x.responseText.split(',')[24] > 0)) {
			document.getElementById("TYPE").src = "../NSDisplay/pluscg.png";
			}
			else if((x.responseText.split(',')[12] == 1) && (x.responseText.split(',')[24] > 0)) {
			document.getElementById("TYPE").src = "../NSDisplay/minuscg.png";
			}
			else if((x.responseText.split(',')[13] == 1) && (x.responseText.split(',')[24] > 0)) {
			document.getElementById("TYPE").src = "../NSDisplay/pluscc.png";
			}
			else if((x.responseText.split(',')[14] == 1) && (x.responseText.split(',')[24] > 0)) {
			document.getElementById("TYPE").src = "../NSDisplay/minuscc.png";
			}
			else {
			document.getElementById("TYPE").src = "../NSDisplay/nostr.png";
			}
			if(x.responseText.split(',')[24] == 0) {
			document.getElementById("TYPE").src = "../NSDisplay/nostr.png";
			}
*/
		//Since midnight
		set_ajax_obsL("MIDNIGHT", x.responseText.split(',')[15]);

		//Total since X minute
		set_ajax_obsL("LMINTOTAL", x.responseText.split(',')[32]);

		//Peakrate
		set_ajax_obsL("PEAKRATE", "" + x.responseText.split(',')[22] + "/min");

		//Peaktime
		set_ajax_obsL("PEAKTIME", x.responseText.split(',')[23]);
 
 		//Last striketime
		set_ajax_obsL("LASTSTRTIME", x.responseText.split(',')[41]); 

		//Last striketype
		lasttype = x.responseText.split(',')[10];
		set_ajax_obsL("LASTSTRTYPE", lasttype); 

		//Last strikedist
		set_ajax_obsL("LASTSTRDIS", x.responseText.split(',')[7]); 

		//Last strikedirec
		bear = x.responseText.split(',')[6];
		bearing = (bear*100/100).toFixed(0);
		set_ajax_obsL("LASTSTRDIREC", bearing + "°");	

		midnight = x.responseText.split(',')[15];
			cgptod = x.responseText.split(',')[18];
			if(cgptod == 0) { cgptof = "0%"; } else { cgptof = "" + ((cgptod/midnight)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CGPTODPREC", cgptof);

			cgmtod = x.responseText.split(',')[19];
			if(cgmtod == 0) { cgmtof = "0%"; } else { cgmtof = "" + ((cgmtod/midnight)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CG-TODPREC", cgmtof);

			ccptod = x.responseText.split(',')[20];
			if(ccptod == 0) { ccptof = "0%"; } else { ccptof = "" + ((ccptod/midnight)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CCPTODPREC", ccptof);

			ccmtod = x.responseText.split(',')[21];
			if(ccmtod == 0) { ccmtof = "0%"; } else { ccmtof = "" + ((ccmtod/midnight)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CC-TODPREC", ccmtof);

		//Uptime
		len = x.responseText.split(',')[40].length;
		set_ajax_obsL("UPTIME", x.responseText.split(',')[40].substr(0,len-3));

		//Date
		set_ajax_obsL("DATE", "" + x.responseText.split(',')[2] + "." + x.responseText.split(',')[1] + "." + x.responseText.split(',')[0].substr(2, 2) + " " + x.responseText.split(',')[3] + ":" + x.responseText.split(',')[4] + ":" + x.responseText.split(',')[5] + "");

		// Update the map, trac and graph each minute
		thismin = x.responseText.split(',')[4];	
		
		if (thismin*1 >= nextmin*1) {
			//Calling the tracreportshort
			//humm = makeRequest("../NSDisplay/NStracajax.php?lang=" + lang);
			document.getElementById("GRAPH").src = path_to_graph;
			document.getElementById("NEXMAP").src = path_to_map + "?" + new Date().getTime();
			TRACLoader(path_to_trac + '?' + new Date().getTime());
			//nextmin = Number(thismin) + Number(checkmaps);
			nextmin = thismin*1 + 1;  // Now can configure how often to check
			if (nextmin >= 60) {
			nextmin = nextmin - 60;
			}
//set_ajax_obsL("debugline", "Checking Maps " + thismin + " " + nextmin);			
		}


		//set_ajax_obsL("MAXTEMP", maxtemp);


if (maxupdates > 0 && updates >= maxupdates) {
alert(txtPaus);
}


    } // END if (x.readyState == 4 && x.status == 200)
	} // END try
	catch(e){}  // Mike Challis added fix to fix random error: NS_ERROR_NOT_AVAILABLE
    } // x.onreadystatechange = function()
    x.open("GET", url, true);
    x.send(null);
  	if ( (maxupdates == 0) || (updates < maxupdates-1)) {
      setTimeout("ajaxLoader(path_to_data + '?' + new Date().getTime())", 30000); // get new data 
	} 
  }  // end of 'got something back'

} // end NSajaxLoader function


ajaxLoader(path_to_data + '?' + new Date().getTime());



