
//=========================================================
//
// Computes moonrise and moonset for a given date
//
//=========================================================
function moontimes(Y, M, D, Time_Offset)
	{A5 = 0
	D5 = 0
	V0 = 0
	C = 0
	S = 0
	Rstring = ""
	Sstring = ""
	V1 = 0
	m = new Array(14)
	m[13] = 1.00021
	m[14] = 60.40974
	n = new Array
	x = new Array
	moon = 0
	moonrise = "00:-1"
	moonset = "00:-1"
	rise = ""
	set = ""
	Day = D
	Month = M
	m[M] = DaysInMonth[M]
	Year = Y
	mr = 13
	B5 = parseFloat(StationLat)
	L5 = parseFloat(StationLong)
	H = parseFloat(Time_Offset)
	Y = parseFloat(Y)
	M = parseFloat(M)
	D = parseFloat(D)
	P2 = PI_2
	DR = deg2Rad
	R1 = DR
	K1 = 15 * DR * 1.0027379
	P1 = Math.PI
	L5 = L5 / 360
	Z0 = H / 2
	subroutine6(Y, D, M)
	T = F + (J - 2451545)
	TT = T / 36525 + 1
	subroutine2(T, Z0, L5, DR)
	T = T + Z0
	subroutine5(T, TT, P2)
	AX = A5
	DX = D5
	T = T + 1
	subroutine5(T, TT, P2)
	AY = A5
	DY = D5
	if (AY < AX) {AY = AY + P2}
	Z1 = DR * 90.833
	S = Math.sin(B5 * DR)
	C = Math.cos(B5 * DR)
	Z = Math.cos(Z1)
	M8 = 0
	W8 = 0
	A0 = AX
	D0 = DX
	DA = AY - AX
	DD = DY - DX
	for (C0 = 0; C0 <= 23;) 
	   	{P = (C0 + 1) / 24
	   	A2 = AX + P * DA
	   	D2 = DX + P * DD
	   	subroutine3(T0, A0, A2, D0, D2, V0, K1, C0, C, S, Rstring, Z, V1, mr, P1)
	   	A0 = A2
	   	D0 = D2
	   	V0 = V2
	   	hs = A0
	   	if (rise == "") {rise = "00:-1"}
	   	if (set == "") {set = "00:-1"}
	   	if (alt < -.73) {rise = "00:-1", set = "00:-1"}
	   	alt = Math.floor(alt * 100 + .5) / 100
	   	if (alt < 0 && alt > -.73) {alt = 0}
	   	C0++}
	moon = 1
	mr = 14
	Z0 = H / 24
	subroutine6(Y, D, M)
	T = F + (J - 2451545)
	subroutine2(T, Z0, L5, DR)
	T = T + Z0
	subroutine4(T, P2)
	n[1] = A5
	n[2] = D5
	n[3] = R5
	T = T + 0.5
	subroutine4(T, P2)
	n[4] = A5
	n[5] = D5
	n[6] = R5
	T = T + 0.5
	subroutine4(T, P2)
	n[7] = A5
	n[8] = D5
	n[9] = R5
	if (n[4] <= n[1]) {n[4] = n[4] + P2}
	if (n[7] <= n[4]) {n[7] = n[7] + P2}
	Z1 = R1 * (90.567 - 41.685 / n[6])
	S = Math.sin(B5 * R1)
	C = Math.cos(B5 * R1)
	Z = Math.cos(Z1)
	M8 = 0
	W8 = 0
	A0 = n[1]
	D0 = n[2]
	for (var C0 = 0; C0 <= 23;)
	   	{P = (C0 + 1) / 24
	   	F0 = n[1]
	   	F1 = n[4]
	   	F2 = n[7]
	   	subroutine1(F1, F0, F2, P, B)
	   	A2 = F
	   	F0 = n[2]
	   	F1 = n[5]
	   	F2 = n[8]
	   	subroutine1(F1, F0, F2, P, B)
	   	D2 = F
	   	subroutine3(T0, A0, A2, D0, D2, V0, K1, C0, C, S, Rstring, Z, V1, mr, P1)
	   	A0 = A2
	   	D0 = D2
	   	V0 = V2
	   	if (ri == 1) {moonrise = rise}
	   	if (st == 1) {moonset = set}
	   	C0++}
	mrise = moonrise
	mset = moonset}

//=========================================================
function subroutine1(F1, F0, F2, P, B)
	{A = F1 - F0
	B = F2 - F1 - A
	F = F0 + P * (2 * A + B * (2 * P - 1))
	return}

//=========================================================
function subroutine2(T, Z0, L5, DR)
	{T0 = T / 36525
	S = 24110.5 + 8640184.813 * T0
	S = S + 86636.6 * Z0 + 86400 * L5
	S = S / 86400
	S = S - Math.floor(S)
	T0 = S * 360 * DR
	return}

//=========================================================	 
function subroutine3(T0, A0, A2, D0, D2, V0, K1, C0, C, S, Rstring, Z, V1, mr, P1)
	{ri = 0
	st = 0
	L0 = T0 + C0 * K1
	L2 = L0 + K1
	if (mr == 14) {if (A2 < A0) {A2 = A2 + PI_2}}
	H0 = L0 - A0
	H2 = L2 - A2
	H1 = (H2 + H0) / 2
	D1 = (D2 + D0) / 2
	if (moon == 0) {
		dxc = D1 * 57.29578 + .005
	   	dxc = Math.floor(dxc * 100) / 100; 
	   	alt = 90 - StationLat + dxc;
	   	if (alt > 90) {alt = 180 - alt}}
	if (C0 == 0) {V0 = S * Math.sin(D0) + C * Math.cos(D0) * Math.cos(H0) - Z} 
	V2 = S * Math.sin(D2) + C * Math.cos(D2) * Math.cos(H2) - Z
	VS = V0 * V2
	if (VS > 0) {return}
	V1 = S * Math.sin(D1) + C * Math.cos(D1) * Math.cos(H1) - Z
	A = 2 * V2 - 4 * V1 + 2 * V0
	B = 4 * V1 - 3 * V0 - V2
	D = B * B - 4 * A * V0
	if (D < 0) {return}
	D = Math.sqrt(D)
	if (V2 > 0 && V0 < 0) {ri = 1; M8 = 1}
	if (V2 < 0 && V0 > 0) {st = 1; W8 = 1}
	E = (-B + D) / (2 * A)
	if (E > 1 || E < 0) {E = (-B - D) / (2 * A)} 
	T3 = C0 + E + 1 / 120
	H7 = H0 + E * (H2 - H0)
	N7 = -1 * Math.cos(D1) * Math.sin(H7)
	D7 = C * Math.sin(D1) - S * Math.cos(D1) * Math.cos(H7)
	AZ = Math.atan(N7 / D7) / DR
	H3 = Math.floor(T3)
	M3 = Math.floor((T3 - H3) * 60)
	if (ri == 1) {rise = convertTime(H3, M3)}
	if (st == 1) {set = convertTime(H3, M3)}
	return}

//=========================================================	 
function subroutine4(T, P2)
	{L = 0.606434 + 0.03660110129 * T
	M = 0.374897 + 0.03629164709 * T
	F = 0.259091 + 0.03674819520 * T
	D = 0.827362 + 0.03386319198 * T
	N = 0.347343 - 0.00014709391 * T
	G = 0.993126 + 0.00273777850 * T
	L = L - Math.floor(L)
	M = M - Math.floor(M)
	F = F - Math.floor(F)
	D = D - Math.floor(D)
	N = N - Math.floor(N)
	G = G - Math.floor(G)
	L = L * P2
	M = M * P2
	F = F * P2
	D = D * P2
	N = N * P2
	G = G * P2
	V = 0.39558 * Math.sin(F + N)
	V = V + 0.08200 * Math.sin(F)
	V = V + 0.03257 * Math.sin(M - F - N)
	V = V + 0.01092 * Math.sin(M + F + N)
	V = V + 0.00666 * Math.sin(M - F)
	V = V - 0.00644 * Math.sin(M + F - 2 * D + N)
	V = V - 0.00331 * Math.sin(F - 2 * D + N)
	V = V - 0.00304 * Math.sin(F - 2 * D)
	V = V - 0.00240 * Math.sin(M - F - 2 * D - N)
	V = V + 0.00226 * Math.sin(M + F)
	V = V - 0.00108 * Math.sin(M + F - 2 * D)
	V = V - 0.00079 * Math.sin(F - N)
	V = V + 0.00078 * Math.sin(F + 2 * D + N)
	U = 1 - 0.10828 * Math.cos(M)
	U = U - 0.01880 * Math.cos(M - 2 * D)
	U = U - 0.01479 * Math.cos(2 * D)
	U = U + 0.00181 * Math.cos(2 * M - 2 * D)
	U = U - 0.00147 * Math.cos(2 * M)
	U = U - 0.00105 * Math.cos(2 * D - G)
	U = U - 0.00075 * Math.cos(M - 2 * D + G)
	W = 0.10478 * Math.sin(M)
	W = W - 0.04105 * Math.sin( 2 * F + 2 * N)
	W = W - 0.02130 * Math.sin(M - 2 * D)
	W = W - 0.01779 * Math.sin(2 * F + N)
	W = W + 0.01774 * Math.sin(N)
	W = W + 0.00987 * Math.sin(2 * D)
	W = W - 0.00338 * Math.sin(M - 2 * F - 2 * N)
	W = W - 0.00309 *Math.sin(G)
	W = W - 0.00190 * Math.sin(2 * F)
	W = W - 0.00144 * Math.sin(M + N)
	W = W - 0.00144 * Math.sin(M - 2 * F - N)
	W = W - 0.00113 * Math.sin(M + 2 * F + 2 * N)
	W = W - 0.00094 * Math.sin(M - 2 * D + G)
	W = W - 0.00092 * Math.sin(2 * M - 2 * D)
	S = W / Math.sqrt(U - V * V)
	A5 = L + Math.atan(S / Math.sqrt(1 - S * S))
	S = V / Math.sqrt(U)
	D5 = Math.atan(S / Math.sqrt(1 - S * S))
	R5 = 60.40974 * Math.sqrt(U)
	return}

//=========================================================	
function subroutine5(T, TT, P2)
	{L = .779072 + .00273790931 * T
	G = .993126 + .0027377785 * T
	L = L - Math.floor(L)
	G = G - Math.floor(G)
	L = L * P2
	G = G * P2
	V = .39785 * Math.sin(L)
	V = V - .01000 * Math.sin(L - G)
	V = V + .00333 * Math.sin(L + G)
	V = V - .00021 * TT * Math.sin(L)
	U = 1 - .03349 * Math.cos(G)
	U = U - .00014 * Math.cos(2 * L)
	U = U + .00008 * Math.cos(L)
	W = -.00010 - .04129 * Math.sin(2 * L)
	W = W + .03211 * Math.sin(G)
	W = W + .00104 * Math.sin(2 * L - G)
	W = W - .00035 * Math.sin(2 * L + G)
	W = W - .00008 * TT * Math.sin(G)
	return}

//=========================================================	
function subroutine6(Y, D, M)
	{G = 1
	if (Y < 1583) {G = 0}
	D1 = Math.floor(D)
	F = D - D1 - .5
	J = -1 * Math.floor(7 * (Math.floor((M + 9) / 12) + Y) / 4)
	if (G != 0) {S = (M - 9)
	   if (S < 0) {S = -1}
	   else if (S >= 0) {S = 1}
	   A = Math.abs(M - 9)
	   J3 = Math.floor(Y + S * Math.floor(A / 7))
	   J3 = -1 *Math.floor((Math.floor(J3 / 100) + 1) * 3 / 4)}
	J = J + Math.floor(275 * M / 9) + D1 + G * J3
	J = J + 1721027 + 2 * G + 367 * Y
	if (F < 0) {F = F + 1;  J = J - 1}
	return}

//=========================================================	
function convertTime(H3, M3)
	{if (H3 < 10) {H3 = "0" + H3}
	if (M3 < 10) {M3 = "0" + M3}
	timestring = H3 + ":" + M3
	return timestring}
	
//=========================================================
//
// Compute current phase and % illumination of the moon
//
//=========================================================
function calcMoonPhase(moon_year, moon_month, moon_day, moon_hour)
	{moondate = new Date(moon_year, moon_month - 1, moon_day, moon_hour, 00)
	moonmsec = moondate.getTime()
	GMT_time = moonmsec + (moondate.getTimezoneOffset() * 60 * 1000)
	startDate = new Date(1989, 11, 31, 00, 00)
	startMsec = startDate.getTime()
	dmsec = GMT_time - startMsec
	Dmoon  = ((((dmsec /1000) /60) /60) /24)
	Nmoon = Dmoon * (360 / 365.249)
	if (Nmoon > 0) {
    Nmoon = Nmoon - Math.floor(Math.abs(Nmoon / 360)) * 360} 
  else {
    Nmoon = Nmoon + (360 + Math.floor(Math.abs(Nmoon / 360)) * 360)
    }
	Mo = Nmoon + 279.403303 - 282.768422
	if(Mo < 0) {
    Mo = Mo + 360}
  Ec = 360 * .016713 * Math.sin(Mo * deg2Rad) / Math.PI
	lambda = Nmoon + Ec + 279.403303
	if (lambda > 360) {
    lambda = lambda - 360}
	Lmoon = 13.1763966 * Dmoon + 318.351648
	if (Lmoon > 0) {
    Lmoon = Lmoon - Math.floor(Math.abs(Lmoon / 360)) * 360} 
  else {
    Lmoon = Lmoon + (360 + Math.floor(Math.abs(Lmoon / 360)) * 360)};
	Mm = Lmoon - .1114041 * Dmoon - 36.34041
	if (Mm > 0) {
    Mm = Mm - Math.floor(Math.abs(Mm / 360)) * 360} 
  else {
    Mm = Mm + (360 + Math.floor(Math.abs(Mm / 360)) * 360)}
	N65 = 318.510107 - .0529539 * Dmoon
	if (N65 > 0) {
    N65 = N65 - Math.floor(Math.abs(N65 / 360)) * 360} 
  else {
  N65 = N65 + (360 + Math.floor(Math.abs(N65 / 360)) * 360)}
	Ev = 1.2739 * Math.sin((2 * (Lmoon - lambda) - Mm) * deg2Rad)
	Ae = .1858 * Math.sin(Mo * deg2Rad)
	A3 = .37 * Math.sin(Mo * deg2Rad)
	Mmp = Mm + Ev - Ae - A3
	Ec = 6.2886 * Math.sin(Mmp * deg2Rad)
	A4 = .214 * Math.sin((2 * Mmp) * deg2Rad)
	lp = Lmoon + Ev + Ec - Ae + A4
	Vmoon = .6583 * Math.sin((2 * (lp - lambda)) * deg2Rad)
	lpp = lp + Vmoon
	D67 = lpp - lambda
	percent_visible = Math.round(.5 * (1 - Math.cos(D67 * deg2Rad)) * 100000)
	percent_visible = percent_visible / 1000
	trend = (Math.sin(D67 * deg2Rad))
//	if(percent_visible >= 0) {MoonPhase = "New Moon"}
//	if(percent_visible >= 98) {MoonPhase = "Full Moon"}
//	if((percent_visible > 40) && (percent_visible < 60) && (trend > 0)) {MoonPhase = "First Quarter"}
//	if((percent_visible > 40) && (percent_visible < 60) && (trend < 0)) {MoonPhase = "Last Quarter"}             
//	if((percent_visible >= 1) && (percent_visible <= 40) && (trend > 0)) {MoonPhase = "Waxing Crescent"}
//	if((percent_visible >= 1) && (percent_visible <= 40) && (trend < 0)) {MoonPhase = "Waning Crescent"}
//	if((percent_visible >= 60) && (percent_visible <= 98) && (trend > 0)) {MoonPhase = "Waxing Gibbous"}
//	if((percent_visible >= 60) && (percent_visible <= 98) && (trend < 0)) {MoonPhase = "Waning Gibbous"}
  
  MoonTrend = ""
	if(percent_visible >= 0) {MoonPhase = "New Moon"}
	if(percent_visible >= 98) {MoonPhase = "Full"}
	if((percent_visible > 40) && (percent_visible < 60) && (trend > 0)) {MoonPhase = "First Quarter"}
	if((percent_visible > 40) && (percent_visible < 60) && (trend < 0)) {MoonPhase = "Last Quarter"}             
	if((percent_visible >= 1) && (percent_visible <= 40) && (trend > 0)) {MoonPhase = "Evening Crescent"}
	if((percent_visible >= 1) && (percent_visible <= 40) && (trend < 0)) {MoonPhase = "Morning Crescent"}
	if((percent_visible >= 60) && (percent_visible <= 98) && (trend > 0)) {MoonPhase = "Gibbous"}
	if((percent_visible >= 60) && (percent_visible <= 98) && (trend < 0)) {MoonPhase = "Gibbous"}
	if (trend > 0) {MoonTrend = "Waxing"}
	if (trend < 0) {MoonTrend = "Waning"}
	
	if (MoonPhase == "Full")
    {MoonPhase += " "
	   MoonPhase += FullName[UTCMonth]}
	    
	tMoonAge = setDecimal(MoonAge, 0)
	if (tMoonAge > 28)
    {tMoonAge = 0}
  
  MoonDay = PadDigits(tMoonAge,2)  
	
	MoonFile = ('<img src="http://wxnow.net/Common/Luna/luna')+MoonDay+('.gif" alt="Moon Picture">')
	return MoonPhase, MoonTrend}
	
//=========================================================
//
// Computes sun time relative to local noon
//
//=========================================================

function calcSunTime(sunrise, sunset, time_now)
	{solar_noon = (sunrise + sunset)/2 
	SunTime = solar_noon - time_now
	return SunTime}
	
//=========================================================
//
// Computes declension of the sun
//
//=========================================================

function calcSolarDecl(jd) 	{
  cos_eps = 0.917482
	sin_eps = 0.397778				
	T = (jd - 2451545.0) / 36525.0
	M = PI_2 * frac(0.993133 + 99.997361 * T)
	DL = 6893.0 * Math.sin(M) + 72.0 * Math.sin(2.0 * M)
	L = PI_2 * frac(0.7859453 + M / PI_2 + (6191.2 * T + DL) / 1296000)
	SL = Math.sin(L)
	X = Math.cos(L)
	Y = cos_eps * SL
	Z = sin_eps * SL
	R = Math.sqrt(1.0 - Z * Z)
	SunDecl = (360.0 / PI_2) * Math.atan(Z / R)
	return SunDecl}



//=========================================================
//
// Calculates solar altitude angle based on lat, date, time
//
//=========================================================
function calcSolarAltitude(SunDecl, SunTime, StationLat)
	{sun_hour = (15 * SunTime) * deg2Rad
	decl = SunDecl * deg2Rad
	lat = StationLat * deg2Rad
	sin_alt = (Math.cos(lat) * Math.cos(decl) * Math.cos(sun_hour)) + (Math.sin(lat) * Math.sin(decl))
	SunAltitude = Math.asin(sin_alt) * rad2Deg
	SunAltitudeString = (setDecimal(SunAltitude,2))+'°'
	if (SunAltitude < -12)
     {SunAltitudeString = "Dark"}
	return SunAltitude}

//=========================================================
//
// Calculates solar azimuth angle based on lat, date, time
//
//=========================================================
function calcSolarAzimuth(SunDecl, SunTime, StationLat)
	{sun_hour = (15 * SunTime) * deg2Rad
	lat = StationLat * deg2Rad
	decl = SunDecl * deg2Rad
	y_azm = (-1 * (Math.cos(sun_hour)) * Math.cos(decl) * Math.sin(lat)) + (Math.cos(lat) * Math.sin(decl))
	x_azm = Math.sin(sun_hour) * Math.cos(decl)
	SunAzimuth = Math.atan(x_azm/y_azm) * rad2Deg
	if (x_azm > 0 && y_azm > 0) {SunAzimuth = SunAzimuth}
	if (x_azm >= 0 && y_azm <= 0 || x_azm < 0 && y_azm < 0) {SunAzimuth = 180 + SunAzimuth}
	if (x_azm <= 0 && y_azm >= 0) {SunAzimuth = 360 + SunAzimuth}
	return SunAzimuth}

//=========================================================
//
// Calculates max possible radiation for given sun angle
//
//=========================================================
function calcMaxSolar(SunElev)
	{MaxPossSolarRad = solar_constant * Math.sin(SunElev * deg2Rad)
	attenuation = MaxPossSolarRad * (solar_attenuation_factor * Math.sqrt(Math.cos(SunElev * deg2Rad) + 1))
	MaxPossSolarRad = MaxPossSolarRad - attenuation
	if (MaxPossSolarRad < 0) {MaxPossSolarRad = 0}
	return MaxPossSolarRad}


//=========================================================
//
// Computes sunrise and sunset for a given date
//
//=========================================================
function calcSunRiseSet(SolarYear, SolarMonth, SolarDay, SolarEvent, TestElev) 	{
  sh = Math.sin((deg2Rad*TestElev)*-1)
	RISE = false
	SET = false
	if (SolarEvent == "Rise") {
    StartHour = EarliestUTCSunrise}
	if (SolarEvent == "Set") {
    StartHour = EarliestUTCSunset} 
	SolarHour = StartHour
	
	while (SolarHour <= StartHour + SolsticeMaxSunRiseSetDiff)   
	  { 
		jd = calcJulDay (SolarYear, SolarMonth, SolarDay, SolarHour)
		dec = calcSolarDecl(jd)
		gha = computeGHA (SolarDay, SolarMonth, SolarYear, SolarHour)	
		Y0 = Math.sin(deg2Rad*computeHeight(SunDecl, StationLat, StationLong, gha)) - sh
		gha = computeGHA (SolarDay, SolarMonth, SolarYear, SolarHour + 1.0)
		yPlus = Math.sin(deg2Rad*computeHeight(SunDecl, StationLat, StationLong, gha)) - sh
		gha = computeGHA (SolarDay, SolarMonth, SolarYear, SolarHour - 1.0)
		yMinus = Math.sin(deg2Rad*computeHeight(SunDecl, StationLat, StationLong, gha)) - sh
		calcQuadratic(yMinus,yPlus)
		switch (NZ){
			case 1:	if (yMinus < 0.0) {
                  UTSunrise = SolarHour + zero1; RISE = true}
              else {
                  UTSunset = SolarHour + zero1; SET = true}
			break
	
			case 2:	if (YE < 0.0) {
                  UTSunrise = SolarHour + zero2; UTSunset = SolarHour + zero1}
              else {
                  UTSunrise = SolarHour + zero1; UTSunset = SolarHour + zero2}
			RISE = true
			SET = true
			break}
			
    if (SolarEvent == "Rise" && RISE == true) {
      return}
    if (SolarEvent == "Set" && SET == true) {
      return}	
    SolarHour++}
    }

//=========================================================
function calcQuadratic(yMinus, yPlus) {
	NZ = 0
	A = 0.5 * (yMinus + yPlus) - Y0
	B = 0.5 * (yPlus - yMinus)
	C = Y0
	XE = -B / (2 * A)
	YE = (A * XE + B)* XE + C
	DIS = B * B - 4.0 * A * C
	if (DIS>=0) {
		DX = 0.5 * Math.sqrt(DIS) / Math.abs(A)
		zero1 = XE - DX
		zero2 = XE + DX
		if (Math.abs(zero1) <= 1.0) {
      NZ = NZ + 1}
		if (Math.abs(zero2) <= 1.0) {
      NZ = NZ + 1}
		if (zero1 < -1.0) {
      zero1 = zero2}
      }
    }

//=========================================================	 
function computeHeight(de, la, lo, gh) {
	lat_K = la * deg2Rad
	dec_K = de * deg2Rad
	x = Number(gh) + Number(lo)
	sinHeight = Math.sin(dec_K) * Math.sin(lat_K) + Math.cos(dec_K) * Math.cos(lat_K) * Math.cos(deg2Rad*x)	
	return Math.asin(sinHeight) / deg2Rad}

//=========================================================	 
function computeGHA(T, M, J, STD) 
	{N = 365 * J + T + 31 * M - 46	 
	if (M < 3) {N = N + Math.floor((J-1)/4)} else {N = N - Math.floor(0.4 * M + 2.3) + Math.floor(J/4)}		 
	P = STD / 24.0
	X = (P + N - 7.22449E5) * 0.98564734 + 279.306
	X = X * (deg2Rad)
	XX = -104.55 * Math.sin(X) - 429.266 * Math.cos(X) + 595.63 * Math.sin(2.0 * X) - 2.283 * Math.cos(2.0 * X)
	XX = XX + 4.6 * Math.sin(3.0 * X) + 18.7333 * Math.cos(3.0 * X)
	XX = XX - 13.2 * Math.sin(4.0 * X) - Math.cos(5.0 * X) - Math.sin(5.0 * X) / 3.0 + 0.5 * Math.sin(6.0 * X) + 0.231
	XX = XX / 240.0 + 360.0 * (P + 0.5)
	if (XX > 360.0) XX = XX - 360.0
	return XX}

//=========================================================	 
function calcJulDay(yr, mo, dy, UT) {
	if (mo <= 2) {
    mo = mo + 12, yr = yr - 1}
	B = Math.floor(yr / 400.0) - Math.floor(yr / 100.0)  + Math.floor(yr / 4.0)
	A = 365.0 * yr - 679004.0
	jd = A + B + Math.floor(30.6001 * (mo + 1)) + dy + UT / 24.0
	jd = jd + 2400000.5
	return jd}
	
function getMoonAge(jd)	
  {
  MoonIteration = normaliseValue( (jd - 2451550.1 ) / 29.530588853 );
  MoonCycle = Math.floor( (jd - 2451550.1 ) / 29.530588853 ) + 953
  MoonAge = MoonIteration*29.530588853;
  return}

function normaliseValue( v )
  {
  v = v - Math.floor( v  ); 
  if( v < 0 )
      {v = v + 1;}
        
    return v}


