
//Form button calendar (author unknown)
//For this script and more
//Visit http://javascriptkit.com

function setDate(str) {

//GlobalArrayGiorniEventi
str=trim(str);

if (trim(str)!="") {
		if (giornoEvento (str, GlobalArrayGiorniEventi)) {
			meseCorrente=parseInt(document.forms[1].month.value) + 1;
//alert (yr+"-"+meseCorrente+"-"+str);
			
			getEvents (document.forms[1].year.value+"-"+meseCorrente+"-"+str);
			//alert (yr+"-"+meseCorrente+"-"+str);
			
		} else {
			return;
		}
} else {
		return;
}



/*
 if (str == "   ") {
  return;
 }

 mnth1 = document.forms[0].month.value;
 mnth = mnth1;
 mnth++;
 year = document.forms[0].year.value;
 dateStr = mnth+"/"+str+"/"+year;

 dateStr = trim(dateStr);
 
 document.forms[1].dateField.value = dateStr;

*/
return;
}//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 = "Gennaio";
 }else if(mnth==1) {
  name = "Febbraio";
 }else if(mnth==2) {
  name = "Marzo";
 }else if(mnth==3) {
  name = "Aprile";
 }else if(mnth==4) {
  name = "Maggio";
 } else if(mnth==5) {
  name = "Giugno";
 } else if(mnth==6) {
  name = "Luglio";
 } else if(mnth==7) {
  name = "Agosto";
 } else if(mnth==8) {
  name = "Settembre";
 } else if(mnth==9) {
  name = "Ottobre";
 } else if(mnth==10) {
  name = "Novembre";
 } else if(mnth==11) {
  name = "Dicembre";
 }

 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 getGiorniInMese (dayOfWeek1,noOfDaysInmnth, Imese, Ianno) {
	// Simulazione chiamata Ajax
	Imese=Imese+1;
	//alert ("Imese:"+Imese+" - Ianno:"+Ianno);
	getEventsRange (dayOfWeek1,noOfDaysInmnth, Ianno+"-"+Imese);
	
/*
	var newArray;
	if ((Imese==11) && (Ianno==2006)) {
		newArray = Array(10, 22, 26);
	} else {
		newArray = Array();
	}
	return newArray;
*/
}

function giornoEvento (Igiorno, IarrayGiorni) {
	var I = 0;
	var minI = 0;
	var maxI = IarrayGiorni.length - 1;
	var s = "";
	
	var elementFound = false;
	
	I = minI - 1;
	while ((I <= maxI) && ( !(elementFound))) {
		I = I + 1;
		s = IarrayGiorni[I];
		elementFound = (Igiorno==s);
	}
	if (elementFound) {
		return true;
	} else {
		return false;
	}
}

function fillDates(dayOfWeek1,noOfDaysInmnth) {

	
 for(var i=1; i<43; i++) {
   str = "s"+i;
   document.forms[1].elements[str].value="   ";
   document.forms[1].elements[str].className = 'btnNormal';
 }


 startSlotIndx = dayOfWeek1;
 slotIndx = startSlotIndx;

 for(var i=1; i<(noOfDaysInmnth+1); i++) {
  slotName = "s"+slotIndx;

  val="";
  if (i<10) {
    val = " "+i+" ";
  } else {
    val = i;
  }

  document.forms[1].elements[slotName].value = val ;
  if (giornoEvento (val, GlobalArrayGiorniEventi)) document.forms[1].elements[slotName].className='btnEvents';
  slotIndx++;
 }
   
 /*  for (j=i; j<42; j++) {
 	slotName = "s"+slotIndx;
  	document.forms[0].elements[slotName].style.visibility="hidden";
	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(); /*0-6*/
  yr = dt.getFullYear(); /*4-digit year*/

  //alert("month:"+(mnth+1)+":dayofMnth:"+dayOfMnth+":dayofweek:"+dayOfWeek+":year:"+yr);

  mnthName = getMonthName(mnth)+ " ";
  document.forms[1].month.value = mnth;
  document.forms[1].year.value = yr;
  document.forms[1].currMonth.value = mnth;
  document.forms[1].currYear.value = yr;
  
  document.forms[1].monthYear.value = mnthName+yr;
  //document.forms[1].dateField.value = (mnth+1)+"/"+dayOfMnth+"/"+yr;

  startStr = (mnth+1)+"/1/"+yr;
  dt1 = new Date(startStr);
  dayOfWeek1 = dt1.getDay(); /*0-6*/

  noOfDaysInMnth = getNoOfDaysInMnth(mnth+1,yr);
  //fillDates(dayOfWeek1+1,noOfDaysInMnth, mnth, yr);
  getGiorniInMese(dayOfWeek1+1,noOfDaysInMnth, mnth, yr);
 

}//thisMonth()

/**
 * The function that will be used to display the calendar of the next month.
 */

function nextMonth() {

 var currMnth = document.forms[1].month.value;
 currYr = document.forms[1].year.value;

 if (currMnth == "11") {
    nextMnth = 0;
    nextYr = currYr;
    nextYr++;
 } else {
   nextMnth=currMnth;
   nextMnth++;
   nextYr = currYr;
 }

 mnthName = getMonthName(nextMnth);
 document.forms[1].month.value=nextMnth;
 document.forms[1].year.value=nextYr;
 document.forms[1].monthYear.value= mnthName+" "+nextYr;

 str = (nextMnth+1)+"/1/"+nextYr;
 dt = new Date(str);
 dayOfWeek = dt.getDay();

 noOfDays = getNoOfDaysInMnth(nextMnth+1,nextYr);

 //fillDates(dayOfWeek+1,noOfDays, nextMnth, nextYr);
 getGiorniInMese(dayOfWeek+1,noOfDays, nextMnth, nextYr);

}//nextMonth()

/**
 * The method to display the calendar of the previous month.
 */

function prevMonth() {

 var currMnth = document.forms[1].month.value;
 currYr = document.forms[1].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();

 /***********************************************
  * Remove the comment if do not want the user to 
  * go to any previous month than this current month.
  ***********************************************/

 /*

 runningMonth = document.forms[0].currMonth.value;
 rMonth=runningMonth;
 rMonth++;
 runningYear = document.forms[0].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);
 document.forms[1].month.value=prevMnth;
 document.forms[1].year.value=prevYr;
 document.forms[1].monthYear.value= mnthName+" "+prevYr;

 noOfDays = getNoOfDaysInMnth(prevMnth+1,prevYr);
 //fillDates(dayOfWeek+1,noOfDays, prevMnth, prevYr);
  getGiorniInMese(dayOfWeek+1,noOfDays, prevMnth, prevYr);
}//prevMonth()

