function loadCalendario(div,ddd){
    var obj;
    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);
    date_Form = new Date();  
    ano = today.getFullYear();
    mmonth = today.getMonth ();
    day = today.toString ().substr (8,2);    
  
    umonth = new Array ("Janeiro", "Fevereiro", "Mar&ccedil;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 - 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 = "<span title='Ir para o ano anterior' id='voltatudo' onclick=loadCalendario('"+div+"','"+((mmonth+1).toString() +"/01/"+(ano-1).toString())+"')><a href='javascript:void(0)'>&lt;</a></span>";
    txt += "<span title='ir para o m&ecirc;s anterior' id='volta' onclick=loadCalendario('"+div+"','"+( "01/" + (month_prior+1).toString() + "/" + year_prior.toString())+"')><a href='javascript:void(0)'>&laquo;</a></span>";
    txt += "<span>"+umonth[mmonth]+" de "+ano.toString()+"</span>";	
    txt += "<span title='Ir para o pr&oacute;ximo m&ecirc;s' id='avanca' onclick=loadCalendario('"+div+"','"+( "01/" + (month_next+1).toString()  + "/" + year_next.toString())+"')><a href='javascript:void(0)'>&raquo;</a></span>";
    txt += "<span title='Ir para o pr&oacute;ximo ano' id='avancatudo' onclick=loadCalendario('"+div+"','"+((mmonth+1).toString() +"/01/"+(ano+1).toString())+"')><a href='javascript:void(0)'>&gt;</a></span>";
    txt += "<table>";
    txt += "<thead>";
    txt += "<tr>";
    txt += "<td class='folga'>Dom</td>";
    txt += "<td>Seg</td>";
    txt += "<td>Ter</td>";
    txt += "<td>Qua</td>";
    txt += "<td>Qui</td>";
    txt += "<td>Sex</td>";
    txt += "<td class='folga'>S&aacute;b</td>";
    txt += "</tr>";
    txt += "<tr class='line_hide'>";
    txt += "<td id='mescorrente'>"+(mmonth+1)+"</td>";
    txt += "<td id='anocorrente'>"+ano+"</td>";
    txt += "<tr>";
    txt += "</thead>";
    txt += "<tbody>";
    today1 = new Date((mmonth+1).toString() +"/01/"+ano.toString());
    diainicio = today1.getDay () + 1;
    week = d = 1;
    start = false;
    classtr = false;
    for (n=1;n<= 42;n++){
        if (d > days[mmonth]) {
            classtr= "class = 'line_hide' ";
        }else{
            classtr="";
        }
        if (week == 1)  txt += "<tr "+classtr+">";
        if (week==diainicio) {
            start = true;
        }
        if (d > days[mmonth]) {
            start=false;
        }
        
        if (start){
            dat = new Date((mmonth+1).toString() + "/" + d + "/" + ano.toString());
            
            dat.setFullYear(ano);
            dat.setMonth(mmonth);
            dat.setDate(d);
            
            day_dat   = dat.toString().substr(0,10);
            day_today  = date_Form.toString().substr(0,10);
            year_dat  = dat.getFullYear ();
            year_today = date_Form.getFullYear ();
            
            mes = (mmonth+1).toString();            
            ano = ano.toString();            
            dia = ((d < 10) ? "0"+d : d);
            m = ((mes < 10) ? "0"+mes : mes);
            
                       
            if((day_dat == day_today) && (year_dat == year_today) && dat.getDay()!=0){
                colorcell = " class= 'hoje' ";
            }
            else if(dat.getDay()==0 || dat.getDay()==6){
                colorcell = " class = 'folga' ";
            }else{
                colorcell = "";
            }     
                                    
            txt += "<td  "+colorcell+" id='"+dia+m+ano+"'>"+ dia.toString() + "</td>";
            
            d ++; 
        }else{ 
            txt += "<td></td>";
        }
        week ++;
        if (week == 8){ 
            week = 1;
            txt += "</tr>";
        } 
    }
    txt += "<tr class='line_hide'>";
    txt += "<td id='ultimodia'>"+dia+"</td>";
    txt += "<tr>";
    txt += "</table>";
    txt += "<tbody>";
                
    jQuery("#calendario").html(txt);
    jQuery(".line_hide").hide();            
}

function selecionar(id){
	desmarcar();
	$("#"+id).addClass("selecionado");
}

function desmarcar(){
	$(".selecionado").each(function(){
		$(this).removeClass("selecionado");
	});
}
