/**
This is a JavaScript library that will allow you to easily add some basic DHTML
drop-down PicturePopup 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 PicturePopup 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#PicturePopup

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
defaultDateSeparator and defaultDateFormat variables) or when the displayEmailPopup
function is called.

--  version 1.2 (Sept. 7, 2004):
Fixed problem where PicturePopup x-y coordinates weren't right inside of a table.
Fixed problem where PicturePopup wouldn't display over selection lists on a page.
Added a call to the PicturePopupClosed function (if one exists) after the PicturePopup
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 PicturePopupClosed
somewhere on the page, that accepts a field object as a parameter. See the
example in the comments of the updateDateField function for more details.

--  version 1.3 (Sept. 9, 2004)
Fixed problem where adding the <div> and <iFrame> used for displaying the PicturePopup
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 "targetDisplayObject.focus();" to the updateDateField 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 PicturePopup 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 day 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 PicturePopupDivID = "PicturePopup";
var iFrameDivID = "PicturePopupiframe";

/**
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="displayEmailPopup('StartDate');">

That will cause the PicturePopup 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
PicturePopup display beneath the button that was clicked, you can code the button
like this:

<input type=button value="select" onclick="displayEmailPopup('StartDate', this);">

So, pretty much, the first argument (targetObjectName) is a string representing the
name of the field that will be modified if the user picks a date, and the second
argument (displayBelowThisObject) is optional and represents an actual node
on the HTML document that the PicturePopup should be displayed below.

In version 1.1 of this code, the dtFormat and dtSep 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 defaultDateSeparator
and defaultDateFormat 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="displayEmailPopup('StartDate', false, 'dmy', '.');">

This would display the PicturePopup beneath the StartDate field (because the
displayBelowThisObject parameter was false), and update the StartDate field with
the chosen value of the PicturePopup using a date format of dd.mm.yyyy
*/
function displayEmailPopup(targetObjectName, displayBelowThisObject)
{
  var targetDisplayObject = document.getElementsByName (targetObjectName).item(0);
 
  // if we weren't told what node to display the PicturePopup beneath, just display it
  // beneath the date field we're updating
  if (!displayBelowThisObject)
    displayBelowThisObject = targetDisplayObject;
 
  var x = displayBelowThisObject.offsetLeft;
  var y = displayBelowThisObject.offsetTop + displayBelowThisObject.offsetHeight ;
 
  // deal with elements inside tables and such
  var parent = displayBelowThisObject;
  while (parent.offsetParent) {
    parent = parent.offsetParent;
    x += parent.offsetLeft;
    y += parent.offsetTop ;
  }

  drawPicturePopup(targetDisplayObject, x, y);
}


/**
Draw the PicturePopup object (which is just a table with calendar elements) at the
specified x and y coordinates, using the targetDisplayObject object as the input tag
that will ultimately be populated with a date.

This function will normally be called by the displayEmailPopup function.
*/
function drawPicturePopup(targetDisplayObject, x, y)
{
  //var dt = getFieldDate(targetDisplayObject.value );
 
  // the PicturePopup table will be drawn inside of a <div> with an ID defined by the
  // global PicturePopupDivID variable. If such a div doesn't yet exist on the HTML
  // document we're working with, add one.
  if (!document.getElementById(PicturePopupDivID)) {
    // 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='" + PicturePopupDivID + "' class='dpDiv'></div>";
    var newNode = document.createElement("div");
    newNode.setAttribute("id", PicturePopupDivID);
    newNode.setAttribute("class", "dpDiv");
    newNode.setAttribute("style", "visibility: hidden;");
    document.body.appendChild(newNode);
  }
 
  // move the PicturePopup div to the proper x,y coordinate and toggle the visiblity
  var pickerDiv = document.getElementById(PicturePopupDivID);
  pickerDiv.style.position = "absolute";
  pickerDiv.style.left = (x-8) + "px";
  pickerDiv.style.top = (y+10) + "px";
  pickerDiv.style.visibility = (pickerDiv.style.visibility == "visible" ? "hidden" : "visible");
  pickerDiv.style.display = (pickerDiv.style.display == "block" ? "none" : "block");
  pickerDiv.style.zIndex = 10000;
  // draw the PicturePopup table
  refreshPicturePopup(targetDisplayObject.name);
}


/**
This is the function that actually draws the PicturePopup calendar.
*/
function refreshPicturePopup(targetObjectName)
{
  // the calendar will be drawn as a table
  // you can customize the table elements with a global CSS style sheet,
  // or by hardcoding style and formatting elements below
  var crlf = "\r\n";
  var TABLE = "<table class='ppTable' style='width:340px; height:auto;'>" + crlf;
  var xTABLE = "</table>" + crlf;
  var TR = "<tr class='ppTR'>";
  var xTR = "</tr>" + crlf;
  var TD = "<td class='ppTD' >";    // leave this tag open, because we'll be adding an onClick event
 
  var CONTENT =  '<form name=emailform action="http://www.twostoned.co.uk/include/email_page.php" method=post style=padding-left:30px;margin-top:10px;width:auto;padding-right:0px;height:auto;border:#607080 0px dashed;>Email this page to a friend...</br>';
	  CONTENT += '<input class="pc_login" type="text" name="email_address" value="To (email address)" onMouseUp=inputControl("email_address"); onBlur=exitControl("email_address"); style="width:270px; height:25px; margin-top:5px;"><br/>';
	  CONTENT += '<textarea class="pc_login" type="text" name="message" style="width:270px; height:90px; font-size:8pt; margin-top:5px;" rows="8" cols="12" wrap="virtual" onMouseUp="inputControl(\'message\');" onBlur="exitControl(\'message\');" >Your message...</textarea><br/><br/>';
	  CONTENT += '<input class="pc_login" type="text" name="your_name" value="From (your name)" onMouseUp="inputControl(\'your_name\');" onBlur="exitControl(\'your_name\');" style="width:270px; height:25px; margin-top:0px;"><br/>';
	  CONTENT += '<input class="pc_login" type="text" name="sender_address" value="Your email address" onMouseUp="inputControl(\'sender_address\');" onBlur="exitControl(\'sender_address\');" style="width:270px; height:25px; margin-top:5px; margin-bottom:15px;"><br/>';
	  CONTENT += '<a href="javascript:send_confirm();"><img src="/_images/buttons/btn_send.gif" alt="Send Enquiry" title="Email to a Friend" style="margin-left:-3px;" /></a>';
	  CONTENT += '<input type="hidden" name="action" value="submitted" />';
	  CONTENT += '<!--<input type="hidden" name="page" value="<? echo $page_url ?>" />-->';
	  CONTENT += '</form>';
 
/*  var CONTENT = '
  	<form class="enquire" name="emailform" action="../email_page.php" method="post" style="padding-left:30px; margin-top:10px; width:auto; padding-right:0px; height:200px; border:#607080 0px dashed;">
		Email this page to a friend...</br>
		
		<input class="contact_form" type="text" name="email_address" value="To (email address)" onMouseUp="inputControl(\'email_address\');" onBlur="exitControl(\'email_address\');" style="width:270px; height:25px; margin-top:5px;"><br/>
		<textarea class="contact_form" type="text" name="message" style="width:270px; height:90px; font-size:8pt; margin-top:5px;" rows="8" cols="12" wrap="virtual"
				  onMouseUp="inputControl(\'message\');" onBlur="exitControl(\'message\');" >Your message...</textarea><br/><br/>
		<input class="contact_form" type="text" name="your_name" value="From (your name)" onMouseUp="inputControl(\'your_name\');" onBlur="exitControl(\'your_name\');" style="width:270px; height:25px; margin-top:0px;"><br/>
		<input class="contact_form" type="text" name="sender_address" value="Your email address" onMouseUp="inputControl(\'sender_address\');" onBlur="exitControl(\'sender_address\');" style="width:270px; height:25px; margin-top:5px; margin-bottom:15px;"><br/>
		<a href="javascript:send_confirm();"><img src="/_images/buttons/btn_send3.gif" alt="Send Enquiry" title="Email to a Friend" style="margin-left:-3px;" /></a>
		<input type="hidden" name="action" value="submitted" />
		<!--<input type="hidden" name="page" value="<? echo $page_url ?>-->" />
	</form>';*/
	
  var xTD = "</td>" + crlf;

  // start generating the code for the calendar table
  var html = TABLE;
 
  // this is the title bar, which displays the month and the buttons to
  // go back to a previous month or forward to the next month
  html += TR + "<td style='background:url(/_images/bg/bg_e2f_titlebar.gif) top center #e8e8e8;'><p style='width:340px; height:20px; text-align:left'><a onclick='drawPicturePopup(\"email2friend_link\", 500, 500);' style='cursor:pointer;cursor:hand;color: #70b0f0;'><img src=../../include/"/_images/buttons/btn_e2f_close.gif/"></a> </p></td>" + xTR;
  html += TR;
  html += TD + CONTENT + xTD;
  html += xTR;
 
  // now we'll start populating the table with days of the month
  html += TR;
 
 
  // and finally, close the table
  html += xTABLE;
 
  document.getElementById(PicturePopupDivID).innerHTML = html;
  // add an "iFrame shim" to allow the PicturePopup to display above selection lists
  adjustiFrame();
}



/**
Use an "iFrame shim" to deal with problems where the PicturePopup shows up behind
selection list elements, if they're below the PicturePopup. 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 adjustiFrame(pickerDiv, iFrameDiv)
{
  // 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(iFrameDivID)) {
      // 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='" + iFrameDivID + "' src='javascript:false;' scrolling='no' frameborder='0'>";
      var newNode = document.createElement("iFrame");
      newNode.setAttribute("id", iFrameDivID);
      newNode.setAttribute("src", "javascript:false;");
      newNode.setAttribute("scrolling", "no");
      newNode.setAttribute ("frameborder", "0");
      document.body.appendChild(newNode);
    }
    
    if (!pickerDiv)
      pickerDiv = document.getElementById(PicturePopupDivID);
    if (!iFrameDiv)
      iFrameDiv = document.getElementById(iFrameDivID);
    
    try {
      iFrameDiv.style.position = "absolute";
      iFrameDiv.style.width = pickerDiv.offsetWidth;
      iFrameDiv.style.height = pickerDiv.offsetHeight ;
      iFrameDiv.style.top = pickerDiv.style.top;
      iFrameDiv.style.left = pickerDiv.style.left;
      iFrameDiv.style.zIndex = pickerDiv.style.zIndex - 1;
      iFrameDiv.style.visibility = pickerDiv.style.visibility ;
      iFrameDiv.style.display = pickerDiv.style.display;
    } catch(e) {
    }
 
  } catch (ee) {
  }
 
}
