/*
     FILE ARCHIVED ON 11:57:58 Aug 29, 2007 AND RETRIEVED FROM THE
     INTERNET ARCHIVE ON 16:16:53 Jun 27, 2011.
     JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.

     ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
     SECTION 108(a)(3)).
*/
var timeStamp = "XX.XX.XXXX";
var Result="location xxx";
var hs,ms = "XX";
var ydays,hh,mm,jday = 0;
var inday = false,dlstime=false;
var lat = 65.85;
var lon = 24.12;
var pi = Math.PI;
var rads = pi/180.0;
var daylen, ha=0.0;
var eqtime, cosfi, altit;
var utct,loctime,tzone = 2.0;
var SummerTime = 0.0;
var SunDia = 0.53;     // Sunradius degrees //
// refraction AirRefr at horizon and Refr2 up to 15 degr angle	//
// Air refraction for T=0 C and P=1000 mbar //
 var AirRefr = 35.0/60.0, Refr2=0.0,Ta=0.0;	
var Nousu,fo,fi,dfo,dfi,twidev,twib,twie,delta,kork,noont,powers, csha = 0.0;
var fi2,twidev2,twib2,twie2,x,theta;
var wkday = 0;
var now,inkuu,inpv,invuos, hr, mn, sc;
//
//   the function below returns an angle in the range
//   0 to two pi
//
function range(x) {
    var b = x /pi/2.0;
    var a = 2*pi * (b - Math.round(b));
    if (a < 0) a = 2*pi + a;
    return a;
}

function aika() {
var y2k = new Date(2000,0,1,0);

now = new Date();
hr= now.getHours();
mn=now.getMinutes();
sc=now.getSeconds();
utct=now.getUTCHours();
tzone = hr-utct;
var yearNow = now.getYear();
//   Get the days to J2000, daynumber Y2K-bug fixed
//   jday = day number since Y2K
//
if (inday) now = new Date(invuos,inkuu,inpv+1,11);
if (inday) yearNow = invuos;
if (yearNow<1000) yearNow+= 1900;
// Time in milliseconds at noontime
var timeNow = now.getTime()/1000.0;

jday = Math.round((now.getTime() - y2k.getTime())/(24*3600*1000));

//   Find the ecliptic longitude of the Sun
//   mean longitude of the Sun
var L = range(280.461 * rads + .9856474 * rads * jday);

//   mean anomaly of the Sun
var g = range(357.528 * rads + .9856003 * rads * jday);

//   Ecliptic longitude of the Sun
var lambda = range(L + 1.915 * rads * Math.sin(g) + 0.02 * rads * Math.sin(2 * g));

//   Obliquity of the ecliptic
var obliq = 23.439 * rads - .0000004 * rads * jday;

//   Find the RA and DEC of the Sun

var alpha = Math.atan2(Math.cos(obliq) * Math.sin(lambda), Math.cos(lambda));
delta = Math.asin(Math.sin(obliq) * Math.sin(lambda));
eqtime = 1440.0 -720.0 *(L - alpha)/pi;
if (eqtime>1440.0) eqtime-= 1440.0;
var uusi = new Date(yearNow,0,1,12,0,0);
var NewYear = uusi.getTime()/1000.0;
ydays = Math.round((timeNow-NewYear)/3600/24)+1;
SummerTime = 1.0; if ((ydays<90) || (ydays>300)) SummerTime=0.0;
if (!dlstime) SummerTime = 0.0; 
dekl = 180*delta/pi;

wkday =  now.getDay();

var timeStamp = ""+yearNow +"-"+fillzero(now.getMonth()+1)+"-"+now.getDate();
return timeStamp;
}

function makeArray(numElements) {
   this.length = numElements
   for (count = 1; count <= numElements; count++)
      this[count] = 0;
   return (this);
}

var WkDays = new makeArray(14);
WkDays[0] = "Sunday"; WkDays[7] = "Sonntag";
WkDays[1] = "Monday"; WkDays[8] = "Montag";
WkDays[2] = "Tuesday"; WkDays[9] = "Dienstag";
WkDays[3] = "Wednesday"; WkDays[10] = "Mittwoch";
WkDays[4] = "Thursday";  WkDays[11] = "Donnerstag";
WkDays[5] = "Friday"; WkDays[12] = "Freitag";
WkDays[6] = "Saturday"; WkDays[13] = "Samstag";

   var tulokset = "\t ©" + "  2001,   Jarmo Lammi \r";
var lastitem = 41;
var Latit = new makeArray(lastitem);
var Longit = new makeArray(lastitem);
var tZones = new makeArray(lastitem);
var tzr=0.0;
var inlat = false;
var pvm = "xx-xx-xx";
function SetLocs() {
Latit[lastitem] = 0.0; Longit[lastitem] = 0.0; tZones[lastitem]=  0;
Latit[0] = 69.87;   Longit[0] = 27.00;  tZones[0] = 2;  // Utsjoki
Latit[1] = 65.85;   Longit[1] = 24.12;  tZones[1] = 2;  // Tornio
Latit[2] = 65.02;   Longit[2] = 25.50;  tZones[2] = 2;  // Oulu
Latit[3] = 63.14;   Longit[3] = 21.58;  tZones[3] = 2;  // Vaasa
Latit[4] = 61.53;   Longit[4] = 23.57;  tZones[4] = 2;  // Tampere
Latit[5] = 60.52;   Longit[5] = 22.21;  tZones[5] = 2;  // Turku
Latit[6] = 60.17;   Longit[6] = 24.97;  tZones[6] = 2;  // Helsinki
Latit[7] = 59.33;   Longit[7] = 18.05;  tZones[7] = 1;  // Stockholm
Latit[8] = 53.55;   Longit[8] = 10.00;  tZones[8] = 1;  // Hamburg
Latit[9] = 51.50;   Longit[9] = -0.17;  tZones[9] = 0;  // London
Latit[10] = 52.52; Longit[10] = 13.4;  tZones[10]= 1;  // Berlin
Latit[11] = 48.87; Longit[11] =  2.33;  tZones[11]= 1; // Paris
Latit[12] = 48.22; Longit[12] = 16.37;  tZones[12]= 1; // Vienna
Latit[13] = 48.13; Longit[13] = 11.57;  tZones[13]= 1; // Munich
Latit[14] = 35.18; Longit[14] = -101.83; tZones[14]= -6; // Amarillo
Latit[15] = 61.22; Longit[15] = -149.88; tZones[15]= -9; // Anchorage
Latit[16] = 33.75; Longit[16] = -84.38; tZones[16]= -5; // Atlanta
Latit[17] = 39.30; Longit[17] = -76.63; tZones[17]= -5; // Baltimore
Latit[18] = 41.83; Longit[18] = -87.75; tZones[18]= -6; // Chicago
Latit[19] = 32.78; Longit[19] = -96.82;tZones[19]= -6; // Dallas
Latit[20] = 39.73; Longit[20] = -104.98;tZones[20]= -7; // Denver
Latit[21] = 42.38; Longit[21] = -83.08; tZones[21]= -5; // Detroit MI
Latit[22] = 21.32; Longit[22] = -157.87; tZones[22]= -10; // Honolulu
Latit[23] = 29.77; Longit[23] = -95.40; tZones[23]= -6; // Houston
Latit[24] = 36.17; Longit[24] = -115.20; tZones[24]= -8; // Las Vegas
Latit[25] = 34.07; Longit[25] = -118.25; tZones[25]= -8; // Los Angeles
Latit[26] = 25.77; Longit[26] = -80.2; tZones[26]= -5; // Miami
Latit[27] = 45.50; Longit[27] = -73.6; tZones[27]= -5; // Montreal
Latit[28] = 40.72; Longit[28] = -74.02; tZones[28]= -5;// NYK
Latit[29] = 46.82; Longit[29] = -71.18; tZones[29]= -5;// Quebec
Latit[30] = 37.8; Longit[30] = -122.4; tZones[30]= -8;// San Francisco
Latit[31] = 47.35; Longit[31] = -122.2; tZones[31]= -8; // Seattle
Latit[32] = 43.65; Longit[32] = -79.33; tZones[32]= -5; // Toronto
Latit[33] = 35.7; Longit[33] = 139.77; tZones[33]=  9; // Tokyo
Latit[34] = -37.83; Longit[34] = 145.0; tZones[34]=  10; // Melbourne
Latit[35] = -33.87; Longit[35] = 151.22; tZones[35]=  10; // Sydney
Latit[36] = -27.50; Longit[36] = 153.00; tZones[36]=  10; // Brisbane
Latit[37] = -36.87; Longit[37] = 174.77; tZones[37]=  12; // Auckland
Latit[38] = -33.93; Longit[38] = 18.37; tZones[38]=  2; // Cape Town
Latit[39] =  27.93; Longit[39] = -15.38; tZones[39]=  0; // Las Palmas
Latit[40] =  44.90; Longit[40] = -63.50; tZones[40]=  -4; // Halifax Can
}

function hourAngle(zenith,latitude,declination) {
 var cosha,ha;
 var deg2rad = pi/180.0;
     cosha = Math.cos(zenith)/Math.cos(deg2rad*latitude)/Math.cos(deg2rad *declination)
     - Math.tan(deg2rad *latitude)*Math.tan(deg2rad *declination);
    if (cosha<-1.0) cosha= -1.0; // no darkness
    ha = Math.acos(cosha)/deg2rad; // Hour angle for given zenith angle [degr]
    return ha;
}

  function DoCalc(mytongue) {
// Check inputmode first //
with (document.myform) {
        if (dates[1].checked) inday = true;
        if (dates[0].checked) inday = false;

        if (locs[0].checked) {
           inlat=false;
           Item = Paikkakunta.selectedIndex;
         }

        if (locs[1].checked) {inlat=true;Item = lastitem;}

       Result = Paikkakunta.options[Item].text;
	     invuos = anyyear.value;
       inkuu = kuukausi.selectedIndex;
       inpv = paiva.selectedIndex;
       dlstime = savin.checked;
       pvm =  aika();
	
	var yearNow = now.getYear();
	if (yearNow<1000) yearNow+= 1900;
	if (!inday) anyyear.value=yearNow;
 if (!inday) {
      if (dlstime) tzone-=1;
      if (!inlat) hr= hr - tzone + tZones[Item]
      lochr.value = fillzero(hr);
      locmn.value = fillzero(mn);
//      if (inlat) anytzone.value = tzone;
}
  if (inday) {hr = parseFloat(lochr.value); mn = parseFloat(locmn.value);}

 	  lat =  Latit[Item];
        lon = Longit[Item];
        if (!inlat) tzr = tZones[Item];
        if (!inlat) anytzone.value = tzr;
// Testi

// tulokset = "Testi: latitudi" +lat+", longitudi: "+lon+" , tzone: "+tzr;

     if (inlat) {
        lat = parseFloat(anylatit.value);
// add latitude minutes
var ltmin = parseFloat(latmins.value);
lat = lat + ltmin/60.0;
// Set south to negative latitude if button checked  //
     if (lat>0){if (northsouth[1].checked) lat = -lat};
// add longitude minutes
        lon = parseFloat(anylong.value);
	var lomin = parseFloat(longmins.value);
	lon = lon + lomin/60.0;
// Set west to negative longitude and timezone if button checked  //
     if (lon>0){if (eastwest[0].checked) lon = -lon};

// check legal input values //
  if ((lat < -90) || (lat > 90)) {
    alert("The value for latitude must be between 0 and 90.");
    anylatit.focus();
    return;
  }

 if ((lon < -180) || (lon > 180)) {
    alert("The value for longitude must be between 0 and 180.");
    anylong.focus();
    return;
  }

     tzr = parseFloat(anytzone.value);
     if (tzr>0){if (eastwest[0].checked) tzr = -tzr};

 if ((tzr < -12) || (tzr > 12)) {
    alert("The value for timezone must be between -12 and 12.");
    anytzone.focus();
    return;
  }

} // if inlat

 } // end with ()

       dgmn(parseFloat(lon)+0.00001);

with (document.myform) {
    Ta=tathor.value;
    AirRefr = 159.4/(273.0+1.0*Ta);
    dgmn(AirRefr); refraktio.value=ms+" '";
// Northern hemisphere   //
        kork=90-lat+dekl; 
// Southern hemisphere	//
        if (dekl > lat ) kork=90+lat-dekl;
				if (kork < 15.0) Refr2 = 1000.0*(0.1594+0.0196*kork+kork*kork* 2.0E-05)/(1.0+0.505*kork+0.0845*kork*kork)/(273.0+1.0*Ta);
		   if (kork < 15.0) kork+= Refr2;

// Calculating the hourangle //
		dfo = 0.5*SunDia+AirRefr;
    fo = hourAngle(rads*(90.0+dfo),lat,dekl);
// Calculating the hourangle for the twilight //
// Conversion of angle to hours and minutes //
     daylen = fo/7.5; 
     if (daylen<0.01) {daylen = 0.0;}     // arctic winter     //
    fi = hourAngle(1.675516,lat,dekl);
    twidev = fi - fo; 
    fi2 = hourAngle(1.780236,lat,dekl);
    twidev2 = fi2 - fo; 
     hrmn(daylen);
     tulokset = "\t ©" + "  2001,   Jarmo Lammi  (revised 3-Sep-01)\r";
     tulokset = tulokset + "  Daylength:  "+hs+":"+ms+" hours \r";

     ha = 15.0*(hr-tzr-SummerTime) +0.25*(mn + eqtime) +1.0*lon -180.0;
     if (ha>360.0) ha-=360.0;
     if (ha>180.0) ha-=360.0;
     cosfi = Math.sin(rads*lat)*Math.sin(rads*dekl) 
               +Math.cos(rads*lat)*Math.cos(rads*dekl)*Math.cos(rads*ha);
     altit = 90.0 - Math.acos(cosfi)/rads; // altitude degrees
		 if (altit < 6.0) altit+= AirRefr;

// Azimuth theta:
   x = Math.sin(rads*dekl)/Math.cos(rads*lat);
   x = x/Math.cos(rads*altit);
   x-= Math.tan(rads*lat)*Math.tan(rads*altit);
  theta = Math.acos(x)/rads; // morning azimuth

     loctime= hr + mn/60.0;
    utct = loctime-tzr-SummerTime; if (utct<0.0) utct+=24.0;
     Nousu = 12.0 + eqtime/60.0 - fo/15.0 + tzr - lon/15.0 + SummerTime;
     Lasku = 12.0 + eqtime/60.0 + fo/15.0 + tzr - lon/15.0 + SummerTime; 
     // noont =  Nousu + fo/15.0; hrmn(noont);
     noont = 12.0 + eqtime/60.0  + tzr - lon/15.0 + SummerTime;
 if (loctime>noont) theta = 360.0 - theta; // afternoon azimuth
      twib = Nousu - twidev/15.0;	// Begin civil twilight
      twie = Lasku + twidev/15.0;	// End civil twilight
      twib2 = Nousu - twidev2/15.0;	// Begin nautical twilight
      twie2 = Lasku + twidev2/15.0;	// End nautical twilight
      if (twie>25.0) twie-= 24.0;  // it may overflow with DLS
      if (twie2>25.0) twie2-= 24.0;  // it may overflow with DLS

     if (fi2 < 180.0) {
        hrmn(twib2);
        tulokset = tulokset + "  Begin of Nautical Twilight:  " + hs+":"+ms + " hours \r";
       }

     if (fi < 180.0) {
        hrmn(twib);
        tulokset = tulokset + "  Begin of Civil Twilight:  " + hs+":"+ms + " hours \r";
    } else tulokset = tulokset + "  There is no dark time \r";
     if ((daylen<23.95) && (daylen>0.05)) {
     hrmn(Nousu);
        tulokset = tulokset + "  Sunrise Time:  " + hs+":"+ms + " hours \r";
     hrmn(Lasku);
        tulokset = tulokset + "  Sunset  Time:  " + hs+":"+ms + " hours \r";
     } else if (daylen>23.9) {tulokset = tulokset + "  Sun is visible all day \r";}
       else tulokset = tulokset + "  Sun is below the horizon all day \r";
     hrmn(twie);
     if (fi < 180.0) {
        tulokset = tulokset + "  End of Civil Twilight:  " + hs+":"+ms + " hours \r";
       } else
         tulokset = tulokset + "  Midnight:      " + hs+":"+ms + " hours \r";
     if (fi2 < 180.0) {
          hrmn(twie2);
          tulokset = tulokset + "  End of Nautical Twilight:  " + hs+":"+ms + " hours \r";
       }

// Solar radiation power at noon for extinction 0.9 //
	  if (kork>0.0) powers = 1370.0*Math.sin(rads*kork)*Math.exp(Math.log(0.9)/Math.sin(rads*kork));
    else  powers = 0.0;

		    dgmn(kork);
        tulokset = tulokset + "  Max. height angle is " + hs+":"+ms + " degr at noon ";
        hrmn(noont);
        tulokset = tulokset + "time  " + hs+":"+ms + "  \r";
        dgmn(dekl);
        tulokset = tulokset + "  Declination of Sun:   " +  hs+":"+ms + " degr \r";
        dgmn(altit);
        tulokset = tulokset + "  Sun altitude " + hs +":"+ms + " degr";
        dgmn(theta);
       tulokset = tulokset + ", azimuth " + hs +":"+ms + " degr";
        hrmn(utct);
        tulokset = tulokset + " (UTC " + hs + ":" + ms + ')';
     if (dlstime) {tulokset = tulokset + " DLST"};
        tulokset = tulokset + '\r';
	       tulokset = tulokset + "  Solar power " + Math.floor(powers + 0.5) + " W/m2 at noon \r";

// Weekdays either in English or German
var wd = WkDays[wkday + 7*(mytongue==2)];
        tulokset = tulokset + "  Date: " + wd + " on " + pvm + ", the "+ ydays + ". day of the year \r"; 
        tulokset = tulokset + "  Location: " + Result;

        dgmn(lat+0.00001);
        tulokset = tulokset + ", latitude " + hs+":"+ms;

        dgmn(parseFloat(lon)+0.00001);
        tulokset = tulokset + ", longitude " + hs+":"+ms;
        outs.value = tulokset;

  } // end with ()
}

function fillzero(x) {
var s="x";
var snull = "0";
if (x<10) s = snull+x; else s=x;
return s
}

function dgmn(x) {
var sign = 1; if (x<0.0) {sign = -1; x = Math.abs(x);}
hh = Math.round(x + 0.0002);
mm = Math.round((x - hh)*60 + 0.0002);
if (mm<0.0) {mm+=60; hh-=1;}
hs =  hh; if (sign<0) hs = "-"+hh;
ms = fillzero(mm); 
}

function hrmn(x) {
hs="00"; ms="00";

if (x>0.0001) { 
hh = Math.round(x + 0.001);
mm = Math.round((x - hh)*60 + 0.001);
if (mm<0.0) {mm+=60; hh-=1;}
// normalize times to max 24 hours	//
if (hh>24.0) {hh-= 24};
if ((hh==24)&&(mm>10)) {hh-= 24};
	hs = fillzero(hh);
	ms = fillzero(mm);}
}
