/*
****************************************************
Author : Lea Smart
Source : www.totallysmartit.com
Date : 7/3/2001
DHTML Calendar
Version 1.2

You are free to use this code if you retain this header.
You do not need to link to my site (be nice though!)

Amendments
22 Jan 2002; Added ns resize bug code; rewrote date functions into BetterDate 'class';
Added support for yyyy-mm-dd date format
Added support for calendar beginning on any day
// Added ValidDate functions
// Added French support
// - Glenn MacStravic, www.wipo.int
******************************************************
*/
var timeoutDelay = 2000; // milliseconds, change this if you like, set to 0 for the calendar to never auto disappear
var g_startDay = 1; // 0=sunday, 1=monday
// preload images
var imgUp = new Image(8,12);
imgUp.src = 'http://www.wipo.int/ipdl/include/up.gif';
var imgDown = new Image(8,12);
imgDown.src = 'http://www.wipo.int/ipdl/include/down.gif';

// used by timeout auto hide functions
var timeoutId = false;

// the now standard browser sniffer class
function Browser(){
  this.dom = document.getElementById?1:0;
  this.ie4 = (document.all && !this.dom)?1:0;
  this.ns4 = (document.layers && !this.dom)?1:0;
  this.ns6 = (this.dom && !document.all)?1:0;
  this.ie5 = (this.dom && document.all)?1:0;
  this.ok = this.dom || this.ie4 || this.ns4;
  this.platform = navigator.platform;
}
var browser = new Browser();

// dom browsers require this written to the HEAD section

if (self.browser.dom || self.browser.ie4) {
  document.writeln('<style>');
  document.writeln('#container {');
  document.writeln('position : absolute;');
  document.writeln('left : 100px;');
  document.writeln('top : 100px;');
  document.writeln('width : 134px;');;
  self.browser.platform=='Win32'?height=140:height=145;
  document.writeln('height : ' + height +'px;');
  document.writeln('clip:rect(0px 134px ' + height + 'px 0px);');
  //document.writeln('overflow : hidden;');
  document.writeln('visibility : hidden; z-index: 10;');
  document.writeln('background-color : #ffffff');
  document.writeln('}');
  document.writeln('</style>')
  document.write('<div id="container"');
  if (timeoutDelay) document.write(' onmouseout="calendarTimeout();" onmouseover="if (timeoutId) clearTimeout(timeoutId);"');
  document.write('></div>');
}

var g_Calendar = false;  // global to hold the calendar reference, set by constructor
var debugFlag = 0;
//###
function initialize(display, hidden) {
  new Calendar(new Date());
  if (display) {
    g_Calendar.selectDate(0, display, hidden);
    g_Calendar.setDocType(0);
  }
}
function calendarTimeout() {
  if (!self.browser) {
    self.browser = new Browser();
  }
  if (self.browser.ie4 || self.browser.ie5) {
    if (window.event.srcElement && window.event.srcElement.name!='month') timeoutId=setTimeout('g_Calendar.hide();',timeoutDelay);
  }
  if (self.browser.ns6 || self.browser.ns4){
    timeoutId=setTimeout('g_Calendar.hide();',timeoutDelay);
  }
}

// constructor for calendar class
function Calendar(){
  if (self.validPCTDates || self.validPCTI2CDates) {
    this.checkValidDates = true;
  }
  else {
    this.checkValidDates = false;
  }
  if (!self.browser) {
    self.browser = new Browser();
  }
    
  g_Calendar = this;
  // some constants needed throughout the program
  this.daysOfWeek = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
  this.frDaysOfWeek = new Array("Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa");
  this.months = new Array("January&nbsp;&nbsp;","February&nbsp;","March&nbsp;&nbsp;&nbsp;&nbsp;","April&nbsp;&nbsp;&nbsp;&nbsp;","May&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","June&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","July&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","August&nbsp;&nbsp;&nbsp;","September","October&nbsp;&nbsp;","November&nbsp;&nbsp;","December&nbsp;");
  this.frMonths = new Array("janvier&nbsp;&nbsp;","fevrier&nbsp;&nbsp;","mars&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","avril&nbsp;&nbsp;&nbsp;&nbsp;","mai&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","juin&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","juillet&nbsp;&nbsp;","ao&ucirc;t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;","septembre","octobre&nbsp;&nbsp;","novembre&nbsp;","d&eacute;cembre&nbsp;");
  this.daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  this.docType = 0;
  // 0 for PCT, 1 for PCTI2C
  if (self.browser.ns4){
    var tmpLayer = new Layer(127);
    if (timeoutDelay){
      tmpLayer.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
      tmpLayer.onmouseover = function(event) { if (timeoutId) clearTimeout(timeoutId); };
      tmpLayer.onmouseout = function(event) { timeoutId=setTimeout('g_Calendar.hide()',timeoutDelay);};
    }
    tmpLayer.x = 100;
    tmpLayer.y = 100;
    tmpLayer.bgColor = "#ffffff";
  }
  if (self.browser.dom || self.browser.ie4){
    var tmpLayer = self.browser.dom?document.getElementById('container'):document.all.container;
  }
  this.containerLayer = tmpLayer;
  if (self.browser.ns4 && self.browser.platform=='Win32') {
    this.containerLayer.clip.height=134;
    this.containerLayer.clip.width=127;
  }
}

Calendar.prototype.getFirstDOM = function() {
  var thedate = new Date();
  thedate.setDate(1);
  thedate.setMonth(this.month);
  thedate.setFullYear(this.year);
  return thedate.getDay();
}

Calendar.prototype.checkDate = function(searchDate) {
  if (this.checkValidDates == false) {
    return true;
  }
  var found, mid, low, high, top, bottom, localSearch, localCheck, x;
  mid = 0;
  found = false;
  top = 0;
  low = top;
  if (this.docType == 0) {
    bottom = self.validPCTDates.length-1;
  }
  else {
    bottom = self.validPCTI2CDates.length-1;
  }
  high = bottom;
  localSearch = parseInt(searchDate);
  while ( (found == false) && ( low <= high ) ) {
    mid = Math.floor(( low + high ) / 2);
    if ( mid < top ) {
      mid = top;
    }
    else if ( mid > bottom ) {
      mid = bottom;
    }
    if (this.docType == 0) {
      localCheck = parseInt(self.validPCTDates[mid]);
    }
    else {
      localCheck = parseInt(self.validPCTI2CDates[mid]);
    }
    if (localSearch < localCheck) {
      x = -1;
      low = mid+1;
    }
    else if (localSearch > localCheck) {
      x = 1;
      high = mid-1;
    }
    else {
      x = 0;
      found = true;
    }
  }
  if (found == true) {
    return mid;
  }
  else {
    if (x == -1) {
      mid++;
    }
    if (mid > bottom) {
      mid = bottom;
    }
    return (-1*mid)-1;
  }
}

Calendar.prototype.getDaysInMonth = function (month){
  if (month!=1) {
    return this.daysInMonth[month]
  }
  else {
    // is it a leap year
    if (BetterDate.isLeapYear(this.year)) {
      return 29;
    }
    else {
      return 28;
    }
  }
}

Calendar.prototype.buildString = function(){
  var tmpStr = '<form onSubmit="this.year.blur();return false;"><table width="100%" border="0" cellspacing="0" cellpadding="2" class="calBorderColor"><tr><td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
  tmpStr += '<tr>';
  tmpStr += '<td width="60%" class="cal" align="left">';
  var dateToLimit = parseInt(''+this.dateToYear+this.dateToMonth);
  var dateFromLimit = parseInt(''+this.dateFromYear+this.dateFromMonth);
  var monthValue;
  monthValue = parseInt(''+this.year+padZero(this.month+1));
  if (monthValue < dateFromLimit) {
    this.month = parseInt(''+this.dateFromMonth)-1;
  }
  else if (monthValue > dateToLimit) {
    this.month = parseInt(''+this.dateToMonth)-1;
  }
  var localMonth = this.month;
  if (this.hasDropDown) {
    tmpStr += '<select class="month" name="month" onchange="g_Calendar.selectChange();">';
    for (var i=0;i<this.months.length;i++){
      monthValue = parseInt(''+this.year+padZero(i+1));
      if ((monthValue > dateFromLimit) && (monthValue <= dateToLimit)) {
        tmpStr += '<option value="' + i + '"';
        if (i == localMonth) {
	  tmpStr += ' selected';
        }
        tmpStr += '>';
        if (this.language == "ENG") {
	    tmpStr += this.months[i];
        }
        else if (this.language == "FRE") {
	    tmpStr += this.frMonths[i];
        }
        tmpStr += '</option>';
      }
    }
    tmpStr += '</select>';
  }
  else {
    tmpStr += '<table border="0" cellspacing="0" cellpadding="0"><tr><td><a href="javascript: g_Calendar.changeMonth(-1);"><img name="calendar" src="http://www.wipo.int/ipdl/include/down.gif" width="8" height="12" border="0" alt=""></a></td><td class="cal" width="100%" align="center">';
    if (this.language == "ENG") {
      tmpStr += this.months[localMonth];
    }
    else if (this.language == "FRE") {
      tmpStr += this.frMonths[localMonth];
    }
    tmpStr += '</td><td class="cal"><a href="javascript: g_Calendar.changeMonth(+1);"><img name="calendar" src="http://www.wipo.int/ipdl/include/up.gif" width="8" height="12" border="0" alt=""></a></td></tr></table>';
  }
  tmpStr += '</td>';
  /* observation : for some reason if the below event is changed to 'onChange' rather than 'onBlur' it totally crashes IE (4 and 5)!
   */
  tmpStr += '<td width="40%" align="right" class="cal">';
  
  if (this.hasDropDown && false) { 
    tmpStr += '<input class="year" type="text" size="';
    // get round NS4 win32 lenght of year input problem
    (self.browser.ns4 && self.browser.platform=='Win32')?tmpStr += 1:tmpStr += 4;
    tmpStr += '" name="year" maxlength="4" onBlur="g_Calendar.inputChange();" value="' + this.year + '">';
  }
  else {
    tmpStr += '<table border="0" cellspacing="0" cellpadding="0"><tr><td class="cal">';
    if (parseInt(this.dateFromYear) < parseInt(this.year)) {
      tmpStr += '<A href="javascript: g_Calendar.changeYear(-1);"><img name="calendar" src="http://www.wipo.int/ipdl/include/down.gif" width="8" height="12" border="0" alt=""></a>';
    }
    else {
      tmpStr += '&nbsp;';
    }
    tmpStr += '</td><td class="cal" width="100%" align="center">' + this.year + '</td><td class="cal">';
    if (parseInt(this.dateToYear) > parseInt(this.year)) {
      tmpStr += '<a href="javascript: g_Calendar.changeYear(+1);"><img name="calendar" src="http://www.wipo.int/ipdl/include/up.gif" width="8" height="12" border="0" alt=""></a>';
    }
    else {
	tmpStr += '&nbsp;';
    }
    tmpStr += '</td></tr></table>';
  }
  tmpStr += '</td>';
  tmpStr += '</tr>';
  tmpStr += '</table>';
  var iCount = 1;
  var iFirstDOM = this.getFirstDOM()-g_startDay; // to prevent calling it in a loop
  if (iFirstDOM < 0 ) {
    iFirstDOM = iFirstDOM + 7;
  }
  //alert(iFirstDOM);
  var iDaysInMonth = this.getDaysInMonth(localMonth); // to prevent calling it in a loop
  
  tmpStr += '<table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
  tmpStr += '<tr>';
  for (var i=0;i<7;i++){
    tmpStr += '<td align="center" class="calDaysColor">';
    if (this.language == "ENG") {
      tmpStr += this.daysOfWeek[(g_startDay+i)%7];
    }
    else if (this.language == "FRE") {
      tmpStr += this.frDaysOfWeek[(g_startDay+i)%7];
    }      
    tmpStr += '</td>';
  }
  tmpStr += '</tr>';
  var tmpFrom = parseInt('' + this.dateFromYear + this.dateFromMonth + this.dateFromDay,10);
  var tmpTo = parseInt('' + this.dateToYear + this.dateToMonth + this.dateToDay,10);
  var tmpCompare;
  var isValidDate;
  var weekNum;
  for (var j=1;j<=6;j++) {
    tmpStr += '<tr>';
    for (var i=1;i<=7;i++) {
      tmpStr += '<td width="16" align="center" ';
      if ( (7*(j-1) + i)>=iFirstDOM+1  && iCount <= iDaysInMonth){
	tmpCompare = parseInt('' + this.year + padZero(localMonth+1) + padZero(iCount),10);
	isValidDate = false;
	if (tmpCompare >= tmpFrom && tmpCompare <= tmpTo) {
	  if (this.checkValidDates == true) {
	    weekNum = this.checkDate(tmpCompare);
	    if (weekNum >= 0) {
	      isValidDate = true;
	    }
	  }
	  else {
	    isValidDate = true;
	  }
	}
	tmpStr += 'class="cal ';
	if (isValidDate == true) {
	  tmpStr += ' calValidDateColor';
	}
	if (iCount==this.day && this.year==this.oYear && localMonth==this.oMonth) {
	  tmpStr += ' calHighlightColor';
	}
	if (i==6 || i==7) {
	  tmpStr += ' calWeekend';
	}
	tmpStr += '">';
	/* could create a date object here and compare that but probably more efficient to convert to a number
	   and compare number as numbers are primitives */
	if (isValidDate == true) {
	  tmpStr += '<a class="cal" href="javascript: g_Calendar.clickDay(' + iCount + ','+weekNum+','+localMonth+');">' + iCount + '</a>';
	}
	else {
	  tmpStr += '<span class="disabled">' + iCount + '</span>';
	}
	iCount++;
      }
      else {
	if  (i==1 || i==7) {
	  tmpStr += 'class="calWeekend"';
	}
	else {
	  tmpStr +='class="cal"';
	}
	tmpStr += '>&nbsp;';
      }
      tmpStr += '</td>'
	}
    tmpStr += '</tr>'
      }
  tmpStr += '</table></td></tr></table></form>'
  return tmpStr;
}

Calendar.prototype.selectChange = function(){
  this.month = parseInt(''+self.browser.ns6?this.containerLayer.ownerDocument.forms[0].month.options[this.containerLayer.ownerDocument.forms[0].month.selectedIndex].value:this.containerLayer.document.forms[0].month.options[this.containerLayer.document.forms[0].month.selectedIndex].value);
  this.writeString(this.buildString());
}

Calendar.prototype.inputChange = function(){
  var tmp = self.browser.ns6?this.containerLayer.ownerDocument.forms[0].year:this.containerLayer.document.forms[0].year;
  if (tmp.value >=1900 || tmp.value <=2100){
    this.year = tmp.value;
    this.writeString(this.buildString());
  } else {
    tmp.value = this.year;
  }
}
Calendar.prototype.changeYear = function(incr){
  (incr==1)?this.year++:this.year--;
  this.writeString(this.buildString());
}
Calendar.prototype.changeMonth = function(incr){
  if (this.month==11 && incr==1){
    this.month = 0;
    this.year++;
  } else {
    if (this.month==0 && incr==-1){
      this.month = 11;
      this.year--;
    } else {
      (incr==1)?this.month++:this.month--;
    }
  }
  this.writeString(this.buildString());
}

Calendar.prototype.clickDay = function(day, week, localMonth){
  
  var tmp = eval('document.' + this.target);
  if (tmp) {
    if (this.dateFormat=='dd-mmm-yyyy') tmp.value = day + this.dateDelim + this.months[localMonth].substr(0,3) + this.dateDelim + this.year;
    if (this.dateFormat=='dd/mm/yyyy') tmp.value = padZero(day) + this.dateDelim + padZero((localMonth+1)) + this.dateDelim + this.year;
    if (this.dateFormat=='mm/dd/yyyy') tmp.value = (localMonth+1) + this.dateDelim + day + this.dateDelim + this.year;
    if (this.dateFormat=='yyyy-mm-dd') tmp.value = this.year + this.dateDelim + (localMonth+1) + this.dateDelim + day;
  }
  else {
    document.location.href = 'index.jsp?month='+(this.month+1)+'&year='+this.year+'&day='+day;
  }
  if (this.target2) {
    var tmp = eval('document.' + this.target2);
    if (tmp) {
      if (this.docType == 0) {
	if (self.validPCTWeeks) {
	  tmp.value = self.validPCTWeeks[parseInt(week)];
	}
      }
      else {
	if (self.validPCTI2CWeeks) {
	  tmp.value = self.validPCTI2CWeeks[parseInt(week)];
	}
      }
    }
  }
  if (self.afterCalendarSelection) {
    self.afterCalendarSelection(this.year, localMonth, day);
  }
  if (self.browser.ns4) this.containerLayer.hidden=true;
  if (self.browser.dom || self.browser.ie4){
    this.containerLayer.style.display='none';
    this.containerLayer.style.visibility='hidden'
  }
}

Calendar.prototype.writeString = function(str){
  if (self.browser.ns4){
    this.containerLayer.document.open();
    this.containerLayer.document.write(str);
    this.containerLayer.document.close();
  } 
  if (self.browser.dom || self.browser.ie4){
    this.containerLayer.innerHTML = str;
  }
}

Calendar.prototype.setOffsets = function(x, y) {
    this.xOffset = parseInt(x);
    this.yOffset = parseInt(y);
}

Calendar.prototype.show = function(event, target, target2, bHasDropDown, dateFormat, newLanguage, dateFrom, dateTo) {
  // calendar can restrict choices between 2 dates, if however no restrictions
  // are made, let them choose any date between 1900 and 3000
  if (!this.dateFrom) {
    if (dateFrom) {
      this.dateFrom = dateFrom;
    }
    else {
      this.dateFrom = new Date(1900,0,1);
    }
  }
  this.dateFromDay = padZero(this.dateFrom.getDate());
  this.dateFromMonth = padZero(this.dateFrom.getMonth());
  this.dateFromYear = this.dateFrom.getFullYear();
  if (newLanguage) {
	this.language = newLanguage;
  }
  else {
    this.language = "ENG";
  }
  if (!this.dateTo) {
    if (dateTo) {
      this.dateTo = dateTo; 
    }
    else {
      this.dateTo = new Date(3000,0,1);
    }
  }
  this.dateToDay = padZero(this.dateTo.getDate());
  this.dateToMonth = padZero(this.dateTo.getMonth());
  this.dateToYear = this.dateTo.getFullYear();
  this.hasDropDown = bHasDropDown;
  if (dateFormat) this.dateFormat = dateFormat; else this.dateFormat = 'dd-mmm-yyyy';
  switch (this.dateFormat){
  case 'dd-mmm-yyyy':
  case 'yyyy-mm-dd':
    this.dateDelim = '-';
    break;
  case 'dd/mm/yyyy':
  case 'mm/dd/yyyy':
    this.dateDelim = '/';
    break;
  }
  
  if (self.browser.ns4) {
    if (!this.containerLayer.hidden) {
      this.containerLayer.hidden=true;
      return;
    }
  }
  if (self.browser.dom || self.browser.ie4){
    if (this.containerLayer.style.visibility=='visible') {
      this.containerLayer.style.display='none';
      this.containerLayer.style.visibility='hidden';
      return;
    }  
  }
  if (!this.xOffset) {
    if ((self.browser.ie5) || (self.browser.ie4)) {
      this.xOffset = 35;
    }
    else {
      this.xOffset = 10;
    }
  }
  if (!this.yOffset) {
    if (self.browser.ns6){
      this.yOffset = -5;
    }
    else {
      this.yOffset = 0;
    }
  }
  
  if (self.browser.ie5 || self.browser.ie4){
    var event = window.event;
  }
  if (self.browser.ns4){
    this.containerLayer.x = event.x+10;
    this.containerLayer.y = event.y-100;
  }
  if (self.browser.ie5 || self.browser.ie4){
    var obj = event.srcElement;
    x = 0;
    while (obj.offsetParent != null) {
      x += obj.offsetLeft;
      obj = obj.offsetParent;
    }
    x += obj.offsetLeft;
    y = 0;
    var obj = event.srcElement;
    while (obj.offsetParent != null) {
      y += obj.offsetTop;
      obj = obj.offsetParent;
    }
    y += obj.offsetTop;
    this.containerLayer.style.left = x+this.xOffset;
    if (event.y>0)this.containerLayer.style.top = y+this.yOffset;
  }
  if (self.browser.ns6){
    this.containerLayer.style.left = event.pageX+this.xOffset+"px";
    this.containerLayer.style.top = event.pageY+this.yOffset+"px";
  }
  this.target = target;
  this.target2 = target2;
  var tmp = eval('document.' + this.target);
  if (tmp && tmp.value && tmp.value.split(this.dateDelim).length==3){
    var atmp = tmp.value.split(this.dateDelim)
      switch (this.dateFormat){
	case 'dd-mmm-yyyy':
	for (var i=0;i<this.months.length;i++){
	  if (atmp[1].toLowerCase()==this.months[i].substr(0,3).toLowerCase()){
	    this.month = this.oMonth = i;
	    break;
	  }
	}
	this.day = parseInt(atmp[0],10);
	this.year = parseInt(atmp[2],10);
	break;
	case 'dd/mm/yyyy':
	case 'dd-mm-yyyy':
	this.month = this.oMonth = parseInt(atmp[1]-1,10); 
	this.day = parseInt(atmp[0],10);
	this.year = parseInt(atmp[2],10);
	break;
	case 'mm/dd/yyyy':
	case 'mm-dd-yyyy':
	this.month = this.oMonth = parseInt(atmp[0]-1,10);
	this.day = parseInt(atmp[1],10);
	this.year = parseInt(atmp[2],10);
	break;
	case 'yyyy-mm-dd':
	this.month = this.oMonth = parseInt(atmp[1]-1,10);
	this.day = parseInt(atmp[2],10);
	this.year = parseInt(atmp[0],10);
	break;
      }
  }
  else { // no date set, default to today
    var theDate = new Date();
    this.year = this.oYear = theDate.getFullYear();
    this.month = this.oMonth = theDate.getMonth();
    this.day = this.oDay = theDate.getDate();
  }
  this.writeString(this.buildString());
  
  // and then show it!
  if (self.browser.ns4) {
    this.containerLayer.hidden=false;
  }
  if (self.browser.dom || self.browser.ie4){
    this.containerLayer.style.display='';
    this.containerLayer.style.visibility='visible';
  }
}

Calendar.prototype.hide = function(){
  if (self.browser.ns4) this.containerLayer.hidden = true;
  if (self.browser.dom || self.browser.ie4){
    this.containerLayer.style.visibility='hidden';
    this.containerLayer.style.display='none';
  }
}


Calendar.prototype.setDocType = function(docType) {
  if (((typeof validPCTDates == "undefined") && (typeof validPCTI2CDates == "undefined")) || (!(validPCTDates || validPCTI2CDates))) {
    return;
  }
  var tmp;
  this.docType = docType;

  if (this.docType == 0) {
    tmp = ''+validPCTDates[validPCTDates.length-1];
  }
  else {
    tmp = ''+validPCTI2CDates[validPCTI2CDates.length-1];
  }
  this.dateFrom = new Date(tmp.substr(0,4), tmp.substr(4,2), tmp.substr(6,2));
  if (this.docType == 0) {
    tmp = ''+validPCTDates[0];
  }
  else {
    tmp = ''+validPCTI2CDates[0];
  }
  this.dateTo = new Date(tmp.substr(0,4), tmp.substr(4,2), tmp.substr(6,2));
  this.dateFromDay = padZero(this.dateFrom.getDate());
  this.dateFromMonth = padZero(this.dateFrom.getMonth());
  this.dateFromYear = this.dateFrom.getFullYear();  
  this.dateToDay = padZero(this.dateTo.getDate());
  this.dateToMonth = padZero(this.dateTo.getMonth());
  this.dateToYear = this.dateTo.getFullYear();
  //if (this.target.value) {
  if (0) {
    tmp = ''+this.target.value.substr(7,4)+this.target.value.substr(4,2)+this.target.value.substr(0,2);
    if (checkDate(tmp) < 0) {
      this.selectDate(0);
    }
  }
  else {
    this.selectDate(0);
  }
}

Calendar.prototype.selectDate = function(index, display, hidden) {
  if (this.checkValidDates == false) {
    return;
  }
  var tmp;
  if (display) {
    this.target = display;
  }
  tmp = eval('self.document.'+this.target);
  if (!tmp) {
    return;
  }
  if (debugFlag == 1) {
    alert("target: "+this.target+" is: "+tmp);
  }
  var tempStr;
  if (this.docType == 0) {
    tempStr = ''+self.validPCTDates[index];
  }
  else {
    tempStr = ''+self.validPCTI2CDates[index];
  }
  var dateStr = tempStr.substr(6,2)+'/'+tempStr.substr(4,2)+'/'+tempStr.substr(0,4);
  tmp.value = dateStr;
  tmp.value = dateStr;
  if (this.target2) {
    if (hidden) {
      this.target2 = hidden;
    }
    tmp = eval('document.'+this.target2);
    if (this.docType == 0) {
      if (self.validPCTWeeks) {
	tmp.value = self.validPCTWeeks[0];
      }
    }
    else {
      if (self.validPCTI2CWeeks) {
	tmp.value = self.validPCTI2CWeeks[0];
      }
    }
  }
}

function handleDocumentClick(e) {
  if (self.browser.ie4 || self.browser.ie5) e = window.event;
  
  if (self.browser.ns6){
    if (g_Calendar.containerLayer) {
      var bTest = (e.pageX > parseInt(g_Calendar.containerLayer.style.left,10) && e.pageX <  (parseInt(g_Calendar.containerLayer.style.left,10)+125) && e.pageY < (parseInt(g_Calendar.containerLayer.style.top,10)+125) && e.pageY > parseInt(g_Calendar.containerLayer.style.top,10));
      if (e.target.name!='imgCalendar' && e.target.name!='month'  && e.target.name!='year' && e.target.name!='calendar' && !bTest){
        g_Calendar.hide(); 
      }
    }
  }
  if (self.browser.ie4 || self.browser.ie5){
    // extra test to see if user clicked inside the calendar but not on a valid date, we don't want it to disappear in this case
    var bTest = (e.x > parseInt(g_Calendar.containerLayer.style.left,10) && e.x <  (parseInt(g_Calendar.containerLayer.style.left,10)+125) && e.y < (parseInt(g_Calendar.containerLayer.style.top,10)+125) && e.y > parseInt(g_Calendar.containerLayer.style.top,10));
    if (e.srcElement.name!='imgCalendar' && e.srcElement.name!='month' && e.srcElement.name!='year' && !bTest & typeof(e.srcElement)!='object'){
      g_Calendar.hide(); 
    }
  }
  if (self.browser.ns4) g_Calendar.hide();
}

// utility function
function padZero(num) {
  return ((num <= 9) ? ("0" + num) : num);
}
// javascript does not seem to allow you to inherit from the native date object
// so here is my makeshift extension of Date();
function BetterDate(year,month,day){
  this.date = (arguments.length == 3)?new Date(year,month,day):new Date();
}
BetterDate.isLeapYear = function(year){ if (year%4==0 && ((year%100!=0) || (year%400==0))) return true; else return false; }
BetterDate.daysInYear = function(year){ if (BetterDate.isLeapYear(year)) return 366; else return 365;}
BetterDate.prototype.addDays = function(numDays){
  // body of this code is adapted from http://tech.irt.org/articles/js052/index.htm 
  var accumulate    = new Array(0,31, 59, 90,120,151,181,212,243,273,304,334);
  var accumulateLY  = new Array(0,31, 60, 91,121,152,182,213,244,274,305,335);
  var year = this.date.getFullYear();
  var month = this.date.getMonth();
  var day = this.date.getDate();
  if (BetterDate.isLeapYear(year)) var number = day + accumulateLY[month] + numDays;
  else var number = day + accumulate[month]   + numDays;
  var days = BetterDate.daysInYear(year);
  while (number > days) {
    number -= days;
    days = BetterDate.daysInYear(++year);
  }
  while (number < 1) {
    days = BetterDate.daysInYear(--year);
    number += days;
  }
  month = 0;
  if (BetterDate.isLeapYear(year)) {
    while (number > accumulateLY[month]) { month++; }
    day = number - accumulateLY[--month];
  }
  else {
    while (number > accumulate[month]) { month++; }
    day = number - accumulate[--month];
  }
  return new Date(year,month,day);
}		/* end of adaptation */

// events capturing, careful you don't override this by setting something in the onload event of 
// the body tag
function showCalendar(event, target, bHasDropDown, dateFormat, dateFrom, dateTo, target2) {
  if (!g_Calendar) {
    new Calendar(new Date());
  }
  g_Calendar.show(event, target, target2, bHasDropDown, dateFormat, "ENG", dateFrom, dateTo);
}

window.onload=function(){initialize(self.displayField, self.hiddenField);};
if (self.browser.ns4) window.onresize = function(){self.document.location.reload(false);} // ns4 resize bug workaround
window.document.onclick=handleDocumentClick;
//window.onerror = function(msg,url,line){
//  alert('******* an error has occurred ********' +
//	'\n\nPlease check that' + 
	//'\n\n1)You have not added any code to the body onload event,'
	//+  '\nif you want to run something as well as the calendar initialisation'
	//+ '\ncode, add it to the onload event in the calendar library.'
	//+ '\n\n2)You have set the parameters correctly in the g_Calendar.show() method '
	//+ '\n\nSee www.totallysmartit.com\\examples\\calendar\\simple.asp for examples'
	//+ '\n\n------------------------------------------------------'
	//+ '\nError details'
	//+ '\nText:' + msg + '\nurl:' + url + '\nline:' + line);
//}

