/*
Scope Calculator v1.0JavaScript copyright (c) Adrian R. Ashford, November 21st 2007.

Used with permission by Sky & Telescope magazine.*/
function round_fifth(num){return Math.floor(num / 5 + 0.5) * 5;}

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 round_1000(num){return Math.floor((num + 0.0005) * 1000) / 1000;}function scope_calc(){with (Math){var aperture = parseFloat(document.scope.aperture.value);
if(isNaN(aperture)){alert("Please enter a valid figure for the telescope's aperture.");
return;}
document.scope.aperture.value = aperture;

var inch_or_mm = document.scope.metric.value;
aperture = aperture * inch_or_mm;
var focal_ratio = parseFloat(document.scope.focalratio.value);
if(isNaN(focal_ratio)){alert("Please enter a valid figure for the telescope's focal ratio.");
return;}
document.scope.focalratio.value = focal_ratio;
var eyepiece_focal_length = parseFloat(document.scope.eyepiecefocallength.value);
if(isNaN(eyepiece_focal_length)){alert("Please enter a valid figure for the eyepiece's focal length.");
return;}
document.scope.eyepiecefocallength.value = eyepiece_focal_length;
var eyepiece_apparent_field = parseFloat(document.scope.apparentfield.value);
var barlow_factor = parseFloat(document.scope.barlowlens.value);
var focal_reducer = parseFloat(document.scope.focalreducer.value);
var focal_length = aperture * focal_ratio * barlow_factor * focal_reducer;var focal_ratio = round_10(focal_length / aperture);var magnification = focal_length / eyepiece_focal_length;var true_field = round_10(eyepiece_apparent_field / magnification);var exit_pupil = round_10(aperture / magnification);var resolving_power = round_100(115.824 / aperture);

var M = magnification;
if (magnification > (22 * aperture / 25.4)) M = 22 * aperture / 25.4;
if (magnification > 1200) M = 1200;

var limiting_magnitude = round_10(6.5 + 2.5 * log(M * aperture / 25.4) / log(10));

if (eyepiece_apparent_field == 100 && eyepiece_focal_length != 13){alert("The TeleVue Ethos is only available in a 13mm focal length.");
return;}if (magnification > aperture * 2){alert("This is a high magnification for such an aperture. Unless you're primarily a double-star observer, try using an eyepiece with a focal length greater than " + floor(focal_length / (2 * aperture) + 0.5) + "mm.");}if (exit_pupil > 7){alert("To ensure that the eyepiece's exit pupil is fully accommodated by your dark adapted eye, try selecting an eyepiece with a focal length less than " + floor(7 * focal_ratio) + "mm.");}

if (barlow_factor != 1 && focal_reducer != 1)
{
alert("Are you sure that you wish to use a focal reducer and a Barlow lens together?");
}var scope_info = "Focal Length:  " + round_fifth(focal_length) + "mm   ";

if (focal_reducer != 1.0)
{
    scope_info += "(You are using a " + focal_reducer + " x focal reducer)   ";
}

if (barlow_factor != 1.0)
{
    scope_info += "(You are using a " + barlow_factor + " x Barlow lens)";
}

scope_info += "\n";scope_info += "Magnification: " + floor(magnification + 0.5) + "x\n"scope_info += "True Field of View: " + true_field + "\u00B0   ";

if (true_field >= 3.5 && true_field < 7){	scope_info += "(Orion's Belt could easily fit into the field of view)\n";}

if (true_field >= 2.8 && true_field < 3.5){	scope_info += "(Orion's Belt would fit snuggly into the field of view)\n";}

if (true_field >= 1.5 && true_field < 2.8){	scope_info += "(The Pleiades would easily fit into the field of view)\n";}

if (true_field >= 1.0 && true_field < 1.5){	scope_info += "(The Pleiades could fit snuggly into the field of view)\n";}
if (true_field >= 0.6 && true_field < 1.0){	scope_info += "(The full Moon would easily fit into the field of view)\n";}if (true_field >= 0.50 && true_field < 0.6){	scope_info += "(The full Moon could just fit within the field of view)\n";}if (true_field >= 0.35 && true_field < 0.5){	scope_info += "(The full Moon would not quite fit into the field of view)\n";}if (true_field >= 0.25 && true_field < 0.35){	scope_info += "(About half of the full Moon's disc would fit into the field of view)\n";}if (true_field < 0.25){	scope_info += "(Less than half of the full Moon's disc would fit into the field of view)\n";}scope_info += "Exit Pupil: " + exit_pupil + " mm\n";

scope_info += "Theoretical Resolving Power: " + resolving_power + " arcseconds\n";scope_info += "Approximate Limiting Magnitude of Telescope: +" + limiting_magnitude + " (under dark, moonless skies)";document.scope.result.value = scope_info;}}