var colors = [ "red",       "blue",  "purple",  "black",       "green"     ];
var Labels = [ "Shacharit", "Class", "Minchah", "Cand. light", "Havdallah" ];
var ndx    = [ 1,           2,       4,         3,             6           ];



var jewishHolidays = 1;
var civilHolidays  = 0;

/*-------------------*/
/*-- selectCurrent --*/
/*-------------------*/
function selectCurrent()
{
   var now = new Date();
   var y = now.getYear();
   var m = now.getMonth();
   if( y < 1000 )
      y += 1900;

   document.calendar_control.month.selectedIndex = m;
   document.calendar_control.yr.value = y;
   doCal(m, y);
}

/*----------------*/
/*-- selectNext --*/
/*----------------*/
function selectNext()
{
   var y = parseInt(document.calendar_control.yr.value);
   var m = document.calendar_control.month.selectedIndex;

   if(m < 11)
      m++;
   else {
      m = 0;
      y++;
   }

   document.calendar_control.month.selectedIndex = m;
   document.calendar_control.yr.value = y;
   doCal(m, y);
}

/*----------------*/
/*-- selectPrev --*/
/*----------------*/
function selectPrev()
{
   var y = parseInt(document.calendar_control.yr.value);
   var m = document.calendar_control.month.selectedIndex;

   if(m > 0)
      m -= 1;
   else {
      m = 11;
      y -= 1;
   }

   document.calendar_control.month.selectedIndex = m;
   document.calendar_control.yr.value = y;
   doCal(m, y);
}

/*--------------------*/
/*-- selectNextYear --*/
/*--------------------*/
function selectNextYear(num)
{
   var y = parseInt(document.calendar_control.yr.value);
   var m = document.calendar_control.month.selectedIndex;
   y += num;
   document.calendar_control.yr.value = y;
   doCal(m, y);
}

/*--------------------*/
/*-- selectPrevYear --*/
/*--------------------*/
function selectPrevYear(num)
{
   var y = parseInt(document.calendar_control.yr.value);
   var m = document.calendar_control.month.selectedIndex;
   y -= num;
   document.calendar_control.yr.value = y;
   doCal(m, y);
}

/*----------------*/
/*-- selectForm --*/
/*----------------*/
function selectForm(form)
{
   var y = parseInt(form.yr.value);
   var m = form.month.selectedIndex;
   doCal(m, y);
}

/*-----------*/
/*-- doCal --*/
/*-----------*/
function doCal(month, year)
{
   var ret = calendar(month, year);
   var result = BuildLuachHTML(ret);
   maincal.innerHTML = result;
}

/*--------------*/
/*-- calendar --*/
/*--------------*/
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 Array();
   retVal[1] = startPos;
   retVal[2] = d;
   retVal[3] = m;
   retVal[4] = y;
   return (retVal);
}

function BuildLuachHTML(parms)
{
   var border        = 0;            // 3D height of table's border
   var cellspacing   = 0;            // width of table's border
   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;

   // 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

   result = '';

   // set up our table structure
   result += '<TABLE style="border-collapse:collapse" width="100%" height="100%" BORDER=' + 1 + ' CELLSPACING=' + cellspacing + '>';      // table settings
   div_heb.innerHTML = "<b>" + hebSpan + "</b>";


   var openCol = '<TD bgcolor="' + headColor + '">';
   openCol += '<FONT face="Arial" COLOR="' + dayColor + '"><b>';
   var closeCol = '</b></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;

// So far so good


    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>&nbsp;</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;
            var bs = 'border-color:' + bordColor + '; border-style:solid; border-width:1px';

            if((cDay == tday) && (parms[3] == (tmonth+1)) && (parms[4] == tyear))
            {
               // highight the current day
               bg    = ''
               bs    = 'border-color:' + corBordColor + '; border-style:solid; border-width:2px'
            }
            if (moed != "")
            {
               bg = 'bgColor=' + holydayBG;
            }
            else if (holiday != "")
            {
               bg = 'bgColor=' + holydayBG;
            }
            else
            {
               bg = "";
            }


            colw = (col==7)?16:14;

            when = "";
            movr = "";
            if( col>=6 )
            {
               when   = "" + cMonth + "/" + cDay + "/" + cYear;
            }
            if( col==6 )
            {
               movr += " onmouseover='popUp(\"" + cMonth + "/" + cDay + "/" + cYear + "\");' ";
               movr += " onmouseout='popOut();' ";
            }


dbl      = "";
allTable = "";
if( notes[cMonth + "/" + cDay + "/" + cYear] != null )
   dbl = "onDblClick='alert(\"" + notes[cMonth + "/" + cDay + "/" + cYear] + "\");' ";

allTable += "<table " + dbl + " cellpadding=0 cellspacing=0 border=0>";
tmp = dates[cMonth + "/" + cDay + "/" + cYear];

tmp = tmp==null?"":tmp;
tmp = tmp.split( "|" );
for( i=0; i<ndx.length; i++ )
{
	if( tmp[ndx[i]] != "" && tmp[ndx[i]] != "?" )
	{
		if( tmp[ndx[i]] != null )
		{
			allTable += "<tr>";
			allTable += "<td nowrap style='color:" + colors[i] + "; font-size:9px'>" + Labels[i] + "   </td>";
			allTable += "<td style='color:" + colors[i] + "; font-size:9px'>&nbsp;</td>";
			allTable += "<td nowrap style='color:" + colors[i] + "; font-size:9px'>" + tmp[ndx[i]] + "</td>";
			allTable += "</tr>";
		}
	}
}
allTable += " </td></tr>";
allTable += "<tr><td height=1></td></tr>";

//alert( cMonth + "/" + cDay + "/" + cYear );
if( notes[cMonth + "/" + cDay + "/" + cYear] != null )
   allTable += "<tr><td align=center colspan=3 height=1><img src='dbl.gif'></td></tr>";


allTable += "</table>";


            result += '<TD ' + movr + ' style="' + bs + '" nowrap width="' + colw + '%" align="center" ' + bg + '>';
            result +=   '<table width="100%" BORDER=0>';
            result +=     '<tr>';
            result +=       '<td nowrap valign=top style="font-size:11px">';
            result +=         '<FONT COLOR="' + civColor + '">';
            result +=           cDay;
            result +=         '</font>';
            result +=       '</td>';
            result +=       '<td nowrap valign=top style="font-size:11px">';
            result +=         '<FONT face="Arial" COLOR="' + hebColor + '">';
            result +=           '<div align=right>' + hebDay + "&nbsp;&nbsp;" + hebMonth[hMonth+1] + '</div>';
            result +=         '</FONT>';
            result +=       '</td>';
            result +=     '</tr>';


            result +=     '<tr>';
            result +=     '<td colspan=2 class=cell>';
            result +=     '<center>';

/*
            if( (col==6) && (cYear>1970) )
               result += '<img src="shabcal.gif">';
            else
               result += '<img border=0 height=23 src="blank.gif">';
            if( (col==6) && (cYear>1970) )
               result += "&nbsp;" + shabbatTime( when );
*/


            result +=     allTable;
            result +=     '</center>';
            result +=     '</td>';
            result +=     '</tr>';



            result +=   '</table>';




            result +=   '<FONT style="font-size:10px">'
            if (moed != "")
               result += "<b style='background-color:#AABBFF; border:1px solid blue; padding:0px; padding-right:5px; padding-left:5px'>" + moed + "</b>";
/*
            else
               result += "&nbsp;";
            if (moed != "" && holiday != "")
               result += '<br>AAA';
*/

            if (holiday != "")
               result += holiday;
            result +=   '</FONT>'
            result += '</TD>';

              cDay++








            }

            if (cDay <= lastDate)
                cell++
            else
                break;
        }

        result += '</TR>'
        if(cDay > parms[2])
                break;
    }

    result += '</table>'

    return result;
}
