/**
*
* START OF TRIPTIC FUNCTIONS
* IMPORTANT: months are from 1-12 while, in dynCalendar and other js, months are from 0-11
*
*/
var formFieldTimes = new Array(); //added by tt

function timeFieldZero(t)
{
    return (t<10?'0':'')+t;
} //function timeFieldZero

function timeFieldOnChange(obj, name, type)
{
    sels = new Array();
    if (obj.date) {
        sels['d'] = document.getElementById(name + '[d]');
        sels['m'] = document.getElementById(name + '[m]');
        sels['y'] = document.getElementById(name + '[y]');
    }
    if (obj.time) {
        sels['h'] = document.getElementById(name + '[h]');
        sels['i'] = document.getElementById(name + '[i]');
        if (obj.seconds) {
            sels['s'] = document.getElementById(name + '[s]');
        }
    }
    if (formFieldTimes[name].isEmpty) {
        a = ['d','m','y','h','i','s'];
        for (t in a) {
            if (sels[a[t]]) {
                sel = sels[a[t]];
                n = sel.options.length;
                sv = sel.options[sel.options.selectedIndex].value;
                if (type!='y') {
                    if (type!='m') {
                        if (type!='d') {
                            if (type!='h') {
                                if (type!='i' && a[t]=='i') {
                                    sv = obj.defaultMinute;
                                }
                                if (a[t]=='h') {
                                    sv = obj.defaultHour;
                                }
                            }
                            if (a[t]=='d') {
                                sv = obj.defaultDay;
                            }
                        }
                        if (a[t]=='m') {
                            sv = obj.defaultMonth;
                        }
                    }
                    if (a[t]=='y') {
                        sv = obj.defaultYear;
                    }
                }
                for (i=0; i<n-1; i++) {
                    val = sel.options[i+1].value;
                    sel.options[i] = new Option(timeFieldZero(val), val, sv==val,sv==val);
                    if (sv==val) {
                        sel.selectedIndex = i;
                    }
                }
                sel.options[n-1] = null;
            }
        }
        formFieldTimes[name].isEmpty = false;
    }
    if (obj.date && obj.days) {
        day   = Number(sels['d'][sels['d'].selectedIndex].value);
        month = Number(sels['m'][sels['m'].selectedIndex].value);
        year  = Number(sels['y'][sels['y'].selectedIndex].value);
        daysInMonth = new Array(31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
        maxDay = daysInMonth[month];
        currentDays = sels['d'].options[sels['d'].options.selectedIndex].value;
        if (month==2 && (year%400==0 || (year%4==0 && year%100!=0))) { //jumpyear
            maxDay = 29;
        }
        if (sels['d'].selectedIndex>maxDay-1) {
            sels['d'].selectedIndex = maxDay-1;
        }
        n = sels['d'].options.length;
        for (i=0; i<n-maxDay; i++) {
            sels['d'].options[maxDay] = null;
        }
        for (i=n; i<maxDay; i++) {
            sels['d'].options[i] = new Option(i+1,i+1);
        }
    }
        /*if (!month) {
            obj.calendar.setCurrentMonth(curDate.getMonth());
        } else {
            obj.calendar.setCurrentMonth(month-1);
        }
        if (!year) {
            obj.calendar.setCurrentYear(curDate.getFullYear());
        } else {
            obj.calendar.setCurrentYear(year);
        }*/

} //function timeFieldOnChange

function timeFieldEmpty(name)
{
    if (!formFieldTimes[name].isEmpty) {
        a = ['d','m','y','h','i','s'];
        for (t in a) {
            sel = document.getElementById(name + '['+a[t]+']');
            if (sel) {
                n = sel.options.length;
                for (i=n; i>0; i--) {
                    sel.options[i] = new Option(timeFieldZero(sel.options[i-1].value), sel.options[i-1].value);
                }
                sel.options[0] = new Option('','',true,true);
            }
        }
    }
    formFieldTimes[name].isEmpty = true;
    return false;
} //function timeFieldEmpty

function dateSelectFill(name, day, month, year, withTime)
{
    daysInMonth = new Array(31,31,28,31,30,31,30,31,31,30,31,30,31); // 0 => 31 when there is no month selected
    selDay   = document.getElementById(name + '[d]');
    selMonth = document.getElementById(name + '[m]');
    selYear  = document.getElementById(name + '[y]');
    // empty all
    for (i=0; i < 12; i++) {
        selYear[0] = null;
    }
    for (i=0; i < 32; i++) {
        selDay[0] = null;
    }
    // fill years
     startYear = year - 6;
    if (startYear < 0) {
        curDate  = new Date();
        startYear = curDate.getFullYear() - 6;
    }
    for (i=1; i<12; i++) {
        value = startYear+i;
        selYear.options[selYear.length] = new Option(value,value);
    }
    // omstreeks het jaar 6 gaat dit fout, maar ja boeijuh!
    selYear.selectedIndex = (year ? 6 : 0);
    // fill months
    if (selMonth.length<3) {
        for (i=1; i<=12; i++) {
            value = (i<10?'0':'')+i;
            selMonth[selMonth.length] = new Option(value, value);
        }
    }
    selMonth.selectedIndex = month;
    for (i=1; i <= daysInMonth[month]; i++) {
        value = (i<10?'0':'')+i;
        selDay[selDay.length] = new Option(value,value);
    }
    maxday = daysInMonth[month];
    if (month==2 && (year%400==0 || (year%4==0 && year%100!=0))
    ) { //jumpyear
        maxday = 29;
        selDay[selDay.length] = new Option(29,29);
    }
    if (day>maxday) {
        day = maxday;
    }
    selDay.selectedIndex = day;
    if (withTime) {
        selHour    = document.getElementById(name + '[h]');
        if (!selHour.selectedIndex && day) { //date is set but time is not!
            selMinute  = document.getElementById(name + '[i]');
            selSecond  = document.getElementById(name + '[s]');
            curDate = new Date();
            selHour.selectedIndex   = curDate.getHours()    +1;
            selMinute.selectedIndex = curDate.getMinutes()  +1;
            selSecond.selectedIndex = curDate.getSeconds()  +1;
        }
    }
} //function dateSelectFill

/**
*
* START OF DYNCALENDER CODE
*
* Global variables
*/

  // EDITED! Original dynCalendar used a browserSniffer.js of 10kb, I ripped out 90% of the code.
  // Functionality however should still be the same.
  var agt=navigator.userAgent.toLowerCase();
  var is_nav6up = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                    && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                    && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1)
                    && (parseInt(navigator.appVersion) >= 5));
  var is_gecko = (agt.indexOf('gecko') != -1);
  var is_ie5up  = ((parseInt(navigator.appVersion) >= 4) && (agt.indexOf("msie 4")==-1));

  dynCalendar_layers          = new Array();
  dynCalendar_mouseoverStatus = false;
  dynCalendar_mouseX          = 0;
  dynCalendar_mouseY          = 0;

/**
* The calendar constructor
*
* @access public
* @param string objName      Name of the object that you create
* @param string callbackFunc Name of the callback function
* @param string OPTIONAL     Optional layer name
* @param string OPTIONAL     Optional images path
*/
  function dynCalendar(objName, objName2, withTime)
  {
    /**
        * Properties
        */
    // Todays date
    this.today          = new Date();
    this.date           = this.today.getDate();
    this.month          = this.today.getMonth();
    this.year           = this.today.getFullYear();
    this.withTime       = withTime;

    this.objName        = objName;
    this.objName2       = objName2;
    this.callbackFunc   = 'dateSelectFill';
    this.imagesPath     = 'images/';
    this.layerID        = arguments[2] ? arguments[2] : 'dynCalendar_layer_' + dynCalendar_layers.length;

    this.offsetX        = 5;
    this.offsetY        = 5;

    this.useMonthCombo  = true;
    this.useYearCombo   = true;
    this.yearComboRange = 5;

    this.currentMonth   = this.month;
    this.currentYear    = this.year;

    /**
        * Public Methods
        */
    this.show              = dynCalendar_show;
    this.writeHTML         = dynCalendar_writeHTML;

    // Accessor methods
    this.setOffset         = dynCalendar_setOffset;
    this.setOffsetX        = dynCalendar_setOffsetX;
    this.setOffsetY        = dynCalendar_setOffsetY;
    this.setImagesPath     = dynCalendar_setImagesPath;
    this.setMonthCombo     = dynCalendar_setMonthCombo;
    this.setYearCombo      = dynCalendar_setYearCombo;
    this.setCurrentMonth   = dynCalendar_setCurrentMonth;
    this.setCurrentYear    = dynCalendar_setCurrentYear;
    this.setYearComboRange = dynCalendar_setYearComboRange;

    /**
        * Private methods
        */
    // Layer manipulation
    this._getLayer         = dynCalendar_getLayer;
    this._hideLayer        = dynCalendar_hideLayer;
    this._showLayer        = dynCalendar_showLayer;
    this._setLayerPosition = dynCalendar_setLayerPosition;
    this._setHTML          = dynCalendar_setHTML;

    // Miscellaneous
    this._getDaysInMonth   = dynCalendar_getDaysInMonth;
    this._mouseover        = dynCalendar_mouseover;

    /**
        * Constructor type code
        */
    dynCalendar_layers[dynCalendar_layers.length] = this;
    this.writeHTML();
  }

/**
* Shows the calendar, or updates the layer if
* already visible.
*
* @access public
* @param integer month Optional month number (0-11)
* @param integer year  Optional year (YYYY format)
*/
  function dynCalendar_show()
  {
    // Variable declarations to prevent globalisation
    var month, year, monthnames, numdays, thisMonth, firstOfMonth;
    var ret, row, i, cssClass, linkHTML, previousMonth, previousYear;
    var nextMonth, nextYear, prevImgHTML, prevLinkHTML, nextImgHTML, nextLinkHTML;
    var monthComboOptions, monthCombo, yearComboOptions, yearCombo, html;

    this.currentMonth = month = arguments[0] != null ? arguments[0] : this.currentMonth;
    this.currentYear  = year  = arguments[1] != null ? arguments[1] : this.currentYear;

    monthnames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    numdays    = this._getDaysInMonth(month, year);

    thisMonth    = new Date(year, month, 1);
    firstOfMonth = thisMonth.getDay();

    // First few blanks up to first day
    ret = new Array(new Array());
    for(i=0; i<firstOfMonth; i++){
      ret[0][ret[0].length] = '<td class="dynCalendar_blank">&nbsp;</td>';
    }

    // Main body of calendar
    row = 0;
    i   = 1;
    while(i <= numdays){
      if(ret[row].length == 7){
        ret[++row] = new Array();
      }

      /**
            * Generate this cells' HTML
            */
      cssClass = (i == this.date && month == this.month && year == this.year) ? 'dynCalendar_today' : 'dynCalendar_day';
      linkHTML = '<a class="dynCalendar_link" href="javascript: ' + this.callbackFunc + '(\'' + this.objName2 + '\',' + i + ', ' + (Number(month) + 1) + ', ' + year + ',' +this.withTime+'); ' + this.objName + '._hideLayer()">' + (i++) + '</a>';
      ret[row][ret[row].length] = '<td align="center" class="' + cssClass + '">' + linkHTML + '</td>';
    }
    // last few blanks
    while (ret[row].length < 7 ) {
      ret[row][ret[row].length] = '<td class="dynCalendar_blank">&nbsp;</td>';
    }

    // Format the HTML
    for(i=0; i<ret.length; i++){
      ret[i] = ret[i].join('\n') + '\n';
    }

    previousYear  = thisMonth.getFullYear();
    previousMonth = thisMonth.getMonth() - 1;
    if(previousMonth < 0){
      previousMonth = 11;
      previousYear--;
    }

    nextYear  = thisMonth.getFullYear();
    nextMonth = thisMonth.getMonth() + 1;
    if(nextMonth > 11){
      nextMonth = 0;
      nextYear++;
    }

    prevImgHTML  = '<img src="' + this.imagesPath + 'prev.gif" alt="<<" border="0" />';
    prevLinkHTML = '&nbsp;<a href="javascript: ' + this.objName + '.show(' + previousMonth + ', ' + previousYear + ')">' + prevImgHTML + '</a>&nbsp;';
    nextImgHTML  = '<img src="' + this.imagesPath + 'next.gif" alt="<<" border="0" />';
    nextLinkHTML = '&nbsp;<a href="javascript: ' + this.objName + '.show(' + nextMonth + ', ' + nextYear + ')">' + nextImgHTML + '</a>&nbsp;';

    /**
        * Build month combo
        */
    if (this.useMonthCombo) {
      monthComboOptions = '';
      for (i=0; i<12; i++) {
        selected = (i == thisMonth.getMonth() ? 'selected="selected"' : '');
        monthComboOptions += '<option value="' + i + '" ' + selected + '>' + monthnames[i] + '</option>';
      }
      monthCombo = '<select name="months" onchange="' + this.objName + '.show(this.options[this.selectedIndex].value, ' + this.objName + '.currentYear)">' + monthComboOptions + '</select>';
    } else {
      monthCombo = monthnames[thisMonth.getMonth()];
    }

    /**
        * Build year combo
        */
    if (this.useYearCombo) {
      yearComboOptions = '';
      for (i = thisMonth.getFullYear() - this.yearComboRange; i <= (thisMonth.getFullYear() + this.yearComboRange); i++) {
        selected = (i == thisMonth.getFullYear() ? 'selected="selected"' : '');
        yearComboOptions += '<option value="' + i + '" ' + selected + '>' + i + '</option>';
      }
      yearCombo = '<select style="border: 1px groove" name="years" onchange="' + this.objName + '.show(' + this.objName + '.currentMonth, this.options[this.selectedIndex].value)">' + yearComboOptions + '</select>';
    } else {
      yearCombo = thisMonth.getFullYear();
    }

    html = '<table class="dynCalendar_table" cellspacing="1" cellpadding="1">';
    html += '<tr class="dynCalendar_header"><td colspan="7">' + prevLinkHTML +  monthCombo + '&nbsp;' + yearCombo + nextLinkHTML + '</td></tr>';
    html += '<tr class="dynCalendar_dayname">';
    html += '<td>Su</td>';
    html += '<td>Mo</td>';
    html += '<td>Tu</td>';
    html += '<td>We</td>';
    html += '<td>Th</td>';
    html += '<td>Fr</td>';
    html += '<td>Sa</td></tr>';
    html += '<tr>' + ret.join('</tr>\n<tr>') + '</tr>';
    html += '</table>';

    this._setHTML(html);
    if (!arguments[0] && !arguments[1]) {
      this._showLayer();
      this._setLayerPosition();
    }
  }

/**
* Writes HTML to document for layer
*
* @access public
*/
  function dynCalendar_writeHTML()
  {
    if (is_ie5up || is_nav6up || is_gecko) {
      document.write('<a href="javascript: ' + this.objName + '.show()"><img src="' + this.imagesPath + 'dyncalendar.gif" style="vertical-align:middle" border="0" width="16" height="16" /></a>');
      document.write('<div class="dynCalendar" id="' + this.layerID + '" onmouseover="' + this.objName + '._mouseover(true)" onmouseout="' + this.objName + '._mouseover(false)"></div>');
    }
  }

/**
* Sets the offset to the mouse position
* that the calendar appears at.
*
* @access public
* @param integer Xoffset Number of pixels for vertical
*                        offset from mouse position
* @param integer Yoffset Number of pixels for horizontal
*                        offset from mouse position
*/
  function dynCalendar_setOffset(Xoffset, Yoffset)
  {
    this.setOffsetX(Xoffset);
    this.setOffsetY(Yoffset);
  }

/**
* Sets the X offset to the mouse position
* that the calendar appears at.
*
* @access public
* @param integer Xoffset Number of pixels for horizontal
*                        offset from mouse position
*/
  function dynCalendar_setOffsetX(Xoffset)
  {
    this.offsetX = Xoffset;
  }

/**
* Sets the Y offset to the mouse position
* that the calendar appears at.
*
* @access public
* @param integer Yoffset Number of pixels for vertical
*                        offset from mouse position
*/
  function dynCalendar_setOffsetY(Yoffset)
  {
    this.offsetY = Yoffset;
  }

/**
* Sets the images path
*
* @access public
* @param string path Path to use for images
*/
  function dynCalendar_setImagesPath(path)
  {
    this.imagesPath = path;
  }

/**
* Turns on/off the month dropdown
*
* @access public
* @param boolean useMonthCombo Whether to use month dropdown or not
*/
  function dynCalendar_setMonthCombo(useMonthCombo)
  {
    this.useMonthCombo = useMonthCombo;
  }

/**
* Turns on/off the year dropdown
*
* @access public
* @param boolean useYearCombo Whether to use year dropdown or not
*/
  function dynCalendar_setYearCombo(useYearCombo)
  {
    this.useYearCombo = useYearCombo;
  }

/**
* Sets the current month being displayed
*
* @access public
* @param boolean month The month to set the current month to
*/
  function dynCalendar_setCurrentMonth(month)
  {
    this.currentMonth = month;
  }

/**
* Sets the current month being displayed
*
* @access public
* @param boolean year The year to set the current year to
*/
  function dynCalendar_setCurrentYear(year)
  {
    this.currentYear = year;
  }

/**
* Sets the range of the year combo. Displays this number of
* years either side of the year being displayed.
*
* @access public
* @param integer range The range to set
*/
  function dynCalendar_setYearComboRange(range)
  {
    this.yearComboRange = range;
  }

/**
* Returns the layer object
*
* @access private
*/
  function dynCalendar_getLayer()
  {
    var layerID = this.layerID;

    if (document.getElementById(layerID)) {

      return document.getElementById(layerID);

    } else if (document.all(layerID)) {
      return document.all(layerID);
    }
  }

/**
* Hides the calendar layer
*
* @access private
*/
  function dynCalendar_hideLayer()
  {
    this._getLayer().style.visibility = 'hidden';
  }

/**
* Shows the calendar layer
*
* @access private
*/
  function dynCalendar_showLayer()
  {
    this._getLayer().style.visibility = 'visible';
  }

/**
* Sets the layers position
*
* @access private
*/
  function dynCalendar_setLayerPosition()
  {
    this._getLayer().style.top  = (dynCalendar_mouseY + this.offsetY) + 'px';
    this._getLayer().style.left = (dynCalendar_mouseX + this.offsetX) + 'px';
  }

/**
* Sets the innerHTML attribute of the layer
*
* @access private
*/
  function dynCalendar_setHTML(html)
  {
    this._getLayer().innerHTML = html;
  }

/**
* Returns number of days in the supplied month
*
* @access private
* @param integer month The month to get number of days in
* @param integer year  The year of the month in question
*/
  function dynCalendar_getDaysInMonth(month, year)
  {
    monthdays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (month != 1) {
      return monthdays[month];
    } else {
      return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 29 : 28);
    }
  }

/**
* onMouse(Over|Out) event handler
*
* @access private
* @param boolean status Whether the mouse is over the
*                       calendar or not
*/
  function dynCalendar_mouseover(status)
  {
    dynCalendar_mouseoverStatus = status;
    return true;
  }