
/* Common Scripts */

function getElementsByClass(searchClass,node,tag) {
	//alert("Entrou 2!");
	var classElements = new Array();
	if (node == null) node = document;
	if (tag == null) tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	//alert(elsLen);
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		//alert(els[i].className + " == " + searchClass);
		if (els[i].className == searchClass) {
			classElements[j] = els[i];
			j++;
		}
	}
	//alert(classElements.length);
	return classElements;
}

/* Functions Dealing with Dates */

function formatDate(Month, Day, Year) {
	Month++; // adjust javascript month
	if (Month < 10) Month = '0' + Month; // add a zero if less than 10
	if (Day < 10) Day = '0' + Day; // add a zero if less than 10
	var dateString = Day + '/' + Month + '/' + Year;
	return dateString;
}

function formatDateBR(Day, Month, Year) {
	if (Day < 10) Day = '0' + Day; // add a zero if less than 10
	Month++; // adjust javascript month
	if (Month < 10) Month = '0' + Month; // add a zero if less than 10
	var dateString = Day + '/' + Month + '/' + Year;
	return dateString;
}

function getMonthName(month) {
	var monthNames = new Array('Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro');
	return monthNames[month];
}

function getDayName(day) {
	var dayNames = new Array('Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado')
	return dayNames[day];
}

function getDaysInMonth(year, month) {
	return 32 - new Date(year, month, 32).getDate();
}

function getFirstDayOfMonth(year, month) {
	var day;
	day = new Date(year, month, 0).getDay();
	return day;
}

/* Position Functions */

function setPos(targetObj,moveObj) {
	var coors = findPos(targetObj);
	moveObj.style.position = 'absolute';
	moveObj.style.top = coors[1] + 30 + 'px';
	moveObj.style.left = coors[0] + 'px';
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

var popUpCal = {

	selectedMonth: new Date().getMonth(), // 0-11

	selectedYear: new Date().getFullYear(), // 4-digit year

	selectedDay: new Date().getDate(),

	calendarId: "calendarDiv", //'calendarDiv',

	inputClass: "calendarSelectdate",

	init: function () {
		var x = getElementsByClass(popUpCal.inputClass, document, 'input'); //, 'image');
		var y = document.getElementById('calendarDiv');
		// set the calendar position based on the input position
		//alert("Entrou 1!");
		for (var i = 0; i < x.length; i++) {
			x[i].onfocus = function () {
			//x[i].onclick = function () {
                window.document.getElementById('select_selecionado').style.display = 'none';
				popUpCal.selectedMonth = new Date().getMonth();
				setPos(this, y); // setPos(targetObj,moveObj)
				y.style.display = 'block';
				popUpCal.drawCalendar(this);
				popUpCal.setupLinks(this);
			}
		}
	},

	drawCalendar: function (inputObj) {

		var html = '';
		html = '';
		html += '<div>';
		html += '<table width="100%" align="right" cellpadding="0" cellspacing="0" border="0">';
		html += '<tr>';
		html += '	<td width="100%" align="right"><a id="closeCalendar">Fechar</a></td>';
		html += '</tr>';
		html += '</table>';
		html += '</div>';
        html += '<br>';
        html += '<br>';
		html += '<table id="linksTable" width="100%" cellpadding="0" cellspacing="0" border="0">';
		html += '<tr>';
		html += '	<td><a id="prevMonth"> << Antes</a></td>';
		html += '	<td><a id="nextMonth">Depois >> </a></td>';
		html += '</tr>';
		html += '</table>';
		html += '<table id="calendar" width="100%" cellpadding="0" cellspacing="0">';
		html += '<tr>';
		html += '<th class="calendarHeader" colspan="7">' + getMonthName(popUpCal.selectedMonth) + ' ' + popUpCal.selectedYear + '</th>';
		html += '</tr>';
		html += '<tr class="weekDaysTitleRow">';
		var weekDays = new Array('D','S','T','Q','Q','S','S');
		for (var j = 0; j < weekDays.length; j++) {
			html += '<td>' + weekDays[j] + '</td>';
		}

		var daysInMonth = getDaysInMonth(popUpCal.selectedYear, popUpCal.selectedMonth);
		var startDay = getFirstDayOfMonth(popUpCal.selectedYear, popUpCal.selectedMonth);
		var numRows = 0;
		var printDate = 1;
		if (startDay != 7) {
			numRows = Math.ceil(((startDay + 1) + (daysInMonth)) / 7); // calculate the number of rows to generate
		}

		// calculate number of days before calendar starts
		if (startDay != 7) {
			var noPrintDays = startDay + 1; 
		} else {
			var noPrintDays = 0; // if sunday print right away
		}
		var today = new Date().getDate();
		var thisMonth = new Date().getMonth();
		var thisYear = new Date().getFullYear();

		// create calendar rows
		for (var e = 0; e < numRows; e++) {
			html += '<tr class="weekDaysRow">';

			// create calendar days
			var entireWeek = false;
			for (var f = 0; f < 7; f++) {
				if (noPrintDays == 7) {
					entireWeek = true;
				}
				if (!entireWeek) {
					if ((printDate == today) &&
						(popUpCal.selectedYear == thisYear) &&
						(popUpCal.selectedMonth == thisMonth) &&
						(noPrintDays == 0)) {
						html += '<td id="today" class="weekDaysCell">';
					} else {
						html += '<td class="weekDaysCell">';
					}
					if (noPrintDays == 0) {
						if (printDate <= daysInMonth) {
							html += '<a>' + printDate + '</a>';
						} else {
							html += '&nbsp;';
						}
						printDate++;
					} else {
						html += '&nbsp;';
					}
					html += '</td>';
				}
				if (noPrintDays > 0) noPrintDays--;
				if (noPrintDays == 0) {
					entireWeek = false;
				}
			}
			html += '</tr>';
		}
		html += '</table>';

		// add calendar to element to calendar Div
		var calendarDiv = document.getElementById(popUpCal.calendarId);
		calendarDiv.innerHTML = html;
		calendarDiv.style.width = '185px';

		// close button link
		document.getElementById('closeCalendar').onclick = function () {
			document.getElementById('select_selecionado').style.display = 'block';
            calendarDiv.style.display = 'none';
		}

		// setup next and previous links
		document.getElementById('prevMonth').onclick = function () {
			popUpCal.selectedMonth--;
			if (popUpCal.selectedMonth < 0) {
				popUpCal.selectedMonth = 11;
				popUpCal.selectedYear--;
			}
			popUpCal.drawCalendar(inputObj); 
			popUpCal.setupLinks(inputObj);
		}
		document.getElementById('nextMonth').onclick = function () {
			popUpCal.selectedMonth++;
			if (popUpCal.selectedMonth > 11) {
				popUpCal.selectedMonth = 0;
				popUpCal.selectedYear++;
			}
			popUpCal.drawCalendar(inputObj); 
			popUpCal.setupLinks(inputObj);
		}

		//alert("Entrou 3!");
	}, // end drawCalendar function

	setupLinks: function (inputObj) {
		// set up link events on calendar table
		var y = document.getElementById('calendar');
		var x = y.getElementsByTagName('a');
		for (var i = 0; i < x.length; i++) {
			x[i].onmouseover = function () {
				this.parentNode.className = 'weekDaysCellOver';
			}
			x[i].onmouseout = function () {
				this.parentNode.className = 'weekDaysCell';
			}
			x[i].onclick = function () {
				document.getElementById(popUpCal.calendarId).style.display = 'none';
				//alert(this.innerHTML);
				popUpCal.selectedDay = this.innerHTML;

				selectedDate = formatDateBR(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear);
				//alert(selectedDate);
				//alert(Number(selectedDate.substring(6,10)));
				//alert(Number(selectedDate.substring(3,5)));
				//alert(Number(selectedDate.substring(0,2)));

				todayMonth = new Date().getMonth();
				todayDay = new Date().getDate();
				todayYear = new Date().getFullYear();

				dateToday = formatDateBR(todayDay, todayMonth, todayYear);

				dateOk = false;

				//alert(Number(dateToday.substring(6,10)) + " < " + Number(selectedDate.substring(6,10)));
				if (Number(dateToday.substring(6,10)) < Number(selectedDate.substring(6,10))) {
					dateOk = true;
				} else {
					//alert(Number(dateToday.substring(6,10)) + " == " + Number(selectedDate.substring(6,10)));
					if (Number(dateToday.substring(6,10)) == Number(selectedDate.substring(6,10))) {
						//alert(Number(dateToday.substring(3,5)) + " < " + Number(selectedDate.substring(3,5)));
						if (Number(dateToday.substring(3,5)) < Number(selectedDate.substring(3,5))) {
							dateOk = true;
						} else {
							//alert(Number(dateToday.substring(3,5)) + " == " + Number(selectedDate.substring(3,5)));
							if (Number(dateToday.substring(3,5)) == Number(selectedDate.substring(3,5))) {
								//alert(Number(dateToday.substring(0,2)) + " < " + Number(selectedDate.substring(0,2)));
								if (Number(dateToday.substring(0,2)) <= Number(selectedDate.substring(0,2))) {
									dateOk = true;
								}
							}
						}
					}
				}

				dateOk = true;

				if (dateOk) {
					dateString = formatDateBR(popUpCal.selectedDay, popUpCal.selectedMonth, popUpCal.selectedYear);
					inputObj.value = dateString;
				} else {
					alert("Apenas datas a partir do dia corrente são permitidas neste campo!");
				}

			}
		}
	}
}



	if (typeof window.addEventListener != "undefined") {
		window.addEventListener("load", popUpCal.init, false);
	} else if (typeof window.attachEvent != "undefined") {
		window.attachEvent("onload", popUpCal.init);
	} else {
		if (window.onload != null) {
			var oldOnload = window.onload;
			window.onload = function (e) {
				oldOnload(e);
				popUpCal.init();
			};
		} else {
			window.onload = popUpCal.init;
		}
	}


