// glue code to adapt jscalendar the way we want ;jw

function selectValue(select, value) {
	select = document.getElementsByName(select)[0];
    select.value = value;
    if (select.value != value) {
        for (var i = 0; i < select.options.length; ++i) {
            if (select.options[i].value == value) {
                select.selectedIndex = i;
                return true;
            }
        }
    }
    return false;
}

function getValue(select) {
	select = document.getElementsByName(select)[0];
	var val = select.value;
	if (val==null) {
		val = select.options[select.selectedIndex].value;
	}
    return val;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
	var fieldName = cal.fieldName;
    selectValue(fieldName + ".year", cal.date.getFullYear());
    selectValue(fieldName + ".month", cal.date.getMonth()+1);
    selectValue(fieldName + ".day", cal.date.getDate());
    if (cal.fieldName2!=null && getValue(cal.fieldName2 + ".year")=="%!NOCHOOSE!%") {
        selectValue(cal.fieldName2 + ".year", cal.date.getFullYear());
        selectValue(cal.fieldName2 + ".month", cal.date.getMonth()+1);
        selectValue(cal.fieldName2 + ".day", cal.date.getDate());    	
    }
    // if we add this call we close the calendar on single-click.
	// instead of double-click
    if (cal.dateClicked) {
    	cal.callCloseHandler();
    }
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

function showCalendar(idImg, fieldName, fieldName2) {
  var el = document.getElementById(idImg);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // hide the week numbers
    cal.weekNumbers = false;
    cal.showsOtherMonths = true;
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.  
    cal.create();
  }
  //set the specified date format. not used, since we use the date object directly
  _dynarch_popupCalendar.setDateFormat("%Y-%m-%d");
  // set start date
  var now = new Date();
  var year = getValue(fieldName+".year");
  var month = getValue(fieldName+".month");
  var day = getValue(fieldName+".day");
  // fix for konqueror which displays NaNs in the date popup if we
  // give an invalid date ;jw
  if (year=="%!NOCHOOSE!%") {
	  year = now.getFullYear();
  }
  if (month=="%!NOCHOOSE!%") {
	  month = now.getMonth()+1;
  }
  if (day=="%!NOCHOOSE!%") {
	  day = now.getDate();
  }
  _dynarch_popupCalendar.setDate(new Date(
		  year, 
		  month-1,
		  day));

  _dynarch_popupCalendar.fieldName = fieldName;                 // inform it what input field we use
  _dynarch_popupCalendar.fieldName2 = fieldName2;                 // inform it what input field we use
  
  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el, "Br");        // show the calendar
  return false;
}

