colorO = "#F8F8F8"; // Orginalny kolor tla
colorZ = "#BDD8F2"; // Kolor zaznaczenia
//colorZ = "#C3DFDE"; // Kolor zaznaczenia

function getElement(item) 
{
  if (document.all) return document.all[item];
  else if (document.getElementById) return document.getElementById(item);
  else return;
}

function setDate(str) {
 if (str == "   ") {
   return;
  }

  mnth1 = getElement('month').value;
  mnth = mnth1;
  mnth++;
  year = getElement('year').value;
  dateStr = mnth+"/"+str+"/"+year;

  dateStr = trim(dateStr);
 
  for(i=0;i<getElement('f_rok').options.length;i++){
	 if (year == getElement('f_rok').options[i].value){
	   idRok = i;
	   getElement('cYear').value = year;
	 }	
  }
  //getElement('dateField').value 			= dateStr;
  getElement('f_dzien').selectedIndex 	= str;
  getElement('f_miesiac').selectedIndex 	= mnth;
  getElement('f_rok').selectedIndex 		= idRok;
  
  getElement('cMon').value = mnth;

  for(var i=1; i<43; i++) {
    str1 = "s"+i;
    getElement(str1).style.backgroundColor = colorO;
    if (getElement(str1).value == str){
	  //document.forms[0].elements[str1].style.textDecoration="underline";
	  //document.forms[0].elements[str1].style.color="red";
	  getElement(str1).style.backgroundColor = colorZ;
	  getElement('cDay').value = getElement(str1).value;
    }
  } 

}//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 = "Styczeń";
 }else if(mnth==1) {
  name = "Luty";
 }else if(mnth==2) {
  name = "Marzec";
 }else if(mnth==3) {
  name = "Kwiecień";
 }else if(mnth==4) {
  name = "Maj";
 } else if(mnth==5) {
  name = "Czerwiec";
 } else if(mnth==6) {
  name = "Lipiec";
 } else if(mnth==7) {
  name = "Sierpień";
 } else if(mnth==8) {
  name = "Wrzesień";
 } else if(mnth==9) {
  name = "Październik";
 } else if(mnth==10) {
  name = "Listopad";
 } else if(mnth==11) {
  name = "Grudzień";
 }

 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 DispDateC(string){
	str = string;
	if(!string) str = "Komuniakt..."
	alert( str + "\n" +  
	       "cDay: " + getElement('cDay').value + "\n" + 
	       "cMon: " + getElement('cMon').value + "\n" + 
	       "cYear: " + getElement('cYear').value
	      );
}

function fillDates(dayOfWeek1,noOfDaysInmnth) {
  //DispDateC("fillDates()")
  for(var i=1; i<43; i++){
    str = "s"+i;
    getElement(str).value="   ";
    getElement(str).style.backgroundColor = colorO;
  }
  if(dayOfWeek1 == 0) startSlotIndx = 7;
  else startSlotIndx = dayOfWeek1;
  slotIndx = startSlotIndx;

  for(var i=1; i<(noOfDaysInmnth+1); i++) {
    slotName = "s"+slotIndx;

   val="";
   if (i<10) val = " "+i+" ";
   else val = i;
   
    
   if (getElement('cDay').value == val) getElement(slotName).style.backgroundColor = colorZ;	

   getElement(slotName).value = val;
   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() {
	
  //DispDateC("thisMonth() " );
  if(getElement('f_rok').value > 0){
	rok = getElement('f_rok').value;
    mie = getElement('f_miesiac').value;
    dzi = getElement('f_dzien').value;
    if(dzi == 0) dzi = 1
	dt = new Date(rok, mie - 1, dzi, 0,0,1,999);
	/*
	alert(
		"getDate: " + dt.getDate() + "\n" + 
		"getMonth: " + dt.getMonth() + "\n" + 
		"getFullYear: " + dt.getFullYear()  + "\n" + 
		"indeks f_rok: " + rok  + "\n" + 
		"indeks f_mon: " + mie  + "\n" +
		"indeks f_day: " + dzi  + "\n" 
		);
	*/	 
  }
  else{
	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*/

//alert('month: '+getElement('month').value + ' currMonth: '+ getElement('currMonth').value + " " + getElement('f_rok').value + '\n' +
//	  'mnth: '+mnth + ' | dayOfMnth: '+ dayOfMnth + ' | dayOfWeek: ' + dayOfWeek + ' | yr: ' + yr + " dt.getDay(): "+dt.getDay());

  mnthName = getMonthName(mnth)+ " ";
  getElement('month').value = mnth;
  getElement('year').value = yr;
  getElement('currMonth').value = mnth;
  getElement('currYear').value = yr;
  
  
  
  getElement('monthYear').value = mnthName+yr;
  //getElement('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);
  fillDates(dayOfWeek1,noOfDaysInMnth);
 
  //}	
}//thisMonth()

/**
 * The function that will be used to display the calendar of the next month.
 */

function nextMonth() {

 var currMnth = getElement('month').value;
 currYr = getElement('year').value;

 if (currMnth == "11") {
    nextMnth = 0;
    nextYr = currYr;
    nextYr++;
 } else {
   nextMnth=currMnth;
   nextMnth++;
   nextYr = currYr;
 }

 mnthName = getMonthName(nextMnth);
 getElement('month').value=nextMnth;
 getElement('year').value=nextYr;
 getElement('monthYear').value= mnthName+" "+nextYr;

 str = (nextMnth+1)+"/1/"+nextYr;
 dt = new Date(str);
 dayOfWeek = dt.getDay();
 noOfDays = getNoOfDaysInMnth(nextMnth+1,nextYr);
  
  
 getElement('f_miesiac').selectedIndex 	= nextMnth+1;
 getElement('cMon').value  = nextMnth+1;
 getElement('cYear').value = nextYr;
 idRokn = 0;
 for(i=0;i<getElement('f_rok').options.length;i++){
	if (nextYr == getElement('f_rok').options[i].value){
	  idRokn = i;
	}	
  }
 getElement('f_rok').selectedIndex 		= idRokn;

 //fillDates(dayOfWeek+1,noOfDays);
 fillDates(dayOfWeek,noOfDays);
}//nextMonth()

/**
 * The method to display the calendar of the previous month.
 */

function prevMonth() {

 var currMnth = getElement('month').value;
 currYr = getElement('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);
 getElement('month').value=prevMnth;
 getElement('year').value=prevYr;
 getElement('monthYear').value= mnthName+" "+prevYr;

 noOfDays = getNoOfDaysInMnth(prevMnth+1,prevYr);
 
 getElement('f_miesiac').selectedIndex 	= prevMnth+1;
 getElement('cMon').value  = prevMnth+1;
 getElement('cYear').value = prevYr;
 
 idRokn = 0;
 for(i=0;i<getElement('f_rok').options.length;i++){
	if (prevYr == getElement('f_rok').options[i].value){
	  idRokn = i;
	}	
  }
 getElement('f_rok').selectedIndex 		= idRokn;
 
 //fillDates(dayOfWeek+1,noOfDays);
 fillDates(dayOfWeek,noOfDays);
 
}//prevMonth()
