var clockID = 0;
var timezone = 0;
var offset = "";
var tzoffset = "";
var entries = 65;
var criteria = "";

var zone = new MakeArray(entries);

function MakeArray(n) {
   for (var i = 1; i <= n; i++) { this[i] = 0; }
   this.maxlen = n; this.len = 0; return this;
}

function addZone(zonename, timediff) {
  this.name = zonename; this.time = timediff;
}

function Add(name, time) {
  zone.len++; zone[zone.len] = new addZone(name, time);
}

/////////////////////////////////////////////////////////////////////////////
// Add Details for each timezone.
// ie. Add("Timezone Name", "Time Difference");

Add("Dateline - Eniwetok, Kwajalein", "-12.00");
Add("Samoa - Midway Island", "-11.00");
Add("Hawaii", "-10.00");
Add("Alaska", "-9.00");
Add("Pacific Time (US & Canada); Tijuana", "-8.00");
Add("US Mountain - Arizona", "-7.00");
Add("Mountain Time (US & Canada)", "-7.00");
Add("Central Time (US & Canada)", "-6.00");
Add("Mexico City, Tegucigalpa", "-6.00");
Add("Canada Central - Saskatchewan", "-6.00");
Add("SA Pacific - Bogota, Lima, Quito", "-5.00");
Add("Eastern Time (US & Canada)", "-5.00");
Add("US Eastern - Indiana (East)", "-5.00");
Add("Atlantic Time (Canada)", "-4.00");
Add("SA Western - Caracas, La Paz", "-4.00");
Add("Newfoundland", "-3.50");
Add("E. South America - Brasilia", "-3.00");
Add("SA Eastern - Buenos Aires, Georgetown", "-3.00");
Add("Mid-Atlantic", "-2.00");
Add("Azores, Cape Verde Is.", "-1.00");
Add("GMT: Casablanca, Monrovia", "+0.00");
Add("GMT: Dublin, Edinburgh, Lisbon, London", "+0.00");
Add("Romance - Amsterdam, Copenhagen, Madrid, Paris, Vilnius", "+1.00");
Add("Central European - Belgrade, Sarajevo, Skopje, Sofija, Zagreb", "+1.00");
Add("Central Europe - Bratislava, Budapest, Ljubljana, Prague, Warsaw", "+1.00");
Add("W. Europe - Brussels, Berlin, Bern, Rome, Stockholm, Vienna", "+1.00");
Add("GFT - Athens, Istanbul, Minsk", "+2.00");
Add("E. Europe - Bucharest", "+2.00");
Add("Egypt - Cairo", "+2.00");
Add("South Africa - Harare, Pretoria", "+2.00");
Add("FLE - Helsinki, Riga, Tallinn", "+2.00");
Add("Israel", "+2.00");
Add("Saudi Arabia - Baghdad, Kuwait, Riyadh", "+3.00");
Add("Russian - Moscow, St. Petersburg, Volgograd", "+3.00");
Add("E. Africa - Nairobi", "+3.00");
Add("Iran - Tehran", "+3.50");
Add("Arabian - Abu Dhabi, Muscat", "+4.00");
Add("Caucasus - Baku, Tbilisi", "+4.00");
Add("Afghanistan - Kabul", "+4.50");
Add("Ekaterinburg", "+5.00");
Add("West Asia - Islamabad, Karachi, Tashkent", "+5.00");
Add("India - Bombay, Calcutta, Madras, New Delhi", "+5.50");
Add("Central Asia - Almaty, Dhaka", "+6.00");
Add("Sri Lanka - Colombo", "+6.00");
Add("Bangkok, Hanoi, Jakarta", "+7.00");
Add("China - Beijing, Chongqing, Hong Kong, Urumqi", "+8.00");
Add("W. Australia - Perth", "+8.00");
Add("Singapore", "+8.00");
Add("Taipei", "+8.00");
Add("Tokyo - Osaka, Sapporo", "+9.00");
Add("Korea - Seoul", "+9.00");
Add("Yakutsk", "+9.00");
Add("Cen. Australia - Adelaide", "+9.50");
Add("AUS Central - Darwin", "+9.50");
Add("E. Australia - Brisbane", "+10.00");
Add("Sydney - Canberra, Melbourne", "+10.00");
Add("West Pacific - Guam, Port Moresby", "+10.00");
Add("Tasmania - Hobart", "+10.00");
Add("Vladivostok", "+10.00");
Add("Central Pacific - Magadan, Solomon Is., New Caledonia", "+11.00");
Add("New Zealand - Auckland, Wellington", "+12.00");
Add("Fiji, Kamchatka, Marshall Is.", "+12.00");

// End of timezone entries.
/////////////////////////////////////////////////////////////////////////////

function searchTimeZone() {
   var doc = document.forms['clock'];
   doc.timezone.selectedIndex = 0;
   document.clock.date.value = "";
   document.clock.time.value = "";
   KillClock();
   criteria = prompt("Enter the time zone you are looking for?", criteria);
   if (criteria) criteria = criteria.toLowerCase();
   zonefound = 0;
   for (i=1; i<=zone.len; i++) {
      zonefind = zone[i].name.toLowerCase();
      if (zonefind.indexOf(criteria) >= 0 && (!zonefound) && (criteria)) {
         doc.timezone.selectedIndex = i;
         timezone = parseFloat(zone[i].time);
         StartClock();
         zonefound++;
      }
   }
   if (!zonefound && (criteria)) {
      alert("Unable to find the specified time zone.");
      setTimeout("searchTimeZone()", 200);
   }
}

function changeTimeZone(tz) {
   var doc = document.forms['clock'];
   var newzone = doc.timezone.options[doc.timezone.selectedIndex].text
   KillClock();
   if (!doc.timezone.selectedIndex) {
       KillClock();
       document.clock.date.value = "";
       document.clock.time.value = "";
   }else{
       newzone = zone[doc.timezone.selectedIndex].name;
       timezone = parseFloat(tz);
       StartClock();
   }
}

function UpdateClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
   var tDate = new Date();
   tDate.setTime(tDate.getTime() + (timezone * 3600000)); tDate.toGMTString();
   newtime = tDate.toGMTString(); newtime = newtime.substring(0,25);
   ndate = newtime.substring(0,16); ntime = newtime.substring(17,19);
   otime = newtime.substring(19); ohour = parseInt(ntime.substring(0,2));
   ampm = (parseInt(ohour) < 12) ? " AM" : " PM";
   nhour = (parseInt(ntime) >= 13) ? "" + (parseInt(ntime) - 12) + "" : ntime;
   if(ntime == "00" || ntime == "0") { nhour = "12"; }
   if (nhour.substring(0,1) == "0") nhour = nhour.substring(1,2);
   if (nhour.length == 1) nhour = " " + nhour;
   ntime = nhour + otime + ampm;
   document.clock.date.value = ndate;
   document.clock.time.value = ntime;
   clockID = setTimeout("UpdateClock()", 1000);
}

function StartClock() {
   clockID = setTimeout("UpdateClock()", 200);
}

function KillClock() {
   if(clockID) {
      document.clock.date.value = "";
      document.clock.time.value = "";
      clearTimeout(clockID);
      clockID  = 0;
   }
}

function howToUse() {
    var installmsg = '<font face="MS Sans Serif,Arial,Helvetica" size=-1>It\'s simple to install this world clock onto your ';
    installmsg += 'web page, just copy and paste the following lines to anywhere on your page;</font><p>\n\n';
    installmsg += '<tt><font face="MS Sans Serif,Arial,Helvetica" size=-2 color=blue>&lt;script language="JavaScript" src="http://www.whitemagic.com.au/worldtime/worldclock.js"&gt;\n';
    installmsg += '<br>&lt;/script&gt;</font></tt><p>\n\n';
    instructions = document.open('', 'instructions', 'width=500,height=100,scrollbars=no,menubar=no');
    instructions.document.write('<body onLoad="self.focus()">\n');
    instructions.document.write(installmsg);
    instructions.document.close();
}

document.write('<table border=0 cellpadding=0 cellspacing=0><caption><b>');
document.write('<font face="Arial Black,Arial,Helvetica,MS Sans Serif" size=+4>');
document.write('World Time Clock</font></b></caption><tr><td colspan=5><center><form name="clock">');
document.write('<font face="MS Sans Serif,Arial,Helvetica" size=-2>Select the timezone from the list or ');
document.write('click the image to search for a specific timezone.</font></center><hr width=400></td></tr>');
document.write('<tr><td rowspan=2><a href="#" onClick="searchTimeZone()"><img src="http://www.whitemagic.com.au/worldtime/globe.gif"');
document.write(' border=0 align=LEFT ></a></td><td colspan=4>\n');
document.write('<select name=timezone onChange="changeTimeZone(this.options[this.selectedIndex].value)">');
document.write('<option value=0>Select a timezone to display the current time</option>');
// document.write('<option value=0>Search for a specific time zone</option>');
for (i=1; i<=zone.len; i++) {
    offset = zone[i].time.split(".");
    hoffset = offset[0];
    moffset = (parseInt(offset[1])) ? "30" : "00";
    tzoffset = hoffset + ":" + moffset 
 
    document.write('<option value="' + zone[i].time + '">' + zone[i].name);
    if (parseInt(zone[i].time)) document.write(" GMT(" + tzoffset + ")");
    document.write('</option>\n');
}
document.write('</select></td></tr>\n');
document.write('<tr><td><font face="MS Sans Serif,Arial,Helvetica" size=-1>Current Time: </font></td>');
document.write('<td><input type=text name="time" size=11 onFocus="this.blur()"></td>\n');
document.write('<td><font face="MS Sans Serif,Arial,Helvetica" size=-1>Current Date: </font></td>');
document.write('<td><input type=text name="date" size=16 onFocus="this.blur()"> &nbsp;</td></tr>');
document.write('<tr><td colspan=5><center></form><font face="MS Sans Serif,Arial,Helvetica" size=-2>');
document.write('<hr width=400>This free services is brought to you by <a href="http://www.whitemagic.com.au" target="_blank">The Realm Of White Magic</a>.<br>');
document.write('<a href="#" onClick="howToUse()">Click here</a> to find out how to install this clock on ');
document.write('your web page.</font></center></td></tr>');
document.write('</table>\n</form>\n');
