// JavaScript Document

/**
This is a JavaScript library that will allow you to easily add some basic DHTML
drop-down datepicker functionality to your Notes forms. This script is not as
full-featured as others you may find on the Internet, but it's free, it's easy to
understand, and it's easy to change.

You'll also want to include a stylesheet that makes the datepicker elements
look nice. An example one can be found in the database that this script was
originally released with, at:

http://www.nsftools.com/tips/NotesTips.htm#datepicker

I've tested this lightly with Internet Explorer 6 and Mozilla Firefox. I have no idea
how compatible it is with other browsers.

version 1.5
December 4, 2005
Julian Robichaux -- http://www.nsftools.com

HISTORY
--  version 1.0 (Sept. 4, 2004):
Initial release.

--  version 1.1 (Sept. 5, 2004):
Added capability to define the date format to be used, either globally (using the
defaultDateSeparator2 and defaultDateFormat2 variables) or when the displayDatePicker
function is called.

--  version 1.2 (Sept. 7, 2004):
Fixed problem where datepicker x2-y coordinates weren't right inside of a table.
Fixed problem where datepicker wouldn't display over selection lists on a page.
Added a call to the datePickerClosed2 function (if one exists) after the datepicker
is closed, to allow the developer to add their own custom validation after a date
has been chosen. For this to work, you must have a function called datePickerClosed2
somewhere on the page, that accepts a field object as a parameter. See the
example in the comments of the updateDateField2 function for more details.

--  version 1.3 (Sept. 9, 2004)
Fixed problem where adding the <div> and <iFrame> used for displaying the datepicker
was causing problems on IE 6 with global variables that had handles to objects on
the page (I fixed the problem by adding the elements using document.createElement()
and document.body.appendChild() instead of document.body.innerHTML += ...).

--  version 1.4 (Dec. 20, 2004)
Added "targetDateField2.focus();" to the updateDateField2 function (as suggested
by Alan Lepofsky) to avoid a situation where the cursor focus is at the top of the
form after a date has been picked. Added "padding: 0px;" to the dpButton CSS
style, to keep the table from being so wide when displayed in Firefox.

-- version 1.5 (Dec 4, 2005)
Added display=none when datepicker is hidden, to fix problem where cursor is
not visible on input fields that are beneath the date picker. Added additional null
date handling for date errors in Safari when the date is empty. Added additional
error handling for iFrame creation, to avoid reported errors in Opera. Added
onMouseOver event for day2 cells, to allow color changes when the mouse hovers
over a cell (to make it easier to determine what cell you're over). Added comments
in the style sheet, to make it more clear what the different style elements are for.
*/

var datePickerDivID2 = "avail";
var iFrameDivID2 = "availframe";
var day2ArrayShort2 = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
var day2ArrayMed2 = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
var day2ArrayLong2 = new Array('Sunday2', 'Monday2', 'Tuesday2', 'Wednesday2', 'Thursday2', 'Friday2', 'Saturday2');
var month2ArrayShort2 = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
var month2ArrayMed2 = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
var month2ArrayLong2 = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
 
// these variables define the date formatting we're expecting and outputting.
// If you want to use a different format by default, change the defaultDateSeparator2
// and defaultDateFormat2 variables either here or on your HTML page.
var defaultDateSeparator2 = "/";        // common values would be "/" or "."
var defaultDateFormat2 = "dmy"    // valid values are "mdy", "dmy", and "ymd"
var dateSeparator2 = defaultDateSeparator2;
var dateFormat2 = defaultDateFormat2;

/**
This is the main function you'll call from the onClick event of a button.
Normally, you'll have something like this on your HTML page:

Start Date: <input name="StartDate">
<input type=button value="select" onclick="displayDatePicker('StartDate');">

That will cause the datepicker to be displayed beneath the StartDate field and
any date that is chosen will update the value of that field. If you'd rather have the
datepicker display beneath the button that was clicked, you can code the button
like this:

<input type=button value="select" onclick="displayDatePicker('StartDate', this);">

So, pretty much, the first argument (dateFieldName2) is a string representing the
name of the field that will be modified if the user picks a date, and the second
argument (displayBelowThisObject2) is optional and represents an actual node
on the HTML document that the datepicker should be displayed below.

In version 1.1 of this code, the dt2Format2 and dt2Sep2 variables were added, allowing
you to use a specific date format or date separator for a given call to this function.
Normally, you'll just want to set these defaults globally with the defaultDateSeparator2
and defaultDateFormat2 variables, but it doesn't hurt anything to add them as optional
parameters here. An example of use is:

<input type=button value="select" onclick="displayDatePicker('StartDate', false, 'dmy', '.');">

This would display the datepicker beneath the StartDate field (because the
displayBelowThisObject2 parameter was false), and update the StartDate field with
the chosen value of the datepicker using a date format of dd.mm.yyyy
*/
function displayDatePicker2(dateFieldName2, displayBelowThisObject2, dt2Format2, dt2Sep2)
{
	
	  var targetDateField2 = document.getElementById (dateFieldName2);
  
 
  // if we weren't told what node to display the datepicker beneath, just display it
  // beneath the date field we're updating
  if (!displayBelowThisObject2)
    displayBelowThisObject2 = targetDateField2;
 
  // if a date separator character was given, update the dateSeparator2 variable
  if (dt2Sep2)
    dateSeparator2 = dt2Sep2;
  else
    dateSeparator2 = defaultDateSeparator2;
 
  // if a date format was given, update the dateFormat2 variable
  if (dt2Format2)
    dateFormat2 = dt2Format2;
  else
    dateFormat2 = defaultDateFormat2;
 
  var x2 = displayBelowThisObject2.offsetLeft;
 if (dateFieldName2=='checkin'){ 
  var y2 = displayBelowThisObject2.offsetTop + displayBelowThisObject2.offsetHeight-170 ;
  } 
  else {
	  var y2 = displayBelowThisObject2.offsetTop + displayBelowThisObject2.offsetHeight-175 ;
	  } 
  
 
  // deal with elements inside tables and such
  var parent2 = displayBelowThisObject2;
  while (parent2.offsetParent) {
    parent2 = parent2.offsetParent;
    x2 += parent2.offsetLeft;
    y2 += parent2.offsetTop ;
  }
 
  drawDatePicker2(targetDateField2, x2, y2);
}


/**
Draw the datepicker object (which is just a table with calendar elements) at the
specified x2 and y2 coordinates, using the targetDateField2 object as the input tag
that will ultimately be populated with a date.

This function will normally be called by the displayDatePicker function.
*/
function drawDatePicker2(targetDateField2, x2, y2)
{
  var dt2 = getFieldDate2(targetDateField2.value );
 
  // the datepicker table will be drawn inside of a <div> with an ID defined by the
  // global datePickerDivID2 variable. If such a div doesn't yet exist on the HTML
  // document we're working with, add one.
  if (!document.getElementById(datePickerDivID2)) {
    // don't use innerHTML to update the body, because it can cause global variables
    // that are currently pointing to objects on the page to have bad references
    //document.body.innerHTML += "<div id='" + datePickerDivID2 + "' class='dpDiv'></div>";
    var newNode2 = document.createElement("div");
    newNode2.setAttribute("id", datePickerDivID2);
    newNode2.setAttribute("class", "dpDiv");
    newNode2.setAttribute("style", "visibility: hidden;");
    document.body.appendChild(newNode2);
  }
 
  // move the datepicker div to the proper x2,y2 coordinate and toggle the visiblity

  var pickerDiv2 = document.getElementById(datePickerDivID2);
   pickerDiv2.style.position = "absolute";
  pickerDiv2.style.left = x2 + "px";
  pickerDiv2.style.top = y2 + "px";
  pickerDiv2.style.visibility = (pickerDiv2.style.visibility == "visible" ? "hidden" : "visible");
  pickerDiv2.style.display = (pickerDiv2.style.display == "block" ? "none" : "block");
  pickerDiv2.style.zIndex = 10000;
  
  
 
 
  // draw the datepicker table
  refreshDatePicker2(targetDateField2.name, dt2.getFullYear(), dt2.getMonth(), dt2.getDate());
}

/**
Convenience function for writing the code for the buttons that bring us back or forward
a month2.
*/
function getButtonCode2(dateFieldName2, dateVal2, adjust, label)
{
  var newMonth2 = (dateVal2.getMonth () + adjust) % 12;
  var newYear2 = dateVal2.getFullYear() + parseInt((dateVal2.getMonth() + adjust) / 12);
  if (newMonth2 < 0) {
    newMonth2 += 12;
    newYear2 += -1;
  }
 
  return "<button class='dpButton' onClick='refreshDatePicker2(\"" + dateFieldName2 + "\", " + newYear2 + ", " + newMonth2 + ");'>" + label + "</button>";
}


/**
Convert a JavaScript Date object to a string, based on the dateFormat2 and dateSeparator2
variables at the beginning of this script library.
*/
function getDateString2(dateVal2)
{
  var day2String = "00" + dateVal2.getDate();
  var month2String = "00" + (dateVal2.getMonth()+1);
  day2String = day2String.substring(day2String.length - 2);
  month2String = month2String.substring(month2String.length - 2);
 
  switch (dateFormat2) {
    case "dmy" :
      return day2String + dateSeparator2 + month2String + dateSeparator2 + dateVal2.getFullYear();
    case "ymd" :
      return dateVal2.getFullYear() + dateSeparator2 + month2String + dateSeparator2 + day2String;
    case "mdy" :
    default :
      return month2String + dateSeparator2 + day2String + dateSeparator2 + dateVal2.getFullYear();
  }
}


/**
Convert a string to a JavaScript Date object.
*/
function getFieldDate2(dateString2)
{
  var dateVal2;
  var dArray2;
  var d, m, y;
 
  try {
    dArray2 = splitDateString2(dateString2);
    if (dArray2) {
      switch (dateFormat2) {
        case "dmy" :
          d = parseInt(dArray2[0], 10);
          m = parseInt(dArray2[1], 10) - 1;
          y = parseInt(dArray2[2], 10);
          break;
        case "ymd" :
          d = parseInt(dArray2[2], 10);
          m = parseInt(dArray2[1], 10) - 1;
          y = parseInt(dArray2[0], 10);
          break;
        case "mdy" :
        default :
          d = parseInt(dArray2[1], 10);
          m = parseInt(dArray2[0], 10) - 1;
          y = parseInt(dArray2[2], 10);
          break;
      }
      dateVal2 = new Date(y, m, d);
    } else if (dateString2) {
      dateVal2 = new Date(dateString2);
    } else {
      dateVal2 = new Date();
    }
  } catch(e) {
    dateVal2 = new Date();
  }
 
  return dateVal2;
}


/**
Try to split a date string into an array of elements, using common date separators.
If the date is split, an array is returned; otherwise, we just return false.
*/
function splitDateString2(dateString2)
{
  var dArray2;
  if (dateString2.indexOf("/") >= 0)
    dArray2 = dateString2.split("/");
  else if (dateString2.indexOf(".") >= 0)
    dArray2 = dateString2.split(".");
  else if (dateString2.indexOf("-") >= 0)
    dArray2 = dateString2.split("-");
  else if (dateString2.indexOf("\\") >= 0)
    dArray2 = dateString2.split("\\");
  else
    dArray2 = false;
 
  return dArray2;
}

/**
Update the field with the given dateFieldName2 with the dateString2 that has been passed,
and hide the datepicker. If no dateString2 is passed, just close the datepicker without
changing the field value.

Also, if the page developer has defined a function called datePickerClosed2 anywhere on
the page or in an imported library, we will attempt to run that function with the updated
field as a parameter. This can be used for such things as date validation, setting default
values for related fields, etc. For example, you might have a function like this to validate
a start date field:

function datePickerClosed2(dateField)
{
  var dateObj = getFieldDate2(dateField.value);
  var today2 = new Date();
  today2 = new Date(today2.getFullYear(), today2.getMonth(), today2.getDate());
 
  if (dateField.name == "StartDate") {
    if (dateObj < today2) {
      // if the date is before today2, alert the user and display the datepicker again
      alert("Please enter a date that is today2 or later");
      dateField.value = "";
      document.getElementById(datePickerDivID2).style.visibility = "visible";
      adjustiFrame();
    } else {
      // if the date is okay, set the EndDate field to 7 day2s after the StartDate
      dateObj.setTime(dateObj.getTime() + (7 * 24 * 60 * 60 * 1000));
      var endDateField = document.getElementsByName ("EndDate").item(0);
      endDateField.value = getDateString2(dateObj);
    }
  }
}

*/
function updateDateField2(dateFieldName2, dateString2)
{
  var targetDateField2 = document.getElementsByName (dateFieldName2).item(0);
  if (dateString2)
    targetDateField2.value = dateString2;
 
  var pickerDiv2 = document.getElementById(datePickerDivID2);
  pickerDiv2.style.visibility = "hidden";
  pickerDiv2.style.display = "none";
 
  adjustiFrame2();
  targetDateField2.focus();
 
  // after the datepicker has closed, optionally run a user-defined function called
  // datePickerClosed2, passing the field that was just updated as a parameter
  // (note that this will only run if the user actually selected a date from the datepicker)
  if ((dateString2) && (typeof(datePickerClosed2) == "function"))
    datePickerClosed2(targetDateField2);
}


/**
Use an "iFrame shim" to deal with problems where the datepicker shows up behind
selection list elements, if they're below the datepicker. The problem and solution are
described at:

http://dotnetjunkies.com/WebLog/jking/archive/2003/07/21/488.aspx
http://dotnetjunkies.com/WebLog/jking/archive/2003/10/30/2975.aspx
*/
function adjustiFrame2(pickerDiv2, iFrameDiv2)
{
  // we know that Opera doesn't like something about this, so if we
  // think we're using Opera, don't even try
  var is_opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
  if (is_opera)
    return;
  
  // put a try/catch block around the whole thing, just in case
  try {
    if (!document.getElementById(iFrameDivID2)) {
      // don't use innerHTML to update the body, because it can cause global variables
      // that are currently pointing to objects on the page to have bad references
      //document.body.innerHTML += "<iframe id='" + iFrameDivID2 + "' src='javascript:false;' scrolling='no' frameborder='0'>";
      var newNode2 = document.createElement("iFrame");
      newNode2.setAttribute("id", iFrameDivID2);
      newNode2.setAttribute("src", "javascript:false;");
      newNode2.setAttribute("scrolling", "no");
      newNode2.setAttribute ("frameborder", "0");
      document.body.appendChild(newNode2);
    }
    
    if (!pickerDiv2)
      pickerDiv2 = document.getElementById(datePickerDivID2);
    if (!iFrameDiv2)
      iFrameDiv2 = document.getElementById(iFrameDivID2);
    
    try {
      iFrameDiv2.style.position = "absolute";
      iFrameDiv2.style.widt2h = pickerDiv2.offsetWidt2h;
      iFrameDiv2.style.height = pickerDiv2.offsetHeight ;
      iFrameDiv2.style.top = pickerDiv2.style.top;
      iFrameDiv2.style.left = pickerDiv2.style.left;
      iFrameDiv2.style.zIndex = pickerDiv2.style.zIndex - 1;
      iFrameDiv2.style.visibility = pickerDiv2.style.visibility ;
      iFrameDiv2.style.display = pickerDiv2.style.display;
    } catch(e) {
    }
 
  } catch (ee) {
  }
 
}

