﻿var rtime, rday, rmon, ryear, rdw, rntime;
var RPD = 0.01745329251994329577; /* radians per degree (pi/180) */
var TIMZ = 3.0;
var MINAGE = 13.5;
var SUNSET = 19.5; /*approximate */
var TIMDIF = (SUNSET-MINAGE) ;
var NMONTHS = (1405*12+1); 
h = {time: rtime, day: rday , mon: rmon , year: ryear , dw: rdw ,  nmtime: rntime }
dow = ["ஞாயிறு", "திங்கள்", "செவ்வாய்", "புதன்",	"வியாழன்", "வெள்ளி", "சனி" ]
mname = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
hmname = [	"முஹர்ரம்", "ஸபர்", "ரபியுல் அவ்வல்", "ரபியுல் ஆஹிர்", 	"ஜமாத்திலவ்வல்", "ஜமாத்திலாஹிர்", "ரஜப்", "ஷஃபான்",	"ரமலான்", "ஷவ்வால்", "துல்காயிதா", "துல்ஹஜ்"]
ehmname = ["MuHarram", "Safar", "Raby` al-awal", "Raby` al-THaany","Jumaada al-awal", "Jumaada al-THaany", "Rajab", "SHa`baan",	"RamaDHaan", "SHawwal", "Thw al-Qi`dah", "Thw al-Hijjah"]
edow = [	"Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"]
tmname= ["ஜனவரி","பிப்ரவரி","மார்ச்","ஏப்ரல்","மே","ஜுன்","ஜுலை","ஆகஸ்டு","செப்டம்பர்","அக்டோபர்","நவம்பர்","டிசம்பர்"]
g = {time: rtime, day: rday , mon: rmon , year: ryear , dw: rdw ,  nmtime: rntime } // new

var rjd;
var y, m, d;

function Int(x)
{
 if ( x < 0 )
 {
    	x = -x;
	x = Math.floor(x+(x < 0 ? 1 : 0))
	x = -x;
 }
 else
 {
 	x = Math.floor(x+(x < 0 ? 1 : 0))
 } 
 return x;
}

function tmoonphase( n, nph)
{
   nph = Int(nph);
	var jd, t, t2, t3, k, ma, sa, tf, xtra;
	k = n + nph/4.0;  t = k/1236.85;  t2 = t*t; t3 = t2*t;
	jd =  2415020.75933 + 29.53058868 * k - 1.178e-4 * t2 - 1.55e-7  * t3 + 3.3e-4 * Math.sin(RPD * (166.56 +132.87 * t - 0.009173 * t2)); 

	/* Sun's mean anomaly */
	sa =  RPD * (359.2242 + 29.10535608*k - 3.33e-5 * t2 - 3.47e-6 * t3);

	/* Moon's mean anomaly */
	ma =  RPD * (306.0253 + 385.81691806*k + 0.0107306*t2 +1.236e-5 *t3);

	/* Moon's argument of latitude */
	tf = RPD * 2.0 * (21.2964 + 390.67050646*k -0.0016528*t2
		      -2.39e-6 * t3);

	/* should reduce to interval 0-1.0 before calculating further */
	if (nph==0 || nph==2)
		/* Corrections for New and Full Moon */
		xtra = (0.1734 - 0.000393*t) * Math.sin(sa)
		      +0.0021*Math.sin(sa*2)
		      -0.4068*Math.sin(ma) +0.0161*Math.sin(2*ma) -0.0004*Math.sin(3*ma)
		      +0.0104*Math.sin(tf)
		      -0.0051*Math.sin(sa+ma) -0.0074*Math.sin(sa-ma)
		      +0.0004*Math.sin(tf+sa) -0.0004*Math.sin(tf-sa)
		      -0.0006*Math.sin(tf+ma) +0.0010*Math.sin(tf-ma)
		      +0.0005*Math.sin(sa+ 2*ma);
	else if (nph==1 || nph==3) {
		xtra = (0.1721 - 0.0004*t) * Math.sin(sa)
		      +0.0021*Math.sin(sa*2)
		      -0.6280*Math.sin(ma) +0.0089*Math.sin(2*ma) -0.0004*Math.sin(3*ma)
		      +0.0079*Math.sin(tf)
		      -0.0119*Math.sin(sa+ma) -0.0047*Math.sin(sa-ma)
		      +0.0003*Math.sin(tf+sa) -0.0004*Math.sin(tf-sa)
		      -0.0006*Math.sin(tf+ma) +0.0021*Math.sin(tf-ma)
		      +0.0003*Math.sin(sa+ 2*ma) +0.0004*Math.sin(sa-2*ma)
		      -0.0003*Math.sin(2*sa+ma);
		if (nph==1)
			xtra = xtra +0.0028 -0.0004*Math.cos(sa) +0.0003*Math.cos(ma);
		else
			xtra = xtra -0.0028 +0.0004*Math.cos(sa) -0.0003*Math.cos(ma);
	} else {
		printf("tmoonphase: illegal phase number\n");
		exit(1);
	}
	/* convert from Ephemeris Time (ET) to (approximate) 
	   Universal Time (UT) */
	jd += xtra - (0.41 +1.2053*t +0.4992*t2)/1440;
	return (jd);
}

function julianday( year, month, day, time)
{
	var jul;
	var m,y,ja;
	if (year<0) year++;
	if (month>2) {
		y = year; m =month;
	} else {
		y = year-1; m =month+12;
	}
	jul = y * 365.25;
	
	if (y<1) jul -= 0.75;
	
	jul = Int(jul) + (Int(30.6001*(m+1))) +day +time +1720994.5; 
   
	if (year +month*1e-2 +(day+time)*1e-4 >= 1582.1015) {
		/* cross-over to Gregorian calendar */
		ja = Int(0.01*y);
		jul = jul +2 -ja + (Int(0.25*ja));
	}
	return jul;
}


function visible(n, riy)
{
	var jd;
	var tf;
	var d;
    	n = Int(n);
	jd = tmoonphase(n,0);  rjd = jd;
	d = Int(jd);

	tf = (jd - d);
	if (tf<=0.5)  /*new moon starts in the afternoon */
		return(jd+1.0); 
	/* new moon starts before noon */
	tf = (tf-0.5)*24 +TIMZ;  /* local time */ 
	if (tf>TIMDIF) return(jd+1.0);  /*age at sunset < min for visiblity*/

	return(jd);
}

function hdate (y,m,d)
{
	var jd, mjd;
	var k, hm;
  
	jd = julianday(y,m,d,0.00); 
	/* obtain first approx. of how many new moons since the beginning
	   of the year 1900 */
	k = Int(0.6 + (y + (Int(m-0.5)) /12.0 + d/365.0 - 1900) *12.3685);
	do  {
	mjd = visible(k--, rjd); 
	} while (mjd > jd);  
	k++;
	/*first of the month is the following day*/
	hm = Int(k - 1048);  
	h.year = Int(1405 + Int(hm / 12));
	h.mon =  Int((hm % 12) +1); 
	if (hm !=0 && h.mon<=0) {h.mon +=12; h.year--; }
	if (h.year<=0) h.year--;
	h.day = Int(jd - mjd + 1.0);
	h.time = 0.5;
	h.dw = Int(Int(jd+1.5) % 7);
	return(h);
}
function caldate( julday )
{
	julday += 0.5; z = Int(julday); f = julday-z;
	if ( z < 2299161 )  
	{
		a = z;
	}
	else {
		alpha = Int((z-1867216.25) / 36524.25);
		a     = Int(z + 1 +alpha - Int(alpha / 4));
		}

	b = Int(a + 1524);  c = Int((b - 122.1) / 365.25); d = Int(365.25 * c);
	e = Int((b-d) / 30.6001);
	f += b -d - (Int(30.6001*e));
	
	g.day = Int(f);
	g.time = f - g.day;
	g.mon = (e>13) ? e-13 : e-1;
	g.year = (g.mon>2) ? c-4716 : c-4715;
	g.dw = Int((Int(julday - g.time + 1.1)) % 7);
	if ( g.year<=0) g.year--;
	return(g);
}

function gdate( y, m, d)
{
	if (y<0) y++;
	k = Int(m+ y*12 - NMONTHS);   /* # of months since 1/1/1405 */

	jd = visible(k+1048, rjd) +d;

	g = caldate(jd);
	g.nmtime = rjd;
	return (g);
	
}


function main()
{
	var 	d, m, y, hflag=0;

	var time= new Date();
	var m = time.getMonth() + 1;
	var d = time.getDate();
	var y = time.getYear();
	
	if (y < 2000){
	    y = y + 1900;
	}
   
	sd = hdate(y, m, d);
   hijri =  '<font color="#CC0099"><font class="mu">'+dow[h.dw] + '</font> ' + h.day + '<font class="mu"> ' + hmname[h.mon - 1] + '</font> ' + h.year + ' <font class="mu">ஹிஜ்ரி</font></font>'
   return hijri;
}

