// ------------------------------------------------------------------------
//
// Greasemonkey script: Shartak uber mapper (GM_SCM)
// Version 2.34 last modified 2007-12-03
// Copyright (c) 2006, Michael Wright
// 
// All rights reserved. Users of Shartak are freely permitted to use this
// script providing they agree that should I, the author, request it, you
// will cease using the script and thereby sending data to the server, and
// further you won't tamper with the script to send false data or access the
// server side component except by this script.
//
// http://www.itechsc.com/
//
// ------------------------------------------------------------------------
//
// This is a Greasemonkey user script. To install it, proceed as follows.
//
// 1. You need Greasemonkey, get it from http://greasemonkey.mozdev.org/
// 2. Under Tools, there will be a new menu item to "Install User Script".
//    Use it to install this script.
// 3. Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts, select "Shartak uber
// mapper", and click Uninstall.
//
// To see the map: http://www.itechsc.com/misc/shartak/ubermap/ubermap0.png
//
// ------------------------------------------------------------------------
//
// ==UserScript==
// @name          Shartak uber mapper
// @namespace     http://www.itechsc.com/
// @description   Send map data to ITechSC.Com
// @include       http://www.shartak.com/game.cgi
// ==/UserScript==
//
// ------------------------------------------------------------------------
//
// USER DEFINABLE VARIABLES HERE. YOU CAN EDIT THESE IF YOU LIKE. 
//
// Set conf_showstatus to 1 to see the status display at the bottom of the
// screen, under the buttons, or 0 if you do not want this display

var conf_showstatus = 1;
var conf_showlocalarea = 1;
var conf_showextrabuttons = 1;

// ------------------------------------------------------------------------

var conf_gmscriptversion = "2.34";
var conf_useragent = "GreaseMonkey/GM_xmlhttpRequest GM_SCM/2.34 (www.itechsc.com)";
var conf_serverscripturl = "http://www.itechsc.com/misc/shartak/ubermap/recordmapdata2.php";
var conf_mapposscripturl = "http://www.itechsc.com/misc/shartak/ubermap/mapposgen.php";
var conf_localareamapurl = "http://www.itechsc.com/misc/shartak/ubermap/closeup.php";

// ------------------------------------------------------------------------

var estimatedalt = 0;

send_data_to_server();

function send_data_to_server() {

  var mapurl = get_map_urldata();
  var gpsurl = get_gps_urldata();
  var desurl = get_des_urldata();

  if (mapurl == '' && gpsurl == '') {
    show_sum_status('error', 'No GPS detected and terrain ummappable');
    return;
  } else if (mapurl == '') {
    show_sum_status('error', 'Terrain not mappable');
    return;
  } else if (gpsurl == '') {
    show_sum_status('error', 'No GPS detected');
    return;
  }

  if (mapurl && gpsurl) {
    var url = conf_serverscripturl;
    var formdata = "gmsv=" + conf_gmscriptversion + gpsurl + mapurl + desurl;
    GM_xmlhttpRequest( { method: 'POST', headers: { "User-Agent": conf_useragent, "Content-type": "application/x-www-form-urlencoded" }, url: url, data: formdata, onerror: function(r) { show_sum_status('error', 'HTTP send error: ' + r.responseText); }, onload: function(r) { show_sum_status('ssf', r.responseText); } } );
  }

  show_extrabuttons(gpsurl, estimatedalt);
  show_localarea(gpsurl, estimatedalt);

}

function get_map_urldata () {

  var mapdataurl = "";

  var maptdtags = document.evaluate("//table//tr//td//table//tr//td", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  var mapdivtags = document.evaluate("//table//tr//td//table//tr//td//div", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  var maph4tags = document.evaluate("//table//tr//td//table//tr//td//span[@class='blockname']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

  if (maptdtags.snapshotLength != 25 || maph4tags.snapshotLength != 25) {
    return "";
  }

  for (var i = 0; i < maptdtags.snapshotLength; i++) {
    mapdataurl += '&info' + i + '=' + maptdtags.snapshotItem(i).getAttribute('class').replace(/^map\s*/ig,"");
    mapdataurl += ' ' + mapdivtags.snapshotItem(i).getAttribute('class');
  }

  for (var i = 0; i < maph4tags.snapshotLength; i++) {
    mapdataurl += '&desc' + i + '=' + maph4tags.snapshotItem(i).innerHTML;
    if (maph4tags.snapshotItem(i).innerHTML == 'Mid-air') {
      estimatedalt = 1;
    }
  }

  return mapdataurl;

}

function get_gps_urldata () {

  var gpscoords = "";

  var gpsformtags = document.evaluate("//form//input[@value='gps unit']//parent::form", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  if (gpsformtags.snapshotLength) {
    //gpscoords = '&gpscoords=' + gpsformtags.snapshotItem(0).textContent.replace(/[\[\]\.\s\+]/ig,"");
    gpscoords = '&gpscoords=' + gpsformtags.snapshotItem(0).textContent.replace(/\(charge.+\)/,"").replace(/[\[\]\.\s\+]/ig,"");
  } else {
    return "";
  }

  return gpscoords;

}

function get_des_urldata () {

  var desdataurl = "";

  var descptag = document.evaluate("//table//tr//td//p", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  if (!descptag.snapshotLength) {
    return "";
  }
  
  desdataurl = "&edesc12=" + descptag.snapshotItem(0).innerHTML;

  return(desdataurl);

}

function show_sum_status (statustype, statusmsg) {

  if (conf_showstatus != 1) {
    return;
  }

  var buttonsdivtag = document.evaluate("//div[@class='buttons']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  if (buttonsdivtag.snapshotLength == 1) {
    var newele = document.createElement('div');
    buttonsdivtag.snapshotItem(0).parentNode.insertBefore(newele, buttonsdivtag.snapshotItem(0).nextSibling);
    var msg = '<b>Shartak UBER MAPPER: ';
    if (statustype == 'error') { msg += '<font size=2 color=#881111>' + statusmsg + '</font>'; }
    else if (statustype == 'info') { msg += '<font size=2 color=#118811>' + statusmsg + '</font>'; }
    else if (statustype == 'ssf') { msg += statusmsg; }
    newele.innerHTML = msg;
  } else {
    GM_log('Shartak UBER MAPPER script: status display error');
  }

  return;

}

function show_localarea (gpsurl, altitude) {

  if (conf_showlocalarea != 1) {
    return;
  }

  var coords = gpsurl.match(/-?\d+/g);

  if (coords.length != 2) {
    return;
  }

  var localarearequesturl = conf_localareamapurl + '?sk=1&sc=1&cx=' + coords[0] + '&cy=' + coords[1] + '&bc=3&sh=1&dw=420&dh=300&aw=25&ht=1&alt=' + altitude;
  GM_xmlhttpRequest( { method: 'GET', headers: { "User-Agent": conf_useragent }, url: localarearequesturl, onload: function(r) { var htmlmap = r.responseText; show_localarea2(r.responseText); } } );

}

function show_localarea2 (htmlmap) {

  var buttonsdivtag = document.evaluate("//div[@class='buttons']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  if (buttonsdivtag.snapshotLength == 1) {
    var newele = document.createElement('div');
    buttonsdivtag.snapshotItem(0).parentNode.insertBefore(newele, buttonsdivtag.snapshotItem(0).nextSibling);
    newele.innerHTML = htmlmap;
  } else {
    GM_log('Shartak UBER MAPPER script: status display error');
  }

  return;

}

function show_extrabuttons (gpsurl, altitude) {

  if (conf_showextrabuttons != 1) {
    return;
  }

  var coords = gpsurl.match(/-?\d+/g);

  var newbuttonshtml = '';

  if (coords.length == 2) {
    newbuttonshtml = newbuttonshtml + '<a class="txbutton" href="' + conf_mapposscripturl + '?cx=' + coords[0] + '&cy=' + coords[1] + '">Satellite</a>' + "\n";
    newbuttonshtml = newbuttonshtml + '<a class="txbutton" href="' + conf_localareamapurl + '?sk=1&sc=1&cx=' + coords[0] + '&cy=' + coords[1] + '&dn=1&zn=1&alt=' + altitude + '">Local Map</a>' + "\n";
  }

  var buttonsdivtag = document.evaluate("//div[@class='buttons']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  if (buttonsdivtag.snapshotLength == 1) {
    var buttonshtml = buttonsdivtag.snapshotItem(0).innerHTML;
    buttonsdivtag.snapshotItem(0).innerHTML = buttonshtml + newbuttonshtml;
  } else {
    GM_log('Shartak UBER MAPPER script: status display error');
  }  

  return;

}

// ------------------------------------------------------------------------
