﻿function worldClock(zone, region){
var dst = 0
var time = new Date()
var gmtMS = time.getTime() + (time.getTimezoneOffset() * 60000)
var gmtTime = new Date(gmtMS)
var day = gmtTime.getDate()
var month = gmtTime.getMonth()
var year = gmtTime.getYear()
if(year < 1000){
year += 1900
}
var monthArray = new Array("Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December")
var monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
if (year%4 == 0){
monthDays = new Array("31", "29", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
}
if(year%100 == 0 && year%400 != 0){
monthDays = new Array("31", "28", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31")
}

var hr = gmtTime.getHours() + zone
var min = gmtTime.getMinutes()
var sec = gmtTime.getSeconds()

if (hr >= 24){
hr = hr-24
day -= -1
}
if (hr < 0){
hr -= -24
day -= 1
}
if (hr < 10){
hr = " " + hr
}
if (sec < 10){
sec = "0" + sec
}
if (day <= 0){
if (month == 0){
	month = 11
	year -= 1
	}
	else{
	month = month -1
	}
day = monthDays[month]
}
if(day > monthDays[month]){
	day = 1
	if(month == 11){
	month = 0
	year -= -1
	}
	else{
	month -= -1
	}
}

if (region == "NL"){
	var startDST = new Date()
	var endDST = new Date()
	startDST.setMonth(3)
	startDST.setHours(2)
	startDST.setDate(27)
	var dayDST = startDST.getDay()
	startDST.setDate(31-dayDST)
	endDST.setMonth(10)
	endDST.setHours(3)
	endDST.setDate(30)
	dayDST = endDST.getDay()
	endDST.setDate(31-dayDST)
	var currentTime = new Date()
	currentTime.setMonth(month)
	currentTime.setYear(year)
	currentTime.setDate(day)
	currentTime.setHours(hr)
	if(currentTime >= startDST && currentTime < endDST){
		dst = 1
		}
}

if (region == "Chile"){
	var startDST = new Date()
	var endDST = new Date()
	startDST.setMonth(9)
	startDST.setHours(0)
	startDST.setDate(10)
	var dayDST = startDST.getDay()
	if (dayDST != 0){
		startDST.setDate(22-dayDST)
		}
		else{
		startDST.setDate(15)
		}
	endDST.setMonth(3)
	endDST.setHours(0)
	endDST.setDate(4)
	dayDST = endDST.getDay()
	if (dayDST != 0){
		endDST.setDate(21-dayDST)
		}
		else{
		endDST.setDate(14)
		}
	var currentTime = new Date()
	currentTime.setMonth(month)
	currentTime.setYear(year)
	currentTime.setDate(day)
	currentTime.setHours(hr)
	if(currentTime >= startDST || currentTime < endDST){
		dst = 1
		}
}

if (region == "Mexico"){
	var startDST = new Date()
	var endDST = new Date()
	startDST.setMonth(4)
	startDST.setHours(2)
	startDST.setDate(3)
	var dayDST = startDST.getDay()
	startDST.setDate(31-dayDST)
	endDST.setMonth(10)
	endDST.setHours(2)
	endDST.setDate(30)
	dayDST = endDST.getDay()
	endDST.setDate(31-dayDST)
	var currentTime = new Date()
	currentTime.setMonth(month)
	currentTime.setYear(year)
	currentTime.setDate(day)
	currentTime.setHours(hr)
	if(currentTime >= startDST && currentTime < endDST){
		dst = 1
		}
}

if (region == "India"){

	min += 30
	if (min >= 60){
	min = min-60
	hr -= -1
	}
	if (hr >= 24){
	hr = hr-24
	day -= -1
	}
}
	
if (region == "Nepal"){

	min += 45
	if (min >= 60){
	min = min-60
	hr -= -1
	}
	if (hr >= 24){
	hr = hr-24
	day -= -1
	}	
}	
 

if (region == "NL"){

if (dst == 1){
	hr -= -1
	if (hr >= 24){
	hr = hr-24
	day -= -1
	}
	if (hr >= 24){
	hr = hr-24
	day -= -1
	}
	if (hr < 10){
	hr = " " + hr
	}
	if(day > monthDays[month]){
	day = 1
	if(month == 11){
	month = 0
	year -= -1
	}
	if (min < 10){
	min = "0" + min
	}
	else{
	month -= -1
	}
	}

	
	if (min < 10){
	min = "0" + min
	}
return hr + ':' + min
}
else{
if (min < 10){
	min = "0" + min
	}
return hr + ':' + min
}}

if (region == "seconden"){

return sec
}

else {

if (dst == 1){
	hr -= -1
	if (hr >= 24){
	hr = hr-24
	day -= -1
	}
	if (hr >= 24){
	hr = hr-24
	day -= -1
	}
	if (hr < 10){
	hr = " " + hr
	}
	if (min < 10){
	min = "0" + min
	}
	if(day > monthDays[month]){
	day = 1
	if(month == 11){
	month = 0
	year -= -1
	}
	else{
	month -= -1
	}
	}

if (min < 10){
	min = "0" + min
	}
	
return hr + ':' + min
}
else{

if (min < 10){
	min = "0" + min
	}
	
return hr + ':' + min
}}



}

function worldClockZone(){

document.getElementById("Belize").innerHTML = worldClock(-6, "")
document.getElementById("CostaRica").innerHTML = worldClock(-6, "")
document.getElementById("ElSalvador").innerHTML = worldClock(-6, "")
document.getElementById("Guatemala").innerHTML = worldClock(-6, "")
document.getElementById("Honduras").innerHTML = worldClock(-6, "")
document.getElementById("Nicaragua").innerHTML = worldClock(-6, "")
document.getElementById("Santiagodechile").innerHTML = worldClock(-4, "Chile")
document.getElementById("Amsterdam").innerHTML = worldClock(1, "NL")
document.getElementById("Kampala").innerHTML = worldClock(3, "")
document.getElementById("DarEsSalaam").innerHTML = worldClock(3, "")
document.getElementById("NewDelhi").innerHTML = worldClock(5, "India")
document.getElementById("Kathmandu").innerHTML = worldClock(5, "Nepal")
document.getElementById("Jakarta").innerHTML = worldClock(7, "Jakarta")
document.getElementById("Beijing").innerHTML = worldClock(8, "Beijing")
document.getElementById("Singapore").innerHTML = worldClock(8, "")
document.getElementById("KualaLumpur").innerHTML = worldClock(8, "")
document.getElementById("Tokio").innerHTML = worldClock(9, "")
document.getElementById("Wellington").innerHTML = worldClock(12, "")
document.getElementById("Suva").innerHTML = worldClock(12, "")
document.getElementById("seconden").innerHTML = worldClock(1, "seconden")

setTimeout("worldClockZone()", 1000)
}
window.onload=worldClockZone;
