  var daysTable=new Array ("31", "28", "31", "30", "31","30", "31", "31", "30", "31", "30", "31");
var i=1;
 
   function setDays(dayCheck,monthCheck,yearCheck)
   {
 	var leapYear=false;
 	leapYear=( ( (yearCheck%4==0) && (yearCheck%100!=0) ) || (yearCheck%400==0) )
 
 	if (leapYear) daysTable[1]=29;
 		 else daysTable[1]=28;
 
 	with(dayCheck){
 
 		if (length>daysTable[monthCheck])
 
 			for(i=length-1;i>=daysTable[monthCheck];i--)
 				options[i]=null
 
 		else if (length<daysTable[monthCheck])
 
 			for(i=length;i<daysTable[monthCheck];i++)
 
 				options[i]=new Option(""+(i+1),""+(i+1),false,false)
 	}
   
   //if(navigator.appName=="Netscape") dayCheck.selectedIndex=dayCheck.length-1-daysTable[monthCheck]+dayCheck.selectedIndex+1;
   return true;
 
 }

function addYear ( LaDate )
{
 var LaDateI;
 
   // --- Initialize
   LaDateI = new Date();

   // --- Nächster Monat, inkl. Jahreswechsel
   LaDateI.setFullYear ( LaDate.getFullYear()+1, LaDate.getMonth(),  LaDate.getDate() );
   
 return LaDateI;
}

function addMonth ( LaDate )
{
 var LaDateI;
 
   // --- Initialize
   LaDateI = new Date();

   // --- Nächster Monat, inkl. Jahreswechsel
   LaDateI.setFullYear ( LaDate.getFullYear(), LaDate.getMonth()+1, LaDate.getDate() );
   
 return LaDateI;
}

function addDay ( LaDate, nCount )
{
 var LaDateI;
 
   // --- Initialize
   LaDateI = new Date();
   if ( nCount == null )
      nCount = 1;

   // --- Nächster Tag, inkl. Monats / Jahreswechsel
   LaDateI.setFullYear ( LaDate.getFullYear(), LaDate.getMonth(),  LaDate.getDate()+nCount );
   
 return LaDateI;
}

// --- Globale Variablen
var g_bModifiedB = false;
var g_bModifiedE = false;

function checkDate ( oObj )
{
 var oInput;
 var LaDateAkt;
 var LaDateB;
 var LaDateE;
 var LaDateAbreise;

   // --- Initialize
   oInput        = document.Reservation;
   LaDateAkt     = new Date();
   LaDateAbreise = new Date();
   
   
   // --- default Abreise
   LaDateAbreise.setFullYear ( LaDateAkt.getFullYear(), LaDateAkt.getMonth(), LaDateAkt.getDate()+1 ); 
   


   with ( oInput ) 
   {
//alert ( JArrivee.selectedIndex + ',' + MArrivee.selectedIndex + ','  + AArrivee.selectedIndex );   
//alert ( JArrivee.options[JArrivee.selectedIndex].value + MArrivee.options[MArrivee.selectedIndex].value + AArrivee.options[AArrivee.selectedIndex].value );

         // --- Datum Selektboxen auslesen
	 LaDateB = new Date ( AArrivee.options[AArrivee.selectedIndex].value, 
		              MArrivee.options[MArrivee.selectedIndex].value-1, 
		              JArrivee.options[JArrivee.selectedIndex].value );
         LaDateE = new Date ( ADepart.options[ADepart.selectedIndex].value, 
		              MDepart.options[MDepart.selectedIndex].value-1, 
		              JDepart.options[JDepart.selectedIndex].value );
	
//alert (LaDateB + ', ' +LaDateE);
	   // --- Element auswerten
	   switch ( oObj.name )
	   {
	   case 'JArrivee': // --- Starttag wurde verändert
		   if ( LaDateB.getTime() < LaDateAkt.getTime() )
		   {
		      // --- Wechsel zum nächsten Monat
		      LaDateB = addMonth ( LaDateB );
			 
	              // --- Abreisedatum ggf. auch noch anpassen
	              if (    LaDateE.getTime() == LaDateAbreise.getTime() 
		           || LaDateE.getTime() < LaDateB.getTime() )
			 LaDateE = addDay ( LaDateB );
		   }
		   
		   // --- Nur einmal erlauben
		   g_bModifiedB = true;
	       break;

	   case 'JDepart': // --- Endetag wurde verändert
		   time_diff = LaDateAbreise.getTime() - LaDateE.getTime();
		   // Falls die Zeitdifferenz > 1 Tag ist, dann Monat erhöhen 
		   if ( time_diff > 1000*60*60*24  )
		   {
	            // --- Wechsel zum nächsten Monat
			// --- evtl. Logic Fehler: Wenn der Abreisetag um eins erhoeht wird , anschliessend wieder um eins reduziert wird,
			//  --- erhoeht sich der Monat um eins. Mit der Auskommentierung eruebrigt sich das Problem. aw - 20031126
		      LaDateE = addMonth ( LaDateE );
		      
		      // --- Wechsel auf nächsten Monat (Anreise)
		      if (    LaDateB.getDate()     == LaDateAkt.getDate() 
		           && LaDateB.getMonth()    == LaDateAkt.getMonth() 
		           && LaDateB.getFullYear() == LaDateAkt.getFullYear() 
		           && g_bModifiedB == false )
		         // --- Abreisedatum minus 1
			 LaDateB = addDay ( LaDateE, -1 );
		   }
		   
		   // --- Nur einmal erlauben
		   g_bModifiedE = true;
	       break;

	   case 'MArrivee': // --- Startmonat wurde verändert
		if ( LaDateB.getMonth() < LaDateAkt.getMonth() )
		{
		   // --- Wechsel auf das nächste Jahr
		   if ( LaDateB.getFullYear() == LaDateAkt.getFullYear() )
		   {
		      // --- Wechsel zum nächsten Jahr
		      LaDateB = addYear ( LaDateB );

		      // --- Endemonat ggf. auch noch anpassen
	              if ( LaDateB.getTime() > LaDateE.getTime() && LaDateE.getMonth() == LaDateAkt.getMonth() )
			 LaDateE = addMonth ( LaDateE );
		   }
		}
	        else
	        {
		   // --- Endemonat ggf. auch noch anpassen
	           if ( LaDateB.getTime() > LaDateE.getTime() && LaDateE.getMonth() == LaDateAkt.getMonth() )
		      LaDateE = addMonth ( LaDateE );
		}
	       break;

	   case 'MDepart': // --- Endemonat wurde verändert
		   if ( LaDateE.getTime() < LaDateAkt.getTime() )
		   {
		      // --- Wechsel auf das nächste Jahr
		      if ( LaDateE.getFullYear() == LaDateAkt.getFullYear() )
			 // --- Wechsel zum nächsten Jahr
			 LaDateE = addYear ( LaDateE );
		   }
	       break;

	   case 'AArrivee':
	       break;

	   case 'ADepart':  // --- Endejahr wurde verändert
	       if ( LaDateE.getFullYear() == LaDateAkt.getFullYear() )
		  LaDateB.setFullYear ( LaDateAkt.getFullYear() );
	       break;
	   }

//	alert (LaDateB + ', ' +LaDateE);
	   // --- Startdatum nie kleiner als der aktuelle Tag
	   if ( LaDateB.getTime() < LaDateAkt.getTime() )
	      LaDateB = LaDateAkt;

	   // --- Endedatum immer mind. einen Tag größer als Startdatum
	   if ( LaDateE.getTime() <= LaDateB.getTime() )
	      LaDateE = addDay ( LaDateB );
//	alert (LaDateB + ', ' +LaDateE);

           // --- Comboboxen (Anzahl Tage) anpassen
           setDays ( JArrivee, LaDateB.getMonth(), LaDateB.getFullYear() ); 
           setDays ( JDepart, LaDateE.getMonth(), LaDateE.getFullYear() ); 
            
	   // --- Datums-Selektboxen wieder initialisieren
           JDepart.selectedIndex   = LaDateE.getDate()-1;
	   JArrivee.selectedIndex   = LaDateB.getDate()-1;
           MDepart.selectedIndex = LaDateE.getMonth();
	   MArrivee.selectedIndex = LaDateB.getMonth();
	   ADepart.selectedIndex  = LaDateE.getFullYear() - LaDateAkt.getFullYear();
           AArrivee.selectedIndex  = LaDateB.getFullYear() - LaDateAkt.getFullYear();
           
           
   } // --- with ( oInput ) 
 
 return true;
}

