﻿var m_arrCities = new Array();
var m_DaylightSavingTime = 0;
var AMPM = 1;			// am/pm or 24 hour display


function InitHalachicTimes()
{
  //Initialize the array of City objects
  m_arrCities[0] = new City("Jerusalem", "ירושלים", "N", 31, 46, "E", 35, 14, 2, 40);
  m_arrCities[1] = new City("Be'er Sheva", "באר שבע", "N", 31, 15, "E", 34, 47, 2, 30);
  m_arrCities[2] = new City("Eilat", "אילת", "N", 29, 33, "E", 34, 56, 2, 18);
  m_arrCities[3] = new City("Haifa", "חיפה", "N", 32, 49, "E", 34, 59, 2, 18);
  m_arrCities[4] = new City("Hebron", "חברון", "N", 31, 36, "E", 35, 53, 2, 18);
  m_arrCities[5] = new City("Jericho", "יריחו", "N", 31, 52, "E", 35, 27, 2, 18);
  m_arrCities[6] = new City("Karnei Shomron", "קרני שומרון", "N", 32, 10, "E", 35, 26, 2, 22);
  m_arrCities[7] = new City("Netanya", "נתניה", "N", 32, 20, "E", 34, 51, 2, 18);
  m_arrCities[8] = new City("Petach Tikva", "פתח תקוה", "N", 32,  5, "E", 34, 46, 2, 18);
  m_arrCities[9] = new City("Safed", "צפת", "N", 33,  0, "E", 35, 30, 2, 18);
  m_arrCities[10] = new City("Shchem", "שכם", "N", 32, 12, "E", 35, 18, 2, 18);
  m_arrCities[11] = new City("Tel Aviv", "תל אביב", "N", 32,  5, "E", 34, 46, 2, 22);
  m_arrCities[12] = new City("Tiberias", "טבריה", "N", 32, 48, "E", 35, 30, 2, 18);
  
  //Initialize the cboCities combo
  for (i = 0 ; i < m_arrCities.length ; i++)
  {
    var objNewOption = document.createElement("OPTION");
    document.getElementById("cboCities").appendChild(objNewOption);
    objNewOption.innerText = m_arrCities[i].CityNameHebrew;
  }
  document.getElementById("cboCities").selectedIndex = 1;
  cboCities_OnChange();

}


//The 'City' object constructor
function City(strCityName, strCityNameHebrew,
              strLatitude, intLatitudeDegrees, intLatitudeMinutes,
              strLongitude, intLongitudeDegrees, intLongitudeMinutes,
              bytTimeZone,
              bytKnisatShabatOffset)
{
  this.CityName = strCityName;
  this.CityNameHebrew = strCityNameHebrew;
  this.Latitude = strLatitude;
  this.LatitudeDegrees = intLatitudeDegrees;
  this.LatitudeMinutes = intLatitudeMinutes;
  this.Longitude = strLongitude;
  this.LongitudeDegrees = intLongitudeDegrees;
  this.LongitudeMinutes = intLongitudeMinutes;				
  this.TimeZone = bytTimeZone;
  this.KnisatShabatOffset = bytKnisatShabatOffset;
}


function cboCities_OnChange() 
{	
  //Check if the selected option is not the dummy "<בחר יישוב>"								
  if (document.getElementById("cboCities").selectedIndex == 0)
  {
    document.getElementById("hanetz").innerText = '';
    document.getElementById("shkia").innerText = '';
    document.getElementById("alot").innerText = '';
    document.getElementById("tzeit").innerText = '';
    document.getElementById("shema").innerText = '';
    document.getElementById("tefillah").innerText = '';
    
	  return;
  }
  
	var Today = new Date();
	
	//Mind the dummy option "<בחר יישוב>" regarding the selected city index
	var objCity = m_arrCities[document.getElementById("cboCities").selectedIndex - 1];

	// Get the times of hanetz and shkia (SunRise & SunSet)
	var arrTimes = GetTimes(Today.getDate(), Today.getMonth() + 1, Today.getFullYear(),
													90, 50,
													objCity.LongitudeDegrees, objCity.LongitudeMinutes, objCity.Longitude,
													objCity.LatitudeDegrees, objCity.LatitudeMinutes, objCity.Latitude,
													objCity.TimeZone + m_DaylightSavingTime);

	var SunRise, SunSet, ShaaZmanit;

	if(arrTimes[1] == 0)
	{
		SunRise = arrTimes[2];
		SunSet = arrTimes[3];
		document.getElementById("hanetz").innerText = FormatTime(SunRise, AMPM);
		document.getElementById("shkia").innerText = FormatTime(SunSet, AMPM);
		ShaaZmanit = (SunSet - SunRise) / 12;
	}
	else 
	{
		document.getElementById("hanetz").innerText = "";
		document.getElementById("shkia").innerText = "";
	}


	//Get the times of Alot Ha'shachar (עלות השחר)
	arrTimes = GetTimes(Today.getDate(), Today.getMonth() + 1, Today.getFullYear(),
													106, 6,
													objCity.LongitudeDegrees, objCity.LongitudeMinutes, objCity.Longitude,
													objCity.LatitudeDegrees, objCity.LatitudeMinutes, objCity.Latitude,
													objCity.TimeZone + m_DaylightSavingTime);
	if(arrTimes[1] == 0)
		document.getElementById("alot").innerText = FormatTime(arrTimes[2], AMPM);
	else
		document.getElementById("alot").innerText = "";


	//Get the times of tzeit Ha'cochavim (צאת הכוכבים)
	arrTimes = GetTimes(Today.getDate(), Today.getMonth() + 1, Today.getFullYear(),
													96, 6,
													objCity.LongitudeDegrees, objCity.LongitudeMinutes, objCity.Longitude,
													objCity.LatitudeDegrees, objCity.LatitudeMinutes, objCity.Latitude,
													objCity.TimeZone + m_DaylightSavingTime);
	if(arrTimes[1] == 0)
		document.getElementById("tzeit").innerText = FormatTime(arrTimes[3], AMPM);
	else	
		document.getElementById("tzeit").innerText = "";

	document.getElementById("shema").innerText = FormatTime(SunRise + ShaaZmanit * 3, AMPM);
	document.getElementById("tefillah").innerText = FormatTime(SunRise + ShaaZmanit * 4, AMPM);


  //Get the times of KnisatShabat & MotzeiShabat (only if today is Friday or Saturday)
	var DaysLeftForFriday = (7 + (5 - Today.getDay()))%7;
	
	var ComingFriday = new Date();
	ComingFriday.setDate(Today.getDate() + DaysLeftForFriday); 	
	var ComingShabbos = new Date();
	ComingShabbos.setDate(ComingFriday.getDate() + 1);
	
	//Knissat shabbat
  arrTimes = GetTimes(ComingFriday.getDate(), ComingFriday.getMonth() + 1, ComingFriday.getFullYear(),
												  90, 50,
												  objCity.LongitudeDegrees, objCity.LongitudeMinutes, objCity.Longitude,
												  objCity.LatitudeDegrees, objCity.LatitudeMinutes, objCity.Latitude,
												  objCity.TimeZone + m_DaylightSavingTime);

	var bytKnisatShabatOffset = m_arrCities[document.getElementById("cboCities").selectedIndex - 1].KnisatShabatOffset;
	document.getElementById("KnissatShabbat").innerText = FormatTime(arrTimes[3] - (bytKnisatShabatOffset / 60), AMPM);

	//Motzei shabbat tomorrow (3 small stars)
	
  arrTimes = GetTimes(ComingShabbos.getDate(), ComingShabbos.getMonth() + 1, ComingShabbos.getFullYear(),
												  98, 30,
												  objCity.LongitudeDegrees, objCity.LongitudeMinutes, objCity.Longitude,
												  objCity.LatitudeDegrees, objCity.LatitudeMinutes, objCity.Latitude,
												  objCity.TimeZone + m_DaylightSavingTime);
	if(arrTimes[1] == 0)
		document.getElementById("MotzeiShabbat").innerText = FormatTime(arrTimes[3], AMPM);
	else
		document.getElementById("MotzeiShabbat").innerText = "";
	
	
}


function IsLeapYear(Year)
{
	return (new Date(Year, 2 - 1, 29).getDate() == 29);
}      


function GetDayOfYear(DayOfMonth, Month, Year)
{
  var LeapYearExtraDay = 0;
  if (IsLeapYear(Year))
    LeapYearExtraDay = 1;
  
  switch (Month)
  {
    case 1:
      return DayOfMonth;
    case 2:
      return 32 + DayOfMonth;
    case 3:
      return 60 + DayOfMonth + LeapYearExtraDay;
    case 4:
      return 91 + DayOfMonth + LeapYearExtraDay;
    case 5:
      return 121 + DayOfMonth + LeapYearExtraDay;
    case 6:
      return 152 + DayOfMonth + LeapYearExtraDay;
    case 7:
      return 182 + DayOfMonth + LeapYearExtraDay;
    case 8:
      return 213 + DayOfMonth + LeapYearExtraDay;
    case 9:
      return 244 + DayOfMonth + LeapYearExtraDay;
    case 10:
      return 274 + DayOfMonth + LeapYearExtraDay;
    case 11:
      return 305 + DayOfMonth + LeapYearExtraDay;
    case 12:
      return 335 + DayOfMonth + LeapYearExtraDay;
  }
}

function GetTimes(DayOfMonth, Month, Year, 
									SunDegrees, SunMinutes, 
									LongitudeDegrees, LongitudeMinutes, Longitude, 
									LatitudeDegrees, LatitudeMinutes, Latitude,
									TimeZone)
{
	var invalid = 0;	// start out as OK

	var DayOfYear = GetDayOfYear(DayOfMonth, Month, Year);

	var A = 1.5708; 
	var B = 3.14159; 
	var C = 4.71239; 
	var D = 6.28319; 
	var E = 0.0174533 * ((LatitudeDegrees + LatitudeMinutes/60.0) * ((Latitude == "N") ? 1 : -1));
	var F = 0.0174533 * ((LongitudeDegrees + LongitudeMinutes/60.0) * ((Longitude == "E") ? 1 : -1));
	var G = 0.261799 * TimeZone; 

	var R = Math.cos(0.01745 * (SunDegrees + SunMinutes/60.0));

	var J;

	// two times through the loop
	//    i=0 is for sunrise
	//    i=1 is for sunset
	for (i = 0; i < 2; i++) { 

		if(!i)
			J =  A;	// sunrise 
		else
			J = C;	// sunset

		var K = DayOfYear + ((J - F) / D); 
		var L = (K * .017202) - .0574039;              // Solar Mean Anomoly 
		var M = L + .0334405 * Math.sin(L);            // Solar True Longitude 
		M += 4.93289 + (3.49066E-04) * Math.sin(2 * L); 
		
		// Quadrant Determination 
		if (D == 0) {
			alert("Trying to normalize with zero offset...");
			exit;
		} 

		while(M < 0)
			M = (M + D);

		while(M >= D)
			M = (M - D);

		if ((M / A) - Math.floor(M / A) == 0)
			M += 4.84814E-06;

		var P = Math.sin(M) / Math.cos(M);                   // Solar Right Ascension 
		P = Math.atan2(.91746 * P, 1); 

		// Quadrant Adjustment 
		if (M > C)
			P += D;
		else {
			if (M > A)
				P += B;
		} 

		var Q = .39782 * Math.sin(M);      // Solar Declination 
		Q = Q / Math.sqrt(-Q * Q + 1);     // This is how the original author wrote it! 
		Q = Math.atan2(Q, 1); 

		var S = R - (Math.sin(Q) * Math.sin(E)); 
		S = S / (Math.cos(Q) * Math.cos(E)); 

		if(Math.abs(S) > 1)
			invalid = 1;	// uh oh! no sunrise/sunset

		S = S / Math.sqrt(-S * S + 1); 
		S = A - Math.atan2(S, 1); 

		if(!i)
			S = D - S;	// sunrise

		var T = S + P - 0.0172028 * K - 1.73364;  // Local apparent time 
		var U = T - F;                            // Universal timer 
		var V = U + G;                            // Wall clock time 
		
		// Quadrant Determination 
		if(D == 0) {
			alert("Trying to normalize with zero offset...");
			exit;
		} 
		
		while(V < 0)
			V = V + D;
		while(V >= D)
			V = V - D;
		V = V * 3.81972; 

		if(!i)
			SunRise = V;	// sunrise
		else
			SunSet = V;	// sunset
	} 

	var arrTimes = new Array();
	arrTimes[1] = invalid;
	arrTimes[2] = SunRise;
	arrTimes[3] = SunSet;
	return arrTimes;
}


function FormatTime(time, ampm)
{
	var hour = Math.floor(time);
	var min = Math.floor((time - hour) * 60.0 + 0.5);

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

	if(hour < 0)
		hour += 24;

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

	return hour + ':' + ((min < 10) ? '0' : '') + min + ampm_str;
}

