/*********************************************
* calendario javascript
* Jose Callealta Garcia
* Puerto Real, septiembre 2005
*********************************************/

   var calname=0;
   var caldays=1;

function DateCalendar(nyear,nmonth){
   var actual = new Date();
   var newdate;
   var months  = new Array(13);
   var week    = new Array("","L","M","M","J","V","S","D")

   this.year   =  nyear==null?    actual.getFullYear() : nyear;
   this.month  =  nmonth==null?   actual.getMonth()    : nmonth-1;
   this.today  =  nmonth==null?   actual.getDate()     : 1;

   newdate     = new Date(this.year,this.month ,this.today);
   this.dayweek    = newdate.getDay();
   
   months[ 0] = new Array (""          , 0               );
   months[ 1] = new Array ("Enero"     ,31               );
   months[ 2] = new Array ("Febrero"   ,this.year % 4? 28 : 29);
   months[ 3] = new Array ("Marzo"     ,31               );
   months[ 4] = new Array ("Abril"     ,30               );
   months[ 5] = new Array ("Mayo"      ,31               );
   months[ 6] = new Array ("Junio"     ,30               );
   months[ 7] = new Array ("Julio"     ,31               );
   months[ 8] = new Array ("Agosto"    ,31               );
   months[ 9] = new Array ("Septiembre",30               );
   months[10] = new Array ("Octubre"   ,31               );
   months[11] = new Array ("Noviembre" ,30               );
   months[12] = new Array ("Diciembre" ,31               );
   
   
   this.months=months;
   this.monthdays = months[this.month+1][caldays];
   this.monthname = months[this.month+1][calname];
   
   newdate = new Date(this.year,this.month,1);
   this.week=week;
   this.dayfirstweek=(newdate.getDay()==0? 7:newdate.getDay())-1;
   
}

function style(){
   var padding  = "padding-top: 2px; padding-bottom: 2px; padding-left: 2px; padding-right: 2px; ";
   var font_family="Verdana, Geneva, Arial, Helvetica, sans-serif;";
   var font_size= "9px;";
	var td_size="" //"width: 10px; height: 10px;" //tamaño celda

   //Color líneas
	var color_rejilla       ="Black;";  //"#915F20;";

	//Color Cabecera
   var colorback_head      = "#3D515C;" //color_rejilla; //"#7F9EBD;";
   var colortext_head      = "white;" //"#FFFACD;";
   
	//color días semana (lunes a viernes)
   var colorback_week      = "#4F789F;"; //"#98C2D2;" //"#CCBCA5;";
   var colortext_week      = "white;;"  //colortext_head;

	//color días semana (sábado y domingo)	
   var colorback_freeweek  =  "#8F3844" //"#31799F;"//"#CCBCA5"; //"#33435A;"; //colorback_freedays; //"#C7D5C7;";
   var colortext_freeweek  =  "white;"; //colortext_head;

	//Color días libres
   var colorback_freedays  = colorback_freeweek  //"#33435A;"; //"#AE997A;";
   var colortext_freedays  = colortext_freeweek;
   
	//color días laborables
   var colorback_days      = "#98C2D2;";  //"#CCBCA5;";
   var colortext_days      = "black;"; //"#33435A;";
   
	//color día actual
   var colorback_today     = "#CA3B36"  //"#B22222;";
   var colortext_today     =  "white;";
   
   //aspecto de la tabla general
   this.table     = "padding: 0px;"+
                    "background-color:"+color_rejilla+";"+ //#7F9EBD;"+ 
                    "text-align:center;"+
                    "font-family:"+font_family+";"+
                    "font-size:"+font_size+";"+
						  "width: 150px;";
   
   //cabecera (mes y anio)
   this.head      = padding+
                    "font-size: "+font_size+";"+
                    "background-color:"+colorback_head+";"+
                    "color:"+colortext_head+";";
                    
   //Dias de la semana lunes a viernes
   this.week      = padding+
                    "background-color:"+colorback_week+";"+
                    "color:"+colortext_week+";"+
                    "font-weight: Bold;"+td_size;

   //Dias de la semana sabado y domingo
   this.freeweek  = padding+
                    "background-color:"+colorback_freeweek+";"+
                    "color: "+colortext_freeweek+";"+
                    "font-weight: bold;"+td_size;

   //dias del mes
   this.days      = padding+
                    "background-color:"+colorback_days+";"+ //#ECF3EE;"+ //#FFFACD;"+
                    "color:"+colortext_days+";"+
                    "font-weight: normal;"+td_size;

   //Dia actual
   this.today     = padding+
                    "background-color:"+colorback_today+";"+
                    "color:"+colortext_today+";"+
                    "font-weight: normal;"+td_size;
   
   //Sabados y domingos
   this.freedays  = padding+
                    "background-color:"+colorback_freedays+";"+
                    "color:"+colortext_freedays+";"+
                    "font-weight: normal;"+td_size;

}

function calendar(year,month){
   var fecha   = new DateCalendar(year,month);
   var estilo  = new style();
   var stylo
   var d       = new Array(42);
   var i=0, j=1, n=0;
  
   for(i=0; i<=41; i++){
      if(i>=fecha.dayfirstweek & j<=fecha.monthdays)
         d[i]=j++;
      else d[i]="&nbsp;";
   }

   document.write("<table cellspacing='1' style='"+estilo.table+"'>"+
                  "<tr><th colspan='7' style='"+estilo.head+"'>"+
                  fecha.monthname+" - "+fecha.year+"</th></tr>");

	
   //document.write("<table cellspacing='1' style='"+estilo.table+"'>");

   document.write(   "<tr>"+
                     "<td style='"+estilo.week     +"'>"+fecha.week[1]+"</td>"+  //Lunes
                     "<td style='"+estilo.week     +"'>"+fecha.week[2]+"</td>"+  //Martes
                     "<td style='"+estilo.week     +"'>"+fecha.week[3]+"</td>"+  //Miercoles
                     "<td style='"+estilo.week     +"'>"+fecha.week[4]+"</td>"+  //Jueves
                     "<td style='"+estilo.week     +"'>"+fecha.week[5]+"</td>"+  //Viernes
                     "<td style='"+estilo.freeweek +"'>"+fecha.week[6]+"</td>"+  //Sabado
                     "<td style='"+estilo.freeweek +"'>"+fecha.week[7]+"</td>"+  //Domingo
                     "</tr>" );

   for(i=0; i<=5;i++){
      document.write("<tr>");
      for(j=0;j<=6;j++){
         stylo=(j>=5? estilo.freedays: estilo.days);
         stylo=(d[n]==fecha.today? estilo.today: stylo);
         document.write("<td height='20px' style='"+stylo+"'>"+d[n++]+"</td>");
      }
      document.write("</tr>");
   }
   document.write("</table>");
   }

