var event_day = "";

function showCaleder(div,tam,ddd,url)
{
    if (ddd) 
    {
        day = ""
        mmonth = ""
        ano = ""
        c = 1
        char = ""
        for (s=0;s<parseInt(ddd.length);s++)
        {
            char = ddd.substr(s,1)
            if (char == "/") 
            {
                c++; 
                s++; 
                char = ddd.substr(s,1);
            }
            if (c==1) day    += char
            if (c==2) mmonth += char
            if (c==3) ano    += char
        }
        ddd = mmonth + "/" + day + "/" + ano
    }
  
    if(!ddd) {today = new Date()} else {today = new Date(ddd)}
    
	date_Form = new Date()
  
    ano = today.getFullYear();
    mmonth = today.getMonth ();
    day = today.toString ().substr (8,2)
  
    umonth = new Array ("Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro")
    days_Feb = (!(ano % 4) ? 29 : 28)
    days = new Array (31, days_Feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

    if ((mmonth < 0) || (mmonth > 11))  alert(mmonth)
    if ((mmonth - 1) == -1) {month_prior = 11; year_prior = ano - 1} else {month_prior = mmonth - 1; year_prior = ano}
    if ((mmonth + 1) == 12) {month_next  = 0;  year_next  = ano + 1} else {month_next  = mmonth + 1; year_next  = ano}
    txt = "<form action='#' method='post' id='calenAcontece' nome='calenAcontece' style='display:none;'><input name='eventDay' id='eventDay' type='hidden' ></form>"
    txt += "<table bgcolor='#207DD2' st cellspacing='0' cellpadding='1' border='0' width='"+tam+"' >"
    txt += "<tr ><td colspan='7' align='center' style='heigth:25px;' ><table border='0' cellpadding='0' width='100%'  cellpadding='0' bgcolor='#207DD2'><tr>"
    txt += "<td width=8% align=center class='dia' ><a href=javascript:showCaleder('"+div+"','"+tam+"','"+((mmonth+1).toString() +"/01/"+(ano-1).toString())+"','"+url+"') class='Cabecalho_Calendario' title='Ano Anterior'>&laquo;</a></td>"
    txt += "<td width=8% align=center class='dia' ><a href=javascript:showCaleder('"+div+"','"+tam+"','"+( "01/" + (month_prior+1).toString() + "/" + year_prior.toString())+"','"+url+"') class='Cabecalho_Calendario' title='Mês Anterior'>&lsaquo;</a></td>"
    txt += "<td width=65% align=center class='dia' ><a href=javascript:showYear('"+div+"','"+tam+"','" + (mmonth+1) + "') class='mes'>" + ano.toString() + "</a>" + " <a href=javascript:showMonth('"+div+"','"+tam+"','" + ano + "','"+url+"') class='mes'>" + umonth[mmonth] + "</a> <div id='popd' style='position:absolute'></div></td>"
    txt += "<td width=8% align=center class='dia' ><a href=javascript:showCaleder('"+div+"','"+tam+"','"+( "01/" + (month_next+1).toString()  + "/" + year_next.toString())+"','"+url+"') class='Cabecalho_Calendario' title='Próximo Mês'>&rsaquo;</a></td>"
    txt += "<td width=8% align=center class='dia' ><a href=javascript:showCaleder('"+div+"','"+tam+"','"+((mmonth+1).toString() +"/01/"+(ano+1).toString())+"','"+url+"') class='Cabecalho_Calendario' title='Próximo Ano'>&raquo;</a></td>"
    txt += "</tr></table></td></tr>"
    /*txt += "<tr><td colspan='7' align='right' bgcolor='#ccccff' class='mes'><a href=javascript:showYear('"+div+"','"+tam+"','" + (mmonth+1) + "') class='mes'>" + ano.toString() + "</a>"
    txt += " <a href=javascript:showMonth('"+div+"','"+tam+"','" + ano + "') class='mes'>" + umonth[mmonth] + "</a> <div id='popd' style='position:absolute'></div></td></tr>"*/
	//txt = txt.replace("#mes#","<a href=javascript:showYear('"+div+"','"+tam+"','" + (mmonth+1) + "') class='mes'>" + ano.toString() + "</a>" + " <a href=javascript:showMonth('"+div+"','"+tam+"','" + ano + "') class='mes'>" + umonth[mmonth] + "</a> <div id='popd' style='position:absolute'></div>")
	txt += "<tr bgcolor='#7EA3E7' style='heigth:20px;'><td width='14%' class='dia' align=center><b>D</b></td><td width='14%' class='dia' align=center><b>S</b></td><td width='14%' class='dia' align=center><b>T</b></td><td width='14%' class='dia' align=center><b>Q</b></td><td width='14%' class='dia' align=center><b>Q</b></td><td width='14%' class='dia' align=center><b>S<b></td><td width='14%' class='dia' align=center><b>S</b></td></tr>"
    
	today1 = new Date((mmonth+1).toString() +"/01/"+ano.toString());
    diainicio = today1.getDay () + 1;
    week = d = 1
    start = false;

    for (n=1;n<= 42;n++) 
    {
        if (week == 1)  txt += "<tr bgcolor='#EEF3F9' align=center>"
        if (week==diainicio) {start = true}
        if (d > days[mmonth]) {start=false}
        if (start) 
        {
            dat = new Date((mmonth+1).toString() + "/" + d + "/" + ano.toString())
            day_dat   = dat.toString().substr(0,10)
            day_today  = date_Form.toString().substr(0,10)
            year_dat  = dat.getFullYear ()
            year_today = date_Form.getFullYear ()
            //colorcell = ((day_dat == day_today) && (year_dat == year_today) ? " bgcolor='#CAB3EF' " : "" )
            colorcell = ((day_dat == day_today) && (year_dat == year_today) ? "  " : "" )
			if((event_day.indexOf("#"+ formatNumber(d.toString()) + "/" + formatNumber((mmonth+1).toString()) + "/" + ano.toString()+"#",0)!=-1)||event_day.indexOf("#"+ d.toString() + "/" + (mmonth+1).toString() + "/" + ano.toString()+"#",0)!=-1){
				txt += "<td"+colorcell+" align=center class='diaEvento'><a href=javascript:showEvent('"+url+"','"+  d + "/" + (mmonth+1).toString() + "/" + ano.toString() +"') class='data ' alt='Clique para ver os eventos do dia'>"+ d.toString() + "</a></td>";
			}else{
				txt += "<td"+colorcell+" align=center class='data'>"+ d.toString() + "</td>";
			}	
            d ++ 
        } 
        else 
        { 
            txt += "<td class='data' align=center> </td>"
        }
        week ++
        if (week == 8) 
        { 
            week = 1; txt += "</tr>"} 
        }
        txt += "</table>"
        div2 = eval (div)
        div2.innerHTML = txt 
}

function formatNumber(_num){
	if(_num.length<=1){
		_num = "0"+_num;
	}
	return _num
}

// função para exibir a janela com os anos
function showYear(div, tam, umonth)
{
  txt  = "<table bgcolor='#CCCCFF' border='0' width=160>"
  l = 1
  for (n=1991; n<2012; n++)
  {  if (l == 1) txt += "<tr>"
     txt += "<td align=center><a href=javascript:showCaleder('"+div+"','"+tam+"','"+(umonth.toString () +"/01/" + n) +"')>" + n + "</a></td>"
     l++
     if (l == 4) 
        {txt += "</tr>"; l = 1 } 
  }
  txt += "</tr></table>"
  popd.innerHTML = txt 
}
	
// função para exibir a janela com os meses
function showMonth(div, tam, ano)
{
  txt  = "<table bgcolor='#CCCCFF' border='0' width=80>"
  for (n = 0; n < 12; n++) { txt += "<tr><td align=center><a href=javascript:showCaleder('"+div+"','"+tam+"','"+("01/" + (n+1).toString() + "/" + ano.toString())+"')>" + umonth[n] +"</a></td></tr>" }
  txt += "</table>"
  popd.innerHTML = txt
}

function showEvent(url,dt){
	//location.href=url+'?eventDay='+dt;
	var frm = document.getElementById("calenAcontece");
	var inputEventDay = document.getElementById("eventDay");
	frm.action = url;
	inputEventDay.value = dt;
	frm.submit();
}