//  You may not remove or change this notice.
//  Copyright Bret Burkett 2006. All rights reserved.
//
// This states the conditions under which the code may be copied, such that 
// the Copyright Holder maintains some semblance of artistic control over the 
// development of the code, while giving the users of the code the right to use 
// and distribute the code in a more-or-less customary fashion, plus the right to 
// make reasonable modifications.
//
// "Freely Available" means that no fee is charged for the item itself, though 
// there may be fees involved in handling the item. It also means that recipients 
// of the item may redistribute it under the same conditions they received it.
//
// You may make and give away copies of the source code without restriction, provided
// that you duplicate all of the original copyright notices and disclaimers.
// You may otherwise modify your copy of this code in any way, provided that you 
// insert a prominent notice in each changed stating how and when you changed that 
// file. You may use the modified code only within your corporation or organization.
//
//  Do not sell this as your own work or remove this copyright notice. 
//  Please give credit on sites that use this code
//  Revision: 1.0                 Date: 2006/03/05 7:41pm 

// globel variables
var flgIsIE = false;

// first function called from the calendar gif link
function StartCal(PassDivNm, PassToEle, DateSrvr, PassIsIE)
{
flgIsIE = PassIsIE;// set the flag according to .NET 2.0 server check
var dtSrvr = new Date(DateSrvr);
//var intDayWk2 = dtSrvr.getDay();      // returns 0-6
var intMonth2 = dtSrvr.getMonth();    // returns 0-11
var intDate2  = dtSrvr.getDate();     // returns 1-31
var intYear2  = dtSrvr.getFullYear(); // returns 4 digit year
BldCal(intDate2,intMonth2,intYear2,PassDivNm,PassToEle);
VisCal(PassDivNm);// show the calandar
}

//show or hide the calender div's 
function VisCal(PassDivNm)
{
if (document.all[PassDivNm].style.visibility == 'visible')
  {
  document.all[PassDivNm].style.visibility = 'hidden';
  document.all[PassDivNm + "Ifrm"].style.visibility = 'hidden';
  }
else
  {
  document.all[PassDivNm].style.visibility = 'visible'; 
  document.all[PassDivNm + "Ifrm"].style.visibility = 'visible'; 
  }
}

//function called when date is selected from the calendar
function SetDt(PassToEle, PassDivNm, PassMonth, PassDay, PassYear)
{
// add 1 to month since JS uses 0-11       //use sql server divider
this.document.forms[0].elements[PassToEle].value = (PassMonth + 1) + "/" + PassDay + "/" + PassYear;
VisCal(PassDivNm);// hide the calander
}

// builds calendar html and writes it to the div
// and the iframe for IE
function BldCal(intDate, intMonth, intYear, strDivNm, strToEle)
{
var strCal = "";
var dtPass = new Date();
dtPass.setDate(1);
dtPass.setMonth(intMonth);
dtPass.setFullYear(intYear);
var intDayWk = dtPass.getDay();   // returns 0-6
var strClassType;
var cntDays = 1;
var arrMoNms = new Array("Jan.","Feb.","March","April","May","June","July","Aug.","Sept.","Oct.","Nov.","Dec.");
var arrDaysMonth = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
arrDaysMonth[1] = daysInFebruary(intYear);

var holdMo = (intMonth - 1) < 0 ? 11: (intMonth - 1);
var holdYr = holdMo == 11 ? (intYear - 1): intYear;
var strPreMo = '<a class="nostyle" href="JavaScript:BldCal(0,' + holdMo + ',' + holdYr + ",'" + strDivNm + "','" + strToEle + "'" + ');" ';
strPreMo += ' style="color=black;" onmouseover="JavaScript:return StBar(' + "'Previous Month'" + ');" ';
strPreMo += ' onmouseout="JavaScript:return StBar(' + "' '" + ');" ';
strPreMo += ' title="Previous Month" >&lt;-</a>';
holdMo = (intMonth + 1) > 11 ? 0: (intMonth + 1);
holdYr = holdMo == 0 ? (intYear + 1): intYear;
var strNxtMo = '<a class="nostyle" href="JavaScript:BldCal(0,' + holdMo + ',' + holdYr + ",'" + strDivNm + "','" + strToEle + "'" + ');" ';
strNxtMo += ' style="color=black;" onmouseover="JavaScript:return StBar(' + "'Next Month'" + ');" ';
strNxtMo += ' onmouseout="JavaScript:return StBar(' + "' '" + ');" ';
strNxtMo += ' title="Next Month" >-&gt;</a>';

strCal =  '<table style="border:1pt solid black; background-color: white;"><tr><td colspan="7">';
strCal += '<table width="100%"><tr><td class="caltitle" align="left">' + strPreMo + '&nbsp;</td><td class="caltitle" align="middle">&nbsp;';
strCal +=  arrMoNms[intMonth] + " " + intYear + '&nbsp;</td><td class="caltitle" align="right">&nbsp;' + strNxtMo + '</td></tr></table>';
strCal += '</td></tr><tr><td class="caldaynm">S</td><td class="caldaynm">M</td><td class="caldaynm">T</td><td class="caldaynm">W</td>';
strCal += '<td class="caldaynm">T</td><td class="caldaynm">F</td><td class="caldaynm">S</td></tr>';

while (cntDays <= arrDaysMonth[intMonth]) // loop for the month
  {
  strCal += '<tr>';// start the week
  for (var intLoopCnt = 0; intLoopCnt <= 6; intLoopCnt++)// loop for the week
    {
    strCal += '<td class="calday">';
    if (intLoopCnt == intDayWk) intDayWk = 9;//we've written enough start space
    
    if ((intDayWk == 9) && (cntDays <= arrDaysMonth[intMonth]))
      {
      strClassType = cntDays==intDate ? ' style="color=red;" ' : ' style="color=blue;" ';
      strCal += '<a class="nostyle" href="JavaScript:SetDt(' + "'" + strToEle + "',";
      strCal += "'" + strDivNm + "'," + intMonth + ',' + cntDays + ',' + intYear + ');" ';
      strCal += ' onmouseover="JavaScript:CalDtOver(this);return StBar(' + "'Select Date'" + ');" ';
      strCal += ' onmouseout="JavaScript:CalDtOut(this);return StBar(' + "' '" + ');" ';
      strCal += strClassType + '>' + cntDays + '</a>';
      cntDays++;
      }
    else
      { strCal += '&nbsp;'; } // else space
    strCal += '</td>';
    }
  strCal += '</tr>';//end the week
  }
strCal += '</table>';
// write out the calander to the div
this.document.all[strDivNm].innerHTML=strCal;
//browser check for specific code
if (flgIsIE)
 {
 // so it will show over listboxes write an iframe under it
 var strIFrm = '<iframe frameborder="0" scrolling="no" ';
 strIFrm += ' height="' + document.getElementById(strDivNm).offsetHeight + '" ';
 strIFrm += ' width="' + document.getElementById(strDivNm).offsetWidth + '" ';
 strIFrm += ' style="z-index: 1; " ';
 strIFrm += ' ></iframe>';
 //use below only for IE
 this.document.all[strDivNm + "Ifrm"].innerHTML=strIFrm;
 //position it directly under the calender div
 document.all[strDivNm + "Ifrm"].style.left=document.all[strDivNm].offsetLeft;
 document.all[strDivNm + "Ifrm"].style.top=document.all[strDivNm].offsetTop;
 }
else
 {
 //use below for netscape and opera
 document.all[strDivNm].style.left=document.all[strDivNm+ "Img"].offsetLeft;
 }
}

//used when mouse over on the date in the calendar
function CalDtOver(currentitem)
{
currentitem.style.backgroundColor="#dddddd";
//currentitem.style.borderTop="1pt solid #eeeeee";
//currentitem.style.borderLeft="1pt solid #eeeeee";
//currentitem.style.borderBottom="1pt solid #aaaaaa";
//currentitem.style.borderRight="1pt solid #aaaaaa";
currentitem.style.cursor="hand";
 }
 
//used whe mouse out on the date in the calendar
function CalDtOut(currentitem)
{
currentitem.style.backgroundColor="#ffffff";
//currentitem.style.borderTop="1pt solid #ffffff";
//currentitem.style.borderLeft="1pt solid #ffffff";
//currentitem.style.borderBottom="1pt solid #ffffff";
//currentitem.style.borderRight="1pt solid #ffffff";
}
