/**
 * @author Ricardo Gomes
 * Bacharel in Computer Science
 * This script can be used for personal or comercial purpose.
 */
/*
 To call the calendar use the function: calendar(DATE) where date is a variable that
 contains your date. It must be a variable like [var time = new Date()].
 Only change the codes where the coment indicate to change.
 Load the calendar.js in your page and the calendar.css too.
 */
var calendar_data = new Date();
var sundays = 0;
var month=new Array(12);
month[0]="Jan";
month[1]="Feb";
month[2]="Mar";
month[3]="Apr";
month[4]="May";
month[5]="Jun";
month[6]="Jul";
month[7]="Aug";
month[8]="Sep";
month[9]="Oct";
month[10]="Nov";
month[11]="Dec";

//change those values to alter the style of the buttons and date when mouse over
var $overCellBackground = '#31516b';
var $overCellFontColor = '#FFFFFF';
var $overCellMouse = 'pointer';
var $outCellBackground = '#f7efef';
var $outCellFontColor = '#000000';
var $outCellFontColorSunday = '#FF0000';
var $overButtonBackground = '#31516b';
var $overButtonFontColor = '#FFFFFF';
var $overButtonMouse = 'pointer';
var $outButtonBackground = '#f7efef';
var $outButtonFontColor = '#000000';
var calendarToday_html = "<b>";//normal html code, usually open tags
var calendarToday_htmlClose = "</b>";//normal html code, usually close tags

/*
this function (update) is for compatibility purpose,
change the content to better update your page.
@ Remember to call the function that write the calendar in your page
*/
function update(val){
	var time = new Date(calendar_data.getTime()+(calendar_data.getTimezoneOffset() * 60000));//convert timezone
	dia = time.getDate()<10?"0"+time.getDate():time.getDate();
	mes = (time.getMonth()+1)<10?"0"+(time.getMonth()+1):(time.getMonth()+1);
	ano = time.getFullYear();	
	if(val == 0) objeto.value = dia+"/"+mes+"/"+ano;
	calendario(time,2,val,objeto);
}

function overCell(celula){
   celula.style.backgroundColor = $overCellBackground;
	 celula.style.color = $overCellFontColor;
	 celula.style.cursor = $overCellMouse;
}

function outCell(celula,outDay){
	 celula.style.backgroundColor = $outCellBackground;
	 if(outDay == 0) celula.style.color = $outCellFontColorSunday;			 
	 else celula.style.color = $outCellFontColor;			 
}

function overButton(celula){
   celula.style.backgroundColor = $overButtonBackground;
	 celula.style.color = $overButtonFontColor;
	 celula.style.cursor = $overButtonMouse;
}

function outButton(celula){
   celula.style.backgroundColor = $outButtonBackground;
	 celula.style.color = $outButtonFontColor;			 
}			

function yearCalendar(yc) {
	calendar_data.setFullYear(calendar_data.getFullYear()+yc);
	update(1);
}

function monthCalendar(mc) {
	calendar_data.setMonth(calendar_data.getMonth()+mc);
	update(1);
}

function clickCalendar(dC){
	calendar_data.setDate(dC);
	update(0);
}

function closeCalendar() {
	calendario("",2,0,objeto);
}

function calendar(calendarData,obj){
	var calendar_html="";
	var calendar_temp = new Date();
	sundays = 0;
	calendar_data = new Date(calendarData.getTime());
	calendar_temp.setDate(calendarData.getDate());
	calendar_temp.setMonth(calendarData.getMonth());
	calendar_temp.setFullYear(calendarData.getFullYear());
	calendar_temp.setHours(calendarData.getHours());
	calendar_temp.setMinutes(calendarData.getMinutes());
	calendar_temp.setSeconds(calendarData.getSeconds());
	calendar_html += '<div id="calendar">';
	calendar_html += "<table id='calendarTable'>";
	calendar_html += "<tr id='calendarTitle'><td colspan='6' >"+calendar_temp.toDateString()+"</td><td onclick=\"closeCalendar();\" onmouseover=\"overButton(this)\">X</td></tr>";
	cDate = calendar_temp.getDate();
	calendar_temp.setDate(1);
	cMonth = calendar_temp.getMonth();
	cDay = calendar_temp.getDay();
	calendar_html += "<tr id='calendarButtonLine'><td class='calendarButton' id='<<'  onmouseover='overButton(this)' onmouseout='outButton(this)' onclick='yearCalendar(-1)'><<</td><td class='calendarButton' id='<'  onmouseover='overButton(this)' onmouseout='outButton(this)' onclick='monthCalendar(-1)'><</td>";
	calendar_html += "<td colspan='3'><b>"+month[cMonth]+"</b></td>";
	calendar_html += "<td class='calendarButton' id='>'  onmouseover='overButton(this)' onmouseout='outButton(this)' onclick='monthCalendar(1)'>></td><td class='calendarButton' id='>>'  onmouseover='overButton(this)' onmouseout='outButton(this)' onclick='yearCalendar(1)'>>></td></tr>";
	calendar_html+="<tr id='calendarWeek'><td class='calendarWeekSunday'>Su</td><td>Mo</td><td>Tu</td><td>We</td><td>Th</td><td>Fr</td><td>Sa</td></tr>";
	if (cDay != 0) {
  	calendar_html += "<tr>";
  	sundays++;
  }
	if (cDay >= 1) calendar_html+="<td class='calendarEmpty'></td>";
	if (cDay >= 2) calendar_html+="<td class='calendarEmpty'></td>";
	if (cDay >= 3) calendar_html+="<td class='calendarEmpty'></td>";
	if (cDay >= 4) calendar_html+="<td class='calendarEmpty'></td>";		
	if (cDay >= 5) calendar_html+="<td class='calendarEmpty'></td>";
	if (cDay == 6) calendar_html+="<td class='calendarEmpty'></td>";		
	for(;cMonth==calendar_temp.getMonth();) {
		cDay = calendar_temp.getDay();
		if (cDay == 0 && calendar_temp.getDate()!=1) {
  		calendar_html+="</tr><tr>";
		}
		if (cDay == 0) sundays++;
		calendar_html += "<td ";
		if(cDay == 0) calendar_html += "class='calendarWeekSunday' ";
		calendar_html += "id='"+calendar_temp.getDate()+"' onclick=\"clickCalendar("+calendar_temp.getDate()+")\"";
		calendar_html+=" onmouseover='overCell(this)' onmouseout='outCell(this,"+cDay+")'>";
		if (cDate==calendar_temp.getDate()) calendar_html += calendarToday_html; 
		if(calendar_temp.getDate()<10) calendar_html+="0";
		calendar_html+=calendar_temp.getDate();
		if (cDate==calendar_temp.getDate()) calendar_html += calendarToday_htmlClose;
		calendar_html+="</td>";
		calendar_temp.setDate(calendar_temp.getDate()+1);
	}
	if (cDay <= 5) calendar_html+="<td class='calendarEmpty'></td>";
	if (cDay <= 4) calendar_html+="<td class='calendarEmpty'></td>";
	if (cDay <= 3) calendar_html+="<td class='calendarEmpty'></td>";
	if (cDay <= 2) calendar_html+="<td class='calendarEmpty'></td>";		
	if (cDay <= 1) calendar_html+="<td class='calendarEmpty'></td>";
	if (cDay == 0) calendar_html+="<td class='calendarEmpty'></td>";

	calendar_html+="</tr>";
	calendar_html += "</table>";
	calendar_html += "</div>";
	return calendar_html;
}