function listaCompletos() {

	lst_html = $("ul_dias").getElementsByClassName('marcado');
	
	lst_dias = new Array();
	for(var i=0; i<lst_html.length; i++) {
	   lst_dias[i] =  lst_html[i].innerHTML;
	}
	return lst_dias;
}

function limpiaColor() {
 	for(var i=1; i<42; i++) {
  		$("s"+i).removeClassName('ocupado');
  		$("s"+i).removeClassName('diaHoy');
  		$("s"+i).removeClassName('clicable');
  	}
}	

function setToday() {
	
  dt = new Date();
  mnth  = dt.getMonth(); /* 0-11*/
  dayOfMnth = dt.getDate(); /* 1-31*/
  yr = dt.getFullYear(); /*4-digit year*/

  startStr = (mnth+1)+"/1/"+yr;
  dt1 = new Date(startStr);
  dayOfWeek1 = dt1.getDay()-1;
  
  diaHoy = dayOfMnth + dayOfWeek1;
  $("s" + diaHoy).addClassName('diaHoy');
}

function setDate(str) {

 if (str == "   ") {
  return;
 }

 mnth1 = $("formCal").month.value;
 mnth = mnth1;
 mnth++;
 year = $("formCal").year.value;
 dateStr = str+"/"+mnth+"/"+year;

 dateStr = trim(dateStr);
 
 $("formFecha").fecha.value = dateStr;
 $("formFecha").submit();

}//setDate()

/**
 * The function removes spaces from the selected date.
 */

function trim(str) {

 res="";

 for(var i=0; i< str.length; i++) {
   if (str.charAt(i) != " ") {
     res +=str.charAt(i);
  }
 }
   
 return res;

}//trim()
 
/**
 * The method to get the Month name given the Month number of the year.
 */

function getMonthName(mnth) {

 if (mnth == 0) {
  name = "Enero";
 }else if(mnth==1) {
  name = "Febrero";
 }else if(mnth==2) {
  name = "Marzo";
 }else if(mnth==3) {
  name = "Abril";
 }else if(mnth==4) {
  name = "Mayo";
 } else if(mnth==5) {
  name = "Junio";
 } else if(mnth==6) {
  name = "Julio";
 } else if(mnth==7) {
  name = "Agosto";
 } else if(mnth==8) {
  name = "Septiembre";
 } else if(mnth==9) {
  name = "Octubre";
 } else if(mnth==10) {
  name = "Noviembre";
 } else if(mnth==11) {
  name = "Diciembre";
 }

 return name;

}//getMonthName()

/**
 * Get the number of days in the month based on the year.
 */

function getNoOfDaysInMnth(mnth,yr) {
 
 rem = yr % 4;

 if(rem ==0) {
   leap = 1;
 } else {
  leap = 0;
 }

 noDays=0;

 if ( (mnth == 1) || (mnth == 3) || (mnth == 5) ||
      (mnth == 7) || (mnth == 8) || (mnth == 10) ||
      (mnth == 12)) {
  noDays=31;
 } else if (mnth == 2) {
           noDays=28+leap;
        } else {
           noDays=30;
 }

 //alert(noDays);
 return noDays;
 
      
}//getNoOfDaysInMnth()
  
/**
 * The function to reset the date values in the buttons of the 
 * slots.
 */
 
function fillDates(dayOfWeek1,noOfDaysInmnth) {

 if (dayOfWeek1 == 0) {dayOfWeek1 = 7;}
 lst_dias = listaCompletos()

 for(var i=1; i<43; i++) {
   str = "s"+i;
   $("formCal").elements[str].value="   ";   
 }

 startSlotIndx = dayOfWeek1;
 slotIndx = startSlotIndx;

 for(var i=1; i<(noOfDaysInmnth+1); i++) {
  slotName = "s"+slotIndx;
  val="";
  if (i<10) {
    val = " "+i+" ";
  } else {
    val = i;
  }

  /*pone los dias con pdf en #ccc*/
  
  for(var j=0; j<lst_dias.length; j++) {
	  if((slotIndx) == (((lst_dias[j])-1) + (startSlotIndx)))
	  {
	  	$("s"+slotIndx).addClassName('ocupado');
	  }
  }
  
  $("formCal").elements[slotName].value = val;
  if(val != ""){
    $("s"+slotIndx).addClassName('clicable');
  }
  slotIndx++;
 }
  
}//fillDates()
 

/**
 * The function that is called at the time of loading the page.
 * This function displays Today's date and also displays the 
 * the calendar of the current month.
 */

function thisMonth() {

  dt = new Date();
  mnth  = dt.getMonth(); /* 0-11*/
  dayOfMnth = dt.getDate(); /* 1-31*/
  dayOfWeek = dt.getDay()-1; /*0-6*/
  yr = dt.getFullYear(); /*4-digit year*/

  mnthName = getMonthName(mnth)+ " ";
  
  if ($("formCal") == null) { return false; }
  $("formCal").month.value = mnth;
  $("formCal").year.value = yr;
  $("formCal").currMonth.value = mnth;
  $("formCal").currYear.value = yr;
  
  $("formCal").monthYear.value = mnthName+yr;
  
  par = $("fechaText");
  par.innerHTML = mnthName+yr;
  
  $("formFecha").fecha.value = dayOfMnth+"/"+(mnth+1)+"/"+yr;

  startStr = (mnth+1)+"/1/"+yr;
  dt1 = new Date(startStr);
  dayOfWeek1 = dt1.getDay()-1; /*0-6*/

  noOfDaysInMnth = getNoOfDaysInMnth(mnth+1,yr);
  fillDates(dayOfWeek1+1,noOfDaysInMnth);
  
  /*colorear dia actual*/
  setToday();
  
}//thisMonth()


function myMonth(anho, mes, dia) {

  dt = new Date(anho, mes, dia);
  mnth  = dt.getMonth(); /* 0-11*/
  dayOfMnth = dt.getDate(); /* 1-31*/
  dayOfWeek = dt.getDay()-1; /*0-6*/
  yr = dt.getFullYear(); /*4-digit year*/

  mnthName = getMonthName(mnth)+ " ";
  $("formCal").month.value = mnth;
  $("formCal").year.value = yr;
  $("formCal").currMonth.value = mnth;
  $("formCal").currYear.value = yr;
  
  $("formCal").monthYear.value = mnthName+yr;
  
  par = $("fechaText");
  par.innerHTML = mnthName+yr;
  
  $("formFecha").fecha.value = dayOfMnth+"/"+(mnth+1)+"/"+yr;

  startStr = (mnth+1)+"/1/"+yr;
  dt1 = new Date(startStr);
  dayOfWeek1 = dt1.getDay()-1; /*0-6*/

  noOfDaysInMnth = getNoOfDaysInMnth(mnth+1,yr);
  fillDates(dayOfWeek1+1,noOfDaysInMnth);

}//thisMonth()



/**
 * The function that will be used to display the calendar of the next month.
 */

function nextMonth() {
 limpiaColor();
 var currMnth = $("formCal").month.value;
 currYr = $("formCal").year.value;

 if (currMnth == "11") {
    nextMnth = 0;
    nextYr = currYr;
    nextYr++;
 } else {
   nextMnth=currMnth;
   nextMnth++;
   nextYr = currYr;
 }

 mnthName = getMonthName(nextMnth);
 $("formCal").month.value=nextMnth;
 $("formCal").year.value=nextYr;
 $("formCal").monthYear.value= mnthName+" "+nextYr;
 
 par = $("fechaText");
 par.innerHTML = mnthName+" "+nextYr;

 str = (nextMnth+1)+"/1/"+nextYr;
 dt = new Date(str);
 dayOfWeek = dt.getDay()-1;

 noOfDays = getNoOfDaysInMnth(nextMnth+1,nextYr);
  
 new Ajax.Updater($('div_dias'), '?accion=ajax_calendario', {
 parameters: {
       anho: nextYr,
       mes: nextMnth+1
     }
     ,
  onComplete: function(){ fillDates(dayOfWeek+1,noOfDays);}
 });
 
 /*colorear dia actual*/
 fecAct = new Date();
 mActual = parseInt(currMnth) + 1;
 aActual = currYr;
 if((mActual == fecAct.getMonth()) && (aActual == fecAct.getFullYear()))
 {
 	setToday();
 }	

}//nextMonth()

/**
 * The method to display the calendar of the previous month.
 */

function prevMonth() {
 limpiaColor();
 
 var currMnth = $("formCal").month.value;
 currYr = $("formCal").year.value;

 if (currMnth == "0") {
    prevMnth = 11;
    prevYr = currYr;
    prevYr--;
 } else {
   prevMnth=currMnth;
   prevMnth--;
   prevYr = currYr;
 }

 str = (prevMnth+1)+"/1/"+prevYr;
 dt = new Date(str);
 dayOfWeek = dt.getDay()-1;

 /***********************************************
  * Remove the comment if do not want the user to 
  * go to any previous month than this current month.
  ***********************************************/

 /*

 runningMonth = $("formCal").currMonth.value;
 rMonth=runningMonth;
 rMonth++;
 runningYear = $("formCal").currYear.value;
 rYear=runningYear;

 str = (rMonth)+"/1/"+rYear;
 dt1 = new Date(str);
 
 if (dt.valueOf() < dt1.valueOf()) {
   alert('Cannot Go Before Current Month');
   return;
 }
 
 */

 /**************************************************
 * End of comment
 **************************************************/

 mnthName = getMonthName(prevMnth);
 $("formCal").month.value=prevMnth;
 $("formCal").year.value=prevYr;
 $("formCal").monthYear.value= mnthName+" "+prevYr;

 par = $("fechaText");
 par.innerHTML = mnthName+" "+prevYr;
 
 noOfDays = getNoOfDaysInMnth(prevMnth+1,prevYr);
  
 new Ajax.Updater($('div_dias'), '?accion=ajax_calendario', {
 parameters: {
       anho: prevYr,
       mes: prevMnth+1
     }
     ,
  onComplete: function(){ fillDates(dayOfWeek+1,noOfDays);}
 });
 
  /*colorear dia actual*/
 fecAct = new Date();
 mActual = parseInt(currMnth) -1;
 aActual = currYr; 
 if((mActual == fecAct.getMonth()) && (aActual == fecAct.getFullYear()))
 {
 	setToday();
 }	
 
 
}//prevMonth()

Event.observe(window, 'load',
    function() {
		thisMonth();
     }     
);
