
function calendarDateChanged_form(calendar) {
    var field_id = calendar.params.button.id.substr(3);
   	var prefix = 'form'+calendar.actionPrefix+'[';
    if (calendar.dateClicked) {
        var lastYear = document.getElementById(prefix+field_id+'_year]').value; // для IE
        document.getElementById(prefix+field_id+'_year]').value = calendar.date.getFullYear();
        document.getElementById(prefix+field_id+'_month]').value = calendar.date.getMonth()+1;     // integer, 0..11
        document.getElementById(prefix+field_id+'_day]').value = calendar.date.getDate();      // integer, 1..31
    }
    // IE
    if (document.getElementById(prefix+field_id+'_year]').value == '') {
        document.getElementById(prefix+field_id+'_year]').value = lastYear;
    }
};


function timestamp_set_value_form(field_name, value) {
	var field_hidden = 'form['+field_name+'_day]_hid';
	var field_day = 'form['+field_name+'_day]';
	var field_month = 'form['+field_name+'_month]';
	var field_year = 'form['+field_name+'_year]';
	var d = new Date();
	d.setTime(value);
	gebi(field_hidden).value = d.print("%Y/%m/%d");
	gebi(field_day).value = d.getDate();
	gebi(field_month).value = d.getMonth()+1;
	gebi(field_year).value = d.getFullYear();
}

function timestamp_roll_form(field_list, period) {
	if (!Array.prototype.isPrototypeOf([field_list])) {
		field_list = [field_list];
	}
	for (var i in field_list) {
		field_name = field_list[i];
		var field_hidden = 'form['+field_name+'_day]_hid';
		var obj = gebi(field_hidden);
		var d = new Date(obj.value);
		timestamp_set_value_form(field_name, d*1.0+period*1000)
	}
}

function timestamp_set_period_form(field_list) {
	for (var i in field_list) {
		timestamp_set_value_form(i, field_list[i]*1000);
	}
}

function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  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);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // 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.nextSibling, "Br");        // show the calendar

  return false;
}

function selected(cal, date) {
  cal.sel.value = date;
//  cal.callCloseHandler();
}

function closeHandler(cal) {
  cal.hide();
  _dynarch_popupCalendar = null;
}

