// 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;
var defaultMonth = "";
var defaultDay = "";
var defaultMonthTo = "";
var defaultDayTo = "";
function updateFromCalendar(indexFinderMonth, indexFinderDay, formName, dayName, monthName, any) {
  
  var now = new Date();
	curentMonth = now.getMonth();
  year = now.getFullYear();
  
  if (indexFinderMonth < curentMonth) year = year + 1;
  
  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+"));");
  }
  eval("document."+formName+"."+dayName+".options.selectedIndex = "+toIndexDay+";");
  
}



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();
  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 (any == true) toIndex = 0;
      }
      
  } else {
      if (finderMonthTo != 100) {
        if (any == true) { toIndex = finderMonthTo; }
        else if (any == false) { toIndex = finderMonthTo - 1; }
      } else {
        if (any == true) toIndex = 0;
      }
  }
  
  
  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;
	  
    if (curentDay > ile) {
      //curentDay = ile;
      toIndex = ile - 1;
    } else {
     
      toIndex = curentDay - 1;
    }
    
   
    if (nextSelect == false && finderDay != 100) {
        toIndex = finderDay - 1;
        
    } else if (nextSelect == true && finderDayTo != 100) {
        toIndex = finderDayTo - 1;
    } 
    //alert("ile: "+ ile +" curentDay:" + curentDay +" 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);
      //var selIndex = selObj.selectedIndex;
      eval("document."+formName+"."+dayName+".options.selectedIndex = "+toIndex+";");
      
    }
}


//********************** wrzucanie selectów przez dokument.write *************

function defaultMonthWrite(formName, dayName, monthName, nextSelect, any) {
  var now = new Date();
	curentMonth = now.getMonth();
  year = now.getFullYear();
  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 (any == true) toIndex = 0;
      }
      
  } else {
      if (finderMonthTo != 100) {
        if (any == true) { toIndex = finderMonthTo; }
        else if (any == false) { toIndex = finderMonthTo - 1; }
      } else {
        if (any == true) toIndex = 0;
      }
  }
  
  
  var goodYear;
  if (any == false) toIndex = toIndex + 1;
  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;
      }
    }
    
    if (nextSelect == false) {
      if (p == toIndex) defaultMonth = defaultMonth + "<option selected value='"+p+"'>"+goodYear+" "+monthNames[i]+"</option>";
      else defaultMonth = defaultMonth + "<option value='"+p+"'>"+goodYear+" "+monthNames[i]+"</option>";
    } else {
      if (p == toIndex) defaultMonthTo = defaultMonthTo + "<option selected value='"+p+"'>"+goodYear+" "+monthNames[i]+"</option>";
      else defaultMonthTo = defaultMonthTo + "<option value='"+p+"'>"+goodYear+" "+monthNames[i]+"</option>";
    }
  }
 
  if (nextSelect == false) return defaultMonth;
  else return defaultMonthTo;
  
}

function defaultDayWrite(formName, dayName, monthName, nextSelect, any) {

    var now = new Date();
	  curentDay = now.getDate();
	  curentMonth = now.getMonth();
	  
	  
    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 {
        
        var ile = monthLength[finderMonthTo];
      }
      
    } else {
      var ile = monthLength[curentMonth+1];
	  }
	 
	  var toIndex;
	  
    if (curentDay > ile) {
      
      toIndex = ile - 1;
    } else {
     
      toIndex = curentDay - 1;
    }
    
   
    if (nextSelect == false && finderDay != 100) {
        toIndex = finderDay - 1;
        
    } else if (nextSelect == true && finderDayTo != 100) {
        toIndex = finderDayTo - 1;
    } 
    
    
    if ((any == true && nextSelect == false && finderDay == 100) || (any == true && nextSelect == true && finderDayTo == 100)) {
        
        
        return "<option value=''>--</option>";
    
    } else {
      
      var startPosition=0;
      for (i=startPosition; i<ile; i++) {
        p = i + 1;
        
        if (nextSelect == false) {
          if (p == toIndex + 1) defaultDay = defaultDay + "<option selected value='"+p+"'>"+p+"</option>";
          else defaultDay = defaultDay + "<option value='"+p+"'>"+p+"</option>";
        } else {
          if (p == toIndex + 1) defaultDayTo = defaultDayTo + "<option selected value='"+p+"'>"+p+"</option>";
          else defaultDayTo = defaultDayTo + "<option value='"+p+"'>"+p+"</option>";
        }
      }
     
     if (nextSelect == false) return defaultDay;
     else return defaultDayTo;
      
    }
    
    
}




