/**
 * This script will look for buttons with calendar-for attribute set,
 * add a call to show calendar for onclick on those buttons, and bind the textfield with the name
 * the same as the value of the calendar-for.
 *
 * Examples:
 * var USER_DATE_FORMAT = "d-m-y"; //30-08-2005
 * var USER_DATE_DELIMITER = "-";
 */
var ATTRIBUTE_CALENDAR = "calendar-for";
var ATTRIBUTE_TIME_CALENDAR = "time-calendar-for";

/**
 * The function returns a date string in currently defined date format.
 */

function assignCalendars() {
	var allInputs = document.getElementsByTagName("INPUT");
	var calendarButtons = new Array();
	var timeCalendarButtons = new Array();
	var calFieldFormat = "%d-%m-%Y";
	var timeCalFieldFormat = "%d-%m-%Y %H:%M";

	for(var i = 0; i < allInputs.length; ++i) {
		var currentButton = allInputs[i];
		if(currentButton.getAttribute(ATTRIBUTE_CALENDAR)) {
			calendarButtons.push(currentButton);
		}
	}
	
	for(var i = 0; i < allInputs.length; ++i) {
		var currentButton = allInputs[i];
		if(currentButton.getAttribute(ATTRIBUTE_TIME_CALENDAR)) {
			timeCalendarButtons.push(currentButton);
		}
	}
	
	/**
	*here we process fields that must be used only for date enter
	*/
	for(var i = 0; i < calendarButtons.length; ++i) {
		var cButton = calendarButtons[i];
		var cField = document.getElementsByName(cButton.getAttribute(ATTRIBUTE_CALENDAR))[0];
		if(!cField) continue;

		cButton.id = "calendarButton" + i;
		cField.id = "dateField" + i;
		Calendar.setup(
			{
				button: cButton.id,
				inputField: cField.id,
				ifFormat: calFieldFormat,
				align: "Bc",
				cache: true
			}
		);
	}
	
	/**
	*here we process fields that must be used for date and time enter
	*/
	for(var i = 0; i < timeCalendarButtons.length; ++i) {
		var cButton = timeCalendarButtons[i];
		var cField = document.getElementsByName(cButton.getAttribute(ATTRIBUTE_TIME_CALENDAR))[0];
		if(!cField) continue;

		cButton.id = "timeCalendarButton" + i;
		cField.id = "timeDateField" + i;
		Calendar.setup(
			{
				button: cButton.id,
				inputField: cField.id,
				ifFormat: timeCalFieldFormat,
				showsTime : true,
				align: "Bc",
				cache: true
			}
		);
	}
}