// This file contains intellectual property and artistic creations 
// by Dennis Sandow and is copyright 2000-2010 by the author. 
// For use only on websites maintained by Dennis Sandow, or with
// specific permission.  Do not remove this notice.

var NoPrintableAdvice=1, forceMonths, startMonth='' ;
var today=(cur.getMonth()+1)*100+cur.getDate();
var CalCmdLineArgs = (location.search+'|').substring(1,999).split('|')[0]
cleanArg = CalCmdLineArgs.replace(/[a-zA-Z]/g,''); 
startMonth = parseInt(cleanArg.split('+')[0]); 
if ( startMonth ) cur = new Date( MoNames[startMonth]+' 1, '+thisYear); 
forceMonths = parseInt((cleanArg+'+').split('+')[1]); 
if (typeof isMM4 != udf && ! forceMonths) forceMonths=4;
if(! forceMonths) forceMonths=2;
expectedSerialNo = (location.search+'|').split('|')[1];

var day;
function OneMonth(MoNo) {
  DoW = new Array('',0,0,0,0,0,0,0); 
// .getDay() >> Sun=0,Sat=6. 
// If 1st of month is Wed (3), nullDaysOnFirstLine=3 and firstValidDayOFWeek=3 
  Mon1 = new Date(MoNames[MoNo]+' 1, '+thisYear); 
  nullDaysOnFirstLine = firstValidDayOFWeek = Mon1.getDay(); 
  wholeMonth=''; day=1; month_length=MoLengths[MoNo];
  while (day <= month_length) {
// abandon all days before the past sunday
// If today (cur.) is Thu (9th), Start cal at Sun (5th)
// day=1 - (9-4)>1 increment day++ and continue loop
// day=5 - (9-4)!>5 remember day=5 and drop out of if{}
    if( (cur.getMonth()+1) == MoNo && ( cur.getDate()-cur.getDay()) > day ){
      day++; firstValidDayOFWeek=0; nullDaysOnFirstLine=0;
      continue;
    };
    wholeMonth += '<tr valign="top">';
// if we are in the first week of the month and it starts after Sun, add fill cell.    
    if ( nullDaysOnFirstLine ) { 
      wholeMonth += '<td bgcolor=#dddddd colspan='+nullDaysOnFirstLine+'>_</td>' ;
      nullDaysOnFirstLine=0;
    };
// loop through days of the week or last date of the month 
    for ( i=firstValidDayOFWeek; i <= 6 && day <= month_length; i++) {
      oneDayGuts=''; parentCellBG=''; itemsToday=0; testdate=MoNo*100+day; 
      for (xy=0; xy<sortHits.length; xy++ ) {
        oneEntry=new Array(); oneEntry=sortHits[xy].split('~')[2].split('|');

        for ( xz=6; xz<oneEntry.length; xz++ ){
// if(sortHits[xy].split('~')[1]==120) { alert(MoNo+' '+xy+' '+xz+' '+oneEntry+' '+oneEntry[xz]+' '+testdate) ; return;}
          if ( oneEntry[xz] != testdate ) continue;
          isBGklor = cellColor(oneEntry); whitelet='';
          if (isBGklor == -1 ) continue;
          itemsToday++; DoW[i]++;
// alert( oneEntry[4] )
// No entries, no table.  Date only
//    Set after loop, if ! oneDayGuts
// First entry.  Make inner table. 
          if ( itemsToday == 1) {
            oneDayGuts='<table align="left" border="0" cellpadding="1" cellspacing="0" width="100%" height="100%">';
          };
// First entry has a time.  Date only on first line
          if ( itemsToday == 1 && oneEntry[1] ) {
            oneDayGuts += '<tr><td><b>'+day+'</b></td></tr>';
          };
// First entry has NO time.  Make inner table. Date plus BOLD entry on first line. 
// If cell color is red, set up the cell enclosing the table also red.
          
          if ( itemsToday == 1 && ! oneEntry[1] ) {
            if (isBGklor == '#e02020') { parentCellBG=' bgcolor="#e02020" '; whitelet='<span class="whitechars">'; };
            if (isBGklor == '#e04060') { parentCellBG=' bgcolor="#e04060" '; whitelet='<span class="whitechars">'; };
            oneDayGuts+='<tr><td bgcolor="'+isBGklor+'">'+
//            whitelet+ Not working well 9/6/09.  Deferred
            '<b>'+day+'</b>';
          };
// All entries except ( first with no time). Open a cell and color.
          if ( itemsToday > 1 || oneEntry[1] ) {
            oneDayGuts+='<tr><td bgcolor="'+isBGklor+'">';
          };
// Finish cell.
          if ( itemsToday  ) {
            oneDayGuts+=( oneEntry[1]?'<b>'+oneEntry[1]+'</b>':'')+ 
              ( oneEntry[3]?'_<span class="invers"><b>'+oneEntry[3]+'</b></span>':'')+
              '<a href="javascript:calUpdate('+sortHits[xy].split('~')[1]+')" '+
                'onMouseover="window.status=\'\'; return true;" class="noUL">_</a>'+
	        (oneEntry[5]?('<a href="javascript:void(0)" onmouseout="xnd()" onMouseover="return HintPop(\'cal'+sortHits[xy].split('~')[1]+'\',\'More information\')" class=hnf>'+oneEntry[4]+'</a>'):oneEntry[4])+'</td></tr>';
          };
          break;
        }; // end one item


      }; // end items in sorted one-day list
// Close day cell and increments
      if(oneDayGuts) oneDayGuts+='</table>'; else oneDayGuts='<b>'+day+'</b>';
      wholeMonth += '<td align=left '+parentCellBG+ 
        ( today == testdate?'class="rbdr"':'' )+'>'+  // red border around today
        oneDayGuts+'</td>' ;
      day++; firstValidDayOFWeek=0;
    };  // end loop days of one week 
// pad end of last wewk if needed
    if (i <= 6 ) wholeMonth += '<td bgcolor=#dddddd colspan='+(7-i)+'>_</td>' ;
    wholeMonth += '</tr>';
  }; // end of all days in month  

  DaStr = ''; 
  if ( DoW[0] && DoW[6] ) WDoW = new Array(14,14,14,14,14,14,14); 
    else if ( DoW[0] ) WDoW = new Array(16,16,16,16,16,16,4); 
    else if ( DoW[6] ) WDoW = new Array(4,16,16,16,16,16,16); 
    else WDoW = new Array(5,18,18,18,18,18,5); 
  for ( i=0; i< DaNames.length; i++) { 
    DaStr += '<td width="'+WDoW[i]+'%" align=center bgcolor=lightgreen>'+DaNames[i]+'</td>' ; 
  };
// set up bulletin page link.
  if (isPrint && typeof bulletinTitle != udf && bulletinTitle && typeof newWdCal != udf ) {
    MonthBarColspan=6;
    bulletinLink='<td align=center bgcolor="yellow"><a href="javascript:newWdCal('+MoNo+')" class="noUL"><b>Bulletin</b></a></td>';
  } else { MonthBarColspan=7; bulletinLink=''; };

  outStr=('<td align="center">'+
            '<table border=1 cellspacing=0 cellpadding=1 width="100%">'+
              '<tr bgcolor="#00c040"><td colspan='+MonthBarColspan+' align=center>'+
                 '<b>'+MoNames[MoNo]+appendYear+'</b></td>'+
                 bulletinLink+'</tr>'+
                 '<tr>'+DaStr+'</tr>'+
                   wholeMonth.replace(/_/g,'&nbsp;')+
             '</tr></table></td>');
  return outStr;
};

function createPopup(lineIn){
  lineA = new Array(); lineA = lineIn.split('~')[2].split('|');
  xMsg = (lineA[5]+'^').split('^')[0];
  xReg = (lineA[5]+'^').split('^')[1].replace(/ /g,''); 
  if (! xMsg && ! xReg) return;
  msgKey= 'cal'+lineIn.split('~')[1]; regLink='';
  if (xReg) {
    regLink = '&nbsp; <a href="javascript:RegisterEmail3(\''+xReg+'\',\''+lineA[4]+'\')" onMouseout="window.status=window.defaultStatus; return true;" onMouseover="window.status=\'Send Email\'; return true;" class="v10generic"><b>Register</b></a>';
  };
  Mesage[msgKey]=xMsg + regLink ;
};

var sortOrph = new Array(), sortHits = new Array(), appendYear='';
if (typeof highPriority == udf ) highPriority=9;
function makeCalendar(){
  ordHits=0; ordOrph=0;
  for( xx=101; xx<=399; xx++) {
    if ( typeof calA[xx] == udf ) continue
    if( calA[xx].split('|')[6] ){
// if is a date, then set up to sort by inverse type (i.e. cancellations first)
      if(calA[xx].split('|')[2] == highPriority) hp=1; else hp=9;
      sortHits[ordHits++] = hp+(calA[xx].split('|')[0])+'~'+xx+'~'+calA[xx];
//if ( xx == 156 ) alert('Hits '+xx+' '+(ordHits-1)+' '+sortHits[ordHits-1] )
    } else {
// if no date, then set up to sort by type and alpha subject
      sortOrph[ordOrph++] = calA[xx].split('|')[2]+calA[xx].split('|')[4]+'~'+xx+'~'+calA[xx];
//if ( xx == 120 ) alert('Orph '+xx+' '+(ordOrph-1)+' '+sortOrph[ordOrph-1] )
    };
  };
  sortOrph.sort(); sortHits.sort();
  for (xy=0; xy<sortOrph.length; xy++ ) { createPopup(sortOrph[xy]); };
  for (xy=0; xy<sortHits.length; xy++ ) { createPopup(sortHits[xy]); };

  start = cur.getMonth()+1; doMo = start-1; last=doMo+forceMonths;
  for (morow=start; morow <= last; morow++) {
    document.write('<tr valign=top>');
    if (++doMo > 12){
      doMo-=12; thisYear++; appendYear=', '+thisYear
      if (((thisYear % 4)==0) && ((thisYear % 100)!=0) || ((thisYear % 400)==0)) MoLengths[2]=29 ; else MoLengths[2] = 28;
    };
    document.write( OneMonth(doMo) );
    document.write('</tr>');
  };

// show orphans
  orphanList='';
  if (forceMonths == 12) {
    for (xy=0; xy<sortOrph.length; xy++ ) {
      oneEntry=new Array(); oneEntry=sortOrph[xy].split('~')[2].split('|')
      isBGklor = cellColor(oneEntry);
      if (isBGklor == -1 ) continue;

      orphanList+='<tr><td bgcolor="'+isBGklor+'">'+
        ( oneEntry[1]?'<b>'+oneEntry[1]+'</b>':'')+ 
        ( oneEntry[3]?'_<span class="invers"><b>'+oneEntry[3]+'</b></span>':'')+
        '<a href="javascript:calUpdate('+sortOrph[xy].split('~')[1]+')" '+
          'onMouseover="window.status=\'\'; return true;" class="noUL">_</a>'+
	(oneEntry[5]?('<a href="javascript:void(0)" onmouseout="xnd()" onMouseover="return HintPop(\'cal'+sortOrph[xy].split('~')[1]+'\',\'More information\')" class=hnf>'+oneEntry[4]+'</a>'):oneEntry[4]);
	  
      for (xo=6; xo<oneEntry.length; xo++ ) { 
        if (! oneEntry[xo]) continue;
        orphanList+= ' '+oneEntry[xo].replace(/..$/,'')+'/'+oneEntry[xo].substring(oneEntry[xo].length-2,9);
      }; // end days in item
      orphanList+='</td></tr>';
    }; // end items in sorted list

    document.write('<tr><td align="left"><table border=1 cellspacing=0 cellpadding=1 width="100%"><tr><td align=center><b>'+(orphanList?'':'NO ')+'ORPHANS</b></td></tr>'+orphanList.replace(/_/g,'&nbsp;')+'</table></td></tr>');
  };

  if ( typeof dontAllowMore == udf ) {  //generally udf. Blocks only for ab4 & mm4
    clickWd="location.href='"+iAm+".htm?"+(mySpecial?'D':'')+(isExpress?'X':'')+(startMonth?startMonth:'')+"+"+(forceMonths+1)+"#b'";
    if(forceMonths >= 12) clickWd="alert('ENUF')";
    document.write('<tr><td align="center"><form name="moreForm">'+
      '<input type="button" value="More?" onclick="'+clickWd+'">'+
      '<a name="b"></a></form></td></tr>');
  };
};

var passDotOrd='', passNextOrd='', fileSerialNo='';
function calUpdate(arg){
  if ( ! chainTo ) { alert('Sorry'); return; } ;
  passDotOrd=999;
  if ( typeof calA[arg] != udf && calA[arg] ) passDotOrd = arg;
  for( xx=101; xx<=399; xx++) {
    if ( typeof calA[xx] == udf && ! passNextOrd ) passNextOrd = xx;
  };
//  alert( chainTo+'?'+CalCmdLineArgs+'|'+passDotOrd+'|'+passNextOrd+'|'+fileSerialNo );
  NewWd( chainTo+'?'+CalCmdLineArgs+'|'+ passDotOrd+'|'+passNextOrd+'|'+fileSerialNo );
};


