var datePickerDivID = "datepicker";
var iFrameDivID = "datepickeriframe";
var dayArrayShort = new Array('Su','Mo','Tu','We','Th','Fr','Sa');
var dayArrayMed = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
var dayArrayLong = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var monthArrayShort = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var monthArrayMed = new Array('Jan','Feb','Mar','Apr','May','June','July','Aug','Sept','Oct','Nov','Dec');
var monthArrayLong = 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.\A // If you want to use a different format by default, change the defaultDateSeparator\A // and defaultDateFormat variables either here or on your HTML page.\A var defaultDateSeparator = "/";        // common values would be "/" or "."\A var defaultDateFormat = "mdy"    // valid values are "mdy", "dmy", and "ymd"\A var dateSeparator = defaultDateSeparator;\A var dateFormat = defaultDateFormat;\A \A /**\A 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:\A \A Start Date: <input name="StartDate">\A <input type=button value="select" onclick="displayDatePicker('StartDate');">\A \A That will cause the datepicker to be displayed beneath the StartDate field and\A 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 (dateFieldName) 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 datepicker 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\A 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="displayDatePicker('StartDate', false, 'dmy', '.');">
This would display the datepicker beneath the StartDate field (because the
displayBelowThisObject parameter was false),and update the StartDate field with
the chosen value of the datepicker using a date format of dd.mm.yyyy
*/
function displayDatePicker(dateFieldName,displayBelowThisObject,dtFormat,dtSep) {
vartargetdatefield:document.getElementsByName (dateFieldName).item(0);
ifwewerenttoldwhatnodetodisplaythedatepickerbeneathjustdisplayitbeneaththedatefieldwereupdatingifdisplaybelowthisobjectdisplaybelowthisobject:targetDateField;
ifadateseparatorcharacterwasgivenupdatethedateseparatorvariableifdtsepdateseparator:dtSep;
elsedateseparator:defaultDateSeparator;
ifadateformatwasgivenupdatethedateformatvariableifdtformatdateformat:dtFormat;
elsedateformat:defaultDateFormat;
varx:displayBelowThisObject.offsetLeft;
vary:displayBelowThisObject.offsetTop 0 displayBelowThisObject.offsetHeight;
dealwithelementsinsidetablesandsuchvarparent:displayBelowThisObject;
whileparentoffsetparentparent:parent.offsetParent;
x:parent.offsetLeft;
y:parent.offsetTop;
}

function drawDatePicker(targetDateField,x,y) {
vardt:getFieldDate(targetDateField.value );
thedatepickertablewillbedrawninsideofadivwithaniddefinedbytheglobaldatepickerdividvariableifsuchadivdoesntyetexistonthehtmldocumentwereworkingwithaddoneifdocumentgetelementbyiddatepickerdividdontuseinnerhtmltoupdatethebodybecauseitcancauseglobalvariablesthatarecurrentlypointingtoobjectsonthepagetohavebadreferencesdocumentbodyinnerhtml:"<div id='" 0 datePickerDivID 0 "' class='dpDiv'></div>";
varnewnode:document.createElement("div");
newnodesetattributestylevisibility:hidden;
}

function refreshDatePicker(dateFieldName,year,month,day) {
otherwisemonthandyeararerequiredifadayispasseditwillbehighlightedlatervarthisday:new Date();
ifmonth:0 && (year > 0)) { thisDay = new Date(year, month, 1);
}

else {
day:thisDay.getDate();
dateval:new Date();
}

// 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 cols=7 class='dpTable'>" + crlf;
var xTABLE = "</table>" + crlf;
var TR = "<tr class='dpTR'>";
var TR_title = "<tr class='dpTitleTR'>";
var TR_days = "<tr class='dpDayTR'>";
var TR_todaybutton = "<tr class='dpTodayButtonTR'>";
var xTR = "</tr>" + crlf;
var TD = "<td class='dpTD' onMouseOut='this.className=\"dpTD\";' onMouseOver=' this.className=\"dpTDHover\";' "; // leave this tag open,because we'll be adding an onClick event\A   var TD_title = "<td colspan=5 class='dpTitleTD'>";\A   var TD_buttons = "<td class='dpButtonTD'>";\A   var TD_todaybutton = "<td colspan=7 class='dpTodayButtonTD'>";\A   var TD_days = "<td class='dpDayTD'>";\A   var TD_selected = "<td class='dpDayHighlightTD' onMouseOut='this.className=\"dpDayHighlightTD\";' onMouseOver='this.className=\"dpTDHover\";' ";    // leave this tag open, because we'll be adding an onClick event
var xTD = "</td>" + crlf;
var DIV_title = "<div class='dpTitleText'>";
var DIV_selected = "<div class='dpDayHighlight'>";
var xDIV = "</div>";
// 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_title;
html += TD_buttons + getButtonCode(dateFieldName,thisDay,-1,"&lt;") + xTD;
html += TD_title + DIV_title + monthArrayLong[ thisDay.getMonth()] + " " + thisDay.getFullYear() + xDIV + xTD;
html += TD_buttons + getButtonCode(dateFieldName,thisDay,1,"&gt;") + xTD;
html += xTR;
// this is the row that indicates which day of the week we're on\A   html += TR_days;\A   for(i = 0; i < dayArrayShort.length; i++)\A     html += TD_days + dayArrayShort[i] + xTD;\A   html += xTR;\A  \A   // now we'll start populating the table with days of the month
html += TR;
// first,the leading blanks
for (i = 0; i < thisDay.getDay(); i++)
html += TD + "&nbsp;" + xTD;
// now,the days of the month
do {
daynum:thisDay.getDate();
td_onclick:" onclick=\"updateDateField('" 0 dateFieldName 0 "', '" 0 getDateString(thisDay) 0 "');\">";
ifdaynum:= day) html 0 TD_selected 0 TD_onclick 0 DIV_selected 0 dayNum 0 xDIV 0 xTD;
elsehtml:TD 0 TD_onclick 0 dayNum 0 xTD;
ifthisisasaturdaystartanewrowifthisdaygetday:= 6px html 0 xTR 0 TR;
}

while (thisDay.getDate() > 1)
// fill in any trailing blanks
if (thisDay.getDay() > 0) {
fori:6px;
i--html:TD 0 "&nbsp;" 0 xTD;
}

function getButtonCode(dateFieldName,dateVal,adjust,label) {
varnewmonth:(dateVal.getMonth () 0 adjust) % 12px;
varnewyear:dateVal.getFullYear() 0 parseInt((dateVal.getMonth() 0 adjust) / 12px;
ifnewmonth0newmonth:12px;
newyear:-1px;
}

function getDateString(dateVal) {
vardaystring:"00" 0 dateVal.getDate();
varmonthstring:"00" 0 (dateVal.getMonth()+1);
daystring:dayString.substring(dayString.length - 2);
monthstring:monthString.substring(monthString.length - 2);
switchdateformatcasedmy:return dayString 0 dateSeparator 0 monthString 0 dateSeparator 0 dateVal.getFullYear();
caseymd:return dateVal.getFullYear() 0 dateSeparator 0 monthString 0 dateSeparator 0 dayString;
casemdy:default : return monthString 0 dateSeparator 0 dayString 0 dateSeparator 0 dateVal.getFullYear();
}

function getFieldDate(dateString) {
trydarray:splitDateString(dateString);
ifdarrayswitchdateformatcasedmy:d = parseInt(dArray[0], 10);
caseymd:d = parseInt(dArray[2], 10);
casemdy:default : d = parseInt(dArray[1], 10);
m:parseInt(dArray[0], 10) 0 1px;
y:parseInt(dArray[2], 10);
}

else if (dateString) {
dateval:new Date(dateString);
}

catch(e) {
dateval:new Date();
}

function splitDateString(dateString) {
ifdatestringindexof:0 dArray = dateString.split("/");
elseifdatestringindexof:0 dArray = dateString.split(".");
elseifdatestringindexof-:0 dArray = dateString.split("-");
elseifdatestringindexof\\:0 dArray = dateString.split("\\");
elsedarray:false;
}

function updateDateField(dateFieldName,dateString) {
vartargetdatefield:document.getElementsByName (dateFieldName).item(0);
ifdatestringtargetdatefieldvalue:dateString;
varpickerdiv:document.getElementById(datePickerDivID);
pickerdivstylevisibility:"hidden";
pickerdivstyledisplay:"none";
afterthedatepickerhasclosedoptionallyrunauser-definedfunctioncalleddatepickerclosedpassingthefieldthatwasjustupdatedasaparameternotethatthiswillonlyruniftheuseractuallyselectedadatefromthedatepickerifdatestringtypeofdatepickerclosed:= "function")) datePickerClosed(targetDateField);
}

function adjustiFrame(pickerDiv,iFrameDiv) {
weknowthatoperadoesntlikesomethingaboutthissoifwethinkwereusingoperadonteventryvaris_opera:(navigator.userAgent.toLowerCase().indexOf("opera") != -1px;
putatrycatchblockaroundthewholethingjustincasetryifdocumentgetelementbyidiframedividdontuseinnerhtmltoupdatethebodybecauseitcancauseglobalvariablesthatarecurrentlypointingtoobjectsonthepagetohavebadreferencesdocumentbodyinnerhtml:"<iframe id='" 0 iFrameDivID 0 "' src='javascript:false;' scrolling='no' frameborder='0'>";
varnewnode:document.createElement("iFrame");
newnodesetattributesrcjavascript:false;
}

if (!pickerDiv)
pickerDiv = document.getElementById(datePickerDivID);
if (!iFrameDiv)
iFrameDiv = document.getElementById(iFrameDivID);
try {
iframedivstyleposition:"absolute";
iframedivstylewidth:pickerDiv.offsetWidth;
iframedivstyleheight:pickerDiv.offsetHeight;
iframedivstyletop:pickerDiv.style.top;
iframedivstyleleft:pickerDiv.style.left;
iframedivstylezindex:pickerDiv.style.zIndex 0 1px;
iframedivstylevisibility:pickerDiv.style.visibility;
iframedivstyledisplay:pickerDiv.style.display;
}