//Copyright (C) 2007 by The.Modificator

function greg2jd(y,m,d,h,n,s)
{
	var t = h+(s/60+n)/60;
	return greg2jd_ut(y,m,d,t);
}

function jd2dni_num(jd)
{
	jd+=1075123.1369444533333333333333333;
	jd/=365.2421875;
	
	return jd;
}

function greg2dni_num(y,m,d,h,n,s)
{
	var jd=greg2jd(y,m,d,h,n,s);
	return jd2dni_num(jd);
}

function dni2jd(h,v,y,ga,t,go,p)
{
	h+=10/(v-1+29/(y-1+5/(ga+25/(t+25/(go+25/p)))));
	
	h*=365.2421875;
	h-=1075123.1369444533333333333333333;
	
	return h;
}

function _myfix(num)
{
	if(num<0)
		return -Math.floor(-num);
	else
		return Math.floor(num);
}

function jd2greg(jd)
{
	var z=Math.floor(jd - 1721118.5);
	var r=jd - 1721118.5 - z;
	var g=z - 0.25;
	var a=Math.floor(g/36524.25);
	var b=a-Math.floor(a/4);
	var year=Math.floor((b+g) / 365.25);
	var c=b + z - Math.floor(365.25 * year);
	var month=_myfix((5 * c + 456) / 153);
	var day=c-_myfix((153 * month - 457) / 5) + r;
	if(month>12)
	{
		year++;
		month-=12;
	}
	
	var realday=Math.floor(day);
	var tmp=(day-realday)*24;
	var hour=Math.floor(tmp);
	var tmp=(tmp-hour)*60;
	var minute=Math.floor(tmp);
	var tmp=(tmp-minute)*60;
	var second=Math.floor(tmp);

	return new Array(year,month,realday,hour,minute,second);
}

function greg2dni(y,m,d,h,n,s)
{
	var dni_num=greg2dni_num(y,m,d,h,n,s);
	
	var hahr=Math.floor(dni_num);
	var tmp=dni_num-hahr;
	
	tmp*=10;
	var vailee=Math.floor(tmp)+1;
	tmp=tmp-vailee+1;
	
	tmp*=29;
	var yahr=Math.floor(tmp)+1;
	tmp=tmp-yahr+1;
	
	//time of day
	tmp*=5;
	var gahrtahvo=Math.floor(tmp);
	tmp=tmp-gahrtahvo;
	
	tmp*=25;
	var tahvo=Math.floor(tmp);
	tmp=tmp-tahvo;	
	
	tmp*=25;
	var gorahn=Math.floor(tmp);
	tmp=tmp-gorahn;	
	
	tmp*=25;
	var prorahn=Math.floor(tmp);

	return new Array(hahr,vailee,yahr,gahrtahvo,tahvo,gorahn,prorahn);
}


/**********************************************************************\
*       The JavaScript code in this section was written by:            *
*                          Rahul Mittal                                *
*       Sophomore, Dept. of Astronomy, Villanova University            *
* Feel free to use the code below provided this header remains intact! *
*       Drop me e-mail at rasteroid@hotmail.com if you want, and       *
*             that way I can check out your homepage... :)             *
*      Corrected Y2K bug October 3, 2003 (JD 2452915) by Adric Riedel
\**********************************************************************/

// This is a modified version by The.Modificator!
// The original code can be found on http://www.astronomy.villanova.edu/links/jd.htm

function greg2jd_ut(year,month,day,univTime) {
   if (year < 1000 ) {year+=1900;}  // modified 10/03/2003
   if ((100*year+month-190002.5) >= 0) {sign = 1;}
      else {sign = -1;}
   with (Math) {
      part1 = 367 * year;
      part2 = floor((7*(year+floor((month+9)/12)))/4);
      part3 = day+floor((275*month)/9);
      part4 = 1721013.5+(univTime/24);
      part5 = 0.5*sign;
      jd = part1-part2+part3+part4-part5+0.5;
   }
   return jd;
}
