/*
 *  sensors.js - update sensors on Legoguy's robot page. Thanks to infocraft
 *  for the Firefox counter script that this was adapted from.
 *
 *  You may use this script in its entirety, modify it to suit your needs,
 *  adapt its functions, or just use it for inspiration.  However, some form
 *  of acknowledgement, especially a link back to http://www.infocraft.com/, is
 *  always appreciated.
 */


var counter_url = "sensors.xml";	// The URL of your counter mirror.

var status_id = "status";
var nearest_id = "nearest";
var distance_id = "distance";
var light_id = "light";
var voltage_id = "voltage";
var viewers_id = "viewers";
var requests_id = "commands";
var update_status_id = "update_status";
var update_tick_id = "update_tick";
var request = null;
var counter_on = true;

/* This should be called from your HTML, probably as an onload="get_count();"
 * event in your <body> tag. */
function get_count()
{
	var delay = 10000;
	if(update)
		load_xml(counter_url);

	setTimeout("get_count()", delay);

	if (document.getElementById(update_tick_id)) {
		set_status(Math.round(delay / 1000));
	}
}

/* Advances the status counter, indicating the length until the next increment. */
function set_status(time)
{
	var timer = document.getElementById(update_tick_id);

	timer.firstChild.data = time;

	time--;
	if (time > 0) {
		setTimeout("set_status(" + time + ")", 1000);
	}
}


/* Adapted from the good people at ADC:
 * http://developer.apple.com/internet/webcontent/xmlhttpreq.html
 * this starts loading the XML data and sets up the process_request handler. */
function load_xml(url)
{
	set_element(update_status_id, "Send");
	/* branch for native XMLHttpRequest object */
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
		request.onreadystatechange = process_request;
		request.open("GET", url, true);
		request.send(null);

	/* branch for IE/Windows ActiveX version */
	} else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
		if (request) {
			request.onreadystatechange = process_request;
			request.open("GET", url, true);
			request.send();
		}
	}
}

	
/* This function is called once the request state changes.  It pulls the
 * relevant data from the response and sends to update_count(). */
function process_request()
{
	var response;
	var status;
	var status_style;

	set_element(update_status_id, request.readyState);
	if (request.readyState == 4) {
		if (request.status == 200) {
			response = request.responseXML;
			netxt = response.getElementsByTagName('netxt')[0];
			for(i = 0; i < netxt.childNodes.length; i++) {
				//try{p.r.i.n.t}catch(e){debug('process_request: setting value for '+ws2300.childNodes[i].nodeName, '', e.lineNumber)};
				if(element = document.getElementById(netxt.childNodes[i].nodeName))
					document.getElementById(netxt.childNodes[i].nodeName).firstChild.data = netxt.childNodes[i].firstChild.data;
			}

			status = netxt.getElementsByTagName('status')[0].firstChild.data;
			if(status == 'Online' || status == 'Locked' || status == 'Single Mode' ){
				control = (status == 'Online' || status == 'Single Mode' ? true : false);
				status_style = (status == 'Locked'? '#ffa500' : 'limegreen');
				status_style = (status == 'Single Mode'? 'yellow' : status_style);
				set_element_color(status_id, status_style);

				var voltage_tmp;
				var voltage_color = '#000000';
				voltage_tmp = netxt.getElementsByTagName('voltage')[0].firstChild.data.replace(/mV/, '');
				     if (voltage_tmp >= 9000)
					voltage_color = '#00ff00'
				else if (voltage_tmp >= 8750)
					voltage_color = '#15ff00'
				else if (voltage_tmp >= 8500)
					voltage_color = '#2aff00'
				else if (voltage_tmp >= 8250)
					voltage_color = '#40ff00'
				else if (voltage_tmp >= 8000)
					voltage_color = '#55ff00'
				else if (voltage_tmp >= 7750)
					voltage_color = '#6aff00'
				else if (voltage_tmp >= 7500)
					voltage_color = '#80ff00'
				else if (voltage_tmp >= 7250)
					voltage_color = '#95ff00'
				else if (voltage_tmp >= 7000)
					voltage_color = '#aaff00'
				else if (voltage_tmp >= 6750)
					voltage_color = '#bfff00'
				else if (voltage_tmp >= 6500)
					voltage_color = '#d4ff00'
				else if (voltage_tmp >= 6250)
					voltage_color = '#eaff00'
				else if (voltage_tmp >= 6000)
					voltage_color = '#ffff00'
				else if (voltage_tmp >= 5750)
					voltage_color = '#ffea00'
				else if (voltage_tmp >= 5500)
					voltage_color = '#ffd500'
				else if (voltage_tmp >= 5250)
					voltage_color = '#ffbf00'
				else if (voltage_tmp >= 5000)
					voltage_color = '#ffaa00'
				else if (voltage_tmp >= 4750)
					voltage_color = '#ff9500'
				else if (voltage_tmp >= 4500)
					voltage_color = '#ff8000'
				else if (voltage_tmp >= 4250)
					voltage_color = '#ff6a00'
				else if (voltage_tmp >= 4000)
					voltage_color = '#ff5500'
				else if (voltage_tmp >= 3750)
					voltage_color = '#ff4000'
				else if (voltage_tmp >= 3500)
					voltage_color = '#ff2a00'
				else if (voltage_tmp >= 3250)
					voltage_color = '#ff1500'
				else if (voltage_tmp >= 3000)
					voltage_color = '#ff0000';

				set_element_color(voltage_id, voltage_color);
			}
			else
			{
				control = false;
				set_element_color(status_id, "red");
				set_element(nearest_id, '---');
				set_element(distance_id, '---');
				set_element(light_id, '---');
				set_element(voltage_id, '---');
				set_element_color('#000000');
				set_element(viewers_id, '---');
				set_element(requests_id, '---');
			}
			/* alert(status + " " + distance + " " + light + " " + voltage); */
		} else {
			alert("Error: " + request.statusText());
		}

		set_element(update_status_id, (request.statusText ? request.statusText : request.status));
		request = null;
	}
}

/* Sets an HTML element with element_id to the given content. */
function set_element(element_id, content)
{
	var element;

	if (element = document.getElementById(element_id))
		element.firstChild.data = content;
}
function set_element_color(element_id, color)
{
	var element;

	if (element = document.getElementById(element_id)) {
		element.style.color = color;
		//alert(element.style.color + element.style.fontWeight);
	}
}
