/*
Programming and DHTML graphics:
(c) Adrian R. Ashford.
Latest revision: November 12th, 2007.
Used with permission by SkyandTelescope.com
Modified July 15, 2009 by DAF to allow dates up to 2019 instead of stopping at Jan 2009.

Date & time validation scripts:
Sandeep Tamhankar
stamhankar@hotmail.com
http://javascript.internet.com
*/

function grab_UTC()
{
var nowdate = new Date();
var utc_day = nowdate.getUTCDate();
var utc_month = nowdate.getUTCMonth() + 1;
var utc_year = nowdate.getUTCFullYear();
zone = nowdate.getTimezoneOffset() / 1440;
document.triton.timezone.value = -(zone * 24);
var utc_hours = nowdate.getUTCHours();
var utc_mins = nowdate.getUTCMinutes();
var utc_secs = nowdate.getUTCSeconds();
utc_mins += utc_secs / 60.0;
utc_mins = Math.floor((utc_mins + 0.5));
if (utc_mins < 10) utc_mins = "0" + utc_mins;
if (utc_mins > 59) utc_mins = 59;
if (utc_hours < 10) utc_hours = "0" + utc_hours;
if (utc_month < 10) utc_month = "0" + utc_month;
if (utc_day < 10) utc_day = "0" + utc_day;
document.triton.date_txt.value = utc_month + "/" + utc_day + "/" + utc_year;
document.triton.ut_h_m.value = utc_hours + ":" + utc_mins;
}

function setup()
{
grab_UTC();
view_type = "reflector";
reset_flag = 0;
triton();
}

function reloaded()
{
grab_UTC();
triton();
}

function proper_ang(big)
{
with (Math)
{
var tmp = 0;
if (big > 0)
{
tmp = big / 360.0;
tmp = (tmp - floor(tmp)) * 360.0;
}
else
{
tmp = ceil(abs(big / 360.0));
tmp = big + tmp * 360.0;
}
}
return tmp;
}

function round_10(num)
{
return Math.floor((num + 0.05) * 10) / 10;
}

function round_100(num)
{
return Math.floor((num + 0.005) * 100) / 100;
}

function isValidDate(dateStr)
{ 
if (IsValidTime(document.triton.ut_h_m.value) == true)
{
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null) { 
alert("Date is not in a valid format.") 
return false; 
} 
var month = matchArray[1];
var day = matchArray[3]; 
var year = matchArray[4];
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31."); 
return false; 
} 
if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
alert("Month " + month + " doesn't have 31 days!") 
return false 
} 
if (month == 2) {
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
if (day > 29 || (day == 29 && !isleap))
{ 
alert("February " + year + " doesn't have " + day + " days!"); 
return false; 
} 
} 
if (month < 10 && month.length == 1) month = "0" + month;
if (day < 10 && day.length == 1) day = "0" + day;
document.triton.date_txt.value = month + "/" + day + "/" + year;
var dt_str = document.triton.date_txt.value;
if ((dt_str.substring(2,3) != "/") || (dt_str.substring(5,6) != "/"))
{
alert ("Date is not in a valid format.");
return false;
}
var jd = julian_date();
if (jd < 2454405.5 || jd > 2458849.5)
{
alert ("Please choose a date between 1 November 2007 and 31 December 2019.");
return false;
}
triton();
return true;
}
else
{
return false;
}
}

function IsValidTime(timeStr)
{
var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
var matchArray = timeStr.match(timePat);
if (matchArray == null)
{
alert("Time is not in a valid format.");
return false;
}
var hour = matchArray[1];
var minute = matchArray[2];
if (hour < 0 || hour > 23)
{
alert("Hour must be between 0 and 23.");
return false;
}
if (minute < 0 || minute > 59)
{
alert ("Minute must be between 0 and 59.");
return false;
}
if (hour < 10 && hour.length == 1) hour = "0" + hour;
if (minute < 10 && minute.length == 1) minute = "0" + minute;
document.triton.ut_h_m.value = hour + ":" + minute;
var tm_str = document.triton.ut_h_m.value;
if ((tm_str.substring(2,3) != ":") && (dt_str.length != 5))
{
alert ("Time is not in a valid format.");
return false;
}
return true;
}

function time_change(tmp)
{
if (isValidDate(document.triton.date_txt.value) == true)
{
var jd_temp, zz, ff, alpha, aa, bb, cc, dd, ee;
var calendar_day, calendar_month, calendar_year;
var int_day, hours, minutes;

var tm_as_str, ut_hrs, ut_mns, frac_day;

var jd = julian_date();

tm_as_str = document.triton.ut_h_m.value;
ut_hrs = eval(tm_as_str.substring(0,2));
ut_mns = eval(tm_as_str.substring(3,5));
frac_day = ut_hrs / 24.0 + ut_mns / 1440.0;

with (Math) {

jd_temp = jd + frac_day + tmp / 24.0 + 0.5;

zz = floor(jd_temp);
ff = jd_temp - zz;
alpha = floor((zz - 1867216.25) / 36524.25);
aa = zz + 1 + alpha - floor(alpha / 4);
bb = aa + 1524;
cc = floor((bb - 122.1) / 365.25);
dd = floor(365.25 * cc);
ee = floor((bb - dd) / 30.6001);
calendar_day = bb - dd - floor(30.6001 * ee) + ff;
calendar_month = ee;
if (ee < 13.5) calendar_month = ee - 1;
if (ee > 13.5) calendar_month = ee - 13;
calendar_year = cc;
if (calendar_month > 2.5) calendar_year = cc - 4716;
if (calendar_month < 2.5) calendar_year = cc - 4715;
int_day = floor(calendar_day);
hours = (calendar_day - int_day) * 24;
minutes = floor((hours - floor(hours)) * 60 + 0.5);
hours = floor(hours);
if (minutes > 59)
{minutes = 0; hours = hours + 1;}
if (calendar_month < 10) calendar_month = "0" + calendar_month;
if (int_day < 10) int_day = "0" + int_day;
if (hours < 10) hours = "0" + floor(hours);
if (minutes < 10) minutes = "0" + minutes;
}
document.triton.date_txt.value = calendar_month + "/" + int_day + "/" + calendar_year;
document.triton.ut_h_m.value = hours + ":" + minutes;
triton();
return true;
}
else
{
return false;
}
}

function julian_date()
{
var dt_as_str, mm, dd, yy;
var yyy, mmm, a, b;

dt_as_str = document.triton.date_txt.value;
mm = eval(dt_as_str.substring(0,2));
dd = eval(dt_as_str.substring(3,5));
yy = eval(dt_as_str.substring(6,10));

with (Math) {
var yyy=yy;
var mmm=mm;
if (mm < 3)
{
yyy = yy - 1;
mmm = mm + 12;
}
a = floor(yyy/100);
b = 2 - a + floor(a/4);

return floor(365.25*yyy) + floor(30.6001*(mmm+1)) + dd + 1720994.5 + b;
}
}

function isReflector()
{
if (isValidDate(document.triton.date_txt.value) == true)
{
view_type = "reflector";
reset_flag = 1;
if (document.all)
{
document.all.direct.style.zIndex = 1;
document.all.inverted.style.zIndex = 20;
document.all.mirrored.style.zIndex = 1;
}
else
{
if (document.layers)
{
document.layers["direct"].zIndex = 1;
document.layers["inverted"].zIndex = 20;
document.layers["mirrored"].zIndex = 1;
}
else
{
if (document.getElementById)
{
document.getElementById("direct").style.zIndex = 1;
document.getElementById("inverted").style.zIndex = 20;
document.getElementById("mirrored").style.zIndex = 1;
}
}
}
triton();
return true;
}
else
{
return false;
}
}

function isRefractor()
{
if (isValidDate(document.triton.date_txt.value) == true)
{
view_type = "refractor";
reset_flag = 1;
if (document.all)
{
document.all.direct.style.zIndex = 1;
document.all.inverted.style.zIndex = 1;
document.all.mirrored.style.zIndex = 20;
}
else
{
if (document.layers)
{
document.layers["direct"].zIndex = 1;
document.layers["inverted"].zIndex = 1;
document.layers["mirrored"].zIndex = 20;
}
else
{
if (document.getElementById)
{
document.getElementById("direct").style.zIndex = 1;
document.getElementById("inverted").style.zIndex = 1;
document.getElementById("mirrored").style.zIndex = 20;
}
}
}
triton();
return true;
}
else
{
return false;
}
}

function isBinocular()
{
if (isValidDate(document.triton.date_txt.value) == true)
{
view_type = "binocular";
reset_flag = 1;
if (document.all)
{
document.all.direct.style.zIndex = 20;
document.all.inverted.style.zIndex = 1;
document.all.mirrored.style.zIndex = 1;
}
else
{
if (document.layers)
{
document.layers["direct"].zIndex = 20;
document.layers["inverted"].zIndex = 1;
document.layers["mirrored"].zIndex = 1;
}
else
{
if (document.getElementById)
{
document.getElementById("direct").style.zIndex = 20;
document.getElementById("inverted").style.zIndex = 1;
document.getElementById("mirrored").style.zIndex = 1;
}
}
}
triton();
return true;
}
else
{
return false;
}
}

function triton()
{

with (Math)
{
var dt = 65;
var p2 = 2 * PI;
var RAD = 180 / PI;

var jd = julian_date();

var tm_as_str = document.triton.ut_h_m.value;
var ut_hrs = eval(tm_as_str.substring(0,2));
var ut_mns = eval(tm_as_str.substring(3,5));
var frac_day = ut_hrs / 24.0 + ut_mns / 1440.0;

var day_start = jd - 2451545.0 + dt / 86400;

var days = day_start + frac_day;

var d = days;

var de = 37.5;
/*
var pa = 315.7;
*/
var pa = 315.7;
var ad = 2.3;
var scal_fac = 12.2;

var triton_sep = 16.8;
var triton_elong = 2454696.487333;

var triton_rad = triton_sep / ad * scal_fac;

var temp = jd + frac_day + dt / 86400;

var triton_ang = proper_ang((temp - triton_elong) / 5.8765 * 360.0 - 90) / RAD;

var x_triton = triton_rad * sin(triton_ang);
var y_triton = triton_rad * cos(triton_ang) * sin(de / RAD);

var triton_layer = 9;

if (1.571 < triton_ang && triton_ang < 4.712) triton_layer = 10;

var triton_dist = sqrt(pow(x_triton,2) + pow(y_triton,2));

var triton_pos_ang = proper_ang(atan2(x_triton,y_triton) * RAD + pa - 180);

var triton_dist_arcsec = (triton_dist - 6.225) / scal_fac * ad;

x_triton = triton_dist * sin((triton_pos_ang - 180) / RAD);
y_triton = triton_dist * cos((triton_pos_ang - 180) / RAD);

if (reset_flag == 0)
{
if (document.all)
{
document.all.chooseit.style.zIndex = 21;
}
else
{
if (document.layers)
{
document.layers["chooseit"].zIndex = 21;
}
else
{
if (document.getElementById)
{
document.getElementById("chooseit").style.zIndex = 21;
}
}
}
}
else
{
if (document.all)
{
document.all.chooseit.style.zIndex = 1;
}
else
{
if (document.layers)
{
document.layers["chooseit"].zIndex = 1;
}
else
{
if (document.getElementById)
{
document.getElementById("chooseit").style.zIndex = 1;
}
}
}
}

if (view_type == "reflector")
{
x_triton = 408 - x_triton - 3;
y_triton = 151 - y_triton - 1;

if (document.all)
{
document.all.moontriton.style.pixelLeft = x_triton;
document.all.moontriton.style.pixelTop = y_triton;
document.all.moontriton.style.zIndex = triton_layer;
}
else
{
if (document.layers)
{
document.layers["moontriton"].left = x_triton;
document.layers["moontriton"].top = y_triton;
document.layers["moontriton"].zIndex = triton_layer;
}
else
{
if (document.getElementById)
{
document.getElementById("moontriton").style.left = x_triton;
document.getElementById("moontriton").style.top = y_triton;
document.getElementById("moontriton").style.zIndex = triton_layer;
}
}
}
}

if (view_type == "refractor")
{
x_triton = 408 - x_triton - 3;
y_triton = 151 + y_triton - 1;

if (document.all)
{
document.all.moontriton.style.pixelLeft = x_triton;
document.all.moontriton.style.pixelTop = y_triton;
document.all.moontriton.style.zIndex = triton_layer;
}
else
{
if (document.layers)
{
document.layers["moontriton"].left = x_triton;
document.layers["moontriton"].top = y_triton;
document.layers["moontriton"].zIndex = triton_layer;
}
else
{
if (document.getElementById)
{
document.getElementById("moontriton").style.left = x_triton;
document.getElementById("moontriton").style.top = y_triton;
document.getElementById("moontriton").style.zIndex = triton_layer;
}
}
}
}

if (view_type == "binocular")
{
x_triton = 408 + x_triton - 3;
y_triton = 151 + y_triton - 1;

if (document.all)
{
document.all.moontriton.style.pixelLeft = x_triton;
document.all.moontriton.style.pixelTop = y_triton;
document.all.moontriton.style.zIndex = triton_layer;
}
else
{
if (document.layers)
{
document.layers["moontriton"].left = x_triton;
document.layers["moontriton"].top = y_triton;
document.layers["moontriton"].zIndex = triton_layer;
}
else
{
if (document.getElementById)
{
document.getElementById("moontriton").style.left = x_triton;
document.getElementById("moontriton").style.top = y_triton;
document.getElementById("moontriton").style.zIndex = triton_layer;
}
}
}
}

}
}
