// Use Freely as long as following disclaimer is intact ::
//----------------------------------------------------------
// Popup Calendar + Date Picker v1.0 9th November 2004
// This script written by Rik Comery. http://ricom.co.uk
// For support, visit the "Resources" section at http://ricom.co.uk
// All rights reserved.

// Calendar Configuration ============================================================================================================
popStatus = 1                       // Set calendar as always visible, or popup.  0=always visible, 1=popup

outerBorder = "0";            // Colour of outer border.  For no border, enter "0"
monthBackground = "#ffecce";        // Background colour of month strap
monthColor = "#996600";             // Text colour of Month title
monthFace = "";    // Font of Month title
monthSize = ""   ;              // Font size of Month title
monthWeight = "bold";               // Weight of Month title. (bold or normal)

dayBackground = "#ffecce";          // Background colour  of day labels
dayColor = "#996600";               // Text colour of day labels
dayFace = "";      // Font of day labels
daySize = "";                    // Font size of day labels
dayWeight = "bold";                 // Weight of day labels. (bold or normal)

dateFace = "";      // Font of dates
dateSize = "";                   // Font size of dates
dateColor = "#996633";              // Text colour of dates
linkColor = "#996633";              // Safety net
dividingLine = "#fffcfc";           // Colour of dividing lines

emptyBack = "#ffecce";              // Background colour of empty date cells
fullBack = "#ffecce";               // Background colour of full date cells
fullWeekendBack = "#ffecce";        // Background colour of weekend cells
fullBackHover = "#ffecce";          // Background colour of date cells on mouse over

todayBorder = "0";            // Border colour to highlight today's date. Use "0" for no border
todayBack = "#ffecce";              // Background colour of today's date
todayColor = "#996600";             // Text colour of today's date
todayWeight = "bold";               // Weight of today's date. (bold or normal)

plusSymbol = ">";                   // Symbol to forward month.  You may also us images. e.g. plusSymbol = "<img src='plus.gif' />";
minusSymbol = "<";                  // Symbol to reverse month.  You may also us images. e.g. minusSymbol = "<img src='minus.gif' />";

separateWeekend= "1";                  // Places a dividing line between week days and weekends. 1=yes, 0=no

dateFormat = "DD MMMM YYYY";        // Format date will appear in text box. 
                                    // DD = date, 
                                    // MM = month in figures. eg 12 for December
                                    // MMM = Short Month. eg Dec
                                    // MMMM = Long Month. eg. December
                                    // yy = Short Year. eg. 04
                                    // yyyy = Long Year. eg. 2004
                                    
                                    // Other examples are:
                                    // DD - MMM - YYYY (25 - Dec - 2004)
                                    // YYYY-MM-DD (2004-12-25)
                                    // MMMM DD, YYYY (December 25, 2004)
 
// Month + Day Labels ======================================================================================================================
// Change the months and day labels as appropriate.  Ensure they remain within quotes.
rcMonths=new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
rcShortMonths=new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
rcDays=new Array("M","T","W","T","F","S","S")

// End Configuration. Do Not Edit Below This Line ==========================================================================================

//Browser Sniffer
var isIE = (document.getElementById && document.all)?true:false;
var isNS4 = (document.layers)?true:false;
var isNS6 = (document.getElementById && !document.all)?true:false;

if(isIE) var obj = "document.all";
else if(isNS6) var obj = "document.getElementById";
else if(isNS4) var obj = "document.layers";

style='<style type="text/css">';
style+='a{text-decoration:none}';
style+='.monthhead{font-family:'+monthFace+';font-size:'+monthSize+';background-color:'+monthBackground+';color:'+monthColor+'; font-weight:'+monthWeight+'}';
style+='.dayhead{font-family:'+dayFace+';font-size:'+daySize+';background-color:'+dayBackground+';color:'+dayColor+'; text-align:center; cursor:default; font-weight:'+monthWeight+'}';
style+='.divider{background-color:'+dividingLine+'}';
style+='.absent{background-color:'+emptyBack+'; font-family:'+dateFace+'; font-size:'+dateSize+'; color:'+dateColor+'; cursor:default}';
style+='.full{background-color:'+fullBack+'; font-family:'+dateFace+'; font-size:'+dateSize+'; color:'+dateColor+'; cursor:hand}';
style+='.fullweekend{background-color:'+fullWeekendBack+'; font-family:'+dateFace+'; font-size:'+dateSize+'; color:'+dateColor+'; cursor:hand}';
style+='.fullhover{background-color:'+fullBackHover+'; font-family:'+dateFace+'; font-size:'+dateSize+'; color:'+dateColor+'; cursor:hand}';
style+='.today{background-color:'+todayBack+'; font-family:'+dateFace+'; font-size:'+dateSize+'; color:'+todayColor+'; font-weight:'+todayWeight;
if (todayBorder !="0"){style+=';padding:2px;  border:1px solid '+todayBorder;}
style+='; cursor:hand}';
style+='.todayhover{background-color:'+todayBack+'; font-family:'+dateFace+'; font-size:'+dateSize+'; color:'+todayColor+'; font-weight:'+todayWeight;
if (todayBorder !="0"){style+=';padding:2px;  border:1px solid '+todayBorder;}
style+='; cursor:hand}';
style+='.close{font-family:'+dateFace+'; font-size:'+dateSize+'; color:'+dateColor+'; cursor:hand}';
style+='</style>';
document.writeln(style);

function doRCCalendar(){
  doCalc='<div z-index="1000" id="calendarDateDiv"  style="position:relative; width:1px"><table class="calendar"><tr><td nowrap="nowrap"><input class="btn2" type="text" id="calendarDate" name="calendarDate">';
  if(popStatus>0){
    doCalc+='&nbsp;<a href="javascript:void(0)" onClick="showCalendar()"><img src="calendar.gif" border="0"  alt="Calendar Popup" /><\/a>';
  }
  doCalc+='<\/td><\/tr><\/table><\/div>';
  doCalc+='<div z-index="1000" id="calendarBox" name="calendarBox" style="visibility:hidden; width:10px; position:absolute;"><\/div>';
  document.writeln(doCalc);
  getRCCalendar(); 
}

function showCalendar(when){
    if(when==""|| when==null){
      var dateBoxObj = eval(obj+'("calendarBox")')
      var dateDivObj = eval(obj+'("calendarDateDiv")')
      getOffset(dateDivObj,'top')
      getOffset(dateDivObj,'left')
      getOffset(dateDivObj,'width')
      
      dateBoxObj.style.left=oLeft+oWidth+10;
      dateBoxObj.style.top=oTop;
      dateBoxObj.style.visibility = "visible"
    }else{
      if(popStatus==0){
        showCalendar();
      }
    }

}

function closeCalendar(){  
  var dateBoxObj = eval(obj+'("calendarBox")')
  dateBoxObj.style.visibility = "hidden";
}

function highlight(obj, state, rcMonth, rcYear){
  if(obj.id==nowDate&&rcMonth==nowMonth&&rcYear==nowYear){
    obj.className="todayhover";
  }else{
    obj.className=state;
  }
}

// Find positioning for calendar
function getOffset(obj, dim) 
{
  if(dim=="left") 
  {     
    oLeft = obj.offsetLeft; 
    while(obj.offsetParent!=null) 
    {    
      oParent = obj.offsetParent     
      oLeft += oParent.offsetLeft 
      obj = oParent 	
    }
    return oLeft
  }
  else if(dim=="top")
  {
    oTop = obj.offsetTop;
    while(obj.offsetParent!=null) 
    {
      oParent = obj.offsetParent
      oTop += oParent.offsetTop
      obj = oParent 	
    }
    return oTop
  }
  else if(dim=="width")
  {
    oWidth = obj.offsetWidth
    return oWidth
  }  
  else if(dim=="height")
  {
    oHeight = obj.offsetHeight
    return oHeight
  }    
  else
  {
    alert("Error: invalid offset dimension '" + dim + "' in getOffset()")
    return false;
  }
}

function showDate(year, month, date){
  newDate=new Date(year, month, date)
  day = makeString(newDate.getDate());
  if(dateFormat.indexOf("MMM")<0){
    month = makeString(month)
  }
  year = newDate.getYear();
  if (year < 1000) {year += 1900}
  year = makeString(year)
  shortMonth=rcShortMonths[month];
  longMonth=rcMonths[month];
  year = (dateFormat.indexOf("YYYY")>-1)?year:year.substring(2,4);
  newDateFormat=dateFormat.replace(/DD/g, day).replace(/MMMM/g, longMonth).replace(/MMM/g, shortMonth).replace(/Y{1,4}/g, year).replace(/MM/g, month)

  eval(obj+'("calendarDate").value=newDateFormat')

}

function makeString(value){
  return ((value<10)?"0":"")+value
}

function clearForm(){
eval(obj+'("calendarDate").value=""') 
}

function getRCCalendar(year, month){
  now=new Date()
  nowDate=now.getDate()
  nowMonth=now.getMonth()
  nowYear=now.getYear()  
  if (nowYear < 1000) {nowYear += 1900}

  
  if(year==null || month==null){rcNow = new Date()}
  else{rcNow = new Date(year, month, 1)}
  var rcDate = rcNow.getDate()
  var rcMonth = rcNow.getMonth()
  var rcYear = rcNow.getYear()
  if (rcYear < 1000) {rcYear += 1900}

  
  whatMonth=rcMonth;
  whatMonths=rcMonths[whatMonth]  
  thisMonth = new Date(rcYear, whatMonth, 1)
  nextMonth = new Date(rcYear, whatMonth+1, 1)
  
  totalDays=Math.round((nextMonth.getTime() - thisMonth.getTime()) / (1000 * 60 * 60 * 24));
  
  firstDay = thisMonth.getDay();
  lastDay = new Date(rcYear, whatMonth, totalDays).getDay()
  
  cal='<table border="0" cellpadding="0" cellspacing="0"><tr><td colspan="2">';
  cal+='<table border="0" cellpadding="3" cellspacing="0" align="center"'; if(outerBorder!="0") {cal+=' style="border:1px solid '+outerBorder+'"';} cal+='>';
  cal+='<tr class="monthhead"><td align="center"><a href="javascript:void(0)" onClick="getRCCalendar('+rcYear+','+(rcMonth-1)+')" class="monthhead">'+minusSymbol+'<\/a><\/td>';
  cal+='<td colspan="5" align="center" nowrap="nowrap" style="cursor:default">'+whatMonths+' '+rcYear+'<\/td>';  
  cal+='<td align="center"><a href="javascript:void(0)" onClick="getRCCalendar('+rcYear+','+(rcMonth+1)+')" class="monthhead">'+plusSymbol+'<\/a><\/td><\/tr><tr>';
  for(x=0;x<7;x++){cal+='<td style="padding:0px" class="divider"><img src="blank.gif" height="1" width="22" alt="" /><\/td>';}
  cal+='<\/tr>';
  for(y=0;y<7;y++){
    cal+='<td class="dayhead"';
    if(y<6){cal+='style="border-right:1px solid '+dividingLine+'"';}
    cal+='>'+rcDays[y]+'<\/td>';   
  }
  cal+='<\/tr>';
  for(x=0;x<7;x++){cal+='<td style="padding:0px" class="divider"><img src="blank.gif" height="1" width="19" alt="" /><\/td>';}
  cal+='<\/tr><tr>';
  displayDate=1;
  cellID=1;
  
  while (displayDate <= totalDays)
  {
  
    if(cellID<firstDay)
    {cal+='<td class="absent"><p>&nbsp;<\/p><\/td>';}
    else
    {
      if(!(cellID%7-6)||!(cellID%7)){newClass="fullweekend";}
      else{
        if(displayDate==nowDate&&rcMonth==nowMonth&&rcYear==nowYear)
        {newClass="today"; linkColor=todayColor}
        else{newClass="full"; linkColor=dateColor;}
      }
      extender=(dateFormat.indexOf("MMM")>-1)?0:1
      cal+='<td id="'+displayDate+'" class="'+newClass+'"';
      if(!(cellID%7-5)&&separateWeekend==1){cal+='style="border-right:1px solid '+dividingLine+'"';}
      cal+=' align="center" onClick="showDate(\''+rcYear+'\',\''+(rcMonth+extender)+'\',\''+displayDate+'\');';
      cal+=' window.status=rcMonths['+rcMonth+']+\' \'+'+displayDate+'+\', \'+'+rcYear+';';
      if(popStatus==1){cal+=' closeCalendar();';}
      cal+=' return true"';
      cal+=' onMouseOver="highlight(this,\'fullhover\',\''+rcMonth+'\',\''+rcYear+'\'); window.status=rcMonths['+rcMonth+']+\' \'+'+displayDate+'+\', \'+'+rcYear+'; return true"';
      cal+=' onMouseOut="highlight(this,\''+newClass+'\',\''+rcMonth+'\',\''+rcYear+'\'); window.status=\'\'">';
      cal+='<a href="javascript:void(0)" style="color:'+linkColor+'" >'+displayDate+'<\/a><\/td>';
    }
    if (!(cellID%7)) {cal+='<\/tr>'}  
    if(cellID>=firstDay){displayDate++; }
    cellID++
    
  }
  while(((cellID-1)%7))
  {
    newClass="absent";
    cal+='<td class="'+newClass+'"';
    if(!(cellID%7-5)&&separateWeekend==1){cal+='style="border-right:1px solid '+dividingLine+'"';}
    cal+='><p>&nbsp;<\/p><\/td>';
    cellID++
  }  
  cal+='<\/table><\/td><\/tr>';
  cal+='<tr>';
  if(popStatus>0){
    cal+='<td nowrap="nowrap" width="50%" align="left" class="close"><a href="javascript:void(0)" onMouseOver="window.status=\'close calendar\'; return true" onClick="closeCalendar()">close<\/a><\/td>';
  }else{
    cal+='<td width="50%"><p>&nbsp;<\/p><\/td>';
  }
  cal+='<td nowrap="nowrap" width="50%" align="right" class="close"><a href="javascript:void(0)" onMouseOver="window.status=\'clear form\'; return true" onClick="clearForm();';
  if(popStatus==1){
    cal+=' closeCalendar()';
  }
  cal+='">clear<\/a><\/td><\/tr>';
  
  eval(obj+'("calendarBox").innerHTML = cal')  
}