//Programmer: Gudjon Holm Sigurdsson
//e m a i l : gudjon at undri.com (no at simbol becuase of spammers)

//Changes the provided date "d" to Greenwich Mean Time acording to the values of the GMT offset values in the Date() object 
function ToGMT(d) {
  var Milliseconds = d.getTime();
  var GMTZoneOffMilliseconds=d.getTimezoneOffset()*(60*1000);
  Milliseconds = Milliseconds+GMTZoneOffMilliseconds;
  d.setTime(Milliseconds);
}

//is the year a leep year
//
//the function will return
//    1 if iYear is a leap year and
//    0 if iYear is not a leap year
function IsLaepYear(iYear) {
	if (!(iYear%4))
	{
		if (!(iYear%100))
		{
			if (!(iYear%400))
				return 1;
			return 0;
		}
		return 1;
	}
	return 0;
}
//How many days are in a month for a specific year.
//We need to take the leap year in account 

function DaysInMonth(uiManudur, uiAr) {
//                     j  f   m   a   m   j   j   a   s   o   n   d
var months = new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
//                     0  1   2   3   4   5   6   7   8   9   10  11  12
//                     0, 31,59, 90, 120,151,181,212,243,273,304,334,365

	if (uiManudur<1 || uiManudur>12 || uiAr==0)
		return 0;

   if (2==uiManudur)//only need to call this func for feb.
   		return months[2]+=IsLaepYear(uiAr);

   	return months[uiManudur];
}

function WeekdayToString(weekday)
{
	switch (weekday)
	{
		case (weekday= 0)	: return "sunnudagur";break
		case (weekday= 1)	: return "m&aacute;nudagur";break
		case (weekday= 2)	: return "ūri&eth;judagur";break
		case (weekday= 3)	: return "mi&eth;vikudagur";break
		case (weekday= 4)	: return "fimmtudagur";break
		case (weekday= 5)	: return "f&ouml;studagur";break
		case (weekday= 6)	: return "laugardagur";break


		default				: return "Error - Weekday not in range!";break;
	}
}

/*the function LastWeekdayInMonth will return the Day of month
  with the last weekday supplyed in the weekday parameter .  
  weekday possible values : 0=sunday, 1=monday, 2=tuesday, 3=wednesday, 4=thursday, 5=friday, 6=saturday //From the date we will get the month and the year
*/

function LastWeekdayInMonth(date, weekday) {
	var iLastDayInMonth=DaysInMonth(date.getMonth()+1, date.getFullYear());
   var tempDate= new Date();
   tempDate.setTime(date.getTime());
   tempDate.setDate(iLastDayInMonth);
	var iLastDateInMonth=tempDate.getDay();
	var iSub=0;
	if (iLastDateInMonth<weekday)
		iSub=(weekday-iLastDateInMonth);
	else
		iSub=(7+weekday-iLastDateInMonth);

	return iLastDayInMonth+iSub-7;
}
//the function FirstWeekdayInMonth will return a number reprisenting the Day of month with the first weekday supplyed in the weekday parameter //weekday possible values : 0=sunday, 1=monday, 2=tuesday, 3=wednesday, 4=thursday, 5=friday, 6=saturday //From the date we will get the month and the year 

function FirstWeekdayInMonth(date, weekday) {
   var tempDate= new Date();
   tempDate.setTime(date.getTime());
   tempDate.setDate(1);
	var iFirstDateInMonth=tempDate.getDay();
	var iSub=0;
	if (iFirstDateInMonth<=weekday)
		iSub=(weekday-iFirstDateInMonth);
	else
		iSub=(7+weekday-iFirstDateInMonth);

	return 1+iSub;
}

/*The function GetLocalTime
Usage:  //F.example for the local time in Norway
		var LocalTime = new Date();
		LocalTime.setTime(GetLocalTime("Norway"));

Return values: 
		Success: Date & time in milliseconds since midnight of January 1, 1970 in the specified area.
		Fail   : 0 which is the date "Thu Jan 1 00:00:00 UTC 1970"
		
Areas that have bin implemented in this function are:
"UN" : European Union - Start: Last Sunday in March at 1 am UTC End: Last Sunday in October at 1 am UTC
"Norway", "Denmark", "Sweden", "Belgium", Netherlands, "Iceland", "Faroe Islands", "China"

*/
function GetLocalTime(strArea)
{
	
	var now = new Date();
	var DLSstart = new Date();//Daylight Savings Start
	var DLSend = new Date();//Daylight Savings End.
	var GMT=0;
	DLSstart.setTime(0);//because of compainson later
	DLSend.setTime(0);
	DLSstart.setFullYear(now.getFullYear());
	DLSend.setFullYear(now.getFullYear());
	ToGMT(now);//Get the Greenwich Tean Time

	switch (strArea)
	{
		case (strArea = "Iceland"):
		//No change country is using GMT + 0
		break
		case (strArea = "China"):
			GMT=8;
		break;//No daylight savings
		//Denmark, Norway & Faroe Islands are using UN Daylight savings.
		case (strArea = "Denmark"):
		case (strArea = "Norway"):
		case (strArea = "Sweden"):
		case (strArea = "Belgium"):
		case (strArea = "The Netherlands"):				
		case (strArea = "Netherlands"):		
		
			if (0==GMT) 
				GMT=1;//Do not change if another area above using UN settings did change

		case (strArea = "Finland"):
			if (0==GMT) 
				GMT=2;

		case (strArea = "Faroe Islands"):
			//No change country is using GMT + 0 but uses DLS

		case (strArea = "UN"):
			//set the month and hour when the Daylight Savings start & end.
			DLSstart.setHours(1);  DLSstart.setDate(1);  DLSstart.setMonth(3-1);//Start: Last Sunday in March at 1 am UTC 
			DLSend.setHours(1);    DLSend.setDate(1);    DLSend.setMonth(10-1);//End:Last Sunday in October at 1 am UTC
			DLSstart.setDate(LastWeekdayInMonth(DLSstart, 0));
			DLSend.setDate(LastWeekdayInMonth(DLSend  , 0));
		break	
			
		default: now.setTime(0);
		break;

	}//switch (strArea)
	
	if((now > DLSstart) && (now < DLSend))
		GMT+=1;//We are using Daylight Savings
	

	now.setHours(now.getHours()+GMT);
	return now.getTime();
}//function GetTime(strArea);

//returns a string on the time format 00:00:00
function TimeStr(date)
{
	var str="";
	var num=0;
	num=date.getHours();	if (num<10) str="0";	str+=num+":";
	num=date.getMinutes();	if (num<10) str+="0";	str+=num+":";
	num=date.getSeconds();	if (num<10) str+="0";	str+=num;	
	
	return str;

}
/*GetLocalTimeStr returns a local time string on the time format 00:00:00
  See description for the function GetLocalTime
*/
function GetLocalTimeStr(strArea)
{
	var now = new Date();
	now.setTime(GetLocalTime(strArea));
	return TimeStr(now);
}

function display()
{
	document.write("<table border=0>");
	document.write("<tr><td width=100><b>Land</b></td><td><b>T&iacute;mi</b></td></tr>");
	document.write("<tr><td>&Iacute;sland</td><td>" +GetLocalTimeStr("Iceland")      +"</tr>");
	document.write("<tr><td>F&aelig;reyjar</td><td>"+GetLocalTimeStr("Faroe Islands")+"</td></tr>");	
	document.write("<tr><td>Danm&ouml;rk</td><td>"  +GetLocalTimeStr("Denmark")      +"</td></tr>");
	document.write("<tr><td>Noregur</td><td>"       +GetLocalTimeStr("Norway")       +"</td></tr>");
	document.write("<tr><td>Holland</td><td>"       +GetLocalTimeStr("Netherlands")  +"</td></tr>");	
	document.write("<tr><td>Belg&iacute;a</td><td>" +GetLocalTimeStr("Belgium")      +"</td></tr>");
	document.write("<tr><td>Finnland</td><td>"      +GetLocalTimeStr("Finland")      +"</td></tr>");
	document.write("<tr><td>K&iacute;na</td><td>"   +GetLocalTimeStr("China")        +"</td></tr>");		
	document.write("</table>");
}
