//---------------------------------------------------------
// Define constants
//---------------------------------------------------------
latitude = 51.188
longitude = 0.27
pie = Math.PI
degrees_to_radians = pie / 180.0
radians_to_degrees = 1 / degrees_to_radians
solar_constant = 1367
attenuation_factor =-0.047
solar_zero_offset = 285

//---------------------------------------------------------
// Set dates for daylight time
//---------------------------------------------------------
DST_start = new Array(10)  // MMDD that DST begins
DST_start[2005] = "0403"
DST_start[2006] = "0402"
DST_start[2007] = "0318" 
DST_start[2008] = "0316"
DST_start[2009] = "0315"
DST_start[2010] = "0314"
DST_start[2011] = "0320"
DST_start[2012] = "0318"
DST_end = new Array(10)  // MMDD that DST ends
DST_end[2005] = "1030"
DST_end[2006] = "1029"
DST_end[2007] = "1104"  
DST_end[2008] = "1102"
DST_end[2009] = "1101"
DST_end[2010] = "1107"
DST_end[2011] = "1106"
DST_end[2012] = "1104"

//---------------------------------------------------------
// Set starting day of the year
//---------------------------------------------------------
first_day_of_year = new Array(10)
first_day_of_year[2005] = 7 //Saturday
first_day_of_year[2006] = 1 //Sunday
first_day_of_year[2007] = 2 //Monday
first_day_of_year[2008] = 3 //Tuesday
first_day_of_year[2009] = 5 //Thursday
first_day_of_year[2010] = 6 //Friday
first_day_of_year[2011] = 7 //Saturday
first_day_of_year[2012] = 1 //Sunday

//---------------------------------------------------------
// Set text names for months
//---------------------------------------------------------
text_month = new Array(13)
text_month[1] = "January"
text_month[2] = "February"
text_month[3] = "March"
text_month[4] = "April"
text_month[5] = "May"
text_month[6] = "June"
text_month[7] = "July"
text_month[8] = "August"
text_month[9] = "September"
text_month[10] = "October"
text_month[11] = "November"
text_month[12] = "December"

//---------------------------------------------------------
// Set number of days in each month
//---------------------------------------------------------
days_in_month = new Array(13)
days_in_month[1] = 31
days_in_month[2] = 28
days_in_month[3] = 31
days_in_month[4] = 30
days_in_month[5] = 31
days_in_month[6] = 30
days_in_month[7] = 31
days_in_month[8] = 31
days_in_month[9] = 30
days_in_month[10] = 31
days_in_month[11] = 30
days_in_month[12] = 31

//---------------------------------------------------------
// Set text names for days of week
//---------------------------------------------------------
text_day = new Array(8)
text_day[1] = "Sunday"
text_day[2] = "Monday"
text_day[3] = "Tuesday"
text_day[4] = "Wednesday"
text_day[5] = "Thursday"
text_day[6] = "Friday"
text_day[7] = "Saturday"

//---------------------------------------------------------
// Set suffix for numeric days of month
//---------------------------------------------------------
function dateSuffix(numeric_day)
  {suffix = "th"
   if (numeric_day == 1) {suffix = "st"}
   if (numeric_day == 2) {suffix = "nd"}
   if (numeric_day == 3) {suffix = "rd"}
   if (numeric_day == 21) {suffix = "st"}
   if (numeric_day == 22) {suffix = "nd"}
   if (numeric_day == 23) {suffix = "rd"}
   if (numeric_day == 31) {suffix = "st"}
   return suffix}

//---------------------------------------------------------
// Truncates numeric value to specified number of places
//---------------------------------------------------------
function setDecimal(value, places)
{if (value < 0) {sign = "-"} else {sign = ""}
value = Math.abs(value)
factor = Math.pow(10, places)
valInt = Math.round(value * factor)
if (places == 0 && valInt == 0) {sign = ""}
if (places == 0) {return sign + valInt}
valStr = valInt.toString(10)
len = valStr.length
radix = len - places - 1
radShift = 0
if (radix < 0) {radShift = -radix, radix = 0}
for (i = 0; i < radShift; i++) {valStr = "0" + valStr}
intStr = valStr.substring(0, radix + 1)
decStr = valStr.substring(radix + 1, len + radShift)
valStr = sign + intStr + "." + decStr
return valStr}

//---------------------------------------------------------
// Converts Farenheit to Celsius
//---------------------------------------------------------
function Far2Cel(tFar)
{tCel = (tFar-32) * (5/9)
return tCel}

//---------------------------------------------------------
// Converts Celsius to Farenheit
//---------------------------------------------------------
function Cel2Far (tCel)
{tFar = tCel * (9/5) + 32
return tFar}

//---------------------------------------------------------
// Converts inches to millimeters
//---------------------------------------------------------
function Inches2mm (inches)
{mm = inches * 25.4
return mm}

//---------------------------------------------------------
// Converts millibars to inches of mercury
//---------------------------------------------------------
function mb2inches(pressMB)
{pressIN = pressMB/33.864
return pressIN}

//---------------------------------------------------------
// Converts miles per hour to kilometers per hour
//---------------------------------------------------------
function mph2kph(speedMPH)
{speedKPH = speedMPH/0.621
return speedKPH}

function calcMixRatio(pressure, temp, dewpoint, elevation)
//*************************************************************************************************
//Computes mixing ratio (g/kg) from pressure (mb), temp (Far), dewPt (Far) and station elev (Ft)
//*************************************************************************************************
{	t = temp
	td = dewpoint
	altitude = (elevation/3.28)*.001 //station elevation in km
	station_pressure = pressure * Math.pow(2.71828, -altitude/8.5) //reduce pressure for station elevation
	vp_sat = 6.112 * Math.pow(10, (7.5 * t) / (237.7 + t))
	vp = 6.112 * Math.pow(10, (7.5 * td) / (237.7 + td))
	sat_mix_ratio = 621.97 * (vp_sat/(station_pressure - vp_sat))
	mix_ratio = (vp/vp_sat) * sat_mix_ratio
	return mix_ratio}

function calcMixRatio2(pressure, temp, dewpoint, elevation)
//*************************************************************************************************
//Computes mixing ratio (g/kg) from pressure (mb), temp (Far), dewPt (Far) and station elev (Ft)
//*************************************************************************************************
{	t = temp
	td = dewpoint
	altitude = (elevation/3.28)*.001 //station elevation in km
	station_pressure = pressure * Math.pow(2.71828, -altitude/8.5) //reduce pressure for station elevation
	vp_sat = 6.112 * Math.pow(10, (7.5 * t) / (237.7 + t))
	vp = 6.112 * Math.pow(10, (7.5 * td) / (237.7 + td))
	sat_mix_ratio = 621.97 * (vp_sat/(station_pressure - vp_sat))
	mix_ratio = (vp/vp_sat) * sat_mix_ratio
	return vp}

//---------------------------------------------------------
// Computes wet-bulb temperature
//---------------------------------------------------------
function calcWetbulb(pressure_mb, temp_far, dewpoint_far)
{temp_celsius = temp_far
dewpoint_celsius = dewpoint_far
tmin = Math.min(dewpoint_celsius, temp_celsius)
tmax = Math.max(dewpoint_celsius, temp_celsius)
vapor_pressure = 6.112 * Math.pow(10, (7.5 * dewpoint_celsius) / (237.7 + dewpoint_celsius))
while (true)
   {tcur = (tmax + tmin) / 2
   vpcur = 6.112 * Math.pow(10, (7.5 * tcur) / (237.7 + tcur))
   peq = 0.00066 * (1+0.00155 * tcur) * pressure_mb * (temp_celsius - tcur)
   diff = peq - vpcur + vapor_pressure
   if (Math.abs(diff) < 0.01) break
   if (diff < 0) tmax = tcur
   else tmin = tcur}
wetbulb_far = tcur    
return wetbulb_far}

//---------------------------------------------------------
// Calculates monthly precip departure
//---------------------------------------------------------
function monthlyDepart(monthprecip, dayofmonth, monthofyear) 
{normaltodate = 0
i = 1
while (i <= dayofmonth) {normaltodate = normaltodate + parseFloat(normals [monthofyear] [i].substring(6,8)) / 100, i++}
departmonth = monthprecip - normaltodate
return departmonth}

//---------------------------------------------------------
// Calculates yearly precip departure
//---------------------------------------------------------
function yearlyDepart(monthofyear, yearprecip, norm_month_to_date)
{   normal_prior_rain = 0
i = 1
while (i < monthofyear) {normal_prior_rain = normal_prior_rain + monthly_avg_rain[i], i++}
normaltotal = normal_prior_rain + norm_month_to_date
departyear = yearprecip - normaltotal
return departyear} 

//---------------------------------------------------------
// Calculates monthly snowfall departure
//---------------------------------------------------------
function monthlySnowDepart(monthsnow, dayofmonth, monthofyear) 
{normalsnowtodate = (monthly_avg_snow[monthofyear] / days_in_month[monthofyear]) * dayofmonth
departsnow = monthsnow - normalsnowtodate
return departsnow}

//---------------------------------------------------------
// Calculates yearly snowfall departure
//---------------------------------------------------------
function seasonSnowDepart(monthofyear, seasonsnow, norm_month_to_date)
{i = 7
normal_through_dec = 0
while (i <= 12) {normal_through_dec = normal_through_dec + monthly_avg_snow[i], i++}
if (monthofyear < 7) {i = 1, normal_prior_snow = normal_through_dec} else {i = 7, normal_prior_snow = 0}
while (i < monthofyear) {normal_prior_snow = normal_prior_snow + monthly_avg_snow[i], i++}
normalsnow = normal_prior_snow + norm_month_to_date
departseason = seasonsnow - normalsnow
return departseason} 

//---------------------------------------------------------
// Determines if it is a leap year
//---------------------------------------------------------
function LeapYear(current_year)
{a = current_year / 4
aa = Math.floor(a)
if (a != aa) {return}
b = current_year / 100
bb = Math.floor(b)
a = current_year / 400
aa = Math.floor(a)
if (b == bb) {if (a != aa) {return}}
b = current_year / 1000
bb = Math.floor(b)
a = current_year / 4000
aa = Math.floor(a)
if (b == bb) {if (a == aa) {return}}
days_in_month[2] = 29
return}

//---------------------------------------------------------
// Calculates Julian date
//---------------------------------------------------------
function calcJday(current_month, current_day, current_year)
{LeapYear (current_year)
i = 1
prior_days = 0
while (i < current_month) {prior_days = prior_days + days_in_month[i], i++}
Jday = (prior_days + current_day)
return Jday}

//---------------------------------------------------------
// Sets GMT offset
//---------------------------------------------------------
function setGMToffset (current_year, current_month, current_day)
{GMT_offset = 0
DST_start_month = DST_start[current_year].substring(0,2)
DST_start_day = DST_start[current_year].substring(2,4)
DST_end_month = DST_end[current_year].substring(0,2)
DST_end_day = DST_end[current_year].substring(2,4)
if (current_month > DST_start_month && current_month < DST_end_month) {GMT_offset = 0}
if (current_month == DST_start_month && current_day > DST_start_day) {GMT_offset = 0}
if (current_month == DST_start_month && current_day == DST_start_day && hour >= 2) {GMT_offset = 0}
if (current_month == DST_end_month && current_day < DST_end_day) {GMT_offset = 0}
if (current_month == DST_end_month && current_day == DST_end_day && hour >= 2) {GMT_offset = 0}
return GMT_offset}

//---------------------------------------------------------
// Calculates day of week from Julian date
//---------------------------------------------------------
function calcDayOfWeek(Julian_day, current_year)
{week = Math.floor(Julian_day / 7)
day_in_week = Julian_day - (7 * week) + first_day_of_year[current_year] - 1
if (day_in_week > 7) {day_in_week = day_in_week - 7}
return text_day[day_in_week]}

//---------------------------------------------------------
// Computes sun time relative to local noon
//---------------------------------------------------------
function sunTime(sunrise, sunset, time)
{solar_noon = (sunrise + sunset)/2 
suntime = (solar_noon - time)/60
return suntime}

//---------------------------------------------------------
// Calculates solar altitude angle based on lat, date, time
//---------------------------------------------------------
function solarAltitude(Julian_day, suntimeX, latitude)
{sun_hour = (15 * suntimeX) * degrees_to_radians
//decl = 23.45 * Math.sin((Julian_day + solar_zero_offset) * (360/365) * degrees_to_radians)
decl = 23.45 * Math.sin((Julian_day + solar_zero_offset) * (360.5/365) * degrees_to_radians)
//decl = -9.007
decl = decl * degrees_to_radians
lat = latitude * degrees_to_radians
sin_alt = (Math.cos(lat) * Math.cos(decl) * Math.cos(sun_hour)) + (Math.sin(lat) * Math.sin(decl))
altitude_angle = Math.asin(sin_alt) * radians_to_degrees
return altitude_angle}

//---------------------------------------------------------
// Calculates solar azimuth angle based on lat, date, time
//---------------------------------------------------------
function solarAzimuth(Julian_day, suntimeX, latitude)
{sun_hour = (15 * suntimeX) * degrees_to_radians
decl = 23.45 * Math.sin((Julian_day + solar_zero_offset) * (360/365) * degrees_to_radians)
decl = decl * degrees_to_radians
lat = latitude * degrees_to_radians
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)
azimuth_angle = Math.atan(x_azm/y_azm) * radians_to_degrees
if (x_azm > 0 && y_azm > 0) {azimuth_angle = azimuth_angle}
if (x_azm >= 0 && y_azm <= 0 || x_azm < 0 && y_azm < 0) {azimuth_angle = 180 + azimuth_angle}
if (x_azm <= 0 && y_azm >= 0) {azimuth_angle = 360 + azimuth_angle}
return azimuth_angle}

//---------------------------------------------------------
// Calculates max possible radiation for given sun angle
//---------------------------------------------------------
function maxSolar(solar_elevation)
{maxradpossible = solar_constant * Math.sin(solar_elevation * degrees_to_radians)
attenuation = maxradpossible * attenuation_factor * Math.cos(solar_elevation * degrees_to_radians)
maxradpossible = maxradpossible - attenuation
if (maxradpossible < 0) {maxradpossible = 0}
return maxradpossible}

//---------------------------------------------------------
// Computes moonrise and set for a given location & date
//---------------------------------------------------------
function moontimes(Y, M, D)
{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"
GMT_offset = setGMToffset(Y, M, D)
rise = ""
set = ""
Day = D
Month = M
m[M] = days_in_month[M]
Year = Y
mr = 13
B5 = parseFloat(latitude)
L5 = parseFloat(longitude)
H = parseFloat(GMT_offset)
Y = parseFloat(Y)
M = parseFloat(M)
D = parseFloat(D)
P2 = 2*pie
DR = pie / 180
R1 = DR
K1 = 15 * DR * 1.0027379
P1 = pie
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(var 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 + 2 * pie}}
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 - latitude + 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/percent illumination of the moon
//---------------------------------------------------------
function calcMoonPhase()
{moondate = new Date();
moonmsec = moondate.getTime();
GMT_time = moonmsec + (moondate.getTimezoneOffset() * 60 * 1000);
startDate = new Date(89, 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 * pie / 180) / pie;
lamda = Nmoon + Ec + 279.403303;
if (lamda > 360) {lamda = lamda - 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 - lamda) - Mm) * pie / 180);
Ae = .1858 * Math.sin(Mo * pie / 180);
A3 = .37 * Math.sin(Mo * pie / 180);
Mmp = Mm + Ev - Ae - A3;
Ec = 6.2886 * Math.sin(Mmp * pie / 180);
A4 = .214 * Math.sin((2 * Mmp) * pie / 180);
lp = Lmoon + Ev + Ec - Ae + A4;
Vmoon = .6583 * Math.sin((2 * (lp - lamda)) * pie / 180);
lpp = lp + Vmoon;
D67 = lpp - lamda;
illum = .5 * (1 - Math.cos(D67 * pie / 180));
illum_trend = (Math.sin(D67 * pie / 180));
if(illum <= .010) {MoonPhase = "New Moon"};
if((illum > .45) && (illum <= .55) && (illum_trend > 0)) {MoonPhase = "First Quarter"};
if((illum > .45) && (illum <= .55) && (illum_trend < 0)) {MoonPhase = "Last Quarter"};              
if(illum >= .99) {MoonPhase = "Full Moon"};
if((illum > .014) && (illum <= .45) && (illum_trend > 0)) {MoonPhase = "Waxing Crescent"};
if((illum > .014) && (illum <= .45) && (illum_trend < 0)) {MoonPhase = "Waning Crescent"};
if((illum > .55) && (illum < .99) && (illum_trend > 0)) {MoonPhase = "Waxing Gibbous"};
if((illum > .55) && (illum < .99) && (illum_trend < 0)) {MoonPhase = "Waning Gibbous"};
percent_visible = setDecimal(illum * 100,0);
return MoonPhase};
