// Get the time of the zone, where the mousepointer on the map
// even is.
function getTime(theForm, theOffset){
  var ParentForm;
  var Sign = "";
  
  // The Offset in minutes to the GMT. The values depends
  // the standard time zones from january 2001. 
  // Compiled by HM Nautical Almanac Office.
  var Minutes = new Array(-720, -660, -600, -570, -540, -510, -480, -420,
  			  -360, -300, -240, -210, -180, -120, -60, 0,
  			  60, 120, 180, 210, 240, 270, 300, 330, 360,
  			  390, 420, 480, 540, 570, 600, 630, 660, 690,
  			  720, 780, 840);
  			  
  // Get the time at this moment.
  var Now = new Date();
  var LocalTime = new Date();

  // Get the offset from the locale time to GMT in minutes.
  var DifferenceToGMT = Now.getTimezoneOffset();
  var Year = Now.getYear();
  if(Year < 1000){
  	Year = Year + 1900;
  }
  var StartSummertime = new Date(Year, 2, 31);
  var Day = StartSummertime.getDay();
  var StartSummertimeDay = new Date(Year, 2, (31 - Day), 2, 0, 0);
  var EndSummertime = new Date(Year, 9, 31);
  var Day = EndSummertime.getDay();
  var EndSummertimeDay = new Date(Year, 9, (31 - Day), 2, 0, 0);
  if((StartSummertimeDay.getTime() < Now.getTime()) && (Now.getTime() < EndSummertimeDay.getTime())){
  	DifferenceToGMT = DifferenceToGMT + 60;
  }
  
  
  // Calculate the time on the selected zone.
  // Set the new time in the date.
  var Minute = Now.getMinutes();
  Minute = Minute + (DifferenceToGMT) + Minutes[theOffset];
  Now.setMinutes(Minute);
  
  // Check which browser are used and take the form.
  var Browser = navigator.appName;
  
  if(Browser == "Netscape"){
  	ParentForm = theForm.forms[0];
  }else{
  	ParentForm = theForm.forms[1];
  }
  
  // Take the particulars elements from the date object.
  var Dates = new Array(8);
  var TextDates = new Array(8);

  Dates[0] = Now.getDate();
  Dates[1] = Now.getMonth() + 1;
  Dates[2] = Now.getYear();
  Dates[3] = Now.getHours();
  Dates[4] = Now.getMinutes();
  Dates[5] = Now.getSeconds();
  
  // Check which timezone are the locale is.
  var LocalOffset;
  for(i = 0; i < Minutes.length; i++){
  	if((DifferenceToGMT * -1) == Minutes[i]){
  		LocalOffset = i;
  	}
  }
  
  //Calculate the difference from the locale time to the selected timezone.
  var DifferenceMinute = Math.abs(Dates[4] - (LocalTime.getMinutes()));
  var LocalMinute;
  if(LocalOffset > theOffset){
  	LocalMinute = LocalTime.getMinutes() - DifferenceMinute;
  	Sign = "-";
  }else{
  	LocalMinute = LocalTime.getMinutes() + DifferenceMinute;
  	Sign = "+";
  }
  LocalTime.setMinutes(LocalMinute);
 
  // If the Date changes, the hours must be calculated differently.
  if(LocalTime.getDate() < Now.getDate()){
  	Dates[6] = Math.abs((Dates[3] + 24) - (LocalTime.getHours()));
  }else if(LocalTime.getDate() > Now.getDate()){
  		Dates[6] = Math.abs((24 - Dates[3]) + LocalTime.getHours());
  	}else if(Dates[4] < LocalTime.getMinutes() && Dates[3] < LocalTime.getHours()){
  			Dates[6] = Math.abs(LocalTime.getHours() - Dates[3]);
  		}else{
  			Dates[6] = Math.abs(Dates[3] - (LocalTime.getHours()));
  		}
  
  Dates[7] = DifferenceMinute;
  
  // Format the elements for the output.
  for(i = 0; i < Dates.length; i++){
  	if(Dates[i] < 10){
  		TextDates[i] = ("0" + Dates[i]);
  	}else{
  		TextDates[i] = Dates[i];
  	}
  }
  
  // Check whether the selected timezone are the same as the local timezone. 
  // Is it zero, then will be not display the sign.
  if(LocalOffset == theOffset){
  	Sign = "";
  }
  
  // If the date are display only two places of the year element,
  // then will add 1900 to the result.
  if(Dates[2] < 1000){
  	TextDates[2] = Dates[2] + 1900;
  }

  // Arrange the date, the time and the difference output.
  // Is differently arranged depending upon language.
  var DateNow = (TextDates[0] + "." + TextDates[1] + "." + TextDates[2]);
  var TimeNow = (TextDates[3] + ":" + TextDates[4] + ":" + TextDates[5]);
  var Difference = Sign + TextDates[6] + ":" + TextDates[7];

  // Set the Result on the outputfields.
  ParentForm.outputDate.value = DateNow
  ParentForm.outputTime.value = TimeNow;
  ParentForm.outputDifference.value = Difference;
}

// Get the loacle time on the outputfield.
function getLocalTime(theForm){
  var ParentForm;
  
  // Get the time at this moment.
  var Now = new Date();
  
  // Check which browser are used and take the form.
  var Browser = navigator.appName;
  
  if(Browser == "Netscape"){
  	ParentForm = theForm.forms[0];
  }else{
  	ParentForm = theForm.forms[1];
  }

  // Take the particulars elements from the date object.
  var Dates = new Array(6);
  var TextDates = new Array(6);
  
  Dates[0] = Now.getDate();
  Dates[1] = Now.getMonth() + 1;
  Dates[2] = Now.getYear();
  Dates[3] = Now.getHours();
  Dates[4] = Now.getMinutes();
  Dates[5] = Now.getSeconds();
  
  // Format the elements for the output.
  for(i = 0; i < Dates.length; i++){
  	if(Dates[i] < 10){
  		TextDates[i] = ("0" + Dates[i]);
  	}else{
  		TextDates[i] = Dates[i];
  	}
  }
  
  // If the date are display only two places of the year element,
  // then will add 1900 to the result.
  if(Dates[2] < 1000){
  	TextDates[2] = Dates[2] + 1900;
  }

  // Arrange the date, the time and the difference output.
  // Is differently arranged depending upon language.  
  var DateNow = (TextDates[0] + "." + TextDates[1] + "." + TextDates[2]);
  var TimeNow = (TextDates[3] + ":" + TextDates[4] + ":" + TextDates[5]);
  var Difference = "00:00";

  // Set the Result on the outputfields.
  ParentForm.outputDate.value = DateNow
  ParentForm.outputTime.value = TimeNow;
  ParentForm.outputDifference.value = Difference;
} 

//An util-method to make the outputfields read only.
function blurElement(theTextInputElement){
	theTextInputElement.blur();
}

//When the page on load, will set the output on the locale time.
function startTime(theDocument){
  var Browser = navigator.appName;
  
  if(Browser == "Netscape"){
  	getLocalTime(theDocument.ikcol1.document); // ikcol1 is the ID from the DIV, which contains the form.
  }else{
  	getLocalTime(theDocument);
  }
}
