varGLOBAL1 = new Array(0,0);
varGLOBMes = new Array(0,0);
varGLOBAny = new Array(0,0);

function DiesMes (MM, AAAA) {
   if ((AAAA%4) == 0) {
      if ((AAAA%100) == 0) {
         if ((AAAA%400) == 0) { FF = 29 } else { FF = 28 }
      } else { FF = 29 }
   } else { FF = 28 }
   varDies = new Array (31, FF, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
   return varDies[MM];
}

function NomMes (MM) {
   varMesos = new Array('enero','febrero','marzo','abril','mayo','junio','julio','agosto','septiembre','octubre','noviembre','diciembre');
   return varMesos[MM];
}

function ReomplirTaula(ident) {
varData1 = new Array(0,0);
varDiaSetm1 = new Array(0,0);
varData31 = new Array(0,0);
varDiaSetm31 = new Array(0,0);


   varData1[ident] = new Date(varGLOBAny[ident],varGLOBMes[ident],1);
   varDiaSetm1[ident] = varData1[ident].getDay();
   varDiaSetm1[ident] = (varDiaSetm1[ident]==0?7:varDiaSetm1[ident]);
   varGLOBAL1[ident] = varDiaSetm1[ident];
   varData31[ident] = new Date(varGLOBAny[ident],varGLOBMes[ident],DiesMes(varGLOBMes[ident],varGLOBAny[ident]));
   varDiaSetm31[ident] = varData31[ident].getDay();
   varDiaSetm31[ident] = (varDiaSetm31[ident]==0?7:varDiaSetm31[ident]);
   for (i=0; i<42; i++) {
      document.getElementById(ident+'day'+i).value = " ";
      document.getElementById(ident+'day'+i).style.cursor = 'default';
   }
   for (i=1; i<=DiesMes(varGLOBMes[ident],varGLOBAny[ident]); i++) {
      document.getElementById(ident+'day'+(i+varDiaSetm1[ident]-2)).value = i;
      document.getElementById(ident+'day'+(i+varDiaSetm1[ident]-2)).style.cursor = 'hand';
   }
   document.getElementById(ident+'mes').value = NomMes(varGLOBMes[ident]);
   document.getElementById(ident+'any').value = varGLOBAny[ident];
}

function ReomplirTaulaAux(SSS,ident) {
   if (SSS==1) { varGLOBMes[ident]-- }
   if (SSS==2) { varGLOBMes[ident]++ }
   if (SSS==3) { varGLOBAny[ident]-- }
   if (SSS==4) { varGLOBAny[ident]++ }
   if (varGLOBMes[ident]==-1) {
      varGLOBMes[ident]=11;
      varGLOBAny[ident]--;
   }
   if (varGLOBMes[ident]==12) {
      varGLOBMes[ident]=0;
      varGLOBAny[ident]++;
   }
   ReomplirTaula (ident);
}

function DibuixarTaula(ident,MM,AAAA) {
   varGLOBMes[ident] = MM;
   varGLOBAny[ident] = AAAA;
   document.write('<table width="128" bgcolor="#FFFFFF" align="center" cellspacing="0" cellpadding="0" style="border: 1px #000000 solid;">');
   document.write('<tr><td width="6"><img src="imatges/left.gif" style="cursor: hand;" onClick="ReomplirTaulaAux(1,'+ident+');"/></td><td width="60"><input type="button" style="width: 60px; background-color: #FFFFFF; font-size: 10px; text-align: center; font-family: Arial, Helvetica, sans-serif; border: 0px;" id="'+ident+'mes" value="'+NomMes(varGLOBMes[ident])+'"/></td><td width="6"><img src="imatges/right.gif" style="cursor: hand;" onClick="ReomplirTaulaAux(2,'+ident+');"/></td><td width="10"></td><td width="6"><img src="imatges/left.gif" style="cursor: hand;" onClick="ReomplirTaulaAux(3,'+ident+');"/></td><td width="32"><input type="button" style="width: 32px; background-color: #FFFFFF; font-size: 10px; text-align: center; font-family: Arial, Helvetica, sans-serif; border: 0px;" id="'+ident+'any" value="'+varGLOBAny[ident]+'"/></td><td width="6"><img src="imatges/right.gif" style="cursor: hand;" onClick="ReomplirTaulaAux(4,'+ident+');"/></td></tr>');
   document.write('<tr><td width="126" colspan="7">');
   for (i=0; i<42; i++) {
      document.write('<input type="button" style="width: 18px; background-color: #FFFFFF; font-size: 10px; text-align: center; font-family: Arial, Helvetica, sans-serif; border: 0px;" id="'+ident+'day'+i+'" value=" " onMouseOver="Hover('+ident+','+i+',1);" onMouseOut="Hover('+ident+','+i+',0);" onClick="MostrarData('+ident+','+i+');"/>');
      if (((i+1)%7)==0) {
         document.write('<br/>');
      }
   }
   document.write('</td></tr>');
   document.write('</table>');
   ReomplirTaula(ident);
}

function Hover(ident,DD,X) {
   if (document.getElementById(ident+'day'+DD).value != " ") {
      if (X==1) { document.getElementById(ident+'day'+DD).style.background = '#CCCCFF' } else { document.getElementById(ident+'day'+DD).style.background = '#FFFFFF' }
   }
}

function MostrarData(ident,DD) {
   vDIA = ((DD-varGLOBAL1[ident]+2)<10?'0'+(DD-varGLOBAL1[ident]+2):(DD-varGLOBAL1[ident]+2));
   vMES = ((1+varGLOBMes[ident])<10?'0'+(1+varGLOBMes[ident]):(1+varGLOBMes[ident]));
   vANY = varGLOBAny[ident];
   if (document.getElementById(ident+'day'+DD).value != ' ' ) { document.getElementById(ident+'data').value = vDIA+'-'+vMES+'-'+vANY; }
}