/***************************************************************************
*   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();
}*/
function makeCookie() {
   var values = "1";
   values += " " + "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"];

function setup()
{
   processCookie();
   setup_disp();
   setup_disp1();
   if(cookie_array) {
  //    document.getElementById("ampm" + cookie_array[0]).checked = true;
 //     document.getElementById("daylight" + cookie_array[1]).checked = true;
   }
   update_clock();
   //update_clock1();
}

function setup_disp()
{
   s = "<table border=0 cellpadding=\"0\" cellspacing=\"0\" border=\"1\" bordercolor=\"#c0c0c0\" bgcolor=#ffffe0>";
 //  s += "<tr><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=\"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 += "<td class=\"ccb\" >Date/Time</td></tr>"; 
   daylight = (cookie_array && cookie_array[1] == "1")?1:0;
   offset = (new Date().getTimezoneOffset()/60) + daylight;
   //for(i = 0; i < 24;i++) {
   for(i = 19; i < 20;i++) {
      q = "tz" + i;
      j = i-11;
      si = "" + Math.abs(j)
      if(si.length < 2) si = "0" + si;
      si = ((j < 0)?"-":"+") + si;
     // mod = (i-11 == -offset)?" bgcolor=\"#f0f0ff\"":"";
	  mod = (i-11 == -offset)?" bgcolor=\"#f0f0ff\"":"";
 
 	//  s += "<tr" + mod + " id=\"row" + i + "\"><td class=\"cc\">UTC" + si + "</td>";
    //  s += "<td class=\"cc\">" + locations[i] + "</td>"; 
      s += "<td class=\"ccm\" id=\"v" + i + "\"></td></tr>"; 
	
   }
   s += "</table>";
   document.getElementById("clock_disp").innerHTML = s;
}

function setup_disp1()
{
   s = "<table border=0 cellpadding=\"0\" cellspacing=\"0\" border=\"1\" bordercolor=\"#c0c0c0\" bgcolor=#ffffe0>";
 //  s += "<tr><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=\"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 += "<td class=\"ccb\" >Date/Time</td></tr>"; 
   daylight = (cookie_array && cookie_array[1] == "1")?1:0;
   offset = (new Date().getTimezoneOffset()/60) + daylight;
   //for(i = 0; i < 24;i++) {
   for(i = 6; i < 7;i++) {
      q = "tz" + i;
      j = i-11;
      si = "" + Math.abs(j)
      if(si.length < 2) si = "0" + si;
      si = ((j < 0)?"-":"+") + si;
     // mod = (i-11 == -offset)?" bgcolor=\"#f0f0ff\"":"";
	  mod = (i-11 == -offset)?" bgcolor=\"#f0f0ff\"":"";
 
 	//  s += "<tr" + mod + " id=\"row" + i + "\"><td class=\"cc\">UTC" + si + "</td>";
    //  s += "<td class=\"cc\">" + locations[i] + "</td>"; 
      s += "<td class=\"ccm\" id=\"vi" + i + "\"></td></tr>"; 
	
   }
   s += "</table>";
   document.getElementById("clock_disp1").innerHTML = s;
}

function lz(v)
{
   return (v < 10)?"0" + v:v;
}

function lz1(v)
{
   return (v < 10)?"" + v:v;
}

function formatDate(d)
{
	
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

var monthvalue=new Array(12);
monthvalue[1]="January";
monthvalue[2]="February";
monthvalue[3]="March";
monthvalue[4]="April";
monthvalue[5]="May";
monthvalue[6]="June";
monthvalue[7]="July";
monthvalue[8]="August";
monthvalue[9]="September";
monthvalue[10]="October";
monthvalue[11]="November";
monthvalue[12]="December";

/*msia format */
   s = weekday[parseInt(lz(d.getDay()))]
    + " " + lz(d.getDate()) 
   + "/" + lz((d.getMonth()+1))
   + "/" + d.getFullYear() + " ";
   h = d.getHours(); 
/* */

	/*  
   s = weekday[parseInt(lz(d.getDay()))]
   + ", " + monthvalue[parseInt(lz((d.getMonth()+1)))]
   + " " + lz(d.getDate())    
   + ", " + d.getFullYear() + " ";
   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())
      + ":" + lz(d.getSeconds());
   } */
   return s;
}

function formatDate1(d)
{
	
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";

var monthvalue1=new Array(12);
monthvalue1[1]="January";
monthvalue1[2]="February";
monthvalue1[3]="March";
monthvalue1[4]="April";
monthvalue1[5]="May";
monthvalue1[6]="June";
monthvalue1[7]="July";
monthvalue1[8]="August";
monthvalue1[9]="September";
monthvalue1[10]="October";
monthvalue1[11]="November";
monthvalue1[12]="December";

/*msia format 
   s = weekday[parseInt(lz(d.getDay()))]
    + " " + lz(d.getDate()) 
   + "/" + lz((d.getMonth()+1))
   + "/" + d.getFullYear() + " ";
   h = d.getHours(); 
 */

/*	var bulanan = lz((d.getMonth()+1));
	
	if (bulanan == "10") {
	   bulanan = "10";
	} else {
		bulanan = bulanan.replace("0","");
	} */
	
   s = weekday[parseInt(lz(d.getDay()))]
   + ", " + monthvalue1[parseInt(lz1((d.getMonth()+1)))] //sini guna lz1 dan bukan function lz
   + " " + lz(d.getDate())    
   + ", " + d.getFullYear() + " ";
   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())
      + ":" + lz(d.getSeconds());
   } */
   return s;
}

var old_offset = -8;
var hour = 3600000; // one hour in milliseconds
function update_clock() {
	
//var old_offset = -8;
//var hour = 3600000; // one hour in milliseconds

   d = new Date();
   offset = d.getTimezoneOffset()/60;
   // add daylight hour if specified
   daylight = 0 ;//(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 <= 8;i++) {
     //for(i = 8;i <= 8;i++) {
		 
		if (i==-5 ) {
      document.getElementById("vi" + (i+11)).innerHTML = formatDate1(d) + " (Dallas, USA)"  ;
		}		 
		if (i==8 ) {
      document.getElementById("v" + (i+11)).innerHTML = formatDate(d) + " (Kuala Lumpur, Malaysia)"  ;
		}
   /*   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);
} 

/*

function update_clock1() {
	
var old_offset = -8;
var hour = 3600000; // one hour in milliseconds

   d = new Date();
   offset = d.getTimezoneOffset()/60;
   // add daylight hour if specified
   daylight = 0 ;//(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 <= -5;i++) {
     //for(i = 8;i <= 8;i++) {
		if (i==-5 ) {
      document.getElementById("vi" + (i+11)).innerHTML = formatDate1(d) + " (Dallas, USA)"  ;
		}
      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_clock1()', 990);
}  */

window.onload = setup;
window.onunload = makeCookie;

