/* Omer.js

created 	08-April-2004
revised 03-May-2005 to add day mode and revise reminder text

by Rick Pike <calendar@pikesys.com>, webmaster at www.bethisraelmv.org
adapted from the Omer code of www.btikvah.ca and calendar code at calendar.pikesys.com

Usage: for a reminder for the current evening, call OmerText with the numerical values for the year, month, and day of the 2nd seder (1st day)
            for an announcement of the current day, call OmerDay with the numerical values for the year, month, and day of the 2nd Day
            plus an optional 4th argument with a url with Omer blessing or information.

            You may also overide the default values for "omerFormat" and "omerHighlight" 
            before calling OmerText() to change the format. These should be set to the desired html 
            before and after tags separated by a "|" character.
    
Example: in your html page include

	<script src="omer.js"></script>
	<script type="text/javascript" language="JavaScript">
	document.write(OmerText(2004,4,6,"omer.html"));
	</script>
*/

var monthname=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")

var pre, post, tmp;
var omerFormat = '<strong><font face="arial"><font color="#FFFFFF">|</font></font></strong>';
var omerHighlight = '<font color="#00FFFF">|</font>';

var mode = 0; // 0=reminder, 1=announcement
   
function parseFormat(format) {
	// pre and post are globals
	pre = post = "";
	if (format) {		
		var sep = format.indexOf("|");
		if (sep > 0) {  // split format into pre and post strings
			pre = format.substring(0, sep);
			post= format.substring(1+sep, format.length);
		}
	}
}

function DaysUntil(yearval,monthval,dateval) {
    var  msecperday = 86400000;
    Today=new Date();
    Today.setHours(0);
    Today.setMinutes(0);
    Target = new Date(yearval,monthval-1,dateval);
    v=Target.getTime()-Today.getTime();
    return Math.round(v/msecperday);
}

function Omer(yearval,monthval,dateval) {
    var day = 1-DaysUntil(yearval,monthval,dateval);
    return day;
}

function OmerDay(yearval,monthval,dateval,url) {
   mode=1;
   return OmerText(yearval,monthval,dateval,url);
}

function OmerText(yearval,monthval,dateval,url) {
   var count = 1 - mode + Omer(yearval,monthval,dateval);
   tmp = "";
   if (count>0 && count < 50) {
      today = new Date();
      date = weekday[today.getDay()] + " " + monthname[today.getMonth()] + " " + today.getDate();
      parseFormat(omerHighlight);
      link = (url ? "<a href="+url+">Omer</a>" : "Omer");
      if (mode==0) {
		tmp = 'Reminder: Tonight, ' + pre + date + post + ', at nightfall we count day ' + pre + count + post + ' of the ' + link + '.';
      } else {
	    trailer = ( count == 49 ? '.' : ' (the next count is at nightfall).' );
		tmp = 'Today, ' + pre + date + post + ' is day ' + pre + count + post + ' of the ' + link + trailer;
      }
      parseFormat(omerFormat);
      tmp = pre + tmp + post;
   }
   return tmp;
}