#!/usr/bin/tclsh

#
# TCL scripting by Tihomir Heidelberg 9a4gl@9a0tcp.ampr.org (c) 2005
#

package require ncgi
load ./wwl.so

source ./define.tcl
source ./nodes.tcl
source ./links_plan.tcl
source ./users.tcl
source ./utils.tcl
source ./io.tcl
source ./nodes.tcl
source ./show_nodes.tcl
source ./show_links.tcl
source ./create_icons.tcl
source ./qth.tcl
source ./links_plan.tcl

load_arrays "result_check.txt" "::RESULT ::IFACE_IP ::IFACE_MAC ::ARP_IP ::ARP_MAC"
load_one_list "result_links.txt" "::LINKS"

set variables [::ncgi::input]
foreach v $variables {
	if { $v == "type" || $v == "x" || $v == "y" || $v == "z" || $v == "show" || $v == "username" || $v == "password" || $v == "refresh_time" || $v == "network" || $v == "node" } {
		set $v [::ncgi::value $v]
	}
}

if { ![info exists type] } {
	set type "hybrid"
}
if { ![info exists show] } {
	set show "public"
}
if { ![info exists x] } {
  set x "15.9592"
}
if { ![info exists y] } {
  set y "45.7820"
}
if { ![info exists username] } {
  set username ""
}
if { ![info exists password] } {
  set password ""
}
if { ![info exists refresh_time] } {
	set refresh_time 600
}
if { ![info exists network] } {
	set network ""
}

set zoom "9"

if { [info exists node] } {
	if { [info exists nodes($node)] } {
		set node $nodes($node)
		set x [lindex $node 1]
		set y [lindex $node 2]
		set zoom "14"
	}
}

if { ![info exists z] } {
  set z $zoom
}

proc check_username_password { } {
	if { [info exists ::users($::username)] } {
		if { [lindex $::users($::username) 0] == $::password } {
			set ::logged_in "1"
			set ::perms [lindex ::users($::username) 1]
			return "1"
		}
	}
	return "0"
}


set logged_in "0"
set perms     ""

check_username_password

puts "Content-type: text/html\n"

puts "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
# Below xml namespace is needed to make GPolyline work in IE7 (dont ask me why)
puts "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:v=\"urn:schemas-microsoft-com:vml\">"
puts "  <head>"
puts "    <title>Map of 9A Digital Radio Network</title>"
set protocol "https"
if { [info exists env(SERVER_PORT)] } {
	if { $env(SERVER_PORT) == "80" } {
		set protocol "http"
	}
}
set refreshUrl "${protocol}://ham2.cc.fer.hr:${env(SERVER_PORT)}/wireless/map/index.html"
if { [info exists env(HTTP_HOST)] && [info exists env(REQUEST_URI)] } {
	set refreshUrl "${protocol}://$env(HTTP_HOST)$env(REQUEST_URI)"
}
puts "    <style type=\"text/css\">"
puts "    v\\:* \{"
puts "      behavior:url(\#default\#VML);"
puts "    \}"
puts "    </style>"
puts "    <style type=\"text/css\">"
if { $protocol == "https" } {
	puts "      body \{ padding: 0px; margin-left: 3%; margin-right: 3%; margin-top: 3%; margin-bottom: 3% \}"
} else {
	puts "      body \{ padding: 0px; margin-left: 0%; margin-right: 0%; margin-top: 0%; margin-bottom: 0% \}"
}
puts "    </style>"
if { $protocol == "http" } {
	puts "    <style type=\"text/css\">"
	puts "    <!--"
	puts "      html,body \{"
	puts "         overflow:hidden;"
	puts "      \}"
	puts "    -->"
	puts "    </style>"
	puts "    <script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAASbddvPpzG_Qjo0Xu1ZiQdhR8hIkqBiMDhQWGRkNanJ5b1jthkhT3ZQd2edWU8c_0Kkgu17aAyhhnfQ\" type=\"text/javascript\"></script>"
} else {
	puts "    <script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAASbddvPpzG_Qjo0Xu1ZiQdhRpIfl_zsJqn2jFeTeQBs-uB_KK4xSyvMzFAz6L1ONJTAdIGFhgNnFVxA\" type=\"text/javascript\"></script>"
}
puts "    <link rel=\"stylesheet\" type=\"text/css\" href=\"mapStyle2.css\" />"
puts "    <script src=\"utilities.js\"></script>"
puts "    <script src=\"customOverlays.js\"></script>"

puts "    <script type=\"text/javascript\">"
puts "      //<!\[CDATA\["
puts "      var secs;"
puts "      var timerID = null;"
puts "      var timerRunning = false;"
puts "      var delay = 1000;"
puts "      var map;"
puts "      function onLoad() \{"
create_icons
puts "        map = new GMap2(document.getElementById(\"map\"));"
puts "        var cenLatLng = new GLatLng($y, $x)";
if { $type == "satellite" } {
  puts "        map.setCenter(cenLatLng, $z, G_SATELLITE_MAP);"
} elseif { $type == "hybrid" } {
  puts "        map.setCenter(cenLatLng, $z, G_HYBRID_MAP);"
}
puts "        map.addControl(new GLargeMapControl());"
puts "        map.addControl(new GMapTypeControl());"
puts "        map.addControl(new GOverviewMapControl());"
puts "        var bounds = map.getBounds();"
puts "        var width = bounds.maxX - bounds.minX;"
puts "        var height = bounds.maxY - bounds.minY;"
show_nodes
show_links
puts "      \}"
puts "      function InitializeTimer()"
puts "      \{"
puts "        secs = ${::refresh_time};"
puts "        StopTheClock()";
puts "        StartTheTimer();"
puts "      \}"
puts "      function StopTheClock()"
puts "      \{"
puts "        if(timerRunning)"
puts "          clearTimeout(timerID);"
puts "        timerRunning = false;"
puts "      \}"
puts "      function StartTheTimer()"
puts "      \{"
puts "        if (secs==0)"
puts "        \{"
puts "           StopTheClock();"
puts "           var center = map.getCenter();"
puts "           document.forms.Standard.x.value = center.x;"
puts "           document.forms.Standard.y.value = center.y;"
puts "           document.forms.Standard.z.value = map.getZoom();"
puts "           document.forms.Standard.submit();"
puts "        \}"
puts "        else"
puts "        \{"
puts "          self.status = \"Next refresh in \" + secs + \" seconds\";"
puts "          secs = secs - 1;"
puts "          timerRunning = true;"
puts "          timerID = self.setTimeout(\"StartTheTimer()\", delay)"
puts "        \}"
puts "      \}"
puts "      //]]>"
puts "    </script>"
puts "    <script type=\"text/javascript\">"
puts "      function getWindowDimension() \{"
puts "        var myWidth = 0, myHeight = 0;"
puts "        if( typeof( window.innerWidth ) == 'number' ) \{"
puts "          //Non-IE"
puts "          myWidth = window.innerWidth;"
puts "          myHeight = window.innerHeight;"
puts "        \} else if( document.documentElement \&\&"
puts "          ( document.documentElement.clientWidth \|\|"
puts "            document.documentElement.clientHeight ) ) \{"
puts "          //IE 6+ in standards compliant mode"
puts "          myWidth = document.documentElement.clientWidth;"
puts "          myHeight = document.documentElement.clientHeight;"
puts "        \} else if( document.body \&\& ( document.body.clientWidth \|\|"
puts "          document.body.clientHeight ) ) \{"
puts "          //IE 4 compatible"
puts "          myWidth = document.body.clientWidth;"
puts "          myHeight = document.body.clientHeight;"
puts "        \}"
if { $protocol == "https" } {
	puts "        myWidth = myWidth * 0.92;"
	puts "        myHeight = myHeight * 0.80;"
}
puts "        var ret = \"width: \" + myWidth + \"px; height: \" + myHeight + \"px\";"
puts "        return ret;"
puts "      \}"
puts "    </script>"
puts "  </head>"
puts "  <body onload=\"onLoad();InitializeTimer()\" onunload=\"GUnload()\" style=\"overflow:hidden;\">"
if { $protocol == "https" } {
	puts "    <form id=\"Standard\" action=\"${refreshUrl}\" method=\"post\"><div style=\"text-align: center; margin-bottom: 10px;\">"
	puts "      <input type=\"hidden\" name=\"x\" value=\"${x}\"/>"
	puts "      <input type=\"hidden\" name=\"y\" value=\"${y}\"/>"
	puts "      <input type=\"hidden\" name=\"z\" value=\"${z}\"/>"
	if { $::logged_in == "1" } {
		puts "      Hello <b>${username}</b>, you are logged in."
		puts "      <input type=\"hidden\" name=\"username\" value=\"${username}\" size=\"10\"/>"
		puts "      <input type=\"hidden\" name=\"password\" value=\"${password}\" size=\"10\"/>"
		puts "      <input type=\"submit\" name=\"login\" value=\"Refresh\"/>"
	} else {
  	puts "      Username : <input type=\"text\" name=\"username\" size=\"10\"/> Password : "
  	puts "      <input type=\"password\" name=\"password\" size=\"10\"/>"
  	puts "      <input type=\"submit\" name=\"login\" value=\"Login\"/>"
	}
	puts "    </div></form>"
}
puts "    <script type=\"text/javascript\"><!--"
puts "      document.write(\"<div id=\\\"map\\\" style=\\\"\" + getWindowDimension() + \"\\\"><\\/div>\");"
puts "    --></script>"
puts "  </body>"
puts "</html>"

