
function CalculateSatellitePosition( jdTime )
{
	var tsince;
	var ret;

	m_bLatLonAlt = false;
	m_fTime = jdTime;
	tsince = (jdTime - m_Sat.fJulianEpoch) * m_xmnpda;
	if (m_iDeep == 0)	{
		ret = SGP4(tsince);	// Calculation for near earth
	}
	else	{
		ret = SDP4(tsince);	// Calculation for the deep space
	}
	if (ret == false) return false;	// Ooops, there was a mistake within the calculation !!!

	ConvertSatState();	// here the pos and vel vector are 
	return true;
} // Procedure SGP


function ConvertSatState()
{
	m_pos[0] *= m_xkmper;
	m_pos[1] *= m_xkmper;
	m_pos[2] *= m_xkmper;
	m_pos[3] = Math.sqrt(sqr(m_pos[0])+sqr(m_pos[1])+sqr(m_pos[2]));
	m_vel[0] *= m_xkmper/60.0;	// kilometers/second
	m_vel[1] *= m_xkmper/60.0;	// kilometers/second
	m_vel[2] *= m_xkmper/60.0;	// kilometers/second
	m_vel[3] = Math.sqrt(sqr(m_vel[0])+sqr(m_vel[1])+sqr(m_vel[2]));
} //Procedure Convert_Sat_State

function CalculateLatLonAlt( jdTime )
{
// Reference:  The 1992 Astronomical Almanac, page K12. 
	var lat,lon,alt;
	var theta,r,e2,phi,c;
	var arg1, arg2;

	m_lla[0] = m_lla[1] = m_lla[2] = m_lla[3] = 0.0;
	lat = lon = alt = 0.0;
	theta = r = e2 = phi = c = 0.0;

	theta = AcTan(m_pos[1],m_pos[0]);
	
	arg1 = theta - ThetaG(jdTime);
	arg2 = 2.0 * m_PI;
	lon = Modulus(arg1, arg2);	//theta - ThetaG(jdTime),2.0*m_PI);
	
	r = Math.sqrt(sqr(m_pos[0]) + sqr(m_pos[1]));
	e2 = m_f*(2.0 - m_f);
	lat = AcTan(m_pos[2],r);
	do	{
		phi = lat;
		c = 1.0/Math.sqrt(1.0 - e2*sqr(Math.sin(phi)));
		lat = AcTan( m_pos[2] + m_xkmper*c*e2*Math.sin(phi),r);
	}	while (Math.abs(lat - phi) > 1E-10);//1E-7); For speeding up calculation 7 digit
										//is exact enough (123.45
	alt = r/Math.cos(lat) - m_xkmper*c;

	m_lla[0] = lat*180.0/m_PI;   // radians
	m_lla[1] = lon*180.0/m_PI;   // radians
	m_lla[2] = alt;			// kilometers
	m_lla[3] = theta*180.0/m_PI; // radians

	// And at the end adjust to W-style (I.e. -12.5 = 12.5 East / +33.4	= 33.4 West
	if (m_lla[1] > 180.0)
		m_lla[1] = 360.0 - m_lla[1];
	else 
		m_lla[1] = -m_lla[1];
}

function InitSatellite()
{
// Convert sat datas to proper units ...
//	m_Sat.fRadiationCoefficient *= pow(10.0, ibexp)/m_ae;	// Already done
//	m_Sat.fBalisticCoefficient *= pow(10.0, iexp);			//  earlier !!!
	m_Sat.fRightAscending = DegToRad(m_Sat.fRightAscending);
	m_Sat.fPeregee = DegToRad(m_Sat.fPeregee);
	m_Sat.fMeanAnomaly = DegToRad(m_Sat.fMeanAnomaly);
	m_Sat.fInclination = DegToRad(m_Sat.fInclination);
	m_Sat.fMeanMotion *= 2.0*m_PI/m_xmnpda;
//	m_Sat.fSecondMeanMotion *= 2.0*PI/sqr(m_xmnpda);					// Never used variables ...
//	m_Sat.fBalisticCoefficient *= 2.0*PI/(m_xmnpda*m_xmnpda*m_xmnpda);	// Never used variables ...
// determination if it is deep space or not ...
	var a1,a0,del1, del0,temp;
	a1 = Math.pow (m_xke/m_Sat.fMeanMotion, m_tothrd);
	temp = (1.5*m_ck2*(3.0*sqr(Math.cos(m_Sat.fInclination))-1.0)/Math.pow(1.0-m_Sat.fEccentricity*m_Sat.fEccentricity,1.5));
	del1 = temp /(a1*a1);
	a0 = a1*(1-del1*(0.5*m_tothrd+del1*(1.0+134.0/81.0*del1)));
	del0 = temp/(a0*a0);
	xnodp = m_Sat.fMeanMotion/(1.0+del0);
	if (2.0*m_PI/xnodp >= 225.0) m_iDeep = 1;
	else m_iDeep = 0;
	m_iFlag = 1;
//	m_bEclipsed = false; done in global.js
}

function ConvertData( cLine1, cLine2 )
{
	var		iTemp;
	var cLine1, cLine2;
	
	m_Sat.iSatelliteNumber		= GetInt	(3 , 7,cLine1);	// = CString >catnr<
	m_Sat.iLaunchYear			= GetInt	(10,11,cLine1);
	m_Sat.iLaunchNumber			= GetInt	(12,14,cLine1);
	m_Sat.cLaunchPiece			= GetString	(15,17,cLine1);
	m_Sat.iEpochYear			= GetInt	(19,20,cLine1);	// = epoch 19, 14 lang
	m_Sat.fEpochDay				= GetFloat	(21,32,cLine1);	// = epoch 19, 14 lang
	m_Sat.fBalisticCoefficient	= GetFloat	(34,43,cLine1);	// xndt2o is never used so far ...
	m_Sat.fSecondMeanMotion 	= 0.0;							// xndd60 is never used so far ...
	m_Sat.iSecondMeanMotion 	= 0;							// xndd60 is never used so far ...
//	m_Sat.fSecondMeanMotion 	= GetFloat	(45,50,m_cLine1);	// xndd60 is never used so far ...
//	iexp 	= GetInt	(51,52,m_cLine1);						// iexp is never used so far ...
//	m_Sat.fBalisticCoefficient *= pow(10.0, iexp);				// --- '' ---
	
//	m_Sat.fRadiationCoefficient = GetFloat	(54,61,m_cLine1)*1e-5;	// = bstar 54, 6 lang
	// the input format is 12345-x convert to 0.12345^x
	iTemp	= GetInt	(60,61,cLine1);	// get the exponent, and make 0.xxxxx of it
	m_Sat.fRadiationCoefficient	= GetFloat	(54,59,cLine1)*Math.pow(10,-5+iTemp);
	m_Sat.cEmphemeristType		= GetString	(63,63,cLine1);
	m_Sat.iElementNumber		= GetInt	(65,68,cLine1);

	m_Sat.fInclination			= GetFloat	(9 ,16,cLine2);	// = xincl
	m_Sat.fRightAscending		= GetFloat	(18,25,cLine2);	// = xnodeo
	m_Sat.fEccentricity			= GetFloat	(27,33,cLine2)*1e-7;	// = eo
	m_Sat.fPeregee				= GetFloat	(35,42,cLine2);	// = omegao
	m_Sat.fMeanAnomaly			= GetFloat	(44,51,cLine2);	// = xmo
	m_Sat.fMeanMotion			= GetFloat	(53,63,cLine2);	// = xno
	m_Sat.iRevAtEpoch			= GetInt	(64,68,cLine2);

	m_Sat.fJulianEpoch = JulianDate (m_Sat.iEpochYear *1000.0+ m_Sat.fEpochDay);
	m_Sat.iEpochDay = Math.floor( m_Sat.fEpochDay );
	m_Sat.fEpochFraction = m_Sat.fEpochDay - m_Sat.iEpochDay;

	m_Sat.iEpochYear += 1900;
	m_Sat.iLaunchYear += 1900;	
}

function GetFloat ( iStart, iEnd, cLine ) 
{
	var cs;
	var ret;
	iStart --;
	cs = cLine.substring(iStart, iEnd);
	ret = parseFloat(cs);
	return ret;
}

function GetInt   ( iStart, iEnd, cLine ) 
{
	var cs;
	var ret;
	iStart --;
	cs = cLine.substring(iStart, iEnd);
	ret = parseInt(cs);
	return ret;
}

function GetString( iStart, iEnd, cLine ) 
{
	var ret;
	iStart --;
	ret = cLine.substring(iStart, iEnd);
	return ret;
}




