/***************************************************************************
*   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","LondonWrong"
,"London","Spain","Moscow","Dubai"
,"Karachi","Dacca","Bangkok","Hong Kong"
,"Tokyo","Sydney","Noumea","Wellington"];


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 cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"300\">";
   s += "<tr style=\"display:none;\"><td class=\"contant\">";
   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=\"contant\">";
   s += "<input id=\"daylight0\" type=\"radio\" name=\"daylight\" checked=\"checked\">Standard&nbsp;";
   s += "<input id=\"daylight1\" type=\"radio\" name=\"daylight\">Daylight";
   s += "</td></tr>";
   //s += "<tr bgcolor=\"#ccffcc\"><td class=\"ccb\">Zone</td>";
   //s += "<td class=\"ccb\">Place</td>";
   //s += "</tr>";
   daylight = (cookie_array && cookie_array[1] == "1")?1:0;
   offset = (new Date().getTimezoneOffset()/60) + daylight;
   //alert(offset);
   s += "<tr>";
   for(i = 0; i < 24;i++) 
   {
      q = "tz" + i;
      j = i-11;
	  //alert(si);
      si = "" + Math.abs(j)
      if(si.length < 2) si = "0" + si;
      si = ((j < 0)?"-":"+") + si;
	  mod = (i-11 == -offset)?" background=\"images/menu_bg.jpg\"":"";
	   if(i==12 || i==13 || i==15)
		{
      s += "<td" + mod + " id=\"row" + i + "\" class=\"tax_date\" width=\"50\" style=\"text-align:right; padding-left:15px;\">";
	 // alert(s);
      //s += "<span class=\"contant\">";		
	  //alert(i)
	  	s += locations[i] +"&nbsp;";		
	  s += "</td>";
      s += "<td class=\"tax_date\" id=\"v" + i + "\"></td>";
	  }
   }
   s += "</tr>";
   s += "</table>";
   //alert(s)
   document.getElementById("clock_disp").innerHTML = s;
}

function lz(v)
{
   return (v < 10)?"0" + v:v;
}

function formatDate(d)
{
   //s = lz((d.getMonth()+1))
   //+ "/" + lz(d.getDate())
   //+ "/" + d.getFullYear() + " ";
   s='';
   //alert(d);   
   h = d.getHours();   
   //alert(h);   
   if (document.getElementById("ampm1").checked) {
      ap = (h >= 12)?"PM":"AM";
      h = (h % 12);
      if(h == 0) h = 12;
      s += lz(h)
      + "&nbsp;:&nbsp;" + lz(d.getMinutes())
    //  + "&nbsp;:&nbsp;" + lz(d.getSeconds())
      + " " + ap;
   }
   else {
      s += lz(h)
      + "&nbsp;:&nbsp;" + lz(d.getMinutes())
     // + "&nbsp;:&nbsp;" + lz(d.getSeconds());
   }
   return s;
}

var old_offset = -1;
var hour = 3600000; // one hour in milliseconds
function update_clock() {
   d = new Date();
   offset = d.getTimezoneOffset()/60;
   //alert(offset);
   // add daylight hour if specified
   daylight = (document.getElementById("daylight1").checked)?1:0;
   // set initial TZ to UTC-11
   offset += daylight;
   d.setTime(d.getTime() - (11 * hour) + offset * hour);
   
   // create time zone outputs
   /*for(i = -11;i <= 12;i++) {
      document.getElementById("v" + (i+11)).innerHTML = formatDate(d);
      if(old_offset != offset) {
         color=(i == -offset)?"#f0f0ff":"#ffffe0";
         document.getElementById("row" + (i+11)).style.background = color;
      }*/
	for(i = -11;i <= 12;i++) {		
		if(i==0 || i==1 || i==4)		
		{	
			if(i==0 || i==1)
			{
				j=i+1;				
			}
			else
			{
				j=i
			}
			document.getElementById("v" + (j+11)).innerHTML = formatDate(d);
		}
		
      d.setTime(d.getTime() + hour);
   }
   //document.getElementById("v12").innerHTML = d.setTime(d.getTime() + hour);
   old_offset = offset;
   setTimeout('update_clock()', 990);
}

window.onload = setup;
window.onunload = makeCookie;
