/*  DPRS Log Reader by F5VAG   */
// put your http file name in url and the destination division in div
// The CSS classes are dgl for the DPRS gates section and dgk for the table
// edit these parameters  ***************************
var log_url = "http://f5vag.eu/php/dprs.log";
var map_url = "http://f5vag.eu/find/";
var div = "log";  // there must be <div id="log"></div> on the main page
var show_bad_beacons_button = true;
var bad_beacons_url = "http://f5vag.eu/php/bad_dprs.php";   // use default: http://www.aprs-is.net/DPRS.aspx
// ***************************************************
//
var xmlHttp;
var pattern1 = /,qA\w,([A-Z0-9-_]{3,9})[,:]/;
var pattern2 = /([A-Z0-9]{3,9})-?/;
var pattern3 = /^WIDE|^TRACE|^RELAY/;
var to;
var trth = "<tr><th>LastHeard/UTC</th><th>Call</th><th>Type</th><th>DGate</th><th>TO CALL</th><th>Raw Payload of Beacon</th></tr>";
var out_form = '<form action="javascript:searchLog()"><input id="call" type="text" size="9" maxlength="9" /><input type="submit" value="Highlight Prefix" /></form>';
if (show_bad_beacons_button) { out_form += '<form action="'+bad_beacons_url+'" target="_blank"><input type="submit" value="Show Bad DPRS Beacons Log" /></form>'; }
String.prototype.trim = function() {
var re = /^\s*(.*?)\s*$/;
return this.replace(re,"$1");
}
function searchLog() {
   if (to) { clearTimeout(to); }
   to = setTimeout("getLog()",300000);
   var str = document.getElementById("call").value.trim();
document.getElementById("call").value = str;
   if (!str) { return; }
   str = ">" + str.toUpperCase();
   trs = document.getElementById("log_tab").rows;
   for (var i=0; i<trs.length; i++) {
       trs[i].style.backgroundColor = "";
       if ((trs[i].cells[1].innerHTML.indexOf(str) > 0) || (trs[i].cells[3].innerHTML.indexOf(str) > 0)) {
            trs[i].style.backgroundColor = "yellow";
            }
       }
   }
function getLog() {
    if (to) { clearTimeout(to); }
    xmlHttp=GetXmlHttpObject();
    url=log_url+"?sid="+Math.random();
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    to = setTimeout("getLog()",300000);  // five minutes refresh
    }
function stateChanged() {
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
     var ret = xmlHttp.responseText;
     var outstr = out_form;
     outstr += "<table id=\"log_tab\" style=\"margin-top:5px;\" summary=\"DPRS log\">"+trth;
     var lines = ret.split("\r\n");
     var acount = 0;
     var dcount = 0;
     var gcount = 0;
     var dgate = '';
     var type = '';
     var gsa = [];
     var gsc = '';
     var gsi = '';
     var tocall = '';
     for (var i=0; i<lines.length; i++) {
       if (lines[i].length > 5) {
          var parts = lines[i].split("|");
          var d = new Date();
          d.setTime(parts[0]*1000);
          var t2 = d.toUTCString().split(" ");
          var ti = t2[2]+" "+t2[1]+" "+t2[4];
          tocall = parts[3].substring(parts[3].indexOf("&gt;")+4, parts[3].indexOf(","));
          if (tocall.match(pattern3)) { tocall = '<span style="color:red;">' + tocall + '</span>'; }
          var plr = parts[3].split(":");
          if (parts[1] == 1) {
              type = "DPRS";
              dcount++;
              var m = parts[3].match(pattern1);
              if (!m) { m[1] = "?"; }
              dgate = '<a href="' + map_url + m[1]+'" target="_blank" title="Map '+m[1]+'">'+m[1]+'</a>';
              var mm = m[1].match(pattern2);
              if (!mm) { mm[1] = "?"; }
              var gsb = gsa.toString();
              if (gsb.indexOf(mm[1]) < 0) {
                  gsa.push(m[1]);
                  gcount++;
                  }
              } else {
              type = "APRS";
              acount++;
              dgate = "-";
              }
          if (plr[0].match(/,DSTAR,|,D-STAR\*?,/)) { plr = '<b><a href="'+bad_beacons_url+'" target="_blank" style="color:red;">Bad DPRS Beacon</a></b>'; type = "?"; } else { plr = parts[3].slice(parts[3].indexOf(":")+1).substring(0,80); }
          outstr += '<tr><td align="center">'+ti+'</td><td><a href="'+map_url+parts[2]+'" target="_blank" title="Map '+parts[2]+'">'+parts[2]+'</a></td><td>'+type+'</td><td>'+dgate+'</td><td>'+tocall+'</td><td>'+plr+'</td></tr>';
          }
       }
     gsa = gsa.sort();
     for (var i=0; i<gsa.length; i++) {
         gsi = ((i+1)%10)?'&nbsp;&nbsp;':'<br />';
         gsc += '<a href="'+map_url+gsa[i]+'" target="_blank">'+gsa[i]+'</a>'+gsi;
         }
     outstr += trth+"</table>";
     var outh = '<p class="dgl"><b>The log shows '+dcount+' DPRS and '+acount+' "DSTAR"-containing APRS beacons from Europe for the last week.</b></p>';
     outh += '<p class="dgl"><b>There are '+gcount+' active DPRS gates in the log:</b></p><p class="dgk">'+gsc+"</p>";
     document.getElementById(div).innerHTML=outh+outstr;
     }
   }
function GetXmlHttpObject() {
   var xmlHttp=null;
   try {
       xmlHttp=new XMLHttpRequest();
       }
   catch (e) {
       try {
           xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
           }
           catch (e) {
               xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
               }
       }
   return xmlHttp;
   }
