var day;
var month;
var year;
var X_;
var Y_;
var month_names = new Array('Janeiro','Fevereiro','Mar&ccedil;o','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro');
var day_names = new Array('Dom','Seg','Ter','Qua','Qui','Sex','Sab');

/**
 * Formats number to two digits.
 *
 * @param   int number to format.
 */
function formatNum2(i, valtype) {
    f = (i < 10 ? '0' : '') + i;
    if (valtype && valtype != '') {
        switch(valtype) {
            case 'month':
                f = (f > 12 ? 12 : f);
                break;

            case 'day':
                f = (f > 31 ? 31 : f);
                break;

            case 'hour':
                f = (f > 24 ? 24 : f);
                break;

            default:
            case 'second':
            case 'minute':
                f = (f > 59 ? 59 : f);
                break;
        }
    }

    return f;
}

/**
 * Formats number to four digits.
 *
 * @param   int number to format.
 */
function formatNum4(i)
{
    return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
}

/**
 * Initializes calendar window.
 */
function initCalendar(x,y,form,Editor,foco,ler)
{  
	try
	{
		Area="_DivCalendario_";
		var texto=  SelectObjetoForm(form,Editor);
		var div= document.getElementById(Area);
		if((typeof(div)== "undefined") || (div == null))
		{
			var oDoc=document;
			if(!oDoc.body)
				return false;
			mess='<div class="Calendario" id="_DivCalendario_" style="display:block"></div>';
			oDoc.body.innerHTML+= mess ;
			
			div= document.getElementById(Area);
		}
		div.style.display='block';
		if(ler)
		{
			div.style.top=y+"px";
			div.style.left=x+"px";
			X_ = x;
			Y_ = y;
		}
		else
		{
			div.style.top=Y_+"px";
			div.style.left=X_+"px";
		}
		
		new Draggable(div.id,{
		endeffect:function(){
			new Effect.Highlight(div.id,{queue:'end'});
			var _divobj=document.getElementById('_DivCalendario_');
			Y_ = parseInt( _divobj.style.top);
			X_ = parseInt( _divobj.style.left);
		}
	  });
		if ((!year && !month && !day)||(ler))
		   {
			 /* Called for first time */
			 if (texto.value) 
			   {
					value       = texto.value;
					date        = value.split("/");
					if((date[1]=='09')||(date[1]=='08'))
						date[1]=date[1].replace('0','');
					if((date[0]=='09')||(date[0]=='08'))
						date[0]=date[0].replace('0','');
					day         = parseInt(date[0]);
					month       = parseInt(date[1]) - 1;
					year        = parseInt(date[2]);
				}
			 if (isNaN(year) || isNaN(month) || isNaN(day) || day == 0)
			 {
				dt      = new Date();
				year    = dt.getFullYear();
				month   = dt.getMonth();
				day     = dt.getDate();
			}
		  } 
		else 
		 {
			/* Moving in calendar */
			if (month > 11)
			{
				month = 0;
				year++;
			}
			if (month < 0)
			{
				month = 11;
				year--;
			}
		 }
	
		div.innerHTML = "";
	
		str = ""
	
		//heading table&
		str += '<table width="100%" class="TabelaCabecarioCalendario"><tr><th width="45%">';
		str += '<a href="javascript:;" onclick="month--; initCalendar('+X_+','+Y_+',\''+form+'\',\''+texto.id+'\',\''+foco+'\',false);">&laquo;</a> ';
		str += month_names[month];
		str += ' <a href="javascript:;" onclick="month++; initCalendar('+X_+','+Y_+',\''+form+'\',\''+texto.id+'\',\''+foco+'\',false);">&raquo;</a>';
		str += '</th><th width="45%">';
		str += '<a href="javascript:;" onclick="year--; initCalendar('+X_+','+Y_+',\''+form+'\',\''+texto.id+'\',\''+foco+'\',false);">&laquo;</a> ';
		str += year;
		str += ' <a href="javascript:;" onclick="year++; initCalendar('+X_+','+Y_+',\''+form+'\',\''+texto.id+'\',\''+foco+'\',false);">&raquo;</a>';
		str += '</th><th width="10%">';
		str += '<a href="javascript:;" onclick="returnDate(\''+ texto.value + '\',\''+texto.id+'\',\''+form+'\',\''+foco+'\');">X</a> ';
		str += '</th></tr></table>';
		
		str += '<table width="100%"  class="TabelaCorpoCalendario" ><tr>';
		for (i = 0; i < 7; i++) 
		{
			str += '<th >' + day_names[i] + "</th>";
		}
		str += "</tr>";
	
		var firstDay = new Date(year, month, 1).getDay();
		var lastDay = new Date(year, month + 1, 0).getDate();
	
		str += "<tr>";
	
		dayInWeek = 0;
		for (i = 0; i < firstDay; i++)
		{
			str += "<td>&nbsp;</td>";
			dayInWeek++;
		}
		for (i = 1; i <= lastDay; i++)
		{
			if (dayInWeek == 7)
			{
				str += "</tr><tr>";
				dayInWeek = 0;
			}
	
			dispmonth = 1 + month;
			actVal = formatNum2(i, 'day')+ "/" + formatNum2(dispmonth, 'month') + "/" + formatNum4(year) ;
		  
			if (i == day)
			{
				style = 'class="TD_diaCalendario"';
			}
			else
			{
				style = '';
			}
			str += '<td ' + style + ' ><a href="javascript:;" onclick="returnDate(\''+ actVal + '\',\''+texto.id+'\',\''+form+'\',\''+foco+'\');">'+ i + '</a></td>';
			dayInWeek++;
		}
		for (i = dayInWeek; i < 7; i++)
		{
			str += "<td>&nbsp;</td>";
		}
	
		str += "</tr></table>";
	
		div.innerHTML = str;
		
	} 
	catch (ex)
	{ 
			var mes="Nome:"+ex.name;
			mes+="\nMessagem:"+ex.message;
			if(typeof ex.number != "undefined")
				mes+="\nLinha:"+ex.number;
			if(typeof ex.lineNumber != "undefined")
				mes+="\nLinha:"+ex.lineNumber;
			if(typeof ex.fileName != "undefined")
				mes+="\nArquivo:"+ex.fileName;
			if(typeof ex.description != "undefined")
				mes+="\nDescição:"+ex.description;
			if(typeof ex.stack != "undefined")
				mes+="\nDescição:"+ex.stack;
			alert(mes);
			return false;
		}
}

/**
 * Returns date from calendar.
 *
 * @param   string     date text
 */
function returnDate(d,Editor,form,Foco)
{
	try
	{
		var texto=  SelectObjetoForm(form,Editor);
		var div=  document.getElementById('_DivCalendario_');
		var proximo=  SelectObjetoForm(form,Foco);
		texto.value = d;
		//texto.select();
		texto.focus();
		
		proximo.focus();
		div.style.display='none';
	} 
	catch (ex)
	{ 
			var mes="Nome:"+ex.name;
			mes+="\nMessagem:"+ex.message;
			if(typeof ex.number != "undefined")
				mes+="\nLinha:"+ex.number;
			if(typeof ex.lineNumber != "undefined")
				mes+="\nLinha:"+ex.lineNumber;
			if(typeof ex.fileName != "undefined")
				mes+="\nArquivo:"+ex.fileName;
			if(typeof ex.description != "undefined")
				mes+="\nDescição:"+ex.description;
			if(typeof ex.stack != "undefined")
				mes+="\nDescição:"+ex.stack;
			alert(mes);
			return false;
		}
}
