// Initialize the calendar
calendar=null;

// This function displays the calendar associated to the input field 'id'
function showCalendar(id) {
	var el = document.getElementById(id);
	if (calendar != null) {
		// we already have some calendar created
		calendar.hide();  // so we hide it first.
	} else {
		// first-time call, create the calendar.
		var cal = new Calendar(true, null, selected, closeHandler);
		cal.weekNumbers = false;  // Do not display the week number
		cal.showsTime = true;     // Display the time
		cal.time24 = true;        // Hours have a 24 hours format
		cal.showsOtherMonths = false;    // Just the current month is displayed
		calendar = cal;                  // remember it in the global var
		cal.setRange(1900, 2070);        // min/max year allowed.
		cal.create();
	}
	calendar.setDateFormat('%Y-%m-%d %H:%M');    // set the specified date format
	calendar.parseDate(el.value);                // try to parse the text in field
	calendar.sel = el;                           // inform it what input field we use
	// Display the calendar below the input field
	calendar.showAtElement(el, "Br");        // show the calendar
	return false;
}

// This function update the date in the input field when selected
function selected(cal, date) {
	cal.sel.value = date;      // just update the date in the input field.
}

// This function gets called when the end-user clicks on the 'Close' button.
// It just hides the calendar without destroying it.
function closeHandler(cal) {
	cal.hide();                        // hide the calendar
	calendar = null;
}

// Calendario que muestra solo el mes:

var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var days = new Array("S", "M", "T", "W", "T", "F", "S");
today = new getToday();	
var element_id;


function getDays(month, year) {
	// Test for leap year when February is selected.
	if (1 == month)
		return ((0 == year % 4) && (0 != (year % 100))) ||
			(0 == year % 400) ? 29 : 28;
	else
		return daysInMonth[month];
}

function getToday() {
	// Generate today's date.
	this.now = new Date();
	this.year = this.now.getFullYear() ; // Returned year XXXX
	this.month = this.now.getMonth();
	this.day = this.now.getDate();
}
 
function newCalendar() {
//	var parseYear = parseInt(document.all.yearm[document.all.yearm.selectedIndex].text);
	var parseYear = parseInt(today.year);
	var newCal = new Date(parseYear , document.all.mmonth.selectedIndex, 1);
	var day = -1;
	var startDay = newCal.getDay();
	var daily = 0; 
	today = new getToday(); // 1st call
	if ((today.year == newCal.getFullYear() ) &&   (today.month == newCal.getMonth()))
	   day = today.day;
	var intDaysInMonth = getDays(newCal.getMonth(), newCal.getFullYear() );

}
	  
function getTodayDay() {
	document.all[element_id].value = today.day + "/" + (today.month+1) + "/" + today.year; 
	//document.all.calendar.style.visibility="hidden";
	document.all.calendarm.style.display="none";
	document.all.year.selectedIndex =100;   
	document.all.mmonth.selectedIndex = today.month; 
}
 
function getDate() {
	// This code executes when the user clicks on a day
	// in the calendar.
	if ("TD" == event.srcElement.tagName)
	// Test whether day is valid.
		if ("" != event.srcElement.innerText) { 
			var mn = document.all.mmonth.selectedIndex+1;
    			var Year = document.all.year [document.all.year.selectedIndex].text;
			document.all[element_id].value=event.srcElement.innerText+"/"+mn +"/"  +Year;
		         //document.all.calendar.style.visibility="hidden";
			document.all.calendarm.style.display="none";
		}
}

function setDate() {
	var fecha;
	var Year = document.all.yearm [document.all.yearm.selectedIndex].text;
	var mes = parseInt(document.all.mmonth.selectedIndex) + 1;
	fecha = Year + "-" + mes;
	document.all[element_id].value = fecha;
	HideCalendar();	
}
 
function GetBodyOffsetX(el_name, shift) {
	var x;
	var y;
	x = 0;
	y = 0;

	var elem = document.all[el_name];
	do {
		x += elem.offsetLeft;
		y += elem.offsetTop;
		if (elem.tagName == "BODY")
			break;
		elem = elem.offsetParent; 
	} while  (1 > 0);

	shift[0] = x;
	shift[1] = y;
	return  x;
}	

function SetCalendarOnElement(el_name) {
	if (el_name=="") 
	el_name = element_id;
	var shift = new Array(2);
	GetBodyOffsetX(el_name, shift);
	document.all.calendarm.style.left  = shift[0]; //  - document.all.calendar.offsetLeft;
	document.all.calendarm.style.top = shift[1] + 25;
}

function ShowCalendar(elem_name) {
	if (elem_name=="")
		elem_name = element_id;
	element_id = elem_name; // element_id is global variable
	newCalendar();
	SetCalendarOnElement(element_id);
	//document.all.calendar.style.visibility = "visible";
	document.all.calendarm.style.display="inline";
}

function HideCalendar() {
	//document.all.calendar.style.visibility="hidden";
	document.all.calendarm.style.display="none";
}

function toggleCalendar(elem_name) {
	today = new getToday();	
	var element_id;
//if (document.all.calendar.style.visibility == "hidden")
	if(document.all.calendarm.style.display=="none")
		ShowCalendar(elem_name);
	else 
		HideCalendar();
}

