<!--

// List variables --------------
//satellitelongitude As Double
//vpolarity As Boolean
//oldpolar As Double
//sitelatitude As Double
//sitelongitude As Double
//siteazimuth As Double
//lat As Double
//lng As Double
//x As Double
//y As Double
//z As Double
//dist As Double
//el As Double
//a As Double
//s As Double
//siteelevation As Double
//sitepolarization As Double
//siteRtd As Double
//sitetosatdistance As Double
//totallyrad As Double
//polaroffset As Double
//damaRtd As Double
//siteSymbolRate As Double

// Required variables --------------
//satellitelongitude
//vpolarity
//polaroffset
//sitelatitude
//sitelongitude

// Calculated variables --------------
//siteazimuth
//siteelevation
//sitepolarization


// Set page defaults
function defaults() {

        // Define constants
        rad = 6378137;
        alt = 42165120;
        c = 299792458;
}


function zeros() {
        document.satcalc.sitelatitude.value = '0';
        document.satcalc.sitelongitude.value = '0';
        document.satcalc.sitealtitude.value = '0';
		document.satcalc.sat.value = '0';
}


// Start real calculations -------------------------
function calculate() {

	var satellitelongitude = document.satcalc.sat.value;
	var polaroffset = 0;
	var sitelatitude = eval(document.satcalc.sitelatitude.value);
	var sitelongitude = eval(document.satcalc.sitelongitude.value);
	var sitealtitude = eval(document.satcalc.sitealtitude.value);
	var totallyrad = rad + sitealtitude;

	var lat = sitelatitude * Math.PI / 180;
	if (lat == 0) {
		lat = 0.000000001;			// avoid div/zero errors
	}

	var lng = (satellitelongitude - sitelongitude) * Math.PI / 180;
	if (lng == 0) {
		lng = 0.000000001;			// avoid div/zero errors
	}

	var z = totallyrad * Math.sin(lat);
	var y = totallyrad * Math.cos(lat) * Math.cos(lng);
	var x = totallyrad * Math.cos(lat) * Math.sin(lng);
	var dist = Math.sqrt(Math.pow(x,2) + Math.pow((alt - y),2) + Math.pow(z,2));
	if (dist == 0) {
		dist = 0.000000001;			// avoid div/zero errors
	}

	var el = (alt * y - Math.pow(totallyrad,2)) / (totallyrad * dist);
	if (el == 1) {
		el = 0.999999999;			// avoid div/zero errors
	}

	if (sitelatitude > 0) {
		var a = Math.PI;
	}
	else {
		var a = 0;
	}

	var s = Math.tan(lat) / Math.sin(lng);
	
//	var sitetosatdistance = Math.sqrt(Math.pow(totallyrad,2) + Math.pow(alt,2) - 2 * alt * totallyrad * Math.cos(lat) * Math.cos(lng));
	
//	var sitedelay = 2 * sitetosatdistance / c;			// sitedelay is in seconds
//	var siteRtd = sitedelay * inboundspeed;				// siteRtd is in bits; ANSWER
//	var siteSymbolRate = (9600 / sitemodulation) / siteFEC;
//	var damaRtd = sitedelay * siteSymbolRate;			// damaRtd is in symbols/sec

	if (document.satcalc.polaritybutton[0].checked) {
		var vpolarity = 1;
	}
	else if (document.satcalc.polaritybutton[1].checked) {
		var vpolarity = 0;
	}

	var oldpolar = (Math.atan(s) * 180) / Math.PI + polaroffset;
	oldpolar = oldpolar - 90 * vpolarity * sign(oldpolar);

	if (document.satcalc.polaritybutton[0].checked) {
		// vertical
                //var sitepolarization = 1;
		var sitepolarization = oldpolar + 180 * (sign(oldpolar) * (Math.abs(oldpolar) > 90));     	// ANSWER
	}
	else if (document.satcalc.polaritybutton[1].checked) {
		// horizontal
                //var sitepolarization = 2;
		var sitepolarization = oldpolar + 180 * (sign(oldpolar) * (Math.abs(oldpolar) > 90));      	// ANSWER
	}

	var siteelevation = ( Math.atan(el / (Math.sqrt(1 - Math.pow(el,2)))) ) * 180 / Math.PI;	// ANSWER
	var siteazimuth = ( a - Math.atan(Math.tan(lng) / Math.sin(lat)) ) * 180 / Math.PI;		// ANSWER
	if (siteazimuth < 0) {
	    siteazimuth = siteazimuth + 360;
	}
	
	document.satcalc.azimuth.value = Math.round(siteazimuth*100)/100;
	document.satcalc.elevation.value = Math.round(siteelevation*100)/100;
	document.satcalc.polarization.value = Math.round(sitepolarization*100)/100;
//	document.satcalc.rtd.value = Math.round(siteRtd);
}


function sign(tmpNum) {
        if (tmpNum > 0) { var sign = 1; }
        if (tmpNum == 0) { var sign = 0; }
        if (tmpNum < 0) { var sign = -1; }
        return sign;
}


-->
