
// determines if the date entered in the form is within daylight savings timer
// or not.
function inBST(curForm)
{
   var now = new Date()
   var curYear = now.getYear()
   if(curYear < 2000)
   {
      curYear += 1900
   }
   var year = curYear + curForm.year.selectedIndex
   var beginDay = 31 - ((Math.floor((5 * year) / 4) + 4) % 7)
   var endDay = 31 - ((Math.floor((5 * year) / 4) + 1) % 7)
   var selectedMonth = curForm.month.selectedIndex

   if(selectedMonth  < 2 || selectedMonth  > 9) // January, February, November, December
   {
      return 0
   }
   else if(selectedMonth > 2 && selectedMonth  < 9) // April to September
   {
      return 1
   }
   else if(selectedMonth == 2 && curForm.day.selectedIndex < beginDay) // March and earlier than the start day
   {
      return 0
   }
   else if(selectedMonth == 9 && curForm.day.selectedIndex >= endDay) // October and later than the end day
   {
      return 0
   }

   return 1
}

function isItBST(d)
{
   var now = new Date()
   var curYear = now.getYear()
   if(curYear < 2000)
   {
      curYear += 1900
   }
   var year = curYear + d[0]
   var beginDay = 31 - ((Math.floor((5 * year) / 4) + 4) % 7)
   var endDay = 31 - ((Math.floor((5 * year) / 4) + 1) % 7)
   var selectedMonth = d[1];

   if(selectedMonth  < 2 || selectedMonth  > 9) // January, February, November, December
   {
      return 0
   }
   else if(selectedMonth > 2 && selectedMonth  < 9) // April to September
   {
      return 1
   }
   else if(selectedMonth == 2 && d[2] < beginDay) // March and earlier than the start day
   {
      return 0
   }
   else if(selectedMonth == 9 && d[2] >= endDay) // October and later than the end day
   {
      return 0
   }

   return 1
}

function curDay(data)
{
   var now = new Date()
   var curYear = now.getYear()
   if(curYear < 2000)
   {
      curYear += 1900
   }
   var curDate = new Date(curYear + parseInt(data[0]), parseInt(data[1]), parseInt(data[2]) + 1)

   var dayNames = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
   var dayNum = curDate.getDay()

   return dayNames[dayNum]
}



function getMinutes(time)
{
   var offset = time.indexOf(":")
   var hours = Number(time.substring(0, offset))
   var mins = Number(time.substring(offset + 1))
   return hours * 60 + mins
}


// Passed the times of sunrise, sunset, current time and the length of dusk. It
// will return the status of light at the current time.
function getLightStatus(sunrise, sunset, curTime, dusk)
{
   if(Math.abs(sunset - curTime) < dusk)
   {
      return "Dusk";
   }
   else if(Math.abs(sunrise - curTime) < dusk)
   {
      return "Dawn";
   }
   else if(sunset < curTime || curTime < sunrise)
   {
      return "Dark";
   }

   return "Daylight";
}

/*
 JavaScript Sun Table Calculator
 2002 Juergen Giesen
 http://www.GeoAstro.de
*/


var doy, eqtime, declin, time_offset, tst, ha, Phi, Phi1, Theta, hars;
var sun_rise, sun_set, snoon;
var date, month, hours, minutes, offset, lat, longitude;



function calcDayOfYear(mn, dy, lpyr)
{
   var k = (lpyr ? 1 : 2);
   var doy = Math.floor((275 * mn)/9) - k * Math.floor((mn + 9)/12) + dy -30;
   return doy;
}

function daysInM(m, y) 
{
   var n=31
   m=m-1

   if ((m==3) || (m==5) || (m==8) || (m==10))
   {
      n=30;
   }
   if (m==1) 
   {
      n=28;
      if (((y % 4) == 0) && ((y % 100) != 0) && ((y % 400) == 0)) 
      {
         n=29
      }
   }
   return n;
}

function frac(X) 
{
   X = X - Math.floor(X);
   if (X<0) 
   {
      X += 1.0;
   }
   return X;
}

function HoursMinutesSeconds(time) 
{
   var h = Math.floor(time);
   var min = Math.round(60.0*frac(time));

   if(min == 60)
   {
      h += 1;
      min -= 60;
   }
   
   var str=h+":";
   
   if (min >= 10)
   {
      str=str+min;
   }
   else
   {
      str=str+"0"+min;
   }
   return " " + str;
}

// calculate the fractional day of the year
function dayOfYear() 
{
   var sum=0;
   for (var i=1; i<month; i++)
   {
      sum=sum+daysInM(i,2002);
   }
   
   sum=sum+date;
   var h=hours + offset + minutes/60;
   doy=sum-1+(h-12)/24;
}


// equation of time (in minutes)
function equationTime() 
{
   var x=doy*2*Math.PI/365;
   eqtime = 229.18*(0.000075+0.001868*Math.cos(x)-0.032077*Math.sin(x)-0.014615*Math.cos(2*x)-0.040849*Math.sin(2*x));
}

// declination (in degrees)
function declination() 
{
   var x=doy*2*Math.PI/365; // fractional year in radians
   declin=0.006918-0.399912*Math.cos(x)+0.070257*Math.sin(x)-0.006758*Math.cos(2*x);
   declin=declin+0.000907*Math.sin(2*x)-0.002697*Math.cos(3*x)+0.00148*Math.sin(3*x);
   declin=declin*180/Math.PI;
}

// time offset (in minutes)
function tzo() 
{
   var K=Math.PI/180;
   time_offset = eqtime - 4*longitude - 60*offset;
}

// true solar time (in hours)
function solarTime() 
{
   tst = hours*60 + minutes + time_offset;
}

// solar hour angle (in degrees)
function hourAngle() 
{
   ha = tst/4 - 180;
}

// solar zenith angle (in degrees)
function zenithAngle() 
{
   var K=Math.PI/180;
   Phi = Math.sin(K*lat)*Math.sin(K*declin)+Math.cos(K*lat)*Math.cos(K*declin)*Math.cos(K*ha);
   Phi = Math.acos(Phi)/K;
   Phi1=90-Phi;// altitude
}

// solar azimuth angle (in degrees, clockwise from north)
function azimuthAngle() 
{
   var K=Math.PI/180;
   Theta=-(Math.sin(K*lat)*Math.cos(K*Phi)-Math.sin(K*declin))/(Math.cos(K*lat)*Math.sin(K*Phi));
   Theta = Math.acos(Theta)/K;
   if (ha<0)
   {
      Theta=Theta;
   }
   else
   {
      Theta=360-Theta;
   }
}

// solar azimuth angle for sunrise and sunset corrected for atmospheric refraction (in degrees),
function hourAngleRiseSet() 
{
   var K=Math.PI/180;
   hars = Math.cos(K*90.833)/(Math.cos(K*lat)*Math.cos(K*declin)) - Math.tan(K*lat)*Math.tan(K*declin);
   hars = Math.acos(hars)/K;
}

// sunrise and sunset (local time)
function RiseSet() 
{
   sun_rise = 720 + 4*(longitude-hars) - eqtime;
   sun_rise = sun_rise/60 + offset;
   sun_set = 720 + 4*(longitude+hars) - eqtime;
   sun_set = sun_set/60 + offset;
}

// solar noon (local time)
function noon() 
{
   snoon = 720 + 4*longitude - eqtime;
   snoon = snoon/60 + offset
}

function getInput(curForm) 
{
   month = Number(curForm.month.selectedIndex + 1);
   date = Number(curForm.day.selectedIndex + 1);
   hours = Number(curForm.startTime.selectedIndex);
   minutes = 0;
   offset = inBST(curForm); // Account for BST
   lat = 54.36;
   longitude = 3.08;
}

function writeForm(curForm) 
{
   curForm.sunrise.value = HoursMinutesSeconds(sun_rise);

   if (curForm.sunrise.value.toString().lastIndexOf('N')>0)
   {
      curForm.sunrise.value=" --:--";
   }

   curForm.sunset.value = HoursMinutesSeconds(sun_set);
   if (curForm.sunset.value.toString().lastIndexOf('N')>0)
   {
      curForm.sunset.value=" --:--";
   }
}


function calculate(curForm) {
 getInput(curForm);
 dayOfYear();
 equationTime();
 declination();
 tzo();
 solarTime();
 hourAngle();
 zenithAngle();
 azimuthAngle();
 hourAngleRiseSet();
 RiseSet();
 noon();
 writeForm(curForm);
}

function calcForFormat(cookieData) 
{
  month = Number(cookieData[1]) + 1;
  date = Number(cookieData[2] ) + 1;
  hours = Number(cookieData[3]);
  minutes = 0;
  offset = isItBST(cookieData); // Account for BST
  lat = 54.36;
  longitude = 3.08;
  dayOfYear();
  equationTime();
  declination();
  tzo();
  solarTime();
  hourAngle();
  zenithAngle();
  azimuthAngle();
  hourAngleRiseSet();
  RiseSet();
  noon();
}


