// construindo o calendário
function popdate(obj,div,tam,ddd,vet) {
    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 = eval (obj)
    if (date_Form.value == "") { date_Form = new Date()} else {date_Form = new Date(date_Form.value)}
  
    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  = "<table bgcolor='#033862' cellspacing='0' cellpadding='3' border='1' width='"+tam+"' height='"+tam*1.1 +"'>"
	txt += "<tr><td align=center bordercolor='#033862'><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+( "01/" + (month_prior+1).toString() + "/" + year_prior.toString())+"') class='cabecalho' style='color:#FFFFFF;' title='Mês Anterior'>&laquo;</a></td>"
    txt += "<td colspan='5' align='center' bordercolor='#033862' class='mes'><b>" + umonth[mmonth] + "&nbsp;" + ano.toString() + "</b><div id='popd' style='position:absolute'></div></td>"
	txt += "<td align=center bordercolor='#033862'><a href=javascript:popdate('"+obj+"','"+div+"','"+tam+"','"+( "01/" + (month_next+1).toString()  + "/" + year_next.toString())+"') class='cabecalho' style='color:#FFFFFF;' title='Próximo Mês'>&raquo;</a></td></tr>"
    txt += "<tr bgcolor='#1b648d'><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='#3995c6' 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='#FFFFFF' " : "" )
			classcell = ((day_dat == day_today) && (year_dat == year_today) ? " class='datahoje' style='text-decoration:none' " : " class='data' style='color:#FFFFFF;text-decoration:none' " )
			txt += "<td"+colorcell+" align=center bordercolor='#FFFFFF'><a href=javascript:showCalendar('"+ d + "/" + (mmonth+1).toString() + "/" + ano.toString() +"') "+classcell+">"+ d.toString() + "</a></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 showCalendar(dtevent)
{
	//window.open('estrutura/showCalendar.php?data=' + dtevent,'Calendário');
	//location.href = 'estrutura/showCalendar.php?data=' + dtevent;
	location.href = 'index.php?data=' + dtevent;
}

