/*------------------*/
/*-- dayLightSave --*/
/*------------------*/
   function april( y )
   {
      d = new Date( "April 1, " + y );
      while( d.getDay() != 0 )
      d.setDate( d.getDate() + 1 );
      return d;
   }
   function october( y )
   {
      d = new Date( "November 1, " + y );
      while( d.getDay() != 0 )
      d.setDate( d.getDate() - 1 );
      return d;
   }

   function avril( y )
   {
      d = new Date( "April 1, " + y );
      while( d.getDay() != 0 )
      d.setDate( d.getDate() - 1 );
      return d;
   }
   function octobre( y )
   {
      return october( y );
   }

   function dayLightSave( dt )
   {
      theDD = new Date( dt.toString() );
      s = 0;
      y = theDD.getYear();
      if( !document.all ) y = y + 1900;

      /*---------- US Only ----------*/
      if( theDD>april(y) && theDD<october(y) )
         s++;
      /*---------- Europe ----------*/
//      if( theDD>avril(y) && theDD<octobre(y) )
//         s++;
      return s;
   }

function timeAdd( t, h )
{
   var tmp = t.split( ":" );
   add_h = eval( tmp[0] );
   add_m = eval( tmp[1] );
   add_m += h;

   if( add_m < 0 )
   {
      add_h = add_h - 1;
      add_m += 60;
   }
   if( add_m >= 60 )
   {
      add_h = add_h + 1;
      add_m = add_m - 60;
   }
   return add_h + ":" + (add_m<10?"0":"") + add_m;
}

function shabbatTime( dt )
{
   var yom;
   if( dt == "" )
      yom = new Date();
   else
      yom = new Date( dt );

   addDay = 5 - yom.getDay();
   yom.setDate( yom.getDate() + addDay );
   dayLight = dayLightSave(yom.toString());
   adj  = tmz + dayLight;   //---   GMT -5 EAST + DayLight Saving

   var day_before = new Date(yom.getTime() - 86400000);

   db = day_before.getDate() + 1;
   mb = day_before.getMonth() + 1;
   yb = day_before.getYear();
   if(yb < 1900)
      yb += 1900;

   time = suntime( db, mb, yb, 90, 50, lngd, lngm, ewi, latd, latm, nsi, adj );
//   theTime = timeadj(time[3] - 18.0/60.0, ampm);
   theTime = timeadj(time[3], ampm);
   theTime = theTime.split(" ")[0];
   theTime = timeAdd( theTime, -20 );
   return theTime;
}

function Havdala( dt )
{
   var yom;
   if( dt == "" )
      yom = new Date();
   else
      yom = new Date( dt );

   addDay = 5 - yom.getDay();
   yom.setDate( yom.getDate() + addDay );
   dayLight = dayLightSave(yom.toString());
   adj  = tmz + dayLight;   //---   GMT -5 EAST + DayLight Saving

   var day_before = new Date(yom.getTime() - 86400000);

   db = day_before.getDate() + 2;
   mb = day_before.getMonth() + 1;
   yb = day_before.getYear();
   if(yb < 1900)
      yb += 1900;

   time = suntime( db, mb, yb, 90, 50, lngd, lngm, ewi, latd, latm, nsi, adj );
//   theTime = timeadj(time[3] - 18.0/60.0, ampm);
   theTime = timeadj(time[3], ampm);
   theTime = theTime.split(" ")[0];
   theTime = timeAdd( theTime, 42 );
   return theTime;
}




/* ksun.js - Kaluach suntimes Javascript routines
 *   Version 1.00
 * Copyright (C) 5760,5761 (2000 CE), by Abu Mami and Yisrael Hersch.
 *   All Rights Reserved.
 *   All copyright notices in this script must be left intact.
 * Based on:
 *	 - the program SUN.C by Michael Schwartz
 *   - an algorithm contained in:
 *         Almanac for Computers, 1990
 *         published by Nautical Almanac Office
 *         United States Naval Observatory
 *         Washington, DC 20392
 * Permission will be granted to use this script on your web page
 *   if you wish. All that's required is that you please ask.
 *   (Of course if you want to send a few dollars, that's OK too :-)
 * website: http://www.kaluach.net
 * email: abumami@kaluach.org
 */

var monCount = new makeArray(1, 32, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366);

function makeArray() {
	this[0] = makeArray.arguments.length;
	for (i = 0; i < makeArray.arguments.length; i = i + 1)
		this[i+1] = makeArray.arguments[i];
}

function doy(d, m, y) {
	return monCount[m] + d + (m > 2 && leap(y));
}

function suntime(
	d, m, y,
	zendeg, zenmin,
	londeg, lonmin, ew,
	latdeg, latmin, ns,
	tz)
{
	var lonhr;
	var longitude, latitude;
	var	coslat, sinlat, cosz;
	var sindec, cosdec;
	var xm_rise,  xm_set;
	var xl_rise,  xl_set;
	var a_rise,   a_set;
	var ahr_rise, ahr_set;
	var h_rise,   h_set;
	var t_rise,   t_set;
	var	ut_rise,  ut_set;

	var	retval = 0;	// NORMAL
	var day = doy(d, m, y);

	cosz = Math.cos(0.01745 * todec(zendeg, zenmin));

	longitude = todec(londeg, lonmin) * ((ew == 0) ? 1 : -1);
	lonhr	  = longitude / 15.0;
	latitude  = todec(latdeg, latmin) * ((ns == 0) ? 1 : -1);
	coslat	  = Math.cos(0.01745 * latitude);
	sinlat	  = Math.sin(0.01745 * latitude);

	t_rise = day + (6.0 + lonhr) / 24.0;
	t_set  = day + (18.0 + lonhr) / 24.0;

	xm_rise = M(t_rise);
	xl_rise = L(xm_rise);
	xm_set  = M(t_set);
	xl_set  = L(xm_set);
	
	a_rise = 57.29578 * Math.atan( 0.91746 * Math.tan(0.01745 * xl_rise) );
	a_set  = 57.29578 * Math.atan( 0.91746 * Math.tan(0.01745 * xl_set) );

	if(Math.abs(a_rise + 360.0 - xl_rise) > 90.0)
		a_rise += 180.0;
	if(a_rise > 360.0)
		a_rise -= 360.0;

	if(Math.abs(a_set + 360.0 - xl_set) > 90.0)
		a_set += 180.0;
	if(a_set > 360.0)
		a_set -= 360.0;

	ahr_rise = a_rise / 15.0;
	sindec = 0.39782 * Math.sin(0.01745 * xl_rise);
	cosdec = Math.sqrt(1.0 - sindec * sindec);
	h_rise = (cosz - sindec * sinlat) / (cosdec * coslat);

	ahr_set = a_set / 15.0;
	sindec = 0.39782 * Math.sin(0.01745 * xl_set);
	cosdec = Math.sqrt(1.0 - sindec * sindec);
	h_set = (cosz - sindec * sinlat) / (cosdec * coslat);

	if(Math.abs(h_rise) <= 1.0)
		h_rise = 57.29578 * Math.acos(h_rise);
	else
		retval |= 2;	//NO_SUNRISE;

	if(Math.abs(h_set) <= 1.0)
		h_set = 57.29578 * Math.acos(h_set);
	else
		retval |= 1;	//NO_SUNSET;
	ut_rise  = ((360.0 - h_rise) / 15.0) + ahr_rise + bug_adj(t_rise) + lonhr;
	ut_set  = (h_rise / 15.0) + ahr_set + bug_adj(t_set) + lonhr;

	var ret = new Object();
	ret[1] = retval;
	ret[2] = ut_rise + tz;	// sunrise
	ret[3] = ut_set  + tz;	// sunset
	return ret;
}

function timeadj(t, ampm) {

	var hour;
	var min;

	var time = t;

	if(time < 0)
		time += 24.0;

	var hour = Math.floor(time);
	var min  = Math.floor((time - hour) * 60.0 + 0.5);

	if(min >= 60) {
		hour += 1;
		min  -= 60;
	}

	if(hour > 24)
		hour -= 24;

	if(ampm) {
		ampm_str = (hour > 11) ? ' PM' : ' AM';
		hour %= 12;
		hour = (hour < 1) ? 12 : hour;
	}
	else
		ampm_str = '';

	var str = hour + ':' + ((min < 10) ? '0' : '') + min + ampm_str;
	return str;

}

function todec(deg, min) {
	return (deg + min / 60.0);
}

function M(x) {
	return (0.9856 * x - 3.251);
}

function L(x) {
	return (x + 1.916 * Math.sin(0.01745 * x) + 0.02 * Math.sin(2 * 0.01745 * x) + 282.565);
}

function bug_adj(x) {
	return (-0.06571 * x - 6.620);
}

