Portlet_calendar=function(displayCalendarDate){
  this.displayType=0;
  this.selectedDay=0;
  this.displayElement=displayCalendarDate;
  this.isDisplayed=false;
  
  this.changeDisplayType=function(){
    this.displayType++;
    this.displayType=this.displayType%4;
  }
  
  this.setDate=function(y,m,d){
    var currentDate=new Date();
    var today=currentDate.getDate();
    var tomonth=currentDate.getMonth();;
    var toyear=currentDate.getFullYear();
    var year=y;
    if(year==undefined){
      year=toyear;
    }
    var month=m;
    if(month==undefined){
      month=tomonth;
    }
    var day=d;
    if(day==undefined){
      day=today;
    }
    var firstDay=(new Date(year,month,1)).getDay();
    firstDay=(firstDay-1)%7;
    if(firstDay==-1){
      firstDay=6;
    }
    var lastDay=(new Date((new Date(year,month+1,1))-1)).getDate();
    if(day>lastDay){
      day=lastDay;
    }
    if(this.selectedDay>lastDay){
      this.selectedDay=lastDay;
    }

    var table=document.createElement("table");
    table.setAttribute("id","portlet_calendar_table");
    var tr;
    var td;
    var a;

    tr=document.createElement("tr");
    var th=document.createElement("th");
    th.setAttribute("class","prev");
    tr.appendChild(th);
    th.innerHTML='<a href="#" onclick="portlet_calendar.setDate('+(month-1<0?year-1:year)+','+(month-1<0?11:month-1)+','+day+')"><img src="../images/structure1/portlet_calendar/prev.png" /></a>';
    th=document.createElement("th");
    th.setAttribute("class","display");
    th.setAttribute("colspan","5");
    var tod='';
    switch(this.displayType){
      case(0):
        tod=day+'.'+(month+1)+'.'+year;
        break;
      case(1):
        tod=day+'/'+(month+1)+'/'+year;
        break;
      case(2):
        tod=year+'-'+(month+1<10?'0'+(month+1):(month+1))+'-'+(day<10?'0'+day:day);
        break;
      case(3):
        tod=year+''+(month+1<10?'0'+(month+1):(month+1))+''+(day<10?'0'+day:day);
        break;
    }
    th.innerHTML='<a href="#" onclick="portlet_calendar.changeDisplayType();portlet_calendar.setDate('+year+','+month+','+day+');">'+tod+'</a>';
    tr.appendChild(th);
    th=document.createElement("th");
    th.setAttribute("class","next");
    th.innerHTML='<a href="#" onclick="portlet_calendar.setDate('+(month+1>11?year+1:year)+','+(month+1>11?0:month+1)+','+day+')"><img src="../images/structure1/portlet_calendar/next.png" /></a>';
    tr.appendChild(th);
    table.appendChild(tr);
    var i,j;
    for(i=0;i<6;i++){
      tr=document.createElement("tr");
      for(j=0;j<7;j++){
        td=document.createElement("td");
        if((i==0 && j<firstDay) || 7*i+j-firstDay>=lastDay || 7*i+j-firstDay+1<1){
          td.setAttribute("class","noday");
          td.innerHTML="&nbsp;";
        }else{
          if(today==7*i+j-firstDay+1 && month==tomonth && year==toyear){
            td.setAttribute("class","today");
          }else{
            td.setAttribute("class","day");
          }
          if(this.selectedDay==7*i+j-firstDay+1){
            td.setAttribute("class","selected");
          }
          a=document.createElement("a");
          a.setAttribute("href","#");
          a.setAttribute("onclick","portlet_calendar.selectDateAndClose("+year+","+month+","+(7*i+j-firstDay+1)+");portlet_calendar.setDate("+year+","+month+","+(7*i+j-firstDay+1)+")");
          if(document.all){
            a.innerText=""+(7*i+j-firstDay+1);
          }else{
            a.textContent=""+(7*i+j-firstDay+1);
          }
          //a.innerText=""+(7*i+j-firstDay+1);
          td.appendChild(a);
        }
        tr.appendChild(td);
      }
      table.appendChild(tr);
      var d=document.getElementById('portlet_calendar_table');
      if(d!=null){
        document.getElementById('portlet_calendar_calendar').removeChild(d);
      }
      document.getElementById('portlet_calendar_calendar').appendChild(table);
    }
  }
  
  this.toSQLDate=function(y,m,d){
    return y+"-"+(m<10?"0"+m:m)+"-"+(d<10?"0"+d:d);
  }
  
  this.show=function(){
    this.isDisplayed=!this.isDisplayed;
    if(this.isDisplayed){
      document.getElementById('portlet_calendar').style.display='block';
    }else{
      document.getElementById('portlet_calendar').style.display='none';
    }
  }
  
  this.selectDateAndClose=function(y,m,d){
    this.selectDate(y,m,d);
    this.displayElement.value=this.toSQLDate(y,m+1,d);
    this.isDisplayed=false;
    document.getElementById('portlet_calendar').style.display='none';
  }
  
  this.selectDate=function(y,m,d){
    this.selectedDay=d;
  }
  
  this.setDate();
}
