
/***************************************************************************
*   Copyright (C) 2006, Paul Lutus                                        *
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
*   This program is distributed in the hope that it will be useful,       *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
*   GNU General Public License for more details.                          *
*                                                                         *
*   You should have received a copy of the GNU General Public License     *
*   along with this program; if not, write to the                         *
*   Free Software Foundation, Inc.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

function getCookie(path)
{
   var result = "";
   var a = document.cookie.indexOf(path);
   if(a != -1) {
      a += path.length + 1;
      var b = document.cookie.indexOf(";",a);
      if(b == -1) {
         b = document.cookie.length;
      }
      result = unescape(document.cookie.substring(a,b));
   }
   return result;
}

var cookie_array = null;
function processCookie()
{
   var cookie = getCookie(window.location.pathname);
   if (cookie != ''){
      cookie_array = cookie.split(" ");
   }
}

function makeCookie() {
   var values = ((document.getElementById("ampm1").checked)?"1":"0");
   values += " " + ((document.getElementById("daylight1").checked)?"1":"0");
   var exp = new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000);
   document.cookie=window.location.pathname + "=" + values + "; expires=" + exp.toGMTString();
}

var locations=["Samoa", "Hawaii", "Juneau","San Francisco"
,"Denver","Chicago","New York","Caracas"
,"Rio De Janeiro","Recife","Azores","London"
,"Paris","Cairo","Moscow","Baku"
,"Karachi","Dacca","Bangkok","Hong Kong"
,"Tokyo","Sydney","Noumea","Wellington"];
var thisLocation="Sydney";

function setup()
{
   processCookie();
   setup_disp();
   if(cookie_array) {
      document.getElementById("ampm" + cookie_array[0]).checked = true;
      document.getElementById("daylight" + cookie_array[1]).checked = true;
   }
   update_clock();
}

function setup_disp()
{
   s = "<table width=\"90%\" align=\"center\" class=\"clock\" style=\"font-size:11px; font-weight:bold; color:#c9151c\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
   s += "<tr style=\"display:none\"><td class=\"ccm\" colspan=\"2\">";
   s += "<input id=\"ampm0\" type=\"radio\" name=\"ampm\" checked=\"checked\">24 Hour&nbsp;";
   s += "<input id=\"ampm1\" type=\"radio\" name=\"ampm\">AM/PM";
   s += "</td><td class=\"ccm\">";
   s += "<input id=\"daylight0\" type=\"radio\" name=\"daylight\" checked=\"\">Standard&nbsp;";
   s += "<input id=\"daylight1\" type=\"radio\" name=\"daylight\" checked=\"checked\">Daylight";
   s += "</td></tr>";
   s += "<tr><td height=\"10\"></td></tr><tr>";
   daylight = (cookie_array && cookie_array[1] == "1")?1:0;
   offset = (new Date().getTimezoneOffset()/60) + daylight;
   for(i = 0; i < 24;i++) {
      q = "tz" + i;
      j = i-11;
      si = "" + Math.abs(j)
      if(si.length < 2) si = "0" + si;
      si = ((j < 0)?"-":"+") + si;
      
      if ((locations[i] == "Sydney")||
          (locations[i] == "Hong Kong")||
          (locations[i] == "London")||
          (locations[i] == "New York")) {
          
          //if (locations[i] == "Sydney") {
            cls = "style=\"text-align:right; padding:2px\""
          //}
        
      
        clsDiv = "style=\"text-align:center; width:1px\""
        
      } else {
        cls = "style=\"display:none\""
        clsDiv = "style=\"display:none\""
      }
      
        s += "<td "+ cls +" align=\"right\" nowrap style=\"padding:2px\">" + locations[i] + "&nbsp;&nbsp;</td>";
        s += "<td "+ cls +" nowrap align=\"left\" id=\"v" + i + "\"></td>";
        s += "<td "+ clsDiv +" align=\"center\">&nbsp;|&nbsp;</td>";
   }
   s += "</tr></table>";
   document.getElementById("clock_disp").innerHTML = s;

}

function lz(v)
{
   return (v < 10)?"0" + v:v;
}

function formatDate(d)
{
   s = ""
   h = d.getHours();
   if (document.getElementById("ampm1").checked) {
      ap = (h >= 12)?"PM":"AM";
      h = (h % 12);
      if(h == 0) h = 12;
      s += lz(h)
      + ":" + lz(d.getMinutes())
      + ":" + lz(d.getSeconds())
      + " " + ap;
   }
   else {
      s += lz(h)
      + ":" + lz(d.getMinutes())
      //+ "&nbsp; | &nbsp;";
   }
   return s;
}

var old_offset = -1;
var hour = 3600000; // one hour in milliseconds
function update_clock() {
   gmt = new Date();
   d = new Date();
   offset = d.getTimezoneOffset()/60;
   // add daylight hour if specified
   daylight = (document.getElementById("daylight1").checked)?1:0;
   // set initial TZ to GMT-11 
   d.setTime(d.getTime() + ((offset-11) * hour));
   //offset += daylight;
   // create time zone outputs
   for(i = -11;i <= 12;i++) {
      var thisDate;
      if(locations[i+11]==thisLocation){
          thisDate=formatDate(new Date());
      }
      else{
          thisDate=formatDate(d);
      }
      document.getElementById("v" + (i+11)).innerHTML = thisDate;
      if(old_offset != offset) {
         color=(i == -offset)?"#f0f0ff":"#ffffe0";
         //document.getElementById("row" + (i+11)).style.background = color;
      }
      d.setTime(d.getTime() + hour);
   }
   old_offset = offset;
   setTimeout('update_clock()', 990);
}

window.onload = setup;
window.onunload = makeCookie;

