/* KaluachJS - Kaluach Javascript Hebrew/civil calendar
 *   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.
 * Requires kdate.js - Kaluach Javascript Hebrew date routines
 * Acknowledgment given to scripts by:
 *	 - Tomer and Yehuda Shiran (docjs.com)
 *   - Gordon McComb
 *   - irt.org
 *   - javascripter.net
 * Terms of use:
 *   - Permission will be granted to use this script on personal
 *     web pages. All that's required is that you please ask.
 *     (Of course if you want to send a few dollars, that's OK too :-)
 *   - Use on commercial web sites requires a $50 payment.
 * website: http://www.kaluach.net
 * email: abumami@kaluach.net
 */

var otherHolidays = 0;
var jewishHolidays = 1;
var civilHolidays = 0;

function calendar(selM, selY) {
	
        var m = selM + 1;

	var y = selY;
	var d = civMonthLength(m, y);

	var firstOfMonth = new Date (y, selM, 1);

	var startPos = firstOfMonth.getDay() + 1;

	var retVal = new Object();

	retVal[1] = startPos;

	retVal[2] = d;

	retVal[3] = m;

	retVal[4] = y;

	return (retVal);

}



function BuildLuach(parms)  {
	var hdrSize = "+2";			// size of tables header font
	var border = 1;				// 3D height of table's border
	var cellspacing = 0;			// width of table's border
        var cellpadding = 2;                    // inner margin
	var hdrHeight = 50;			// height of the table's header cell
	var dayCellHeight = 25;			// height of cells containing days of the week
	var dayColor = "black";	         	// color of font representing week days
        var headBGColor = "lightgray";                // color of week days background
	var hebColor = "#990000";			// color of font representing Heb date
	var civColor = "black";	   		// color of font representing Civ date
	var cellWidth  = 100;			// width of columns in table
	var cellHeight = 70;			// height of day cells calendar
	var hebDate;
	var hebDay;
	var now = new Date();

	var tday = now.getDate();

	var tmonth = now.getMonth();

	var tyear = now.getYear(); if(tyear < 1000) tyear += 1900;

	var cMonth = parms[3];
	var cYear = parms[4];
	var monthName = civMonth[cMonth];
	var lastDate = civMonthLength(cMonth, cYear);
	var hm;
	var hMonth;
	var hYear;

        var borderDisp = " BORDERCOLOR=#CCCCCC LIGHTBORDERCOLOR=#FFFFFF DARKBORDERCOLOR=#FFFFFF "
        var mouseEffect = " onmouseover='this.style.backgroundColor=&quot;#dddddd&quot;' onmouseout='this.style.backgroundColor = &quot;&quot;;return;' "
        var mouseAction; // = " onclick='showHalachicTimes(this);' "


	// get starting Heb month in civil month
	hebDate = civ2heb(1, cMonth, cYear);
	hmS = hebDate.substring(hebDate.indexOf(' ')+1, hebDate.length);
	hMonth = eval(hmS.substring(0, hmS.indexOf(' ')));
	hYear = hmS.substring(hmS.indexOf(' ')+1, hmS.length);
	var start = hebMonth[hMonth+1] + ' ' + hYear;

	// get ending Heb month in civil month
	hebDate = civ2heb(lastDate, cMonth, cYear);
	hmE = hebDate.substring(hebDate.indexOf(' ')+1, hebDate.length);
	hMonth = eval(hmE.substring(0, hmE.indexOf(' ')));
	hYear = hmE.substring(hmE.indexOf(' ')+1, hmE.length);
	var end = hebMonth[hMonth+1] + ' ' + hYear;

	var hebSpan;
	// check if start and end Heb months are the same
	if(hmS == hmE)
		hebSpan = start;
	else
		hebSpan = start + ' / ' + end

    var result = '<HTML><HEAD></HEAD>';
	result += '<BODY bgColor=#000000 topmargin="40"><FONT face="Arial,Helvetica" size=2 >';
//    var result += '<BODY bgColor=#000000><FONT face="Arial,Helvetica" size=2>';
        result += '<center><br>';

	// set up our table structure

	result += '<TABLE BORDER=' + border + ' CELLSPACING=' + cellspacing + borderDisp + '>';		
        // table settings
	result +=	 '<TH COLSPAN=7 HEIGHT=' + hdrHeight + ' align="center" BGCOLOR="' + headBGColor + '">';
        // create table header cell
	result +=		'<FONT face="Arial" COLOR="' + "#111111" + '" SIZE=' + hdrSize + '>';

	// set font for table header
	result +=			monthName + ' ' + cYear;
	result +=		'</FONT>';
	// close table header's font settings
	result +=		'<FONT face="Arial" COLOR="' + "antiquewhite" + '" SIZE=' + '+1' + '>';
        // set font for table header
	result +=			'&nbsp&nbsp ' + hebSpan;
	result +=		'</FONT>';
        // close table header's font settings
	result +=	 '</TH>';
        // close header cell

	// variables to hold constant settings
	var openCol = '<TD WIDTH=' + cellWidth + ' HEIGHT=' + dayCellHeight + ' BGCOLOR="' + headBGColor + '"' + borderDisp + '>';
	openCol += '<FONT face="Verdana, Arial" COLOR="' + dayColor + '">';
	var closeCol = '</FONT></TD>';

	// create first row of table to set column width and specify week day
	result += '<TR ALIGN="center" VALIGN="center">';
	for (var dayNum = 1; dayNum < 8; ++dayNum) {
	        result += openCol + weekDay[dayNum] + closeCol;
	}
	result += '</TR>';

 
       var cell = 1
;    
        var cDay = 1
;    
        var row;
    
        for (row = 1; row <= 6; row++) {

          result+='<TR VALIGN="top">'
        
          for (col = 1; col <= 7; col++)  {

			// convert civil date to hebrew
			hebDate = civ2heb(cDay, cMonth, cYear);
			hebDay = eval(hebDate.substring(0, hebDate.indexOf(' ')));

			var hm = hebDate.substring(hebDate.indexOf(' ')+1, hebDate.length);
			var hMonth = eval(hm.substring(0, hm.indexOf(' ')));

	        if (cell < parms[1]) result += '<TD BGCOLOR=#FFFFFF BORDERCOLOR=#FFFFFF></TD>';
	        else {

				var moed = "";
				if(jewishHolidays)
					moed = moadim(cDay, cMonth, cYear, hebDay, hMonth, col);
				var holiday = "";
				if(civilHolidays)
					holiday = holidays(cDay, cMonth, cYear);

				var bg;
		        if((cDay == tday) && (parms[3] == (tmonth+1)) && (parms[4] == tyear))
					// highight the current day
					bg = 'bgColor=whitesmoke'
				else if (moed != "")
					// highlight Heb holiday
					bg = 'bgColor=#FFE9D2'
				else if (holiday != "") {
					// highlight civil holiday
					bg = 'bgColor=#cccccc'
//					moed = holiday;
				}
				else
					// no highlight
					bg = 'bgcolor=#FFFFFF';
                                
                                mouseAction = " onclick='showHalachicTimes(&quot;" + cDay + "/" + cMonth + "/" + cYear + "&quot;);' ";
				// assemble the contents of our day cell
				result += '<TD ALIGN="center" HEIGHT=' + cellHeight + ' ' + bg ;
                                result += ' BORDERCLOR=#FFFFFF day=' + cDay ;
                                result += ' year=' + cYear + ' month=' + (cMonth - 1) ;
                                result += mouseEffect + mouseAction + '>';
				result +=   '<table BORDER=0 COLS=2 WIDTH=100%>';
				result +=     '<tr>';
				result +=       '<td align="left">';
				result +=         '<FONT face="Arial" COLOR="' + civColor + '" SIZE=2>';
				result +=           cDay;
				result +=         '</font>';
				result +=       '</td>';
				result +=       '<td align="right">';
				result +=         '<FONT face="Arial" COLOR="' + hebColor + '" SIZE=2>';
				result +=           '<div align=right>' + hebDay + ' ' + hebMonth[hMonth+1] + '</div>';
				result +=         '</FONT>';
				result +=       '</td>';
				result +=     '</tr>';
				result +=   '</table>';

				result +=   '<FONT face="Arial" COLOR="#cc5555" SIZE=-1>';
				if (moed != "")
					result += moed;
				if (moed != "" && holiday != "")
					result += '<br>';
				if (holiday != "")
					result += holiday;
				result +=   '</FONT>';
                                if (col==7) {
                                  hal = new halach(new Date(cYear, cMonth - 1, cDay));
                                  hal.getHalachTimes();
                                  result += '<br><br><FONT SIZE=1 FACE="Verdana, Helvetica">';
                                  result += hal.knissatShabbat + ' - ' + hal.motzeiShabbat;
                                  result += '</FONT>';
                                }

				result += '</TD>';
                                cDay++;

            }

            if (cDay <= lastDate)

                cell++
;
            else

                break;
  
      }

        result += '</TR>'
 ;
        if(cDay > parms[2])

                break;

    }


    result += '</table>';

    result += '</center></body></html>'
;

    return result;

  }



function showHalachicTimes(tDate) {

  var t = tDate.substring(tDate.indexOf('/')+1, tDate.length);
  var tDay = eval(tDate.substring(0, tDate.indexOf('/')));
  var tMonth = eval(t.substring(0, t.indexOf('/')));
  var tYear = eval(t.substring(t.indexOf('/')+1, t.length));

  var hal = new halach(new Date(tYear, tMonth-1, tDay));
  hal.getHalachTimes();
  hal.showTimes();
}

