// JavaScript Document

var monthLength = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
var monthNames = new Array("dowolny", "styczeń", "luty", "marzec", "kwiecień", "maj", "czerwiec", "lipiec", "sierpień", "wrzesień", "październik", "listopad", "grudzień");

var finderMonth = 100;
var finderMonthTo = 100;
var finderDay = 100;
var finderDayTo = 100;


function newCheck(indexFinderMonth, indexFinderDay, formName, dayName, monthName, any) {
  
  if (any == true) {
    toIndexMonth = indexFinderMonth;
    toIndexDay = indexFinderDay - 1;
  } else {
    toIndexMonth = indexFinderMonth - 1;
    toIndexDay = indexFinderDay - 1;
  }
  eval("document."+formName+"."+monthName+".options.selectedIndex = "+toIndexMonth+";");
  ///************************
  
  if (year/4 == parseInt(year/4)) monthLength[2] = 29;
	
	eval("document."+formName+"."+dayName+".options.length = 0;");
  startPosition = 1;
	if (any == false) var ile = monthLength[(toIndexMonth + 1)];
	else var ile = monthLength[(toIndexMonth)];
	if ((toIndexDay+1) > ile) toIndexDay = ile - 1;

	for (i=startPosition; i<=ile; i++) {
      eval("document."+formName+"."+dayName+".options.add(new Option("+i+","+i+"));");
      	     
     /* if (selectedDay == (i-1)) {
        toIndex = i-1;
        eval("document."+formName+"."+dayName+".options.selectedIndex = "+toIndex+";");
      }*/
  }
  
  ///************************
  eval("document."+formName+"."+dayName+".options.selectedIndex = "+toIndexDay+";");
  
}

function checkDate(formName, dayName, monthName, any) {
	var now = new Date();
	year = now.getYear();
  
 
  eval("var selectedMonth = document."+formName+"."+monthName+".value;");
  eval("var selectedDay = document."+formName+"."+dayName+".options.selectedIndex;");
 

	if (year/4 == parseInt(year/4)) monthLength[2] = 29;
	
	eval("document."+formName+"."+dayName+".options.length = 0;");
  startPosition = 1;
	var ile = monthLength[(selectedMonth)];
	if ((selectedDay+1) > ile) selectedDay = ile - 1;

	for (i=startPosition; i<=ile; i++) {
      eval("document."+formName+"."+dayName+".options.add(new Option("+i+","+i+"));");
      	     
      if (selectedDay == (i-1)) {
        toIndex = i-1;
        eval("document."+formName+"."+dayName+".options.selectedIndex = "+toIndex+";");
      }
  }
  
  if (any == true && (ile == 0 || ile == undefined)) eval("document."+formName+"."+dayName+".options.add(new Option('--',''));");
  monthLength[2] = 28;
}

function defaultMonthFill(formName, dayName, monthName, nextSelect, any) {
  var now = new Date();
	curentMonth = now.getMonth();
  year = now.getFullYear();
  //alert (year);
  var ile = monthNames.length;
  
  if (any == true) startPosition = 0;
  else startPosition = 1;
  if (any == false) {
    if (nextSelect == true) {
      if (curentMonth == 11) toIndex = 0;
      else toIndex = curentMonth + 1;
    } else toIndex = curentMonth;
    
  } else if (any == true) toIndex = 0;
  
  if (nextSelect == false) {
      if (finderMonth != 100) {
          if (any == true) { toIndex = finderMonth; }
          else if (any == false) { toIndex = finderMonth - 1; }
      } 
      
  } else {
      if (finderMonthTo != 100) {
        if (any == true) { toIndex = finderMonthTo; }
        else if (any == false) { toIndex = finderMonthTo - 1; }
      } 
  }
  
  
  var goodYear;
  for (i=startPosition; i<ile; i++) {
    if (any == true && i == 0) {
      p = "''";
      goodYear = "";
    } else {
      p = i;
      if (any == false) {
        if (i < curentMonth + 1) goodYear = year + 1;
        else goodYear = year;
      } else {
        if (i < curentMonth + 1) goodYear = year + 1;
        else goodYear = year;
      }
    }
    eval("document."+formName+"."+monthName+".options.add(new Option('"+goodYear+" "+monthNames[i]+"',"+p+"));");
  }
  //alert("toIndex: " + toIndex);
  eval("document."+formName+"."+monthName+".options.selectedIndex = "+toIndex+";");
  
  
}

function defaultDayFill(formName, dayName, monthName, nextSelect, any) {

    var now = new Date();
	  curentDay = now.getDate();
	  curentMonth = now.getMonth();
	  //alert("! "+ curentMonth);
	  
    if (nextSelect == true && finderDayTo != 100 && finderMonthTo != 100) curentMonth = finderMonthTo - 1;
	  if (nextSelect == false && finderDay != 100 && finderMonth != 100) curentMonth = finderMonth - 1;
	  
	  if (nextSelect == true) {
      if (finderMonthTo == 100) {
        if (curentMonth == 11) var ile = monthLength[1];
        else var ile = monthLength[curentMonth+2];
      } else {
        //alert("!"+ finderMonthTo)
        var ile = monthLength[finderMonthTo];
      }
      
    } else {
      var ile = monthLength[curentMonth+1];
	  }
	  
	  var toIndex = curentDay - 1;
	  
    if (curentDay > ile) curentDay = ile;
    
    if (nextSelect == false && finderDay != 100) {
        toIndex = finderDay - 1;
        
    } else if (nextSelect == true && finderDayTo != 100) {
        toIndex = finderDayTo - 1;
    } 
    //alert("ile: "+ ile +" any:" + any +" finderDay: "+ finderDay +" finderDayTo: "+ finderDayTo +" curentMonth: "+ curentMonth );
    
    if ((any == true && nextSelect == false && finderDay == 100) || (any == true && nextSelect == true && finderDayTo == 100)) {
        
        eval("document."+formName+"."+dayName+".options.add(new Option('--',''));");
    
    } else {
      
      var startPosition=0;
      for (i=startPosition; i<ile; i++) {
        p = i + 1;
        eval("document."+formName+"."+dayName+".options.add(new Option('"+p+"',"+p+"));");
      }
      //alert("toIndex: "+toIndex);
      eval("document."+formName+"."+dayName+".options.selectedIndex = "+toIndex+";");
    }
}

