// JavaScript Document
//-->


//
//	*** Main loop here ***
//

function Main(InForm) {
	var OutString = "";
	var calend;
	var quady = new Array;
	var sunp = new Array;
	var moonp = new Array;
	var y, m, day, glong, glat, tz, numday, mj, lst1, i;
	var rads = 0.0174532925, sinmoonalt;
	
	//
	// parse the form to make sure the numbers are numbers and not strings!
	//
	y = parseInt(InForm.Year.value, 10);
	if( (y<1950) || (y>2050)){
	     alert("Introduceţi anul în limitele propuse");
	     document.InForm.Year.value="";
        document.InForm.Year.focus();
	     return false;
    }

	     
	m = parseInt(InForm.Month.value, 10);
	if((m<1) || (m>12)){
	    alert("Introduceţi o lună validă");
	    document.InForm.Month.value="";
	    document.InForm.Month.focus();
	    return false;
	}
	
	day = parseInt(InForm.Day.value, 10);
	numday = 1;
	glong = parseFloat(InForm.Glong.value);
	glat = parseFloat(InForm.Glat.value);
	tz = parseFloat(InForm.TimeZone.value);
	var mkvar = new Array;
	var n_tithi= 0;
	var eveNadi = "";
	var mornadi = "";
	var aNadi = [0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,1,1,1];

	//
	// main loop. 
	
	 mj = mjd(day, m, y, 0.0);
	alert("Calcul se face pentru 31 de zile de la data selectată de Dvs.");
	alert("Calcularea şi afişarea datelor poate scădea performanţa calculatorului. Dacă browser-ul vă cere să opriţi executarea script-ul, tastaţi No.");
	InForm.OutTable.value=""; 
//mk:	for(i = 0; i < numday; i++) {
     k=0;
	for(i = day-1; i < day+31; i++) {
	    
		mkvar = find_sun_and_twi_events_for_date(mj + k, tz, glong, glat);
		n_tithi = NewCalculate(i+1, m ,y , mkvar[0], tz);
		if (aNadi[n_tithi] ==0)  
			{eveNadi = "PINGLA";
			 morNadi = "IDA   ";}
		else 
			{eveNadi = "IDA   " ;
			 morNadi = "PINGLA";}
		
		
		 InForm.OutTable.value += caldat(mj + k) + " " +
//			(n_tithi + 1) + ":" +
			tith[n_tithi] + "  " +
			hrsmin(mkvar[0]) + ":" + morNadi + "     " +
			hrsmin(mkvar[1]) + ":" + eveNadi + "    " +
			find_moonrise_set(mj + k, tz, glong, glat) + "\n";
			k++;
		}
		

	} // end of main program

//
//

function hrsmin(hours) {
//
//	takes decimal hours and returns a string in hhmm format
//
	var hrs, h, m, dum;
	hrs = Math.floor(hours * 60 + 0.5)/ 60.0;
	h = Math.floor(hrs);
	m = Math.floor(60 * (hrs - h) + 0.5);
	dum = h*100 +  m;
	//
	// the jiggery pokery below is to make sure that two minutes past midnight
	// comes out as 0002 not 2. Javascript does not appear to have 'format codes'
	// like C
	//
	if (dum < 1000) dum = "0" + dum;
	if (dum <100) dum = "0" + dum;
	if (dum < 10) dum = "0" + dum;
	return dum;
	}


function ipart(x) {
//
//	returns the integer part - like int() in basic
//
	var a;
	if (x> 0) {
	    a = Math.floor(x);
		}
	else {
		a = Math.ceil(x);
		}
	return a;
	}


function frac(x) {
//
//	returns the fractional part of x as used in minimoon and minisun
//
	var a;
	a = x - Math.floor(x);
	if (a < 0) a += 1;
	return a;
	}

//
// round rounds the number num to dp decimal places
// the second line is some C like jiggery pokery I
// found in an OReilly book which means if dp is null
// you get 2 decimal places.
//
function round(num, dp) {
   return Math.round (num * Math.pow(10, dp)) / Math.pow(10, dp);
}

function rangle(x) {
//
//	returns an angle in degrees in the range 0 to 360
//
	var a, b;
	b = x / 360;
	a = 360 * (b - ipart(b));
	if (a  < 0 ) {
		a = a + 360
		}
	return a
	}



function mjd(day, month, year, hour) {
//
//	Takes the day, month, year and hours in the day and returns the
//  modified julian day number defined as mjd = jd - 2400000.5
//  checked OK for Greg era dates - 26th Dec 02
//
	var a, b;
	if (month <= 2) {
		month = month + 12;
		year = year - 1;
		}
	a = 10000.0 * year + 100.0 * month + day;
	if (a <= 15821004.1) {
		b = -2 * Math.floor((year + 4716)/4) - 1179;
		}
	else {
		b = Math.floor(year/400) - Math.floor(year/100) + Math.floor(year/4);
		}
	a = 365.0 * year - 679004.0;
	return (a + b + Math.floor(30.6001 * (month + 1)) + day + hour/24.0);
	}

function caldat(mjd) {
//
//	Takes mjd and returns the civil calendar date in Gregorian calendar
//  as a string in format yyyymmdd.hhhh
//  looks OK for Greg era dates  - not good for earlier - 26th Dec 02
//
	var calout;
	var b, d, f, jd, jd0, c, e, day, month, year, hour;
	jd = mjd + 2400000.5;
	jd0 = Math.floor(jd + 0.5);
	if (jd0 < 2299161.0) {
		c = jd0 + 1524.0;
		}
	else {
		b = Math.floor((jd0 - 1867216.25) / 36524.25);
		c = jd0 + (b - Math.floor(b/4)) + 1525.0;
		}
	d = Math.floor((c - 122.1)/365.25);
	e = 365.0 * d + Math.floor(d/4);
	f = Math.floor(( c - e) / 30.6001);
	day = Math.floor(c - e + 0.5) - Math.floor(30.6001 * f);
	month = f - 1 - 12 * Math.floor(f/14);
	year = d - 4715 - Math.floor((7 + month)/10);
	hour = 24.0 * (jd + 0.5 - jd0);
	hour = hrsmin(hour);
	calout = round(year * 10000.0 + month * 100.0 + day + hour/10000, 4);
	return calout + ""; //making sure calout is a string
	}


function quad(ym, yz, yp) {
//
//	finds the parabola throuh the three points (-1,ym), (0,yz), (1, yp)
//  and returns the coordinates of the max/min (if any) xe, ye
//  the values of x where the parabola crosses zero (roots of the quadratic)
//  and the number of roots (0, 1 or 2) within the interval [-1, 1]
//
//	well, this routine is producing sensible answers
//
//  results passed as array [nz, z1, z2, xe, ye]
//
	var nz, a, b, c, dis, dx, xe, ye, z1, z2, nz;
	var quadout = new Array;

	nz = 0;
	a = 0.5 * (ym + yp) - yz;
	b = 0.5 * (yp - ym);
	c = yz;
	xe = -b / (2 * a);
	ye = (a * xe + b) * xe + c;
	dis = b * b - 4.0 * a * c;
	if (dis > 0)	{
		dx = 0.5 * Math.sqrt(dis) / Math.abs(a);
		z1 = xe - dx;
		z2 = xe + dx;
		if (Math.abs(z1) <= 1.0) nz += 1;
		if (Math.abs(z2) <= 1.0) nz += 1;
		if (z1 < -1.0) z1 = z2;
		}
	quadout[0] = nz;
	quadout[1] = z1;
	quadout[2] = z2;
	quadout[3] = xe;
	quadout[4] = ye;
	return quadout;
	}


function lmst(mjd, glong) {
//
//	Takes the mjd and the longitude (west negative) and then returns
//  the local sidereal time in hours. Im using Meeus formula 11.4
//  instead of messing about with UTo and so on
//
	var lst, t, d;
	d = mjd - 51544.5;          //we put semicolon here
	t = d / 36525.0;
	lst = rangle(280.46061837 + 360.98564736629 * d + 0.000387933 *t*t - t*t*t / 38710000);
	return (lst/15.0 + glong/15);
	}


function minisun(t) {
//
//	returns the ra and dec of the Sun in an array called suneq[]
//  in decimal hours, degs referred to the equinox of date and using
//  obliquity of the ecliptic at J2000.0 (small error for +- 100 yrs)
//	takes t centuries since J2000.0. Claimed good to 1 arcmin
//
	var p2 = 6.283185307, coseps = 0.91748, sineps = 0.39778;
	var L, M, DL, SL, X, Y, Z, RHO, ra, dec;
	var suneq = new Array;

	M = p2 * frac(0.993133 + 99.997361 * t);
	DL = 6893.0 * Math.sin(M) + 72.0 * Math.sin(2 * M);
	L = p2 * frac(0.7859453 + M / p2 + (6191.2 * t + DL)/1296000);
	SL = Math.sin(L);
	X = Math.cos(L);
	Y = coseps * SL;
	Z = sineps * SL;
	RHO = Math.sqrt(1 - Z * Z);
	dec = (360.0 / p2) * Math.atan(Z / RHO);
	ra = (48.0 / p2) * Math.atan(Y / (X + RHO));
	if (ra <0 ) ra += 24;
	suneq[1] = dec;
	suneq[2] = ra;
	return suneq;
	}


function minimoon(t) {
//
// takes t and returns the geocentric ra and dec in an array mooneq
// claimed good to 5' (angle) in ra and 1' in dec
// tallies with another approximate method and with ICE for a couple of dates
//
	var p2 = 6.283185307, arc = 206264.8062, coseps = 0.91748, sineps = 0.39778;
	var L0, L, LS, F, D, H, S, N, DL, CB, L_moon, B_moon, V, W, X, Y, Z, RHO;
	var mooneq = new Array;

	L0 = frac(0.606433 + 1336.855225 * t);	// mean longitude of moon
	L = p2 * frac(0.374897 + 1325.552410 * t) //mean anomaly of Moon
	LS = p2 * frac(0.993133 + 99.997361 * t); //mean anomaly of Sun
	D = p2 * frac(0.827361 + 1236.853086 * t); //difference in longitude of moon and sun
	F = p2 * frac(0.259086 + 1342.227825 * t); //mean argument of latitude

	// corrections to mean longitude in arcsec
	DL =  22640 * Math.sin(L)
	DL += -4586 * Math.sin(L - 2*D);
	DL += +2370 * Math.sin(2*D);
	DL +=  +769 * Math.sin(2*L);
	DL +=  -668 * Math.sin(LS);
	DL +=  -412 * Math.sin(2*F);
	DL +=  -212 * Math.sin(2*L - 2*D);
	DL +=  -206 * Math.sin(L + LS - 2*D);
	DL +=  +192 * Math.sin(L + 2*D);
	DL +=  -165 * Math.sin(LS - 2*D);
	DL +=  -125 * Math.sin(D);
	DL +=  -110 * Math.sin(L + LS);
	DL +=  +148 * Math.sin(L - LS);
	DL +=   -55 * Math.sin(2*F - 2*D);

	// simplified form of the latitude terms
	S = F + (DL + 412 * Math.sin(2*F) + 541* Math.sin(LS)) / arc;
	H = F - 2*D;
	N =   -526 * Math.sin(H);
	N +=   +44 * Math.sin(L + H);
	N +=   -31 * Math.sin(-L + H);
	N +=   -23 * Math.sin(LS + H);
	N +=   +11 * Math.sin(-LS + H);
	N +=   -25 * Math.sin(-2*L + F);
	N +=   +21 * Math.sin(-L + F);

	// ecliptic long and lat of Moon in rads
	L_moon = p2 * frac(L0 + DL / 1296000);
	B_moon = (18520.0 * Math.sin(S) + N) /arc;

	// equatorial coord conversion - note fixed obliquity
	CB = Math.cos(B_moon);
	X = CB * Math.cos(L_moon);
	V = CB * Math.sin(L_moon);
	W = Math.sin(B_moon);
	Y = coseps * V - sineps * W;
	Z = sineps * V + coseps * W
	RHO = Math.sqrt(1.0 - Z*Z);
	dec = (360.0 / p2) * Math.atan(Z / RHO);
	ra = (48.0 / p2) * Math.atan(Y / (X + RHO));
	if (ra <0 ) ra += 24;
	mooneq[1] = dec;
	mooneq[2] = ra;
	return mooneq;
	}


function sin_alt(iobj, mjd0, hour, glong, cglat, sglat) {
//
//	this rather mickey mouse function takes a lot of
//  arguments and then returns the sine of the altitude of
//  the object labelled by iobj. iobj = 1 is moon, iobj = 2 is sun
//
	var mjd, t, ra, dec, tau, salt, rads = 0.0174532925;
	var objpos = new Array;
	mjd = mjd0 + hour/24.0;
	t = (mjd - 51544.5) / 36525.0;
	if (iobj == 1) {
		objpos = minimoon(t);
				}
	else {
		objpos = minisun(t);
		}
	ra = objpos[2];
	dec = objpos[1];
	// hour angle of object
	tau = 15.0 * (lmst(mjd, glong) - ra);
	// sin(alt) of object using the conversion formulas
	salt = sglat * Math.sin(rads*dec) + cglat * Math.cos(rads*dec) * Math.cos(rads*tau);
	return salt;
	}


function find_sun_and_twi_events_for_date(mjd, tz, glong, glat) {
//
//	this is my attempt to encapsulate most of the program in a function
//	then this function can be generalised to find all the Sun events.
//
//
	var sglong, sglat, date, ym, yz, above, utrise, utset, j;
	var yp, nz, rise, sett, hour, z1, z2, iobj, rads = 0.0174532925;
	var quadout = new Array;
	var sinho = new Array;
	var   always_up = " ****";
	var always_down = " ....";
	var outstring = "";
//mk:
	var retArray = new Array;
//
//	Set up the array with the 4 values of sinho needed for the 4
//      kinds of sun event
//
	sinho[0] = Math.sin(rads * -0.833);		//sunset upper limb simple refraction
	sinho[1] = Math.sin(rads *  -6.0);		//civil twi
	sinho[2] = Math.sin(rads * -12.0);		//nautical twi
	sinho[3] = Math.sin(rads * -18.0);		//astro twi
	sglat = Math.sin(rads * glat);
	cglat = Math.cos(rads * glat);
	date = mjd - tz/24;
//
//	main loop takes each value of sinho in turn and finds the rise/set
//      events associated with that altitude of the Sun
//
//mk:	for (j = 0; j < 4; j++) {  
	for (j = 0; j < 1; j++) {
		rise = false;

		sett = false;
		above = false;
		hour = 1.0;
		ym = sin_alt(2, date, hour - 1.0, glong, cglat, sglat) - sinho[j];
		if (ym > 0.0) above = true;
		//
		// the while loop finds the sin(alt) for sets of three consecutive
		// hours, and then tests for a single zero crossing in the interval
		// or for two zero crossings in an interval or for a grazing event
		// The flags rise and sett are set accordingly

		//
		while(hour < 25 && (sett == false || rise == false)) {
			yz = sin_alt(2, date, hour, glong, cglat, sglat) - sinho[j];
			yp = sin_alt(2, date, hour + 1.0, glong, cglat, sglat) - sinho[j];
			quadout = quad(ym, yz, yp);
			nz = quadout[0];
			z1 = quadout[1];
			z2 = quadout[2];
			xe = quadout[3];
			ye = quadout[4];
			// case when one event is found in the interval
			if (nz == 1) {
				if (ym < 0.0) {
					utrise = hour + z1;
					rise = true;
					}
				else {
					utset = hour + z1;
					sett = true;
					}
				} // end of nz = 1 case

			// case where two events are found in this interval
			// (rare but whole reason we are not using simple iteration)
			if (nz == 2) {
				if (ye < 0.0) {
					utrise = hour + z2;
					utset = hour + z1;
					}
				else {
					utrise = hour + z1;
					utset = hour + z2;
					}
				} // end of nz = 2 case

			// set up the next search interval
			ym = yp;
			hour += 2.0;

			} // end of while loop
			//
			// now search has completed, we compile the string to pass back
			// to the main loop. The string depends on several combinations
			// of the above flag (always above or always below) and the rise
			// and sett flags
			//

			if (rise == true || sett == true ) {
				if (rise == true) 
//mk:					outstring +=  " " + hrsmin(utrise);
					retArray[0] = utrise;
				else outstring += " ----";
				if (sett == true) 
//mk:					outstring += " " + hrsmin(utset);
					retArray[1] = utset;
				else outstring += " ----";
				}
			else {
				if (above == true) outstring += always_up + always_up;
				else outstring += always_down + always_down;
				}
		} // end of for loop - next condition

//mk:		return outstring;
			return retArray;
	}

function find_moonrise_set(mjd, tz, glong, glat) {
//
//	Im using a separate function for moonrise/set to allow for different tabulations
//  of moonrise and sun events ie weekly for sun and daily for moon. The logic of
//  the function is identical to find_sun_and_twi_events_for_date()
//
	var sglong, sglat, date, ym, yz, above, utrise, utset, j;
	var yp, nz, rise, sett, hour, z1, z2, iobj, rads = 0.0174532925;
	var quadout = new Array;
	var sinho;
	var   always_up = " ****";
	var always_down = " ....";
	var outstring = "";

	sinho = Math.sin(rads * 8/60);		//moonrise taken as centre of moon at +8 arcmin
	sglat = Math.sin(rads * glat);
	cglat = Math.cos(rads * glat);
	date = mjd - tz/24;
		rise = false;
		sett = false;
		above = false;
		hour = 1.0;
		ym = sin_alt(1, date, hour - 1.0, glong, cglat, sglat) - sinho;
		if (ym > 0.0) above = true;
		while(hour < 25 && (sett == false || rise == false)) {
			yz = sin_alt(1, date, hour, glong, cglat, sglat) - sinho;
			yp = sin_alt(1, date, hour + 1.0, glong, cglat, sglat) - sinho;
			quadout = quad(ym, yz, yp);
			nz = quadout[0];
			z1 = quadout[1];
			z2 = quadout[2];
			xe = quadout[3];
			ye = quadout[4];

			// case when one event is found in the interval
			if (nz == 1) {
				if (ym < 0.0) {
					utrise = hour + z1;
					rise = true;
					}
				else {
					utset = hour + z1;
					sett = true;
					}
				} // end of nz = 1 case

			// case where two events are found in this interval
			// (rare but whole reason we are not using simple iteration)
			if (nz == 2) {
				if (ye < 0.0) {
					utrise = hour + z2;
					utset = hour + z1;
					}
				else {
					utrise = hour + z1;
					utset = hour + z2;
					}
				}

			// set up the next search interval
			ym = yp;
			hour += 2.0;

			} // end of while loop

			if (rise == true || sett == true ) {
				if (rise == true) outstring += " " + hrsmin(utrise);
				else outstring += " ----";
				if (sett == true) outstring += "      " + hrsmin(utset);
				else outstring += "    ----";
				}
			else {
				if (above == true) outstring += always_up + always_up;
				else outstring += always_down + always_down;
				}

		return outstring;
	}


// Copied from the Panchang calculator below:
<!--
// globals
d2r = Math.PI/180;
r2d = 180/Math.PI;
var month = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var zn = ["Aries","Taurus","Gemini","Cancer","Leo","Virgo","Libra","Scorpio","Sagittarius","Capricorn","Aquarius","Pisces"];
var wd = "Sunday     Monday     Tuesday    Wednesday  Thursday   Friday     Saturday   ";
var range = [1,31,0,0,-3000,4000,0,23,0,59,-12,12,0,59];
var mas = ["Chaitra","Vaikasha", "Jyestha", "Asarah", "Sravan", "Bhadra", "Kuwar", "Kartik", "Aghan", "Pusha", "Magha", "Phalgun",];
var naks = ["Asvini","Bharani","Krittika","Rohini","Mrigasira","Ardra","Punarvasu","Pusyami","Aslesa","Magha","P.Phalguni","U.Phalguni","Hasta","Citra","Swati","Visakha","Anuradha","Jyestha","Mula","P.Asadha","U.Asadha","Sravana","Dhanista","Satabhisa","P.Bhadra","U.Bhadra","Revati"];
var tith = ["S.Pratipad","S.Dwithiai","S.Trithiai","S.Chathurt","S.Panchami","S.Shashti ","S.Saptami ","S.Ashtami ","S.Navami  ","S.Dasami  ","S.Ekadasi ","S.Dwadasi ","S.Triodasi","S.Chaturda","S.Poornima","K.Pratipad","K.Dwithiai","K.Trithiai","K.Chathurt","K.Panchami","K.Shashti ","K.Saptami ","K.Ashtami ","K.Navami  ","K.Dasami  ","K.Ekadasi ","K.Dwadasi ","K.Triodasi","K.Chaturda","K.Amavasya"];
var kar = ["Bava","Balava","Kaulava","Taitula","Garija","Vanija","Visti","Sakuna","Chatuspada","Naga","Kimstughna"];
var yog = ["Vishkambha","Prithi","Ayushman","Saubhagya","Sobhana","Atiganda","Sukarman","Dhrithi","Soola","Ganda","Vridhi","Dhruva","Vyaghata","Harshana","Vajra","Siddhi","Vyatipata","Variyan","Parigha","Siva","Siddha","Sadhya","Subha","Sukla","Bramha","Indra","Vaidhruthi"];
var tipnaks = [2,5,6,0,1,4,3,2,4,5,5,0,2,1,3,6,1,4,4,5,0,3,3,3,5,0,1];
var Lmoon, Lsun, skor, LmoonYoga, LsunYoga;
var ayanamsa = 0;
var n_wday, n_tithi, n_naksh, n_karana, n_yoga, panch, inpdate;
var  s_wday, s_tithi, s_naksh, s_karana, s_yoga;
n_naksh=1, n_tithi=1;
	
//---------------------------------------------------------------------------
//  Nakshatras data.
//---------------------------------------------------------------------------	
function naksdata(nname, hara, color)
{
this.nname = nname;
this.hara = hara;
this.color = color;
}
var naks = new Array();
var i = 0;
//                        name        ,character   ,color
naks[i++] = new naksdata("Asvini"     ,"light"     ,"#00CCFF");
naks[i++] = new naksdata("Bharani"    ,"awful"     ,"#7D2000");
naks[i++] = new naksdata("Krittika"   ,"mixed"     ,"#CC00FF");
naks[i++] = new naksdata("Rohini"     ,"fixed"     ,"#0000FF");
naks[i++] = new naksdata("Mrigasira"  ,"soft"      ,"#11AC0D");
naks[i++] = new naksdata("Ardra"      ,"sharp"     ,"#FF0000");
naks[i++] = new naksdata("Punarvasu"  ,"mobile"    ,"#FF9900");
naks[i++] = new naksdata("Pusyami"    ,"light"     ,"#00CCFF");
naks[i++] = new naksdata("Aslesa"     ,"sharp"     ,"#FF0000");
naks[i++] = new naksdata("Magha"      ,"awful"     ,"#7D2000");
naks[i++] = new naksdata("P.Phalguni" ,"awful"     ,"#7D2000");
naks[i++] = new naksdata("U.Phalguni" ,"fixed"     ,"#0000FF");
naks[i++] = new naksdata("Hasta"      ,"light"     ,"#00CCFF");
naks[i++] = new naksdata("Citra"      ,"soft"      ,"#11AC0D");
naks[i++] = new naksdata("Swati"      ,"mobile"    ,"#FF9900");
naks[i++] = new naksdata("Visakha"    ,"mixed"     ,"#CC00FF");
naks[i++] = new naksdata("Anuradha"   ,"soft"      ,"#11AC0D");
naks[i++] = new naksdata("Jyestha"    ,"sharp"     ,"#FF0000");
naks[i++] = new naksdata("Mula"       ,"sharp"     ,"#FF0000");
naks[i++] = new naksdata("P.Asadha"   ,"awful"     ,"#7D2000");
naks[i++] = new naksdata("U.Asadha"   ,"fixed"     ,"#0000FF");
naks[i++] = new naksdata("Sravana"    ,"mobile"    ,"#FF9900");
naks[i++] = new naksdata("Dhanista"   ,"mobile"    ,"#FF9900");
naks[i++] = new naksdata("Satabhisa"  ,"mobile"    ,"#FF9900");
naks[i++] = new naksdata("P.Bhadra"   ,"awful"     ,"#663300");
naks[i++] = new naksdata("U.Bhadra"   ,"fixed"     ,"#0000FF");
naks[i++] = new naksdata("Revati"     ,"soft"      ,"#11AC0D");

//---------------------------------------------------------------------------
//  Correction terms for Moon
//---------------------------------------------------------------------------
function corr(mlcor, mscor, fcor, dcor, lcor)
{
this.mlcor = mlcor;
this.mscor = mscor;
this.fcor = fcor;
this.dcor = dcor;
this.lcor = lcor;
}

function corr2(l, ml, ms, f, d)
{
this.l = l;
this.ml = ml;
this.ms = ms;
this.f = f;
this.d = d;
}

var corrMoon = new Array();	         // main correction terms
i = 0;
//                       ml, ms,  f,   d,     l
corrMoon[i++] = new corr( 0,  0,  0,   4,     13.902);
corrMoon[i++] = new corr( 0,  0,  0,   2,   2369.912);
corrMoon[i++] = new corr( 1,  0,  0,   4,      1.979);
corrMoon[i++] = new corr( 1,  0,  0,   2,    191.953);
corrMoon[i++] = new corr( 1,  0,  0,   0,  22639.500);
corrMoon[i++] = new corr( 1,  0,  0,  -2,  -4586.465);
corrMoon[i++] = new corr( 1,  0,  0,  -4,    -38.428);
corrMoon[i++] = new corr( 1,  0,  0,  -6,     -0.393);
corrMoon[i++] = new corr( 0,  1,  0,   4,     -0.289);
corrMoon[i++] = new corr( 0,  1,  0,   2,    -24.420);
corrMoon[i++] = new corr( 0,  1,  0,   0,   -668.146);
corrMoon[i++] = new corr( 0,  1,  0,  -2,   -165.145);
corrMoon[i++] = new corr( 0,  1,  0,  -4,     -1.877);
corrMoon[i++] = new corr( 0,  0,  0,   3,      0.403);
corrMoon[i++] = new corr( 0,  0,  0,   1,   -125.154);
corrMoon[i++] = new corr( 2,  0,  0,   4,      0.213);
corrMoon[i++] = new corr( 2,  0,  0,   2,     14.387);
corrMoon[i++] = new corr( 2,  0,  0,   0,    769.016);
corrMoon[i++] = new corr( 2,  0,  0,  -2,   -211.656);
corrMoon[i++] = new corr( 2,  0,  0,  -4,    -30.773);
corrMoon[i++] = new corr( 2,  0,  0,  -6,     -0.570);
corrMoon[i++] = new corr( 1,  1,  0,   2,     -2.921);
corrMoon[i++] = new corr( 1,  1,  0,   0,   -109.673);
corrMoon[i++] = new corr( 1,  1,  0,  -2,   -205.962);
corrMoon[i++] = new corr( 1,  1,  0,  -4,     -4.391);
corrMoon[i++] = new corr( 1, -1,  0,   4,      0.283);
corrMoon[i++] = new corr( 1, -1,  0,   2,     14.577);
corrMoon[i++] = new corr( 1, -1,  0,   0,    147.687);
corrMoon[i++] = new corr( 1, -1,  0,  -2,     28.475);
corrMoon[i++] = new corr( 1, -1,  0,  -4,      0.636);
corrMoon[i++] = new corr( 0,  2,  0,   2,     -0.189);
corrMoon[i++] = new corr( 0,  2,  0,   0,     -7.486);
corrMoon[i++] = new corr( 0,  2,  0,  -2,     -8.096);
corrMoon[i++] = new corr( 0,  0,  2,   2,     -5.741);
corrMoon[i++] = new corr( 0,  0,  2,   0,   -411.608);
corrMoon[i++] = new corr( 0,  0,  2,  -2,    -55.173);
corrMoon[i++] = new corr( 0,  0,  2,  -4,      0.025);
corrMoon[i++] = new corr( 1,  0,  0,   1,     -8.466);
corrMoon[i++] = new corr( 1,  0,  0,  -1,     18.609);
corrMoon[i++] = new corr( 1,  0,  0,  -3,      3.215);
corrMoon[i++] = new corr( 0,  1,  0,   1,     18.023);
corrMoon[i++] = new corr( 0,  1,  0,  -1,      0.560);
corrMoon[i++] = new corr( 3,  0,  0,   2,      1.060);
corrMoon[i++] = new corr( 3,  0,  0,   0,     36.124);
corrMoon[i++] = new corr( 3,  0,  0,  -2,    -13.193);
corrMoon[i++] = new corr( 3,  0,  0,  -4,     -1.187);
corrMoon[i++] = new corr( 3,  0,  0,  -6,     -0.293);
corrMoon[i++] = new corr( 2,  1,  0,   2,     -0.290);
corrMoon[i++] = new corr( 2,  1,  0,   0,     -7.649);
corrMoon[i++] = new corr( 2,  1,  0,  -2,     -8.627);
corrMoon[i++] = new corr( 2,  1,  0,  -4,     -2.740);
corrMoon[i++] = new corr( 2, -1,  0,   2,      1.181);
corrMoon[i++] = new corr( 2, -1,  0,   0,      9.703);
corrMoon[i++] = new corr( 2, -1,  0,  -2,     -2.494);
corrMoon[i++] = new corr( 2, -1,  0,  -4,      0.360);
corrMoon[i++] = new corr( 1,  2,  0,   0,     -1.167);
corrMoon[i++] = new corr( 1,  2,  0,  -2,     -7.412);
corrMoon[i++] = new corr( 1,  2,  0,  -4,     -0.311);
corrMoon[i++] = new corr( 1, -2,  0,   2,      0.757);
corrMoon[i++] = new corr( 1, -2,  0,   0,      2.580);
corrMoon[i++] = new corr( 1, -2,  0,  -2,      2.533);
corrMoon[i++] = new corr( 0,  3,  0,  -2,     -0.344);
corrMoon[i++] = new corr( 1,  0,  2,   2,     -0.992);
corrMoon[i++] = new corr( 1,  0,  2,   0,    -45.099);
corrMoon[i++] = new corr( 1,  0,  2,  -2,     -0.179);
corrMoon[i++] = new corr( 1,  0, -2,   2,     -6.382);
corrMoon[i++] = new corr( 1,  0, -2,   0,     39.528);
corrMoon[i++] = new corr( 1,  0, -2,  -2,      9.366);
corrMoon[i++] = new corr( 0,  1,  2,   0,      0.415);
corrMoon[i++] = new corr( 0,  1,  2,  -2,     -2.152);
corrMoon[i++] = new corr( 0,  1, -2,   2,     -1.440);
corrMoon[i++] = new corr( 0,  1, -2,  -2,      0.384);
corrMoon[i++] = new corr( 2,  0,  0,   1,     -0.586);
corrMoon[i++] = new corr( 2,  0,  0,  -1,      1.750);
corrMoon[i++] = new corr( 2,  0,  0,  -3,      1.225);
corrMoon[i++] = new corr( 1,  1,  0,   1,      1.267);
corrMoon[i++] = new corr( 1, -1,  0,  -1,     -1.089);
corrMoon[i++] = new corr( 0,  0,  2,  -1,      0.584);
corrMoon[i++] = new corr( 4,  0,  0,   0,      1.938);
corrMoon[i++] = new corr( 4,  0,  0,  -2,     -0.952);
corrMoon[i++] = new corr( 3,  1,  0,   0,     -0.551);
corrMoon[i++] = new corr( 3,  1,  0,  -2,     -0.482);
corrMoon[i++] = new corr( 3, -1,  0,   0,      0.681);
corrMoon[i++] = new corr( 2,  0,  2,   0,     -3.996);
corrMoon[i++] = new corr( 2,  0,  2,  -2,      0.557);
corrMoon[i++] = new corr( 2,  0, -2,   2,     -0.459);
corrMoon[i++] = new corr( 2,  0, -2,   0,     -1.298);
corrMoon[i++] = new corr( 2,  0, -2,  -2,      0.538);
corrMoon[i++] = new corr( 1,  1, -2,  -2,      0.426);
corrMoon[i++] = new corr( 1, -1,  2,   0,     -0.304);
corrMoon[i++] = new corr( 1, -1, -2,   2,     -0.372);
corrMoon[i++] = new corr( 0,  0,  4,   0,      0.418);
corrMoon[i++] = new corr( 2, -1,  0,  -1,     -0.352);


var corrMoon2 = new Array();	// moon additional correction terms
i = 0;
//                          l,     ml, ms,  f,  d
corrMoon2[i++] = new corr2( 0.127,  0,  0,  0,  6);
corrMoon2[i++] = new corr2(-0.151,  0,  2,  0, -4);
corrMoon2[i++] = new corr2(-0.085,  0,  0,  2,  4);
corrMoon2[i++] = new corr2( 0.150,  0,  1,  0,  3);
corrMoon2[i++] = new corr2(-0.091,  2,  1,  0, -6);
corrMoon2[i++] = new corr2(-0.103,  0,  3,  0,  0);
corrMoon2[i++] = new corr2(-0.301,  1,  0,  2, -4);
corrMoon2[i++] = new corr2( 0.202,  1,  0, -2, -4);
corrMoon2[i++] = new corr2( 0.137,  1,  1,  0, -1);
corrMoon2[i++] = new corr2( 0.233,  1,  1,  0, -3);
corrMoon2[i++] = new corr2(-0.122,  1, -1,  0,  1);
corrMoon2[i++] = new corr2(-0.276,  1, -1,  0, -3);
corrMoon2[i++] = new corr2( 0.255,  0,  0,  2,  1);
corrMoon2[i++] = new corr2( 0.254,  0,  0,  2, -3);
corrMoon2[i++] = new corr2(-0.100,  3,  1,  0, -4);
corrMoon2[i++] = new corr2(-0.183,  3, -1,  0, -2);
corrMoon2[i++] = new corr2(-0.297,  2,  2,  0, -2);
corrMoon2[i++] = new corr2(-0.161,  2,  2,  0, -4);
corrMoon2[i++] = new corr2( 0.197,  2, -2,  0,  0);
corrMoon2[i++] = new corr2( 0.254,  2, -2,  0, -2);
corrMoon2[i++] = new corr2(-0.250,  1,  3,  0, -2);
corrMoon2[i++] = new corr2(-0.123,  2,  0,  2,  2);
corrMoon2[i++] = new corr2( 0.173,  2,  0, -2, -4);
corrMoon2[i++] = new corr2( 0.263,  1,  1,  2,  0);
corrMoon2[i++] = new corr2( 0.130,  3,  0,  0, -1);
corrMoon2[i++] = new corr2( 0.113,  5,  0,  0,  0);
corrMoon2[i++] = new corr2( 0.092,  3,  0,  2, -2);

//--------------------------------------------------------------
// Days in one month
//--------------------------------------------------------------
function daysInMonth(m,y) {
   var g_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
   if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) g_days[1] = 29;
   return g_days[m];
}


//--------------------------------------------------------------
// Insert of current time in the form 
//--------------------------------------------------------------
function initFields()
{
    var d = new Date();
    document.forms[0].Year.value = d.getFullYear();
    document.forms[0].Day.value = d.getDate();
    document.forms[0].Month.selectedIndex = d.getMonth();
    document.forms[0].Hours.value = d.getHours();
    document.forms[0].Minutes.value = d.getMinutes();
    var zmins = d.getTimezoneOffset();
    var zsign = -1;
    zmins /= 60;
    if(zmins < 0.0)zsign = 1;
    zmins = Math.abs(zmins);
    document.forms[0].ZoneHour.value = Math.floor(zmins) * zsign;
    document.forms[0].ZoneMin.value = (zmins - Math.floor(zmins)) * 60;
}

	   
//--------------------------------------------------
//mk: NewCalculate modifies the old calculate funtion to pass values
//mk: directly rather than from the form fields
//--------------------------------------------------------
function NewCalculate(day, mon, year, hr, timeZone)
{

  var kday,kmon,kyear,khr,kmin,ksek;

  if(timeZone<0.0) 
	  tzone = timeZone*-1;
  else tzone = timeZone;

  //
  // Julian date on Local Time LT:
  //
  dayhr = day + hr/24;
  jdlt = mdy2julian(mon,dayhr,year);

  // Weekday:
  n_wday = weekDay(jdlt);
  s_wday = wd.substr(n_wday*11,11);

  //
  // Julian day by the beginning of day:
  jd0 = mdy2julian(mon,day,year);
  
  // Julian date on Universal Time UT:
  jdut = jd0 + (hr - tzone)/24;                             

  // Ephemeris correction:
  dt = dTime(jdut);

  // Julian date on Ephemeris Time ET:
  jd = jdut + dt/24;

  // Ayanamsa:
  ayanamsa = calcayan(jd);                                      

  // Ekliptic longitude of Moon:
  Lmoon = moon(jd);

  // Ekliptic longitude of Sun:
  Lsun = sun(jd);

  // Yoga:
  dmoonYoga = (LmoonYoga + ayanamsa - 491143.07698973856);
  dsunYoga = (LsunYoga + ayanamsa - 36976.91240579201);
  zyoga = dmoonYoga + dsunYoga;
  n_yoga = zyoga*6/80;
  while(n_yoga < 0)n_yoga += 27;
  while(n_yoga > 27)n_yoga -= 27;
  n3=n_yoga;
  n_yoga = Math.floor(n_yoga);
  s_yoga = yoga(jd, zyoga, tzone);

  // Nakshatra:
  Lmoon0 = fix360(Lmoon + ayanamsa);
  n_naksh = Math.floor(Lmoon0*6/80);
  s_naksh = nakshatra(jd, n_naksh, tzone);

  // Tithi:
  Lmoon0 = Lmoon;
  Lsun0 = Lsun;
  if (Lmoon0 < Lsun0)Lmoon0 += 360;
  n_tithi = Math.floor((Lmoon0 - Lsun0)/12);
  s_tithi = tithi(jd, n_tithi, tzone, 12);

  // Karana:
  Lmoon0 = Lmoon;
  Lsun0 = Lsun;
  if (Lmoon0 < Lsun0)Lmoon0 += 360;
  nk = Math.floor((Lmoon0 - Lsun0)/6);
  if (nk == 0)n_karana = 10;
  if (nk >= 57)n_karana = nk - 50;
  if (nk > 0 && nk < 57)n_karana = (nk - 1) - (Math.floor((nk - 1)/7))*7;
  s_karana = tithi(jd, nk, tzone, 6);

  return  n_tithi ;
}


//-----------------------------------------------------------------------------------
//  Calculation heocentric ekliptic longitude of Moon and angular speed. 
//                        (accuracy 2 sec. of long.)
//-----------------------------------------------------------------------------------
function moon(jd)
{
// Days from epoch 1900:
tdays = jd - 2415020;
// Time in Julian centuries from epoch 1900: 
t = tdays/36525;
t2 =t*t;
t3 = t*t*t;
  
// inclination of an ecliptic to equator:
ob = 23.452294 - 0.0130125 * t - 0.00000164*t2 + 0.000000503*t3;
// moon's mean longitude:
//l = 270.4341639 + 481267.8831417*t - 0.0011333333*t2 + 0.0000018888889*t3;
l = 270.4337361 + 13.176396544528099*tdays - 5.86*t2/3600 + 0.0068*t3/3600;
// elongation of moon from sun:
d = 350.7374861110581 + 445267.1142166667*t - t2*1.436111132303874e-3 + 0.0000018888889*t3;
// moon's perihel:
pe = 334.329556 + 14648522.52*t/3600 - 37.17*t2/3600 - 0.045*t3/3600;
// sun's mean anomaly:
ms = 358.4758333333334 + 35999.04974999958*t - t2*1.500000059604645e-4 - t3*3.3333333623078e-6;
// moon's mean anomaly:
//ml = 296.1046083333757 + 477198.8491083336*t + 0.0091916667090522*t2 + 0.0000143888893*t3;
ml = fix360(l - pe);
// moon's mean node:
om = 259.183275 - 6962911.23*t/3600 + 7.48*t2/3600 + 0.008*t3/3600;
// moon's mean longitude, counted from node:
//f = 11.25088888890213 + 483202.0251500002*t - t2*3.211111227671305e-3 - .00000033*t3;
f = fix360(l - om);

with(Math){
// The periodic corrections:
r2rad = 360.0 * d2r;
tb  = tdays * 1e-12;              // *10^12
t2c = tdays * tdays * 1e-16;      // *10^16
a1 = sin(r2rad * (0.53733431 -  10104982 * tb + 191 * t2c));
a2 = sin(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
c2 = cos(r2rad * (0.71995354 - 147094228 * tb +  43 * t2c));
a3 = sin(r2rad * (0.14222222 +   1536238 * tb));
a4 = sin(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
c4 = cos(r2rad * (0.48398132 - 147269147 * tb +  43 * t2c));
a5 = sin(r2rad * (0.52453688 - 147162675 * tb +  43 * t2c));
a6 = sin(r2rad * (0.84536324 -  11459387 * tb));
a7 = sin(r2rad * (0.23363774 +   1232723 * tb + 191 * t2c));
a8 = sin(r2rad * (0.58750000 +   9050118 * tb));
a9 = sin(r2rad * (0.61043085 -  67718733 * tb));

dlm = 0.84 * a3 + 0.31 * a7 + 14.27 * a1 + 7.261  * a2 + 0.282 * a4 + 0.237 * a6;
dpm = -2.1 * a3 - 2.076 * a2 - 0.840 * a4 - 0.593 * a6;
dkm = 0.63 * a3 + 95.96 * a2 + 15.58 * a4 + 1.86 * a5;
dls = -6.4  * a3 - 0.27 * a8 - 1.89  * a6 + 0.20 * a9;
dgc = (-4.318 * c2 - 0.698 * c4) / 3600.0 / 360.0;
dgc = (1.000002708 + 139.978 * dgc);

ml  = d2r * (ml + (dlm - dpm) / 3600.0);   //moon's mean anomaly
ms  = d2r * (ms + dls / 3600.0);           //sun's mean anomaly
f   = d2r * (f + (dlm - dkm) / 3600.0);    //moon's mean longitude, counted from node
d   = d2r * (d + (dlm - dls) / 3600.0);    //elongation of moon from sun

lk = 0; lk1 = 0; sk = 0; sinp = 0; nib = 0; g1c = 0;
i1corr = 1.0 - 6.8320e-8 * tdays;
i2corr = dgc * dgc;

for (i = 0; i < 93; i++) {                 // indignation in a longitude
    arg = corrMoon[i].mlcor * ml + corrMoon[i].mscor * ms + corrMoon[i].fcor * f + corrMoon[i].dcor * d;
    sinarg = sin(arg);
    if (corrMoon[i].mscor != 0) {
      sinarg *= i1corr;
      if  (corrMoon[i].mscor == 2 || corrMoon[i].mscor == -2)sinarg *= i1corr;
    }
    if (corrMoon[i].fcor != 0)sinarg *= i2corr;
    lk += corrMoon[i].lcor * sinarg;
}
for (i = 0; i < 27; i++) {                 // indignation in a longitude additional
    arg = corrMoon2[i].ml * ml + corrMoon2[i].ms * ms + corrMoon2[i].f * f + corrMoon2[i].d * d;
    sinarg = sin(arg);
    lk1 += corrMoon2[i].l * sinarg;
}

// Indignation from planets:
dlid =  0.822 * sin(r2rad * (0.32480 - 0.0017125594 * tdays));
dlid += 0.307 * sin(r2rad * (0.14905 - 0.0034251187 * tdays));
dlid += 0.348 * sin(r2rad * (0.68266 - 0.0006873156 * tdays));
dlid += 0.662 * sin(r2rad * (0.65162 + 0.0365724168 * tdays));
dlid += 0.643 * sin(r2rad * (0.88098 - 0.0025069941 * tdays));
dlid += 1.137 * sin(r2rad * (0.85823 + 0.0364487270 * tdays));
dlid += 0.436 * sin(r2rad * (0.71892 + 0.0362179180 * tdays));
dlid += 0.327 * sin(r2rad * (0.97639 + 0.0001734910 * tdays));

l = l + nutation(jd) + (dlm + lk + lk1 + dlid) / 3600.0;
LmoonYoga = l;
//alert("Lmoon="+l); 
l = fix360(l);
    
// Moon's angular speed (deg/day):
vl = 13.176397;
vl = vl + 1.434006*cos(ml);
vl = vl + .280135*cos(2*d);
vl = vl + .251632*cos(2*d - ml);
vl = vl + .09742*cos(2*ml);
vl = vl - .052799*cos(2*f);
vl = vl + .034848*cos(2*d + ml);
vl = vl + .018732*cos(2*d - ms);
vl = vl + .010316*cos(2*d - ms - ml);
vl = vl + .008649*cos(ms - ml);
vl = vl - .008642*cos(2*f + ml);
vl = vl - .007471*cos(ms + ml);
vl = vl - .007387*cos(d);
vl = vl + .006864*cos(3*ml);
vl = vl + .00665*cos(4*d - ml);
vl = vl + .003523*cos(2*d + 2*ml);
vl = vl + .003377*cos(4*d - 2*ml);
vl = vl + .003287*cos(4*d);
vl = vl - .003193*cos(ms);
vl = vl - .003003*cos(2*d + ms);
vl = vl + .002577*cos(ml - ms + 2*d);
vl = vl - .002567*cos(2*f - ml);
vl = vl - .001794*cos(2*d - 2*ml);
vl = vl - .001716*cos(ml - 2*f - 2*d);
vl = vl - .001698*cos(2*d + ms - ml);
vl = vl - .001415*cos(2*d + 2*f);
vl = vl + .001183*cos(2*ml - ms);
vl = vl + .00115*cos(d + ms);
vl = vl - .001035*cos(d + ml);
vl = vl - .001019*cos(2*f + 2*ml);
vl = vl - .001006*cos(ms + 2*ml);
}      
skor = vl;
//l += ay;
//if(l < 0.0)l += 360.0;
return l;
}


//----------------------------------------------------------------------
// Calculation heocentric ekliptic longitude of Sun
//               (accuracy 1 sec. of long.)
//----------------------------------------------------------------------
function sun(jd)
{
// Days from epoch 1900:
tdays = jd - 2415020;
// Time in Julian centuries from epoch 1900: 
t = tdays/36525;
t2 =t*t;
t3 = t*t*t;
  
// sun's mean longitude:
//ls = 279.696678 + 36000.76892*t + 0.0003025*t2;
ls = 279.696678 + 0.9856473354*tdays + 1.089*t2/3600;
// sun's perihel:
pes = 101.220833 + 6189.03*t/3600 + 1.63*t2/3600 + 0.012*t3/3600;
// sun's mean anomaly:
//ms = 358.4758333333334 + 35999.04974999958*t - t2*1.500000059604645e-4 - t3*3.3333333623078e-6;
ms = fix360(ls - pes + 180);
// longperiodic terms:
//g = ms + 0.0017778 * Math.sin((231.19 + 20.2*t)*d2r) + 0.00052278 * Math.sin((57.24 + 150.27*t)*d2r);
g = ms + (0.266 * Math.sin((31.8 + 119.0*t)*d2r) + 6.40 * Math.sin((231.19 + 20.2*t)*d2r) + (1.882-0.016*t) * Math.sin((57.24 + 150.27*t)*d2r)) / 3600.0;
// sun's mean longitude:
oms = 259.18 - 1934.142*t;
// excentricity of earth orbit:
ex = 0.01675104 - 0.0000418*t - 0.000000126*t2;
// moon's mean longitude:
l = 270.4337361 + 13.176396544528099*tdays - 5.86*t2/3600 + 0.0068*t3/3600;
// moon's mean anomaly:
ml = 296.1046083333757 + 477198.8491083336*t + 0.0091916667090522*t2 + 0.0000143888893*t3;
// mean longitude of earth:
le = 99.696678 + 0.9856473354*tdays + 1.089*t2/3600;
// moon's mean node longitude:
om = 259.183275 - 6962911.23*t/3600 + 7.48*t2/3600 + 0.008*t3/3600;

// the Kepler equation:
u = kepler(g, ex, 0.0000003)

with(Math){
// sun's true anomaly:
b = sqrt((1 + ex) / (1 - ex));
if (abs(Math.PI - u) < 1.0e-10) truanom = u;
  else truanom = 2.0 * atan(b * tan(u / 2));
truanom = fix360(truanom * r2d); 

u1 = (153.23 + 22518.7541 * t) * d2r;
u2 = (216.57 + 45037.5082 * t) * d2r;
u3 = (312.69 + 32964.3577 * t) * d2r;
u4 = (350.74 + 445267.1142 * t - 0.00144 * t2) * d2r;
u6 = (353.4 + 65928.71550000001 * t) * d2r;
u5 = (315.6 + 893.3 * t) * d2r;

dl = 0.00134 * cos(u1);
dl += 0.00154 * cos(u2);
dl += 0.002 * cos(u3);
dl += 0.00179 * sin(u4);
dl += 0.202 * sin(u5)/3600;

dr = 0.00000543 * sin(u1);
dr += 0.00001575 * sin(u2);
dr += 0.00001627 * sin(u3);
dr += 0.00003076 * cos(u4);
dr += 9.26999999e-06 * sin(u6);

// sun's true longitude (deg.):
il = ls + dl + truanom - ms;

// aberracion (deg):
r1 = 1.0000002 * (1 - ex * ex) / (1 + ex * cos(truanom*d2r));
rs = r1 + dr;                      // radius-vector
ab = (20.496 * (1 - ex * ex) / rs) / 3600;
ls = il + nutation(jd) - ab;     // visible longitude of sun
LsunYoga = ls;
//alert("Lsun="+ls); 
ls = fix360(ls);
}
return ls;
}

//----------------------------------------------------------------------------
// Beginning/end tithi and karana.
//----------------------------------------------------------------------------
function tithi(jd, n1, tzone, len)
{
var s_t = "";
var flag;
jdt = jd;
knv = Math.floor(((jd - 2415020) / 365.25) * 12.3685);

for (itit = n1; itit < (n1 + 2); ++itit) { 
  aspect = len * itit;
  flag = 0;
  if (aspect == 0) {jdt = novolun(jd, knv); flag = 1;}
  if (aspect == 360) {jdt = novolun(jd, (knv+1)); flag = 1;}
  while (flag < 1) {
    Lsun0 = sun(jdt);
    Lmoon0 = moon(jdt);
    a = fix360(Lsun0 + aspect);
    asp1 = a - Lmoon0;
    if (asp1 > 180) asp1 -= 360;
    if (asp1 < -180) asp1 += 360;
    flag = 1;	
    //if (Math.abs(asp1) > 0.001) {jdt += (asp1 / 12.190749); flag = 0;}
    if (Math.abs(asp1) > 0.001) {jdt += (asp1 / (skor - 1)); flag = 0;}
  }
  if (itit == n1) s_t = calData(jdt + tzone/24) + "&nbsp;&nbsp;to";
  if (itit == (n1 + 1)) s_t += "<br>" + calData(jdt + tzone/24);
}
return s_t;
}


//----------------------------------------------------------------------------
// Entrance and exit of the Moon in nakshatra.
//----------------------------------------------------------------------------
function nakshatra(jd, n_naksh, tzone)
{
var s_t = "";
var flag;
jdt = jd;

for (inak = n_naksh; inak < (n_naksh + 2); ++inak) {
  n1 = fix360(inak*80/6);
  flag = 0;
  while (flag < 1) {
    Lmoon0 = fix360(moon(jdt) + ayanamsa);
    asp1 = n1 - Lmoon0;
    if (asp1 > 180) asp1 -= 360;
    if (asp1 < -180) asp1 += 360;
    flag = 1;	
    if (Math.abs(asp1) > 0.001) {jdt += (asp1 / skor); flag = 0;}
  }
  if (inak == n_naksh) s_t = calData(jdt + tzone/24) + "&nbsp;&nbsp;to";
  if (inak == (n_naksh + 1)) s_t += "<br>" + calData(jdt + tzone/24);
}
return s_t;
}

//----------------------------------------------------------------------------
// Beginning and end the yoga.
//----------------------------------------------------------------------------
function yoga(jd, zyoga, tzone)
{
var s_t = "";
var flag;
jdt = jd;
z = zyoga;
var nn_yoga = new Array(2); 
nn_yoga[0] = Math.floor(z * 6 / 80) * 80 / 6;
nn_yoga[1] = (Math.floor(z * 6 / 80) + 1) * 80 / 6;
//alert(zyoga+"\r"+nn_yoga[0]+"\r"+nn_yoga[1]);
for (iyog = 0; iyog < 2; ++iyog) {
  flag = 0;
  while (flag < 1) {
    Lsun0 = sun(jdt);
    Lmoon0 = moon(jdt);
    dmoonYoga = (LmoonYoga + ayanamsa - 491143.07698973856);
    dsunYoga = (LsunYoga + ayanamsa - 36976.91240579201);
    //alert(LmoonYoga+"\r"+LsunYoga+"\r"+ayanamsa);
    z = dmoonYoga + dsunYoga;
    asp1 = nn_yoga[iyog] - z;
    //alert(asp1+"\r"+nn_yoga[iyog]+"\r"+z);
    flag = 1;	
    if (Math.abs(asp1) > 0.001) {jdt += (asp1 / (skor + 1.0145616633)); flag = 0;}
    //if (Math.abs(asp1) > 0.001) {jdt += (asp1 / skor) + (58.13 * Math.sin(asp1*d2r)); flag = 0;}
  }
  if (iyog == 0) s_t = calData(jdt + tzone/24) + "&nbsp;&nbsp;to";
  if (iyog == 1) s_t += "<br>" + calData(jdt + tzone/24);
}
return s_t;
}

//-----------------------------------------------------------------------------
// Calculation of the moment of the nearest past new moon in JD.
//                   (the error does not exceed 2 minutes)
//-----------------------------------------------------------------------------
function novolun (jd, knv)
{
t = (jd - 2415020) / 36525;
t2 =t*t;
t3 = t*t*t;

with(Math){
jdnv = 2415020.75933 + 29.53058868 * knv + 0.0001178 * t2 - 0.000000155 * t3;
jdnv += 0.00033 * sin((166.56 + 132.87 * t - 0.009173 * t2) * d2r);
m = 359.2242 + 29.10535608 * knv - 0.0000333 * t2 - 0.00000347 * t3;
ml = 306.0253 + 385.81691806 * knv + 0.0107306 * t2 + 0.00001236 * t3;
f = 21.2964 + 390.67050646 * knv - 0.0016528 * t2 - 0.00000239 * t3;
m *= d2r;
ml *= d2r;
f *= d2r;

djd = (0.1734 - 0.000393 * t) * sin(m);
djd += 0.0021 * sin(2 * m);
djd -= 0.4068 * sin(ml);
djd += 0.0161 * sin(2 * ml);
djd -= 0.0004 * sin(3 * ml);
djd += 0.0104 * sin(2 * f);
djd -= 0.0051 * sin(m + ml);
djd -= 0.0074 * sin(m - ml);
djd += 0.0004 * sin(2 * f + m);
djd -= 0.0004 * sin(2 * f - m);
djd -= 0.0006 * sin(2 * f + ml);
djd += 0.001 * sin(2 * f - ml);
djd += 0.0005 * sin(m + 2 * ml);

jdnv += djd;
}
return jdnv;
}

//-----------------------------------------------------
// Solution of the Kepler equation (in radians)
//-----------------------------------------------------
function kepler(m, ex, err)
{
//val u0, delta;

m *= d2r;
u0 = m;
err *= d2r;
delta = 1;
while (Math.abs(delta) >= err) {
    delta = (m + ex * Math.sin(u0) - u0) / (1 - ex * Math.cos(u0));
    u0 += delta;
}
return u0;
}

//-----------------------------------------------------
// Nutation
//-----------------------------------------------------
function nutation(jd)
{
t = (jd - 2415020)/36525;
t2 =t*t;
  
ls = 279.6967 + 36000.7689*t + 0.000303*t2;
l = 270.4341639 + 481267.8831417*t - 0.0011333333*t2;
ms = 358.4758333333334 + 35999.04974999958*t - t2*1.500000059604645e-4;
ml = 296.1046083333757 + 477198.8491083336*t + 0.0091916667090522*t2;
d = 350.7374861110581 + 445267.1142166667*t - t2*1.436111132303874e-3;
om = 259.1832750002543 - 1934.142008333206*t + .0020777778*t2;
ls *= d2r; l *= d2r; ms *= d2r; ml *= d2r; d *= d2r; om *= d2r;
d2 =d*d; l2 = l*l; ls2 = ls*ls;

with(Math){
      nut = (-17.2327 - 0.01737 * t) * sin(om);
      nut += 0.2088 * sin(2.0 * om);
      nut += 0.0675 * sin(ml);
      nut -= 0.0149 * sin(ml - d2);
      nut -= 0.0342 * sin(l2 - om);
      nut += 0.0114 * sin(l2 - ml);
      nut -= 0.2037 * sin(l2);
      nut -= 0.0261 * sin(l2 + ml);
      nut += 0.0124 * sin(ls2 - om);
      nut += 0.0214 * sin(ls2 - ms);
      nut -= 1.2729 * sin(ls2);
      nut -= 0.0497 * sin(ls2 + ms);
      nut += 0.1261 * sin(ms);
      nut = nut/3600.0;
}
return nut;
}

//-----------------------------------------------------
// Ayanamsa (deg.)
//-----------------------------------------------------
function calcayan(jd) 
{
t = (jd - 2415020)/36525;
om = 259.183275 - 1934.142008333206 * t + 0.0020777778 * t * t + 0.0000022222222 * t * t * t;
ls = 279.696678 + 36000.76892 * t + 0.0003025 * t * t;
aya = 17.23 * Math.sin(d2r * om) + 1.27 * Math.sin(d2r * ls * 2) - (5025.64 + 1.11 * t) * t;
aya = (aya - 80861.27)/3600.0;                //  84038.27 = Fagan-Bradley, 80861.27 = Lahiri

return aya;
}

//------------------------------------------------------------------------------------------
// Julian day from calendar day
//------------------------------------------------------------------------------------------
function mdy2julian(m,d,y){
  with(Math){
    im = 12 * (y + 4800) + m - 3;
    j = (2 * (im - floor(im/12) * 12) + 7 + 365 * im)/12;
    j = floor(j) + d + floor(im/48) - 32083;
    if(j > 2299171)j += floor(im/4800) - floor(im/1200) + 38;
    j -=0.5;
  }
return j;
}

//-----------------------------------------------------------------------------------------
// Returns delta t (in julian days) from universal time  (h.)
//-----------------------------------------------------------------------------------------
function dTime(jd)
{
// delta t from 1620 to 2010 (sec.):
var efdt = [124,85,62,48,37,26,16,10,9,10,11,11,12,13,15,16,17,17,13.7,12.5,12,7.5,5.7,7.1,7.9,1.6,-5.4,-5.9,-2.7,10.5,21.2,24,24.3,29.2,33.2,40.2,50.5,56.9,65.7,75.5];
s = calData(jd);
dgod = kyear + (kmon - 1)/12 + (kday - 1)/365.25;
t = (jd - 2378497)/36525;
if (dgod >= 1620  && dgod < 2010) {
    i1 = Math.floor((dgod - 1620)/10); 
    di = dgod - (1620 + i1*10); 
    dt = (efdt[i1] + ((efdt[i1 + 1] - efdt[i1])*di)/10);
    }
    else {
       if (dgod >= 2010) dt = 25.5 * t * t - 39;
       if (dgod >= 948 && dgod < 1620) dt = 25.5 * t * t;
       if (dgod < 948) dt = 1361.7  + 320 * t + 44.3 * t * t;
    }
dt /= 3600;
return dt;
}  

//------------------------------------------------------------------------------------------
// Calendar day from Julian Day
//------------------------------------------------------------------------------------------
function calData(jd)
{
with(Math){
z1 = jd + 0.5;
z2 = floor(z1);
f = z1 - z2;

if(z2 < 2299161)a = z2;
  else {
  alf = floor((z2 - 1867216.25)/36524.25);
  a = z2 + 1 + alf - floor(alf/4);
  }

b = a + 1524;
c = floor((b - 122.1)/365.25);
d = floor(365.25*c);
e = floor((b - d)/30.6001);

days = b - d - floor(30.6001*e) + f;
kday = floor(days);

if(e < 13.5)kmon = e - 1;
else kmon = e - 13;

if(kmon > 2.5)kyear = c - 4716;
if(kmon < 2.5)kyear = c - 4715;

hh1 = (days - kday)*24;
khr = floor(hh1);
kmin = hh1 - khr;
ksek = kmin*60;
kmin = floor(ksek);
ksek = floor((ksek - kmin)*60);
if (kday < 10)kday = " " + kday;
if (kmon < 10)kmon = "0" + kmon;
if (khr < 10)khr = "0" + khr;
if (kmin < 10)kmin = "0" + kmin;
if (ksek < 10)ksek = "0" + ksek;
s = kday + "." + kmon + "." + kyear + " " + khr + ":" + kmin;
}
return s;
}

//------------------------------------------------------------------------------------------
// Translation degrees of a longitude in degrees, minutes, seconds of the zodiac sign 
//------------------------------------------------------------------------------------------
function lon2dmsz(x)
{
	with(Math){
		var d,m,s;
		x = abs(x);
		z = floor(x / 30);
		d = floor(x);
		ss0 = round((x - d) * 3600);
		m = floor(ss0 / 60);
		s = (ss0 % 60) % 60;
		d %= 30;
		str = d + "°" + m + "'" + s + "\" " + zn[z];
	}
	return str;
}

//------------------------------------------------------------------------------------------
// Translation degrees in degrees, minutes, seconds
//------------------------------------------------------------------------------------------
function lon2dms(x)
{
	with(Math){
		var d,m,s;
		x = abs(x);
		d = floor(x);
		ss0 = round((x - d) * 3600);
		m = floor(ss0 / 60);
		s = (ss0 % 60) % 60;
		str = d + "°" + m + "'" + s + "\"";	
	}
	return str;
}

//------------------------------------------------------------------------------------------
// Saving in limits of 360 degrees
//------------------------------------------------------------------------------------------
function fix360(v)
{
	while(v < 0.0)v += 360.0;
	while(v > 360.0)v -= 360.0;
	return v;
}

//------------------------------------------------------------------------------------------
// Day of week
//------------------------------------------------------------------------------------------
function weekDay(jd)
{
jd0 = Math.floor(jd) + 0.5;
if (jd < jd0)jd0 -= 1;

jdn = jd0 + 1.5;
dn1 = Math.floor(jdn/7)*7;
wday = Math.floor(jdn - dn1);
return wday;
}

//------------------------------------------------------------------------------------------
// Print panchang
//------------------------------------------------------------------------------------------
function results() {
  panch='<div align="center"><h1>Swara Panchang</h1></div><br>';
  panch+='<font class="text8">' + (n_tithi + 1) + '. ' + tith[n_tithi] + 
   '</font><br><font class="text3" color="#666666">' + s_tithi + 
   '</font></td><td valign="top" width="78%" colspan="2"><font class="text3">' + 
   '</font></td></tr><tr bgcolor="#fcf8dd"><td valign="middle" bgcolor="#fcf8dd" nowrap colspan="2"><font class="text3" color="#3DB679">Nakshatra:</font><br>';
  document.all.vvv.innerHTML=panch;
  alert("Test");
  }


//------------------------------------------------------------------------------------------
// Replacement of buttons in the menu
//------------------------------------------------------------------------------------------ 
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		menu_moon_2 = newImage("../../img/tithi/menu_moon_2.gif");
		menu_muh_2 = newImage("../../img/tithi/menu_muh_2.gif");
		menu_prog_2 = newImage("../../img/tithi/menu_prog_2.gif");
		menu_scri_2 = newImage("../../img/tithi/menu_scri_2.gif");
		preloadFlag = true;
	}
}

//------------------------------------------------------------------------------------------
// Page upwards
//------------------------------------------------------------------------------------------ 
function scrollit() { 
for (i=100; i>=1; i--) {self.scroll(1,i)}
}
 
//-->
// Copied from the Panchang calculator above:
//--> 
