//alert("funkcije.js");

/* konverzija sin,cos,tan, ... da rade sa stupnjevima */
function sinDeg(x)
{
	return Math.sin(x*Math.PI/180);
}

function cosDeg(x)
{
	return Math.cos(x*Math.PI/180);
}

function tanDeg(x)
{
	return Math.tan(x*Math.PI/180);
}

function atan2Deg(y,x)
{
	return (Math.atan2(y,x)*180/Math.PI);
}

function asinDeg(x)
{
	return Math.asin(x)*180/Math.PI;
}

function acosDeg(x)
{
	return Math.acos(x)*180/Math.PI;
}

function atanDeg(x)
{
	return Math.atan(x)*180/Math.PI;
}

function hour2deg(hour)
{
	return hour*15;
}


function deg2hour(deg)
{
	//return deg;

	var x=deg/15;
	var result=0, min=0, sec=0;

	if (deg>=0)
	{
		min = (x-Math.floor(x))*60;
		sec = (min-Math.floor(min))*60;
		result=(Math.floor(x)+"h"+ Math.floor(min)+"m"+Math.floor(sec)+"s");
	}
	else
	{
		x=Math.abs(x);
		min = (x-Math.floor(x))*60;
		sec = (min-Math.floor(min))*60;
		result=("-"+Math.floor(x)+"h"+ Math.floor(min)+"m"+Math.floor(sec)+"s");
	}

	return result;
}

function degMinSec2Deg(deg,m,sec)
{
 var result=0;
 result=deg+m/60+sec/3600;
 return result;
}

function deg2degSec(deg)
{
	var result=0, min=0, sec=0;
	if (deg>=0)
	{
		min = (deg-Math.floor(deg))*60;
		sec = (min-Math.floor(min))*60;
		result=(Math.floor(deg)+"°"+Math.floor(min)+"'"+Math.floor(sec)+"''");
	}
	else
	{
		deg=Math.abs(deg);
		min=(deg-Math.floor(deg))*60;
		sec=(min-Math.floor(min))*60;
		result=("-"+Math.floor(deg)+"°"+Math.floor(min)+"'"+Math.floor(sec)+"''");
	}
	return result;
}

function sec2MinSec(x)
{
	var result=0, min=0,sec=0;
	if (x>=0)
	{
		min=x/60;
		sec = (min-Math.floor(min))*60;
		result=(Math.floor(min)+"'"+Math.floor(sec)+"''");
	}
	else
	{
		x=Math.abs(x);
		min=x/60;
		sec = (min-Math.floor(min))*60;
		result=("-"+Math.floor(min)+"'"+Math.floor(sec)+"''");
	}
	return result;
}

function norm360(x)
{
	var result=0;
	if (x>=0) 	result = (x-Math.floor(x/360)*360);
 	else 		result =360-(Math.abs(x)-Math.floor(Math.abs(x)/360)*360);

 	//alert("norm360: x="+x+" result="+result);
 	return result;
}
function norm180(x)
{
	var result;
	if (x>=0) result = (x-Math.floor(x/360+0.5)*360);
	else      result = -1*(Math.abs(x)-Math.floor(Math.abs(x)/360+0.5)*360);

	 //alert("norm180: x="+x+" result="+result);

 	return result;
}

function norm24(x)
{
	if (x<0) x+=24;
	else if (x>=24) x-=24;
	return x;
}

function JD2Date(JD)
{
 //alert("ENTER::JD2Date()"+" "+JD);
 var returnDate, Z, F, A, B, C, D, E, alfa, day, month, year;
 
 if (JD<0) { 
  alert ("JD2Date::ERROR - Negative JD not supported!");
  return 0;
  }
 JD=JD+0.5;
 Z=Math.floor(JD);
 F=JD-Z;
 if (Z<2299161) A=Z;
 else if (Z>=2291161) 
 {
  alfa = Math.floor((Z-1867216.25)/36524.25);
  A = Z + 1 + alfa - Math.floor(alfa/4);
 }

  B = A + 1524;
  C = Math.floor((B-122.1)/365.25);
  D = Math.floor(365.25*C);
  E = Math.floor((B-D)/30.6001);
  day = B - D - Math.floor(30.6001*E)+F;
  if (E<14) month = E-1;
  else month = E -13;
  if (month>2) year = C - 4716;
  else year = C - 4715;
  
  hour = (day-Math.floor(day))*24;
  min = (hour - Math.floor(hour))*60;
  sec = (min-Math.floor(min))*60;
  
  //alert(day+" "+hour+" "+min+" "+sec);
    
  returnDate = new Date();
  returnDate.setFullYear(year);
  returnDate.setMonth(month-1);
  returnDate.setDate(Math.floor(day));
  returnDate.setHours(Math.floor(hour));
  returnDate.setMinutes(Math.floor(min));
  returnDate.setSeconds(Math.floor(sec));	
     
  
  //alert("RETURN::JD2Date() :: "+ispisiDate(returnDate));
  
  return returnDate;
}


function calculateJD(Y,M,D)
{
  //alert("ENTER::convert2JulianDate() "+"Y="+Y+" M="+M+" D="+D);
  var A=0, B=0, JD=0;

  if (M<=2) 
  {
    Y=Y-1;
    M=M+12;
  }
  
  // Julian Calendar ends on 04.10.1582   
  if (DaliJeGregorijanksiKalendar(Y,M,D) == true ) {
    A=Math.floor(Y/100);
    B=2-A+Math.floor(A/4);
  } else B=0;
   
  //alert("Y="+Y+" M="+M+" D="+D+" A="+A+" B="+B);  
  JD = Math.floor(365.25*(Y+4716.0)) + Math.floor(30.6001*(M+1.0)) + D + B - 1524.5;
  // otkud ovo ?? JD=JD+leapYearException;
  //alert("RETURN::calculateJD() "+JD);
  return JD;
}


function UT2LT(UT,format)
{
  /* format=0 dd/mm/yyyy hh:mm:ss
     format=1 hh:mm:ss */
  
  var result="", godina=0, mjesec=0, dan=0, sat=0, min=0, sec=0;
  
  //alert("ENTER:UT2LT() :m="+UT.getMonth()+" h="+UT.getHours());
  
  dan = UT.getDate();
  mjesec  = UT.getMonth()+1;
  godina = UT.getFullYear();
  sat = UT.getHours()+GMT;
  min = UT.getMinutes();
  sec = UT.getSeconds();
  
  if (ljetnoF == true ) sat=sat+1;
  
  //alert("sat="+sat+"GMT="+GMT);
  
  if (GMT>0)
  {
   if (sat>=24)
	 {
	  sat=norm24(sat);
	  dan++;
	  switch (mjesec)
	    {
	    case 1: 
	    case 3: 
	    case 5: 
	    case 7: 
	    case 8: 
	    case 10: 
	    case 12:  // mjesec ima 31 dan
	      if (dan>31)
    		{
    		  dan=1;
    		  mjesec++;
    		  if (mjesec>12) 
    		    {
    		      mjesec=1;
    		      god++;
    		    }
    		}
	      break;		
	    case 4: 
	    case 6: 
	    case 9: 
	    case 11: // mjesec ima 30 dana
	      if (dan>30)
    		{
    		  dan=1;
    		  mjesec++;	
    		}
	      break;
	    case 2: //veljaca
	      if ((godina-Math.floor(godina/4)*4)==0) //prestupna godina
      		{
      		  if (dan>29) dan=1;
      		  mjesec++;
      		}
      	      else
      		{
      		  if (dan>28) dan=1;
      		  mjesec++;
      		}
	      break;
	    default: alert("This value is Unexpected: Contact Fox Mulder for details !"); break;
	    }//End switch mjesec
	} // end of sat>24
    } //end if GMT>0
  else
    {
    if (GMT != 0)
	   {
	   if (sat<0)
	    {
	      sat=norm24(sat);
	      dan--;
	      if (dan==0)
		    {
		    mjesec--;
		    if (mjesec==0)
		    {
		      mjesec=12;
		      godina--;
		      dan=31;
		    } //end of mjesec==0
		  else
		    {
		      switch (mjesec)
			{
			case 1: 
			case 3: 
			case 5: 
			case 7: 
			case 8: 
			case 10: 
			case 12:  // mjesec ima 31 dan
			  dan=31;
			  break;		
			case 4: 
			case 6: 
			case 9: 
			case 11: // mjesec ima 30 dana
			  dan=30;
			  break;
			case 2: //veljaca
			  if ((godina-Math.floor(godina/4)*4)==0) //prestupna godina
			    {
			      dan=29;
			    }
			  else
			    {
			      dan=28;
			    }
			  break;
			default: alert("This value is Unexpected: Contact Fox Mulder for details !"); break;
			}//End switch mjesec
		    } // end of mjesec!=0					
		} //end of dan=0
	    } //end of sat<0					
	} //end of GMT !=0		
    } // end of gmt<0
  
  if (format == 0) result = dan.toString()+"/"+mjesec.toString()+"/"+godina.toString()+" "+sat.toString()+":"+min.toString()+":"+sec.toString();
  else             result = sat.toString()+":"+min.toString()+":"+sec.toString()+" ";
  
  return result;
  
}

function cleanUpSamples (sampleArray)
{
  alert("ENTER::cleanUpSamples()");


  alert("RETURN::cleanUpSamples()");
  return;
}

function DaLiJeLjetnoVrijeme (kada)
{
	/* windows 2000 laptop mijenja zimsko na ljetno zadnje nedjelje u ožujku
	a na netu vele prve nedjelje u travnju -- pa cu sada napraviti kao i bil gates
	dakle zadnja u ožujku

	sa ljetnog na zimsko se salta zadnje nedjelje u 10 mjesecu	*/

	var  result=false;
 	var mjesec = parseInt(kada.getMonth()+1);
 	var dan = kada.getDate();

	if (mjesec> 3 && mjesec<10) 		result=true;
	else if (mjesec<3 || mjesec>10) 	result=false;
	else if(mjesec == 3)
	{
		if (dan >=25 && dan <=31)
		{
			if (dan < parseInt(kada.getDay()+25)) 	 result=false;
			else 					                      result=true;
		}
		else result=false;
	}
	else if (mjesec == 10)
	{
		if (dan >=25 && dan <=31)
		{
			if (dan < parseInt(kada.getDay()+25)) 	 result=true;
			else 					                      result=false;
		}
		else result=true;
	}

	//alert(result);
	return result;
}// end of DaLiJeLjetnoVrijeme()


function izracunaj_UT(t_date)
{
	//alert("ENTER: izracunaj_UT");
	var result=0, h=0, m=0, s=0;
	//alert(t_date.toString()+"UT="+result);
	if (document.PlanetOrbitCalculator == null)
	{
		//GMT = 1 for Zagreb ..used in index_new.html
		GMT = 1;
		//GMT = -1*t_date.getTimezoneOffset()/60	;
		//alert(GMT);
	}
	h=norm24(t_date.getHours()-Math.floor(GMT));
	//alert("izracunaj_UT():: h="+h);
	if (document.PlanetOrbitCalculator)
		if (document.PlanetOrbitCalculator.ljetno.checked == true ) h=h-1;
	else
		if (DaLiJeLjetnoVrijeme(t_date)==true) h=h-1;

	if ( (GMT-Math.floor(GMT))==0)		m=Math.floor(t_date.getMinutes()/60);
	else
		if (GMT>0) m=Math.floor((t_date.getMinutes()-30)/60);
		else       m=Math.floor((t_date.getMinutes()+30)/60);
	s=Math.floor(t_date.getSeconds()/3600);
	result=h+m+s;
	//alert(t_date.toString()+"UT="+result);
    return result;
}

function izracunaj_ecl(t_d)
/* ecl=the obliquity of the ecliptic, i.e. the "tilt" of the Earth's axis of rotation
(currently ca 23.4 degrees and slowly decreasing). */
{
	return 23.4393-3.563E-7*izracunaj_d(t_d);
}


function zaokruzi(x,decimala)
{
	var result=0,dec_t=0;

	if (x>0)
	{
		dec_t=x-Math.floor(x);
		dec_t=dec_t*Math.pow(10,decimala);
		dec_t=Math.round(dec_t)*Math.pow(10,-decimala);
		result=Math.floor(x)+dec_t;

		result=(Math.floor(result*Math.pow(10,decimala)))/Math.floor(Math.pow(10,decimala));
	}
	else
	{
		x=Math.abs(x);
		dec_t=x-Math.floor(x);
		dec_t=dec_t*Math.pow(10,decimala);
		dec_t=Math.round(dec_t)*Math.pow(10,-decimala);
		result=Math.floor(x)+dec_t;
		result=(Math.floor(result*Math.pow(10,decimala)))/Math.floor(Math.pow(10,decimala));
		result=-1*result;
	}




	//alert("zaokruzi("+x+"decimala="+decimala+")="+result);
	return result;

}
function izracunaj_d(t_date)
{
	var d=0; // day number
	var UT=0; //Universal Time
	var godina = t_date.getFullYear();
	//alert(t_date.getMonth());
	var mjesec = t_date.getMonth()+1;
	var dan    = t_date.getDate();

	UT=izracunaj_UT(t_date);

	d=367*godina-Math.floor(7*(godina+Math.floor((mjesec+9)/12))/4)+Math.floor(275*mjesec/9)+dan-730530;
	d=d+UT/24.0;

	//alert(t_date.toLocaleString());
	//alert("d="+d);
	return d;
}

function precession_corr(kada)
{
	//Precession correction for epoch
	var Epoch=2000.0;
	//alert("prec_corr="+3.82394E-5 * ( 365.2422 * (Epoch - 2000.0)-izracunaj_d(kada)));
	return  (3.82394E-5 * ( 365.2422 * (Epoch - 2000.0)-izracunaj_d(kada)));
}

/*************************************************************
 *                  pozicija_planeta                         *
 *************************************************************/

function pozicija_planeta(kada, planeta)
{
	var orb_param=new Array();
	var N=0, i=0, w=0, a=0, e=0, M=0, loopProtector=0;
	var E=0, E1=0, E0=0, xv=0, yv=0, v=0, r=0,  xh=0, yh=0, zh=0, lonecl=0, latecl=0;
	var vrati_parametre= new Array();

	//alert("ENTER: pozicija_planeta "+planeta);

	switch (planeta)
	{
		case "mjesec":  orb_param = mjesec_Orb_Elem(kada); break;
		case "merkur":  orb_param = merkur_Orb_Elem(kada); break;
		case "venera":  orb_param = venera_Orb_Elem(kada); break;
		case "mars":    orb_param = mars_Orb_Elem(kada); break;
		case "jupiter": orb_param = jupiter_Orb_Elem(kada); break;
		case "saturn":  orb_param = saturn_Orb_Elem(kada); break;
		case "uran":    orb_param = uran_Orb_Elem(kada); break;
		case "neptun":  orb_param = neptun_Orb_Elem(kada); break;
		default: alert("error: unexisting planet -> please contact Fox: "+planeta); break;
	}

	N = orb_param["N"];
	i = orb_param["i"];
	w = orb_param["w"];
	a = orb_param["a"];
	e = orb_param["e"];
	M = orb_param["M"];

	if (e<=0.05) E = M + e*(180/Math.PI) * sinDeg(M) * ( 1.0 + e * cosDeg(M) );
	else
	{
		E1 = M + e*(180/Math.PI) * sinDeg(M) * ( 1.0 + e * cosDeg(M) );
		//alert("pocinje petlja: E1=E0="+E1);
		do
		{
			E0=E1;
			E1 = E0 - ( E0 - e*(180/Math.PI) * sinDeg(E0) - M ) / ( 1 - e * cosDeg(E0) );
			//alert(Math.abs(E1-E0));
			loopProtector++;
			if (loopProtector>=10) break; //nek se nadje
		}
		while (Math.abs(E1-E0)>1E-3);
		E=E1;
	}
	//alert(planeta+" E="+E);
	xv =  a * ( cosDeg(E) - e );
	yv =  a * ( Math.sqrt(Math.abs(1.0 - e*e)) * sinDeg(E) );

	v = atan2Deg( yv, xv );
	r = Math.sqrt( Math.abs(xv*xv + yv*yv) );

	/* Compute the planet's position in 3-dimensional space,
	For the Moon, this is the geocentric (Earth-centered) position in the ecliptic coordinate system.
	For the planets, this is the heliocentric (Sun-centered) position, also in the ecliptic coordinate system. */

	xh = r * ( cosDeg(N) * cosDeg(v+w) - sinDeg(N) * sinDeg(v+w) * cosDeg(i) );
	yh = r * ( sinDeg(N) * cosDeg(v+w) + cosDeg(N) * sinDeg(v+w) * cosDeg(i) );
	zh = r * ( sinDeg(v+w) * sinDeg(i) );


	/* ecliptic longitude and latitude  */
	lonecl = norm360(atan2Deg( yh, xh ));
    latecl = atan2Deg( zh, Math.sqrt(Math.abs(xh*xh+yh*yh)));

	//precesion correction
	//lonecl += precession_corr(kada);

	//alert(planeta+" xh="+xh+" yh="+yh+" zh="+zh+" lonecl="+lonecl+" latecl="+latecl);
	/* As a check one can compute sqrt(xh*xh+yh*yh+zh*zh),
	which of course should equal r (except for small round-off errors). */

	vrati_parametre["N"]= N;
	vrati_parametre["i"]= i;
	vrati_parametre["w"]= w;
	vrati_parametre["a"]= a;
	vrati_parametre["e"]= e;
	vrati_parametre["M"]= M;
	vrati_parametre["v"]= v;
	vrati_parametre["r"]= r;
	vrati_parametre["lonecl"]= lonecl;
	vrati_parametre["latecl"]= latecl;

	return vrati_parametre;

} // end of pozicija_planeta()


/*************************************************************
 *                  planet_RA_Decl                           *
 *************************************************************/

function planet_RA_Decl(planeta, ecl, r, lonecl, latecl, r_sunce, lon_sunce )
{
	var xh=0, yh=0, zh=0, xs=0, ys=0, xg=0, yg=0, zg=0, xe=0, ye=0, ze=0, RA=0, Dec=0, rg=0;
	var vrati_parametre= new Array();

	//alert("ENTER:planet_RA_Decl"+ planeta);
	xh = r * cosDeg(lonecl) * cosDeg(latecl);
    yh = r * sinDeg(lonecl) * cosDeg(latecl);
    zh = r * sinDeg(latecl);
	if (planeta == "mjesec")
	{
		xg=xh;
		yg=yh;
		zg=zh;
	}
	else
	{
		xs = r_sunce * cosDeg(lon_sunce);
    	ys = r_sunce * sinDeg(lon_sunce);
    	xg = xh + xs;
   		yg = yh + ys;
    	zg = zh;
    }
	xe = xg;
	ye = yg * cosDeg(ecl) - zg * sinDeg(ecl);
	ze = yg * sinDeg(ecl) + zg * cosDeg(ecl);

	RA  = atan2Deg( ye, xe );
	RA  = norm360(RA);
    Dec = atan2Deg( ze, Math.sqrt(xe*xe+ye*ye) );

	// Compute the geocentric distance:
    rg = Math.sqrt(xe*xe+ye*ye+ze*ze);

	//alert(planeta+" xh="+xh+" yh="+yh+" zh="+zh+" xs="+xs+" ys="+ys+" xg="+xg+" yg="+yg+" zg="+zg+
	//" xe="+xe+" ye="+ye+" ze="+ze+" RA="+RA+" Dec="+Dec+" rg="+rg);
	vrati_parametre["RA"]= RA;
	vrati_parametre["Dec"]= Dec;
	vrati_parametre["rg"]= rg;
	return vrati_parametre;

} // end of planet_RA_Decl()




/****************************
* planet_izlazak_zalazak    *
*****************************/

function planet_izlazk_zalazak(kada, h_konvencija, x_lon, x_lat, M_sunce, w_sunce, RA_planet, Dec_planet,
								M_planet_podne, w_planet_podne, RA_planet_podne, Dec_planet_podne, M_sunce_podne, w_sunce_podne)
{
	var GMST0=0; // sidereal time at Greenwich at 00:00 Universal Time
	var GMST0_podne=0;
	var LST=0;   // Local Sidereal Time (LST)
	var LHA=0;   //  Local Hour Angle (LHA): i.e. the angle the Earth has turned since the planet last was in the south
	var sinh=0;  // sin( altitude above the horizon)
	var H; // planet altitude above the horizon
	var UT_in_south=0;
	var cosLHA=0;
	var UT=izracunaj_UT(kada);
	var izlazak_UT=0;
	var zalazak_UT=0;
	var izlazak=new Date(kada);
	var zalazak=new Date(kada);
	var vrati_parametre = new Array();
	var x=0,y=0,z=0, xhor=0, yhor=0,zhor=0,azimuth=0,altitude=0 ;

	//alert("ENTER:planet_izlazk_zalazak");
	GMST0=norm360(M_sunce+w_sunce+180);
	LST=GMST0+norm24(UT)*15+ x_lon;
	LHA=LST-RA_planet;
	sinh=sinDeg(x_lat)*sinDeg(Dec_planet)+cosDeg(x_lat)*cosDeg(Dec_planet)*cosDeg(LHA);
	H=asinDeg(sinh); // planet altitude above the horizon

	x = cosDeg(LHA) * cosDeg(Dec_planet);
	y = sinDeg(LHA) * cosDeg(Dec_planet);
	z = sinDeg(Dec_planet);

	xhor = x * sinDeg(x_lat) - z * cosDeg(x_lat);
	yhor = y;
	zhor = x * cosDeg(x_lat) + z * sinDeg(x_lat);

	azimuth  = atan2Deg( yhor, xhor ) + 180;
	altitude = atan2Deg( zhor, Math.sqrt(xhor*xhor+yhor*yhor));

	/* polozaj u podne po lokalnom vremenu */
	GMST0_podne = norm360(M_sunce_podne+w_sunce_podne+180);
	UT_in_south = 12-norm180( norm360( GMST0_podne+180+x_lon )-RA_planet_podne )/15;
	cosLHA=(sinDeg(h_konvencija)-sinDeg(x_lat)*sinDeg(Dec_planet_podne))/(cosDeg(x_lat)*cosDeg(Dec_planet_podne));

	if ( cosLHA< 1.0 && cosLHA>-1.0)
	{
		izlazak_UT=norm24(zaokruzi(UT_in_south-acosDeg(cosLHA)/15.04107,3));
		zalazak_UT=norm24(zaokruzi(UT_in_south+acosDeg(cosLHA)/15.04107,3));
		izlazak.setHours(izlazak_UT, (izlazak_UT-Math.floor(izlazak_UT))*60,0,0);
		zalazak.setHours(zalazak_UT, (zalazak_UT-Math.floor(zalazak_UT))*60,0,0);

	}
	else
	{
		//planet nikada ne izlazi/zalazi
		izlazak_UT=0;
		zalazak_UT=0;
		izlazak.setHours(Math.floor(izlazak_UT), Math.floor((izlazak_UT-Math.floor(izlazak_UT))*60),0,0);
		zalazak.setHours(Math.floor(zalazak_UT), Math.floor((zalazak_UT-Math.floor(zalazak_UT))*60),0,0);
	}

	vrati_parametre["altitude above the horizon"] 	= H;         //planet altitude above the horizon
	vrati_parametre["UT_in_south"] 					= UT_in_south; // vrijeme  (UT) kada je planet tocno na jugu
	vrati_parametre["cosLHA"] 						= cosLHA;
	vrati_parametre["izlazak UT"] 					= izlazak_UT; // izlazak  u UT
	vrati_parametre["zalazak UT"] 					= zalazak_UT; // zalazak  u UT
	vrati_parametre["izlazak local time"] 			= izlazak;       // izlazak u lokalnom vremenu
	vrati_parametre["zalazak local time"] 			= zalazak;       // zalazak u lokalnom vremenu
	vrati_parametre["azimuth"]                      = azimuth;
    vrati_parametre["altitude"]                     = altitude;

	return vrati_parametre;
}



/*************************************************************
 *                  planet_ostalo                              *
 *************************************************************/
function planet_ostalo (r,R,s, planet, mlon, mlat, slon, B)
{
	// d = apparent diameter; r = planet's heliocentric distance;
	// R = geocentric distance; s = observer distance to the Sun
	// B = the tilt of Saturn's rings
	var faza=0, FV=0,elong=0, d=0, mag=0,  ring_magn=0;
	var vrati_parametre=new Array();

	if (planet != "mjesec")
	{
		elong  = acosDeg( ( s*s + R*R - r*r ) / (2*s*R) );
		FV     = acosDeg( ( r*r + R*R - s*s ) / (2*r*R) );
		faza  = ( 1 + cosDeg(FV) ) / 2;

		/* planet's apparent diameter at a distance of 1 astronomical unit:

		Mercury     6.74"
	    Venus      16.92"
	    Earth      17.59" equ    17.53" pol
	    Mars        9.36" equ     9.28" pol
	    Jupiter   196.94" equ   185.08" pol
	    Saturn    165.6"  equ   150.8"  pol
	    Uranus     65.8"  equ    62.1"  pol
	    Neptune    62.2"  equ    60.9"  pol
		*/

		switch (planet)
		{
			case "merkur":
			{
				d=6.74/R;
				mag=-0.36 + 5*Math.log(r*R)/Math.LN10 + 0.027 * FV + 2.2E-13 * Math.pow(FV,6);
				//alert("merkur mag="+mag);
    			break;
    		}
			case "venera":
			{
				d=16.92/R;
				mag=-4.34 + 5*Math.log(r*R)/Math.LN10 + 0.013 * FV + 4.2E-7  * Math.pow(FV,3);
				break;
			}
			case "mars":
			{
				d=9.36/R;
				mag=-1.51 + 5*Math.log(r*R)/Math.LN10 + 0.016 * FV;
				break;
			}
			case "jupiter":
			{
				d=196.94/R;
				mag=-9.25 + 5*Math.log(r*R)/Math.LN10 + 0.014 * FV;
				break;
			}
			case "saturn":
			{
				d=165.6/R;
				ring_magn = -2.6 * sinDeg(Math.abs(B)) + 1.2 * Math.pow(sinDeg(B),2);
				mag=-9.0  + 5*Math.log(r*R)/Math.LN10 + 0.044 * FV + ring_magn;
				break;
			}
			case "uran":
			{
				d=65.8/R;
				mag=-7.15 + 5*Math.log(r*R)/Math.LN10 + 0.001 * FV;
				break;
			}
			case "neptun":
			{
				d=62.2/R;
				mag= -6.90 + 5*Math.log(r*R)/Math.LN10 + 0.001 * FV;
				break;
			}
			default: alert("Internal error !! unexisting planet:"+planeta); break;
		}
	}
	else
	{ // mjesec
		d=1873.7* 60 / R;
		elong = acosDeg( cosDeg(slon - mlon) * cosDeg(mlat) );
		FV = 180 - elong;
		faza  = ( 1 + cosDeg(FV) ) / 2;
		mag=0;
		//alert("faza="+faza);
	}


	vrati_parametre["elong"]=elong;
	vrati_parametre["fazni kut"]=FV;
	vrati_parametre["faza"]=faza*100;
	vrati_parametre["d"]=d;
	vrati_parametre["magnitude"]=mag;

	return vrati_parametre;
}


function OnMouseRightOver ()
{
	if (document.images["nextPlanet"])  document.images["nextPlanet"].src =  "pics/rightArrow_over.gif";
	else alert ("Error: button image dosn't exist");
}

function OnMouseRightOut ()
{	
	if (document.images["nextPlanet"]) document.images["nextPlanet"].src =  "pics/rightArrow.gif";
	else alert ("Error: button image dosn't exist");
}

function OnMouseRightDown ()
{
	if (document.images["nextPlanet"]) 	document.images["nextPlanet"].src =  "pics/rightArrow_down.gif";
	else alert ("Error: button image dosn't exist");
}


function OnMouseRightUp ()
{
	if (document.images["nextPlanet"]) document.images["nextPlanet"].src =  "pics/rightArrow.gif";
	else alert ("Error: button image dosn't exist");
}


function OnMouseLeftOver ()
{
	if (document.images["previousPlanet"])  document.images["previousPlanet"].src =  "pics/leftArrow_over.gif";
	else alert ("Error: button image dosn't exist");
}

function OnMouseLeftOut ()
{	
	if (document.images["previousPlanet"]) document.images["previousPlanet"].src =  "pics/leftArrow.gif";
	else alert ("Error: button image dosn't exist");
}


function OnMouseLeftDown ()
{
	if (document.images["previousPlanet"])  document.images["previousPlanet"].src =  "pics/leftArrow_down.gif";
	else alert ("Error: button image dosn't exist");
}


function OnMouseLeftUp ()
{
	if (document.images["previousPlanet"]) document.images["previousPlanet"].src =  "pics/leftArrow.gif";
	else alert ("Error: button image dosn't exist");
}

function izracunajDanUTjednu (kada)
{
  var D=kada.getDate();
  var M=kada.getMonth()+1;
  var Y=kada.getFullYear();
  var JD=0,dan_u_tjednu=0;
  
  JD = calculateJD(Y,M,D);
  JD = JD + 1.5;
  
  dan_u_tjednu = JD-Math.floor(JD/7)*7;
  // 0 = Nedjelja, 1 = Ponedjeljak, 2 = Utorak, 3 = Srijeda, 4 = Četvrtak, 5 = Petak, 6 = Subota
  //alert("RETURN: izracunajDanUTjednu() "+dan_u_tjednu+" JD="+JD);
  return dan_u_tjednu;
}


function DaliJeGregorijanksiKalendar(Y,M,D) 
{
  var Gregorian;
  //alert ("04.10.1582 je uzet kao početak koritenja Gregorijanksog kalendara"); 
  //alert("ENTER: DaliJeGregorijanksiKalendar()"+Y+" "+M+" "+D); 
  if ( Y<1582 ) Gregorian=false; //Julian Calendar
  else if (Y==1582 && M<10 ) Gregorian=false; //Julian Calendar
  else if (Y==1582 && M==10 && D<=4 ) Gregorian=false; //Julian Calendar
  else  Gregorian=true;
  //alert("RETURN: DaliJeGregorijanksiKalendar()"+Gregorian); 
  return Gregorian;
}

function DaliJePrestupnaGodina (year)
{
  /* Gregorian Calendar
  Every year that is exactly divisible by four is a leap year, 
  except for years that are exactly divisible by 100; the centurial years
  that are exactly divisible by 400 are still leap years. For example, 
  the year 1900 is not a leap year; the year 2000 is a leap year. 
  In the Julian calendar, all years exactly divisible by 4 are leap years  */
  if  (year<1582)  {
    if (year-Math.floor(year/4)*4==0 ) return true;
    else return false;
  } else {
    if ((year-Math.floor(year/4)*4)==0 && ((year-Math.floor(year/100)*100)!=0 ) )    return true;
    else {
      if ((year-Math.floor(year/400)*400)==0) return true;
      else return false;
      } 
  }
}

function getNumberOfDaysInMonth (month, year)
{
 //alert("ENTER: getNumberOfDaysInMonth "+month);
  var NoOfDays;
  switch (month) 
  {
   case 1 :
   case 3 :
   case 5 :
   case 7 :
   case 8 :
   case 10 :
   case 12 :
      NoOfDays = 31;
      break;    
   case 4 :
   case 6 :
   case 9 :
   case 11 :
      NoOfDays = 30;
      break;
    case 2 :
      if (DaliJePrestupnaGodina(year)== true) NoOfDays = 29;
      else  NoOfDays = 28;
      break;
   default :
      alert("getNumberOfDaysInMonth:: ERROR");
  } 
  //alert("RETURN: getNumberOfDaysInMonth="+NoOfDays);
  return NoOfDays;
}
 
//alert("funkcije.js loaded");

function ispisiDate(xDate)
{
  var result=" ";
  
  result+=(xDate.getDate()).toString()+"/";
  result+=(xDate.getMonth()+1).toString()+"/";
  result+=(xDate.getFullYear()).toString()+" ";
  result+=(xDate.getHours()).toString()+":";
  result+=(xDate.getMinutes()).toString()+":";
  result+=(xDate.getSeconds()).toString();
  
  return result;
}




function ispisiSamoDatum(xDate)
{
  var result=" ";
  
  result+=(xDate.getDate()).toString()+"/";
  result+=(xDate.getMonth()+1).toString()+"/";
  result+=(xDate.getFullYear()).toString()+" ";
  
  return result;
}

function ispisiSamoGodinu(xDate)
{
  var result=" ";
  result+=(xDate.getFullYear()).toString()+" ";
  return result;
}

function  ispisiSamoMjesec(xDate)
{
  var result=" ";
  result+=(xDate.getMonth()+1).toString();
  return result;
}

function  CleanSample(xSample)
{
  if ( xSample.hasChildNodes()) {
  	do {
  		  xSample.removeChild(xSample.lastChild);
  	} while  (xSample.childNodes.length > 0 );
  }
  //  else alert("empty sample");
}

function  GetWinWidth()
{
  var win_width=0;
  if (document.layers)  
  { // Netscape 4.x
    win_width   = parseInt(window.innerWidth);
    // alert("netscape");
  } 
  else if (ie || ienu || ie4 || ie5 || ie5x || ie6 || ie5mac || ie5xwin) 
  {//Microsoft Internet Explorer
	 if (window.document.body != null) 
   {
    win_width	= parseInt(window.document.body.clientWidth);
	 }
	 	//alert("internet explorer");
  }
  else if(moz || moznu)		
  { // mozilla
	  win_width    = parseInt(window.innerWidth);
	  //alert("mozilla");
	}
  else
    { //other
		 win_width	= parseInt(window.document.body.clientWidth);
    //	  alert("other");
		}
		
  return win_width;
} // end of GetWinWidth

function  GetWinHeight()
{
  var win_height=0;
  if (document.layers)  
  { // Netscape 4.x
    win_height	= parseInt(window.innerHeight);
    // alert("netscape");
  } 
  else if (ie || ienu || ie4 || ie5 || ie5x || ie6 || ie5mac || ie5xwin) 
  {//Microsoft Internet Explorer
	 if (window.document.body != null) 
   {
		win_height	= parseInt(document.body.clientHeight);
	 }
	 	//alert("internet explorer");
  }
  else if(moz || moznu)		
  { // mozilla
   	win_height	 = parseInt(window.innerHeight);
	  //alert("mozilla");
	}
  else
    { //other
	 	 win_height	= parseInt(document.body.clientHeight);
    //	  alert("other");
		}
		
  return win_height;
} // end of GetWinHeight

//alert("fukcije.js loaded..");
