/*
JavaScript Sun Calculator
© 2001 Juergen Giesen
http://www.jgiesen.de
*/
var dat, myDay, myMonth, myYear, myHour, myMinute, JD, UT, offset, dIM, DEC, EOT, lat, longit, htb

function daysInMonth(m, y) {
	var n=31
	m=m-1
	if ((m==0) || (m==2) || (m==4) || (m==6) || (m==7) || (m==9) || (m==11))  n=31
	if ((m==3) || (m==5) || (m==8) || (m==10))  n=30;
	if (m==1) {
		n=28;
		if ((y % 4) == 0) n=29
		if ((y % 100) == 0) n=28
		if ((y % 400) == 0) n=29
		}
	dIM=n;			
}

function DateTime() {
  var str = " " + myYear+", "+ document.myform.Monat.options[myMonth-1].value+" "+myDay+"  at "+myHour+":"
	if (myMinute<10) str=str+"0"+myMinute
	else str=str+myMinute
	document.myform.text1.value=str;
	str = " " + Math.round(1000*DEC)/1000
	document.myform.text2.value=str;
	str = " " + EOT
 document.myform.text3.value=str
 lat = document.myform.latitude.value
 document.myform.text4.value=HalberTagbogen(lat)
 //document.myform.rise.value=rise()
}

function thisHour() {
	dat=new Date()
	document.myform.Stunde.options[dat.getHours()].selected=true
	myHour=dat.getHours()
}

function thisMinute() {
	dat=new Date()
	document.myform.Minute.options[dat.getMinutes()].selected=true
	myMinute=dat.getMinutes()
}

function thisUT() {
	dat=new Date()
	offset=dat.getTimezoneOffset()
	if (offset>=1380) offset=offset-1440
	UT = myHour+(myMinute+offset)/60.0+dat.getSeconds()/3600.0
	str=myHour+offset/60.0
	if (myHour+offset/60.0<0)  str= 24+myHour+offset/60.0
	if (myHour+offset/60.0>=24)  str= -24+myHour+offset/60.0
	str=" " + str + ":";
	if (myMinute<10) str=str+"0"+myMinute
	else str=str+myMinute
	document.myform.UTime.value=str
}

function thisJulDay() {
  yy = dat.getYear()
  if (yy<1000) yy=yy+1900		
	document.myform.JulDay.value=" " + Math.round(10000*JD)/10000	
}

function thisDay() {
	dat=new Date()
	document.myform.Tag.options[dat.getDate()-1].selected=true
	myDay=dat.getDate()
}

function thisMonth() {
	dat=new Date()
	document.myform.Monat.options[dat.getMonth()].selected=true
	myMonth=dat.getMonth()+1
}

function thisYear() {
	dat=new Date()
  yy=dat.getYear()-1992
  if (yy<0) {yy=yy+1900; myYear=dat.getYear()+1900} else  myYear=dat.getYear()
	document.myform.Jahr.options[yy].selected=true
}

function theDay() {
	daysInMonth(myMonth,myYear)
	for (var i=0;i<document.myform.Tag.options.length;i++)
		if (document.myform.Tag.options[i].selected==true) myDay=i+1
	if (dIM<myDay) {myDay=dIM; document.myform.Tag.options[dIM-1].selected=true}
}

function theMonth() {
	for (var i=0;i<document.myform.Monat.options.length;i++)
		if (document.myform.Monat.options[i].selected==true) myMonth=i+1
}

function theYear() {
	for (var i=0;i<document.myform.Jahr.options.length;i++)
		if (document.myform.Jahr.options[i].selected==true) myYear=i+1992
	daysInMonth(myMonth,myYear)
	if (dIM<myDay) {myDay=dIM; document.myform.Tag.options[dIM-1].selected=true}
}

function theHour() {
	for (var i=0;i<document.myform.Stunde.options.length;i++)
		if (document.myform.Stunde.options[i].selected==true) myHour=i
}

function theUT() {
  if (offset>=1380) offset=offset-1440
	UT = myHour+(myMinute+offset)/60.0+dat.getSeconds()/3600.0
  str=myHour+offset/60.0
	if (myHour+offset/60.0<0) str= 24+myHour+offset/60.0
	if (myHour+offset/60.0>=24) str= -24+myHour+offset/60.0
	str=" " + str + ":";
	if (myMinute<10) str=str+"0"+myMinute
	else str=str+myMinute	
	document.myform.UTime.value=str
}

function theMinute() {
	for (var i=0;i<document.myform.Minute.options.length;i++)
		if (document.myform.Minute.options[i].selected==true) myMinute=i
}

function theJulDay() {
	if (myYear<1900) myYear=myYear+1900
	JulianDay(myDay,myMonth,myYear,UT)	
	document.myform.JulDay.value=" " + Math.round(10000*JD)/10000	
}

function JulianDay (date, month, year, UT){
	if (year<1900) year=year+1900
	if (month<=2) {month=month+12; year=year-1}
   B = Math.floor(year/400.0) - Math.floor(year/100.0)  + Math.floor(year/4.0)
   A = 365.0*year - 679004.0
	 JD= A + B + Math.floor(30.6001*(month+1)) + date + UT/24.0
   JD = JD + 2400000.5
}

function thisDateTime() {
	thisDay()
  thisMonth()
	thisYear()
	thisHour()
	thisMinute()
	thisUT()
	theJulDay()
	offset=dat.getTimezoneOffset()
	EOT = eot(myDay, myMonth, myYear, UT)
  lat = document.myform.latitude.value
	DateTime()
}

function theDateTime() {
	theDay()
	theMonth()
	theYear()
	theHour()
	theMinute()
	offset=dat.getTimezoneOffset()
	theUT()
	theJulDay()
	EOT = eot(myDay, myMonth, myYear, UT)
	DateTime()
}

function RightAscension(T) {	
	var K = Math.PI/180.0				
	var L, M, C, lambda, RA, eps, delta, theta							
	L = sunL(T)		
	M = 357.52910 + 35999.05030*T - 0.0001559*T*T - 0.00000048*T*T*T
	M = M % 360		
	if (M<0) M = M + 360				
	C = (1.914600 - 0.004817*T - 0.000014*T*T)*Math.sin(K*M)
	C = C + (0.019993 - 0.000101*T)*Math.sin(K*2*M)
	C = C + 0.000290*Math.sin(K*3*M)		
	theta = L + C; // true longitude of the Sun						
	eps = EPS(T)				
	eps = eps + 0.00256*Math.cos(K*(125.04 - 1934.136*T))		
	lambda = theta - 0.00569 - 0.00478*Math.sin(K*(125.04 - 1934.136*T)); // apparent longitude of the Sun
	RA = Math.atan2(Math.cos(K*eps)*Math.sin(K*lambda), Math.cos(K*lambda))				
	RA = RA/K
	if (RA<0) RA = RA + 360.0;			
	delta = Math.asin(Math.sin(K*eps)*Math.sin(K*lambda))
	delta = delta/K		
	DEC = delta				
	return RA		
}

function sunL(T){
	var L = 280.46645 + 36000.76983*T + 0.0003032*T*T	
	L = L % 360		
	if (L<0) L = L + 360
	return L			
}

function deltaPSI(T){
	var K = Math.PI/180.0
	var deltaPsi, omega, LS, LM				
	LS = sunL(T)	
	LM = 218.3165 + 481267.8813*T		
	LM = LM % 360	
	if (LM<0) LM = LM + 360		
	omega = 125.04452 - 1934.136261*T + 0.0020708*T*T + T*T*T/450000
	deltaPsi = -17.2*Math.sin(K*omega) - 1.32*Math.sin(K*2*LS) - 0.23*Math.sin(K*2*LM) + 0.21*Math.sin(K*2*omega)
	deltaPsi = deltaPsi/3600.0		
	return deltaPsi	
}
	
function EPS(T) {
	var K = Math.PI/180.0
	var LS = sunL(T)
	var LM = 218.3165 + 481267.8813*T	
	var eps0 =  23.0 + 26.0/60.0 + 21.448/3600.0 - (46.8150*T + 0.00059*T*T - 0.001813*T*T*T)/3600
	var omega = 125.04452 - 1934.136261*T + 0.0020708*T*T + T*T*T/450000		
	var deltaEps = (9.20*Math.cos(K*omega) + 0.57*Math.cos(K*2*LS) + 0.10*Math.cos(K*2*LM) - 0.09*Math.cos(K*2*omega))/3600
	return eps0 + deltaEps	
}
	
function eot(date, month, year, UT){		
	var K = Math.PI/180.0
	var  T = (JD - 2451545.0) / 36525.0		
	var eps = EPS(T);
	var RA = RightAscension(T)
	var LS = sunL(T)
	var deltaPsi = deltaPSI(T)						
	var E = LS - 0.0057183 - RA + deltaPsi*Math.cos(K*eps)		
	if (E>5) E = E - 360.0		
	E = E*4; // deg. to min		
	E = Math.round(1000*E)/1000								
	return E		
}

function getLatitude() {
 str=document.myform.latitude.value;
 var n=str.length;
 for (var i=0; i<n; i++) {
 c=str.charAt(i);
 if ((c!='0') && (c!='1')  && (c!='2') && (c!='3') && (c!='4') && (c!='5') && (c!='6') && (c!='7') && (c!='8') && (c!='9')  && (c!='-') && (c!='+') && (c!='.')) {alert("Error on latitude value !"+"\n"+"Enter decimal degree value, e.g.:"+"\n"+" 52.34 if North, or -52.34 if South.");document.myform.latitude.value=0; break;};}
 if (Math.abs(Number(str))>90) {alert("Latitude must less or equal to 90 degrees !"); document.myform.latitude.value=0; };
 lat=  Number(str);
 DateTime();
}



function HalberTagbogen (lat) {
 var K = Math.PI/180.0;
 var D, C, Nenner, C2;
 D = DEC;
 Nenner = Math.cos(K*lat)*Math.cos(K*D);
 C = -Math.sin(K*D)*Math.sin(K*lat)/Nenner; 
 C2=C*C;
 if ((C>-1) && (C<1)) {
 htb=90.0 - Math.atan(C / Math.sqrt(1 - C2)) / K;
 htb=2.0*htb/15.0;
 htb=Math.round(htb*100)/100; };
 if (C>1)  htb=0.0;
 if (C<-1) htb=24.0;
 return htb
}

/*
function rise() {
var riseTime=12.0+longit/EOT/60.0-htb/2.0;
var str=Math.floor(riseTime)+":"+Math.round(60*(riseTime-Math.floor(riseTime)))
return str;
}
*/
