var local;
// window.onerror = reportError;

function roundIt(value, digits){
   var factor = Math.pow(10, digits);
   return(Math.round(value * factor) / factor);
}

function reportError(msg,url,line) {
  var str = "Error: " + msg + " on line: " 
  + line + "\nURL: " + url  + "\nWeb: " 
  + navigator.appName + " " + navigator.appName 
  + " " + navigator.appVersion;
  alert(str);
  return true;
}

// This is used by the UDP applet to write the device value to
// the code in the parent frame, which handles all display work.

function writeDeviceVal(deviceid, value){
   // document.udp.msg("writing device: " + deviceid + " value: " + value);
   top.writeDeviceVal(deviceid, value);
}

//function updateCellValues(){
   // document.udp.msg("updating cell values");
//   top.updateCellValues(document.udp);
//}

function displayTitle(location, title){
   if(location != undefined) document.write("<h1 class=screen_title>" + location + "</h1>");
   document.write("<h2 class=screen_title_b>" + title + "</h2>");
}

function drawValueCell(id, classes){
   // alert(classes[0]);
   var deviceid = top.deviceid_xref[id];
   
   var cellstrg = ("<td class='" + classes[0] + "'");
   if(deviceid != undefined){
      cellstrg += (" onmouseover=\"this.className='" + classes[1] + "'; top.status = 'Click for graph'; return true;\"");
      cellstrg += (" onmouseout=\"this.className='" + classes[0] + "'; top.status = '';return true;\"");
      cellstrg += (" onClick=\"openGraphWindow('" + deviceid + "')\"");
   }
   cellstrg += ("><span class='" + classes[2] + "' id='" + id + "'></span></td>"); 
   // alert(cellstrg);
   document.write(cellstrg);
}

function displayLocal(local){
	if(local){
	   document.write("<p>This display shows the most recent data values broadcast by a CWC ");
	   document.write("datalogger on this network.</p>");
	}else{
	   document.write("<p>This display shows the most recent data values uploaded to the ");
	   document.write("CWC server.</p>");
	} 
}

// Opens a new window with a graph in it, showing the last 2 days
// of values for <deviceid>.
function openGraphWindow(deviceid){
   var thewin = window.open('http://cwc-das.com/wcsd/plotit.php?deviceids[]='+deviceid+'&timespec=last2days&width=450&height=300&usef=true', 'graph', 'width=500,height=350');
   thewin.focus();
}

// following sequence takes the query string from the calling URL, and
// sets JavaScript variables. Assume that they are all of the form aaa=bbb.

var querystr;
if(top != self){
	querystr = top.viewframe.location.search.substr(1);
}else{
	querystr = location.search.substr(1);
}
// document.write(querystr + "<br>");
var queryary = querystr.split('&');
for(index in queryary){
  query = queryary[index].split("=");
  if(query.length == 2){
     var valname = query[0];
     if(valname.substring(valname.length - 2) == "[]"){
// something special here to handle arrays.
     }else{
        eval(query[0] + " = '" + decodeURI(query[1]) + "'");
     }
  }
}

if(local != undefined) local = (local == "true")?true:false;


// Contains the functions used to build values being displayed
// Contains a cross reference between cell names and device IDs
var deviceid_xref = new Array;
var displayfuncs = new Array;
var current_display_vals = new Array;

var xml_deviceids = new Array;
var data_values = new Array;
var data_times = new Array;
var last_data_values = new Array;
var last_data_times = new Array;
var cal_scales = new Array;
var cal_offsets = new Array;
var maxvals = new Array;
var minvals = new Array;
var avgvals = new Array;
var val_ds = new Array;
var val_ms = new Array;
var val_ys = new Array;
var co2_factors = new Array;
var so2_factors = new Array;
var nox_factors = new Array;
var par_factors = new Array;
var voc_factors = new Array;
var h2o_factors = new Array;
var hg_factors = new Array;
var coal_pcts = new Array;
var gas_pcts = new Array;
var oil_pcts = new Array;
var crons = new Array;
var locations = new Array;
var datatypes = new Array;

var xml_tag_xref = new Array;
xml_tag_xref["ID"] = xml_deviceids;
xml_tag_xref["VAL"] = data_values;
xml_tag_xref["LASTVAL"] = last_data_values;
xml_tag_xref["TIME"] = data_times;
xml_tag_xref["LASTTIME"] = last_data_times;
xml_tag_xref["CAL_SCALE"] = cal_scales;
xml_tag_xref["CAL_OFFSET"] = cal_offsets;
xml_tag_xref["MAXVAL"] = maxvals;
xml_tag_xref["MINVAL"] = minvals;
xml_tag_xref["AVGVAL"] = avgvals;
xml_tag_xref["VAL_D"] = val_ds;
xml_tag_xref["VAL_M"] = val_ms;
xml_tag_xref["VAL_Y"] = val_ys;
xml_tag_xref["CO2_FACTOR"] = co2_factors;
xml_tag_xref["SO2_FACTOR"] = so2_factors;
xml_tag_xref["NOX_FACTOR"] = nox_factors;
xml_tag_xref["VOC_FACTOR"] = voc_factors;
xml_tag_xref["PAR_FACTOR"] = par_factors;
xml_tag_xref["H2O_FACTOR"] = h2o_factors;
xml_tag_xref["HG_FACTOR"]  = hg_factors;
xml_tag_xref["CRON"]  = crons;
xml_tag_xref["LOCATION"]  = locations;
xml_tag_xref["DATATYPE"]  = datatypes;
