// Original script by Ken True with mods by Ken Challis and others    
// Modified for NexStorm updates by Henkka Roblom
// Some added functionality by Jim McMurry
// July 15, 2011 Version  
//  
// No need to change anything in this script
// Everything is configured in NSconfig-inc.php 
var nstotalupdates = 0;    
var nextmin = 0;
var lasttrac = 0; 
var lastoldtrac = 0;
var maptime = 0; 
var graphtime = 0;
var prevmap = 0;
var prevtrac = 0;
var prevgraph = 0;
var mapcheck = 0;
var graphcheck = 0;
var savetime = 0;
var maprate = 0;
var tracrate = 0;
var graphrate = 0;
var timelaststrike = ""; 
var numplotted = 0;
var fntsize = "1.0em";
var currStrike = 1;
var cycles = 0;
now = new Date().getTime();
var countdowntime = now;
var countdowngraph = now;
var countdowntrac = now;
var mapnow = 0;
var graphnow = 0; 
var tracnow = 0;
var currentZoom = "auto";
var ReqZoom = "auto";
var ZoomTries = 0;
var radaron = false;
var overlay = "x";
var p2_lat, p2_lon;
var heightofimagelatitude, widthofimagelongitude;
var widthofimagedistance, heightofimagedistance;
var centerlat, centerlon;

function ClearMap() {
	
	var i;
	var L = 1;
	for (i=1; i <= MaxLS; i++) {
		var chkspan = document.getElementById("LASTSTRIKE" + i);
		var toolspan = document.getElementById("TOOLTIP" + i);
		var toollink = document.getElementById("TOOLLINK" + i);
		if (chkspan.style.zIndex > 0) {
			var nowtime = new Date().getTime();
			var plottime = chkspan.getAttribute("lasttime");
			diff = ((nowtime - plottime)/1000).toFixed(0);	
			if (diff <= replot*1) {      // Need to re-order it
				var Lspan = document.getElementById("LASTSTRIKE" + L);
				Lspan = chkspan;
				var Tspan = document.getElementById("TOOLTIP" + L);				
				Tspan = toolspan;
				var Tlink = document.getElementById("TOOLLINK" + L);				
				Tlink = toollink;
				if (laststrikeChar == "") {  // We used numbers
					Lspan.innerHTML = L;
				} 
				L++;
			} else {   // Time expired - Make it disappear
				chkspan.style.zIndex = -5;
				toollink.style.left = "0px";
				toollink.style.top = "0px";
			}		
		}
	}
	currStrike = L;
	if (currStrike == 20) {
		currStrike = 1;
	}
}

function AddNewStrike(bearing, distance, type, stktime, fsize, zoom) {
	
	zoom = zoom.replace(" " + measure,"");
	centerlat = 0;                                                    // These work perfectly in both hemisphreres
	centerlon = 0;    		
	widthofimagedistance = zoom * 2;
	heightofimagedistance = (widthofimagedistance / imgwidth) * imgheight;
	if (measure == "km") {
		heightofimagelatitude =  heightofimagedistance / 111.322;      // 1 degree latitude in km x height
		widthofimagelongitude = widthofimagedistance / (cos(deg2rad(centerlat)) * 111.322); // latitude in km x width
	} else {
		heightofimagelatitude = heightofimagedistance / 69.172;        // 1 degree latitude in miles times height of image in miles
		widthofimagelongitude = widthofimagedistance / (cos(deg2rad(centerlat)) * 69.172); // latitude in miles x width
	}
	getpoint(bearing, distance, centerlat, centerlon, measure);
	pt = getlocationcoords(p2_lat, p2_lon, imgwidth, imgheight);
	var xpos = pt[0];	
	var ypos = pt[1];	
	xpos = xpos - 3;   // Tweak to match up with the map 
	ypos = ypos - 5;

	if ((xpos <= imgwidth) && (ypos <= imgheight)) {   // We only want the ones that will show on the map
		// Make sure the tool tip doesn't go off the right or bottom
		if (xpos > imgwidth - 85 ) {  // on right edge
			if ( ypos > imgheight - 90) {
				posit = "uleft";
			} else {
				posit = "lleft";
			} 												
		} else if (ypos > imgheight - 90) {  // on bottom edge
			if ( xpos > imgwidth - 85 ) {
				posit = "uleft";
			} else {
				posit = "uright";
			} 					
		} else  {
			posit = "lright";
		}	
		// Select image and color depending on strike type
		var typecolor;
		if (type == "+CG") {
			typecolor = pCGcolor;
			cloud = path_to_NSfiles + "pluscg.png";
		} else if (type == "-CG") {
			typecolor = nCGcolor;  
			cloud = path_to_NSfiles + "minuscg.png";
		} else if (type == "+IC") {
			cloud = path_to_NSfiles + "pluscc.png";
			typecolor = pICcolor;   
		} else {
			cloud = path_to_NSfiles + "minuscc.png";
			typecolor = nICcolor;
		}	
		var stkspan = document.getElementById("LASTSTRIKE" + currStrike);
		var nowtime = new Date().getTime();
		stkspan.setAttribute("lasttime",nowtime);   // Store the time that we plotted it
		stkspan.style.left = xpos + "px";
		stkspan.style.top = ypos + "px";
		stkspan.style.fontSize = fsize;
		stkspan.style.color = typecolor;
		stkspan.style.zIndex = 2;
		if (laststrikeChar == "") {
			thisstrikeChar = currStrike;     // Uses numbers
		} else {
			thisstrikeChar = laststrikeChar;
		}					
		stkspan.innerHTML = thisstrikeChar;	
		// Set up the tool tip feature	- this runs in a separate <a> otherwise we can't control the z-axis				
		var toollink = document.getElementById("TOOLLINK" + currStrike);
		toollink.style.left = xpos + "px";
		toollink.style.top = ypos + "px";
		toollink.className = "info " + posit;
		var toolspan = document.getElementById("TOOLTIP" + currStrike);
		toolspan.innerHTML = '<img src="' + cloud + '" alt=" Strike Type" /><br /><font color="' + typecolor + '">' + type + '</font><br />' + bearing + '&deg;<br />' + distance + ' ' + measure + '<br />' + stktime + '<br />';
		currStrike++;
		if (currStrike == MaxLS) {
			currStrike = 1;
		}
	}	
}

function getpoint(angle, distance, lat, lon, measure) 
{
//global $p2_lat, $p2_lon;
//Math....
	if (measure == "nm") {
		R = 3444;// earth's mean radius in nautical miles
	} else if (measure == "km") {
		R = 6378;// earth's mean radius in kilometers
	} else {
		R = 3956;// earth's mean radius in miles (default)
	}
	d = distance/R;       // d = angular distance covered on earth's surface
	rad = deg2rad(angle); //decimal degrees to radians
	p1_lat = deg2rad(lat);
	p1_lon = deg2rad(lon);
	p2_lat = asin( sin(p1_lat)*cos(d) + cos(p1_lat)*sin(d)*cos(rad) );
	p2_lon = p1_lon + atan2(sin(rad)*sin(d)*cos(p1_lat), cos(d)-sin(p1_lat)*sin(p2_lat));
	p2_lat = rad2deg(p2_lat);
	p2_lon = rad2deg(p2_lon);
}

function getlocationcoords(lat, lon, iwidth, iheight)            // This and the next function compliments of Tom Chaplin, carterlake.org
{   
// Now we convert the long/lat coordinates into screen coordinates
//	global $heightofimagelatitude, $widthofimagelongitude, $centerlat, $centerlon;
    x = (lon + (abs(centerlon) + (widthofimagelongitude / 2))) * (iwidth / widthofimagelongitude); 
    y = (((lat * -1) + (abs(centerlat) + (heightofimagelatitude / 2))) * (iheight / heightofimagelatitude)); 
	var result =[];
	result[0] = Math.round(x);
	result[1] = Math.round(y);
    return result; 
} 
 
function asin (arg) {
    return Math.asin(arg);
} 
function sin(arg) {
    return Math.sin(arg);
}
function deg2rad (angle) {
    return (angle / 180) * Math.PI;
}
function cos (arg) {
    return Math.cos(arg);
}
function acos (arg) {
    return Math.acos(arg);
}
function atan2 (y, x) {
    return Math.atan2(y, x);
} 
function rad2deg (angle) {
    return (angle / Math.PI) * 180;
}
function abs (mixed_number) {
    return Math.abs(mixed_number) || 0;
} 

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

function convertalign(rawalign) {

		align = Math.floor(Number(rawalign) + Number(alignment));
		if (align > 360) {
		align = Number(align) - Number(360);
		}
		return align;
}

function bearDir ($beardir)
// 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
{
   $bearlabel = new Array("N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW");
   return $bearlabel[Math.floor(((parseInt($beardir) + 11) / 22.5) % 16 )];
}

function subtractTime(time1, time2) {
	seconds = time1-time2; 
	minVar = Math.floor(seconds/60);  // The minutes
	secVar = seconds % 60;              // The balance of seconds
	if (secVar < 10) secVar = "0" + secVar;
	return therate = minVar + ":" + secVar;
}

function secs2Time(seconds) {           // Modified to handle hours now that NSStrikes can be very old
	hrVar = Math.floor(seconds/3600);    // The hours
	minVar = Math.floor(seconds/60);    // The minutes
	secVar = seconds % 60;              // The balance of seconds
	if (hrVar < 10) hrVar = "0" + hrVar;		
	if (minVar < 10) minVar = "0" + minVar;	
	if (secVar < 10) secVar = "0" + secVar;
	if (hrVar != "00") minVar = hrVar + ":" + minVar;	
	return therate = minVar + ":" + secVar;
}

// Functions for graphic display of TRACReport.txt
function makeRequest(url) {
        var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }
        if (!httpRequest) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        httpRequest.onreadystatechange = function() { alertContents(httpRequest); };
        httpRequest.open('GET', url, true);
        httpRequest.send('');
    }


    function alertContents(httpRequest) {

        if (httpRequest.readyState == 4) {
            if (httpRequest.status == 200) {
//set_ajax_obsL("debugline", "Ready State " + httpRequest.readyState  + " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Request Status " + httpRequest.status );
//set_ajax_obsL("debugline", httpRequest.responseText );			
				// Get the tracreport-data for AJAX
				lasttrac = httpRequest.responseText.split('|')[5];			
				if (httpRequest.responseText.split('|')[0] != 21) {  // The TRAC Report isn't empty
					document.getElementById("TRACHEAD").innerHTML = httpRequest.responseText.split('|')[0];
					document.getElementById("TRAC").innerHTML = httpRequest.responseText.split('|')[1];
					document.getElementById("ALRM").innerHTML = httpRequest.responseText.split('|')[2];				
					if (httpRequest.responseText.split('|')[8] != 21) {
//						document.getElementById("OLDSTORM").innerHTML = httpRequest.responseText.split('|')[8];
						set_ajax_obsL("OLDSTORM", httpRequest.responseText.split('|')[8]);
						lastoldtrac = lasttrac;
					}
					countdowntrac = lasttrac * 1000;
					now = new Date().getTime();				
					tracnow = (now - countdowntrac);				
				}				
			   maptime = httpRequest.responseText.split('|')[3];  // map time readable - 12:00pm etc
			   graphtime = httpRequest.responseText.split('|')[4];	   
			   mapupdatetime = httpRequest.responseText.split('|')[6];   // file time of the map
			   countdowntime= mapupdatetime * 1000;
			   countdowngraph = graphtime * 1000;
				// Calculate Update Rates 
				now = new Date().getTime();
				if (prevmap == 0) {      // first time through
					prevmap = mapupdatetime;	
					mapnow = (now - countdowntime);	
				} else if (mapupdatetime != prevmap) {
					if (ReqZoom != "auto" || radaron) {
						document.getElementById("NEXMAP").src = path_to_nslmap + "?range=" + ReqZoom +"&overlay="+ overlay +"&rand="+ maptime;  // get it straight to browser	
					} else {
						document.getElementById("NEXMAP").src = path_to_image +"?rand="+ maptime;            // get the image from disk									
					}
					if (path_to_nslmap != "") {
						UpdateZoom();	
						ZoomTries = 0;                      // the zoom.txt file isn't always done at this point so we'll do another update later
					}							
					maprate = subtractTime(mapupdatetime, prevmap);		
					prevmap = mapupdatetime;
					mapnow = (now - countdowntime);		
					ClearMap();  // gets rid of last strike(s)						
				}
				if (showtime != "") {
					set_ajax_obsL("IMAGETIME", maptime);
				}
				if (prevgraph == 0) {
					prevgraph = graphtime;	
					graphnow = (now - countdowngraph);								
				} else if (graphtime != prevgraph) {
					if (  graphpath.indexOf("?", 0) > 0) {
						document.getElementById("NEXGRAPH").src = graphpath+"&datatime="+graphtime+"&x="+now;     // already has a parameter in graphpath
					} else {						
						document.getElementById("NEXGRAPH").src = graphpath+"?datatime="+graphtime+"&x="+now;						
					}
					if ((showOptGraphic*1 > 0) && (path_to_opt != "")) {
						document.getElementById("OPTGRAPHIC").src = path_to_opt+"?datatime="+graphtime+"&x="+now;
					}	
					if ((showOptGraphic2*1 > 0) && (path_to_opt2 != "")) {
						document.getElementById("OPTGRAPHIC2").src = path_to_opt2+"?rand="+ maptime;
					}		
					graphrate = subtractTime(graphtime, prevgraph);		
					prevgraph = graphtime;
					graphnow = (now - countdowngraph);											
				}
				if (prevtrac == 0) {
					prevtrac = lasttrac;
					tracnow = (now - countdowntrac);								
				} else if (! (lasttrac == prevtrac)) {
					tracrate = subtractTime(lasttrac, prevtrac);		
					prevtrac = lasttrac;
					tracnow = (now - countdowntrac);								
				}
				// For optional update rates
				set_ajax_obsL("MAPRATE", maprate);
				set_ajax_obsL("TRACRATE", tracrate);
				set_ajax_obsL("GRAPHRATE",graphrate);
				} else {
					//alert('No TRAC-data currently available.');
				}
        }
    }

function UpdateTimers() {
	// Map Counter
	now = new Date().getTime();
	difference = ((now - countdowntime - mapnow)/1000+1).toFixed(0);
	set_ajax_obsL("MAPTIME", "" + secs2Time(difference));
	
	// Graph Counter
	difference = ((now - countdowngraph - graphnow)/1000+1).toFixed(0);
	set_ajax_obsL("GRAPHTIME", "" + secs2Time(difference));
	
	// TRAC Counter
	difference = ((now - countdowntrac - tracnow)/1000+1).toFixed(0);
	set_ajax_obsL("TRACTIME", "" + secs2Time(difference));

	// Realtime File Counter
	set_ajax_obsL("LIVETIME", secs2Time(cycles+1));	

	if (cycles < updaterate -1) {
		cycles++;
		setTimeout("UpdateTimers()", 1000);
	} 
}

function UpdateZoom() {
	var url = path_to_nslmap;
	url = url.replace("nslmap.php","zoom.txt");
//set_ajax_obsL("debugline", radaron + " - " +  overlay + " - " +  ReqZoom + " - " +  currentZoom);				
	
	var yo = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(url);
	if (yo) { 
		yo.onreadystatechange = function() {
			try { if (yo.readyState == 4 && yo.status == 200) { 
					yoarray = yo.responseText.split("|"); 
					if (ReqZoom == "auto") {
						document.getElementById("ZOOM").innerHTML = yoarray[0];    // zoom
						if (yoarray[0] != currentZoom) {
							var savereplot = replot;               // this is for the last strike plotting
							replot = 0;  // so they'll all go away
							ClearMap();  // gets rid of last strike(s)												
							replot = savereplot;						
						}						
						currentZoom = yoarray[0];
//set_ajax_obsL("debugline2", "&nbsp;&nbsp;&nbsp;~&nbsp;&nbsp;&nbsp;" + yoarray[0] + " - " +  yoarray[1] + " - " +  ZoomTries);										
					}					
					document.getElementById("PERSIST").innerHTML = yoarray[1];	   // persistence
					
			} 	
			} catch(e){}  
		} 
		yo.open("GET", url, true);
		yo.send(null);
	}
}

function RadarOn() {	
	radaron=!radaron;
	NewMap(ReqZoom);
}

function NewMap(Range) {	
	nstotalupdates = 0;    // give them more time since they're actively using it
	ReqZoom = Range;
	if (radaron) {
		set_ajax_obsL("RADARID", langOn);
		document.getElementById("DBZ").style.zIndex = 10;
		overlay = "R";
	} else {
		set_ajax_obsL("RADARID", langOff);
		document.getElementById("DBZ").style.zIndex = -10;		
		overlay = "x";
	}
//	if (ReqZoom != "auto" || radaron) {	
		document.getElementById("NEXMAP").src = path_to_nslmap + "?range=" + ReqZoom +"&overlay="+ overlay +"&rand="+ maptime;  // get it straight to browser	
//	} else {
//		prevmap = prevmap - 1500;                                              // trick it so we get the new map right away
//		makeRequest(path_to_tracajax + "?tractime=" + lasttrac + "&oldtractime=" + lastoldtrac + "&zoom=" + ReqZoom + "&overlay=" + overlay);  // back to normal mode	
//		nextmin = thismin*1 + checkmaps*1;  // Now can configure how often to check
//		if (nextmin >= 60) {
//				nextmin = nextmin - 60;
//		}		
//	}
	if (ReqZoom != "auto") {
		document.getElementById("ZOOM").innerHTML = langOff;
		document.getElementById("selector").innerHTML = langRange + " " + ReqZoom + " " + measure;
		currentZoom = ReqZoom;
	} else {
		document.getElementById("selector").innerHTML = langSelectRange;
		UpdateZoom();
		ZoomTries = 0;		                // the zoom.txt file isn't always done at this point so we'll do another update later
	}	
	var savereplot = replot;               // this is for the last strike plotting
	replot = 0;  // so they'll all go away
	ClearMap();  // gets rid of last strike(s)												
	replot = savereplot;						
//set_ajax_obsL("debugline", radaron + " - " +  overlay + " - " +  ReqZoom + " - " +  currentZoom);				
}

function NSajaxLoader(url) {
  if (document.getElementById) {
    var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(url);
  }
	if (x && ( nstotalupdates <= nsmaxupdates || nsmaxupdates > 0  )) { // got something back
    x.onreadystatechange = function() {
    try { if (x.readyState == 4 && x.status == 200) { 

		if (nsmaxupdates > 0 ) {nstotalupdates++; } // increment counter if needed 
		mo = x.responseText.split(',')[1];
		dd = x.responseText.split(',')[2];
		if (dateDisp < 3 ) {
			yy = x.responseText.split(',')[0].substr(2, 2);
		} else {
			yy = x.responseText.split(',')[0];
		}
		hh = x.responseText.split(',')[3];
		mm = x.responseText.split(',')[4];
		ss = x.responseText.split(',')[5];
//set_ajax_obsL("debugline", "Checking Maps " + thismin + " " + nextmin + " " + checkmaps);			
//set_ajax_obsL("debugline", ReqZoom);			
		
		if (dateDisp == 1 || dateDisp == 3) {          // mm/dd/yy
			mydate = mo + dateSep + dd + dateSep + yy;	
		} else {                        // dd.mm.yy
			mydate = dd + dateSep + mo + dateSep + yy;	
		}
		if (timeDisp == 2 ) {          // hh:mm:ss am/pm
			if (hh >= 12) {
				if (hh > 12) {
					hh = hh - 12;
				}
				if (AmPm) {
					ss = ss + " pm";
				}				
			} else {
				if (AmPm) {
					ss = ss + " am";
				}								
			}
		}
		mytime = hh + ":" + mm;	
		if (inclSecs) {
			mytime = mytime + ":" + ss;
		}			
		//Date & Time
		set_ajax_obsL("DATE", mydate);
		set_ajax_obsL("TIME", mytime);

		//Since midnight
		set_ajax_obsL("MIDNIGHT", x.responseText.split(',')[15]);

		//Close since midnight
		set_ajax_obsL("CLOSEMIDNIGHT", x.responseText.split(',')[17]);

		//Total since X minute
		set_ajax_obsL("LMINTOTAL", x.responseText.split(',')[32]);

		//Close since X minute
		set_ajax_obsL("LMINCLOSE", x.responseText.split(',')[34]);
		
        //Rate
		NSLupdate = x.responseText.split(',')[39];          // High or Normal
        light = x.responseText.split(',')[24];              // Strikes
	    if ( light == undefined) {light = ""; }
        closelight = x.responseText.split(',')[26];		    // Close  
		if ( closelight == undefined) { closelight = ""; }		
			
		if (NSLupdate == "High") {
            light = "<font color=" + cautioncolor + ">" + light + "</font>";
			closelight = "<font color=" + cautioncolor + ">" + closelight + "</font>";
			graphpath = path_to_graph2;
			updaterate = Hupdaterate;
			checkmaps = Hcheckmaps;
			alarm = "<font color=red>" + langActive + "</font>";
		} else {
            light = "<font color=" + normcolor + ">" + light + "</font>";
            closelight = "<font color=" + normcolor + ">" + closelight + "</font>";
			graphpath = path_to_graph;
			updaterate = Lupdaterate;
			checkmaps = Lcheckmaps;
			alarm = "<font color=white>" + langInactive + "</font>";
		}
		
		set_ajax_obsL("RATE", light);
		set_ajax_obsL("CLOSERATE", closelight);
		if (! use_NSStorms) {
			set_ajax_obsL("ALRM", alarm);
		}

		if (maxview > 0) {
			nsmaxupdates = maxview*(60/updaterate)+1;          // So they get the same viewing time even though the rate may have changed	
		}
//set_ajax_obsL("debugline", updaterate);			

		//Peakrate
		set_ajax_obsL("PEAKRATE", "" + x.responseText.split(',')[22] + "/min");
		//Peaktime
		set_ajax_obsL("PEAKTIME", x.responseText.split(',')[23]);
		//Burst rate
		set_ajax_obsL("BURSTRATE", "" + x.responseText.split(',')[48] + "/sec");
		//Burst time
		set_ajax_obsL("BURSTTIME", x.responseText.split(',')[49]);
 
 		//Last striketime
		set_ajax_obsL("LASTSTRTIME", x.responseText.split(',')[41]); 
		if (timelaststrike == "") {  // Don't want to plot it the first time around because it could be hours old
			timelaststrike = x.responseText.split(',')[41]; 
		}
		
		// Plot  the last strike
		if(plotstrikes) {
			if( ! ( timelaststrike ==  x.responseText.split(',')[41]) || (! x.responseText.split(',')[41] == "---") ) {
				if (light*1 >= strikeLgRate*1) {
					fntsize = strikeLg;
				} else if (light*1 >= strikeMedRate*1) {
					fntsize = strikeMed;
				} else { 
					fntsize = strikeSm;
				}
				                                                       				// bearing,dist,type,font size, current zoom level
				AddNewStrike(x.responseText.split(',')[6], x.responseText.split(',')[7], x.responseText.split(',')[10], x.responseText.split(',')[41],fntsize,currentZoom);
				timelaststrike = x.responseText.split(',')[41];  
			}
		}

		//Last strikedist
		set_ajax_obsL("LASTSTRDIS", x.responseText.split(',')[7] + " " + measure); 

		//Last striketype
		lasttype = x.responseText.split(',')[10];
		set_ajax_obsL("LASTSTRTYPE", lasttype); 

		//The Strike Type "Clouds"		
		if (showicon) {		
			if((x.responseText.split(',')[11] == 1) && (x.responseText.split(',')[24] > 0)) {
			document.getElementById("TYPE").src = path_to_NSfiles + "pluscg.png";
			}
			else if((x.responseText.split(',')[12] == 1) && (x.responseText.split(',')[24] > 0)) {
			document.getElementById("TYPE").src = path_to_NSfiles + "minuscg.png";
			}
			else if((x.responseText.split(',')[13] == 1) && (x.responseText.split(',')[24] > 0)) {
			document.getElementById("TYPE").src = path_to_NSfiles + "pluscc.png";
			}
			else if((x.responseText.split(',')[14] == 1) && (x.responseText.split(',')[24] > 0)) {
			document.getElementById("TYPE").src = path_to_NSfiles + "minuscc.png";
			}
			else {
			document.getElementById("TYPE").src = path_to_NSfiles + "nostr.png";
			}
			if(x.responseText.split(',')[24] == 0) {
			document.getElementById("TYPE").src = path_to_NSfiles + "nostr.png";
			}
		}	
		//Last strikedirec
		bear = x.responseText.split(',')[6];
		if(showbearing == 1) bearing = bearDir(bear);
		if(showbearing == 2) bearing = "" + bear + "" + bearword +"/" + bearDir(bear) +"";
		if(showbearing == 0) bearing = "" + bear + "" + bearword +"";
		set_ajax_obsL("LASTSTRDIREC", bearing);	

		// START DISTRIBUTION
		midnight = x.responseText.split(',')[15];
		xmin = x.responseText.split(',')[32];
		
		// The 1-min bar graphs
		if (bgraphs > 0) {  // These can't be running if not being used in NSDisplay or they'll stop the rest of the javascript
			//CG+ Today
			set_ajax_obsL("CGPTOD", x.responseText.split(',')[18]); 
			cgptod = x.responseText.split(',')[18];
			if(cgptod == 0) { cgptof = "0%"; } else { cgptof = "" + ((cgptod/midnight)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CGPTODPREC", cgptof);
			cgptofbar = ((cgptod/midnight)*80).toFixed(0);
			document.getElementById("CGPTODBAR").src = path_to_NSfiles + 'BarGraph.php?a=' + cgptofbar + '&c=' + pCGcolor.substring(1);
	
			//CG- Today
			set_ajax_obsL("CG-TOD", x.responseText.split(',')[19]);
			cgmtod = x.responseText.split(',')[19];
			if(cgmtod == 0) { cgmtof = "0%"; } else { cgmtof = "" + ((cgmtod/midnight)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CG-TODPREC", cgmtof);
			cgmtofbar = ((cgmtod/midnight)*80).toFixed(0);
			document.getElementById("CGMTODBAR").src = path_to_NSfiles + "BarGraph.php?a=" + cgmtofbar + '&c=' + nCGcolor.substring(1);

			//CC+ Today
			set_ajax_obsL("CCPTOD", x.responseText.split(',')[20]); 
			ccptod = x.responseText.split(',')[20];
			if(ccptod == 0) { ccptof = "0%"; } else { ccptof = "" + ((ccptod/midnight)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CCPTODPREC", ccptof);
			ccptofbar = ((ccptod/midnight)*80).toFixed(0);
			document.getElementById("CCPTODBAR").src = path_to_NSfiles + "BarGraph.php?a=" + ccptofbar + '&c=' + pICcolor.substring(1);
	
			//CC- Today
			set_ajax_obsL("CC-TOD", x.responseText.split(',')[21]);
			ccmtod = x.responseText.split(',')[21];
			if(ccmtod == 0) { ccmtof = "0%"; } else { ccmtof = "" + ((ccmtod/midnight)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CC-TODPREC", ccmtof);
			ccmtofbar = ((ccmtod/midnight)*80).toFixed(0);
			document.getElementById("CCMTODBAR").src = path_to_NSfiles + "BarGraph.php?a=" + ccmtofbar + '&c=' + nICcolor.substring(1);
		}
		
		// The x-min bar graphs
		if (bgraphs > 1) {  // These can't be running if not used in NSDisplay or they'll stop the rest of the javascript
			//CG+ Xmin
			set_ajax_obsL("CGPXMIN", x.responseText.split(',')[35]); 
			cgpxmin = x.responseText.split(',')[35];
			if(cgpxmin == 0) { cgpymin = "0%"; } else { cgpymin = "" + ((cgpxmin/xmin)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CGPXMINPREC", cgpymin);
			cgpxminbar = ((cgpxmin/xmin)*80).toFixed(0);
			document.getElementById("CGPXMINBAR").src = path_to_NSfiles + "BarGraph.php?a=" + cgpxminbar + '&c=' + pCGcolor.substring(1);
 
 			//CG- Xmin
			set_ajax_obsL("CG-XMIN", x.responseText.split(',')[36]);
			cgmxmin = x.responseText.split(',')[36];
			if(cgmxmin == 0) { cgmymin = "0%"; } else { cgmymin = "" + ((cgmxmin/xmin)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CG-XMINPREC", cgmymin);
			cgmxminbar = ((cgmxmin/xmin)*80).toFixed(0);
			document.getElementById("CGMXMINBAR").src = path_to_NSfiles + "BarGraph.php?a=" + cgmxminbar + '&c=' + nCGcolor.substring(1);

			//CC+ Xmin
			xmin = x.responseText.split(',')[32];
			set_ajax_obsL("CCPXMIN", x.responseText.split(',')[37]); 
			ccpxmin = x.responseText.split(',')[37];
			if(ccpxmin == 0) { ccpymin = "0%"; } else { ccpymin = "" + ((ccpxmin/xmin)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CCPXMINPREC", ccpymin);
			ccpxminbar = ((ccpxmin/xmin)*80).toFixed(0);
			document.getElementById("CCPXMINBAR").src = path_to_NSfiles + "BarGraph.php?a=" + ccpxminbar + '&c=' + pICcolor.substring(1);

			//CC- Xmin
			set_ajax_obsL("CC-XMIN", x.responseText.split(',')[38]);
			ccmxmin = x.responseText.split(',')[38];
			if(ccmxmin == 0) { ccmymin = "0%"; } else { ccmymin = "" + ((ccmxmin/xmin)*100).toFixed(0) + "%"; }
			set_ajax_obsL("CC-XMINPREC", ccmymin);
			ccmxminbar = ((ccmxmin/xmin)*80).toFixed(0);
			document.getElementById("CCMXMINBAR").src = path_to_NSfiles + "BarGraph.php?a=" + ccmxminbar + '&c=' + nICcolor.substring(1);
		}

		//Uptime
		len = x.responseText.split(',')[40].length;
		set_ajax_obsL("UPTIME", x.responseText.split(',')[40].substr(0,len-3));

		// Update the map, trac and graph each minute
		thismin = x.responseText.split(',')[4];	
//		var d = new Date();
//		thismin = d.getMinutes();  // this should be better in case the realtime file doesn't get there?
		
		
		if (ZoomTries < 2) {       // this only comes into play when we sent the map straight to the browser after a zoom change
			UpdateZoom();
			ZoomTries++;
		}

		if (thismin*1 >= nextmin*1) {   
			//Calling the tracreportshort
			makeRequest(path_to_tracajax + "?tractime=" + lasttrac + "&oldtractime=" + lastoldtrac + "&zoom=" + ReqZoom + "&overlay=" + overlay);			
			nextmin = thismin*1 + checkmaps*1;  // Now can configure how often to check
			if (nextmin >= 60) {
					nextmin = nextmin - 60;
			}
		}
			document.getElementById("PAUSE").src = path_to_pause_graphic;			
		
		// Show the pausemessage instead of TRACheader if needed
		if (nsmaxupdates > 0 && nstotalupdates > nsmaxupdates-1) { /* chg indicator to pause message */
			set_ajax_obsL("TRACHEAD", langPauseMsg);
//			var pimage = "<img src=" + path_to_pause_graphic + " alt=\"Pause Message\" />";
			set_ajax_obsL("PAUSE", "<img src=" + path_to_pause_graphic + " alt=\"Pause Message\" />");			
		}

		// For debugging
		//You can enable one of theese at time for checking updates. Appears in the topsection of Nexstormdisplay
		//debupdates = "" + nstotalupdates + "/" + nsmaxupdates + "";
		//set_ajax_obsL("UPDATES", "<br>Debug:<br/>" + nstotalupdates + "/ " + nsmaxupdates + "");


    } // 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 ( (nsmaxupdates == 0) || (nstotalupdates < nsmaxupdates-1)) {
		setTimeout("NSajaxLoader(path_to_data + '?' + new Date().getTime())", updaterate*1000); // get new data 
		cycles = 0;
		UpdateTimers();
    }
  }  // end of 'got something back'
} // end NSajaxLoader function


