var spot_a = new Array();
var spot_d = new Array();
var feld_a = new Array();
var feld_d = new Array();

var lauf_year  = vierstelligesJahr();

var calendar_year_a = '2000';
var calendar_year_d = '2000';
var calendar_month_a = '3';
var calendar_month_d = '3';
var startingPos_a;
var startingPos_d;
var focusDay; 
var activeDay_a;
var activeDay_d;
var currentDay_a;
var currentDay_d;
var zugang_a = 0;
var zugang_d = 0;
var focusDay_a;
var focusDay_d;
var monat_wechseln = 0;

var mal_opera =0;

var zuruecksetzen = 0;
var zDatumB;

var zDatumE;
var loc;
function getnumberofdays() //Diese Funktion dient NUR der Ermittlung der Anzahl der Uebernachtungstage
{
    input = document.Reservation;
    anreise = new Date(input.AArrivee.options[input.AArrivee.options.selectedIndex].text, input.MArrivee.options[input.MArrivee.options.selectedIndex].text, input.JArrivee.options[input.JArrivee.options.selectedIndex].text,0,0,0);
    abreise = new Date(input.ADepart.options[input.ADepart.options.selectedIndex].text, input.MDepart.options[input.MDepart.options.selectedIndex].text, input.JDepart.options[input.JDepart.options.selectedIndex].text,0,0,0);
    anreise_zeit = anreise.getTime() / 1000;
    abreise_zeit = abreise.getTime() / 1000;
    var index = Math.floor(abreise_zeit-anreise_zeit)/60/60/24;
    index = Math.ceil(index);
	
}

function setDate() 
{
   var oInput;
   
   oInput = document.Reservation;
 
	// SET DAY, MONTH AND YEAR TO ANREISE
   with (oInput) {
  
   
      var day = focusDay = parseInt(JArrivee.options[JArrivee.selectedIndex].value);      

      var month = parseInt(MArrivee.options[MArrivee.selectedIndex].value);      

      month--;

      var year = parseInt(AArrivee.options[AArrivee.selectedIndex].value);      


 //     alert("day="+day+" month="+month+" year="+year+" which=a");   

        if ( !zDatumB ) { zDatumB = new Date ( year, month, day);}


        displayCalendar(day, month, year, 'a');

  // SET DAY, MONTH AND YEAR TO  ABREISE
      
      var day = focusDay = parseInt(JDepart.options[JDepart.selectedIndex].value);      
      

      var month = parseInt(MDepart.options[MDepart.selectedIndex].value);      
      month--;

        var year = parseInt(ADepart.options[ADepart.selectedIndex].value);      

        if ( !zDatumE ) { zDatumE = new Date ( year, month, day);}

 //    alert("day="+day+" month="+month+" year="+year+" which=d");    

       displayCalendar(day, month, year, 'd');
  }

}

function datum_eintragen()
{
    
 	input = document.Reservation;

	input.JArrivee.selectedIndex = zDatumB.getDate()-1;
	input.MArrivee.selectedIndex = zDatumB.getMonth();
	input.AArrivee.selectedIndex = zDatumB.getFullYear()-lauf_year;
	input.JDepart.selectedIndex = zDatumE.getDate()-1
	input.MDepart.selectedIndex = zDatumE.getMonth();
	input.ADepart.selectedIndex = zDatumE.getFullYear()-lauf_year;

}

function setToday() {
    // SET DAY MONTH AND YEAR TO TODAY'S DATE
  zuruecksetzen = 1;
 /*  Zurueck setzen datum zum Heute */
    zDatumB = new Date();
 
   focusDay = zDatumB.getDate();
   displayCalendar(zDatumB.getDate(), zDatumB.getMonth(), zDatumB.getFullYear(), 'a');

 
   zDatumE.setTime( zDatumB.getTime()  + 86400000 );
   focusDay = zDatumE.getDate();
   displayCalendar(zDatumE.getDate(), zDatumE.getMonth(), zDatumE.getFullYear(), 'd');


    datum_eintragen();
 var nav = navigator.appVersion;
//alert(nav);

 if (window.opera ) {
         if ( parseInt(nav.substr(0,1))>4 ) {
              
	     calendar = window.open('./hrsparser?%%cd_hv["COMPLETE"]&%%cd_hv_name["NEXT_PAGE"]=79','cal','WIDTH=390,HEIGHT=260');
             calendar.location = calendar.location;

        }
         else {
 
            if ( navigator.userAgent.indexOf('Opera 6.0') != -1){
      //            alert("OPERA 6.01");
 
                   opera6_01(mal_opera,loc);
   
                     mal_opera++;  
 
            }
            else {
//               alert("opera7.0");
               calendar.location.reload(loc);
           }

//       calendar.location= loc;
      }
  
  }
  else {
   
   // keine Opera
   
       calendar.location = calendar.location;
  }
  
 
 zuruecksetzen = 0;

 }


function vierstelligesJahr()
{
	var now = new Date();
	var year = now.getYear();
	year += (year < 1900) ? 1900 : 0;
 
	return year;
}


function setnewMonth(number, which) {
    var year  = eval("calendar_year_" + which);
    var month = number;
    var day   = 0;
if ( which == 'a' ) {    

    document.Reservation.MArrivee.selectedIndex = month;
}
else {
    document.Reservation.MDepart.selectedIndex = month;
}

    checkDate(document.Reservation);
      
    setDate();
    
//    displayCalendar(day, month, year, which);

    if ( document.layers && which == 'a' ) {
       monat_wechseln= 1;
    
    }
     if ( navigator.userAgent.indexOf('Opera 6.0') != -1){
                  opera6_01(mal_opera,loc);
  
                    mal_opera++;  
   
     }
 }

function setnewYear(number, which) {
    var month  = eval("calendar_month_" + which);
    var year = number;
    var day   = 0;

if ( which == 'a' ) {    

    document.Reservation.AArrivee.selectedIndex = year-lauf_year;
}
else {
    document.Reservation.ADepart.selectedIndex = year-lauf_year;
}

    checkDate(document.Reservation);
    
    setDate();


//    displayCalendar(day, month, year, which);

    if ( navigator.userAgent.indexOf('Opera 6.0') != -1){
                  opera6_01(mal_opera,loc);
  
                    mal_opera++;  
   
     }


}


function displayCalendar(day, month, year, which) {       

    var markierung = new Array(42);
    var datum = new Array(42);

    var oInput =document.Reservation;
    day = parseInt(day);
    var startingPos  = "";

    if (which == 'a') {
	   calendar_month_a  = parseInt(month);
    	    calendar_year_a  = parseInt(year);
		     spot_a  = markierung; 
		     feld_a  = datum;
    }
    else {
	    calendar_month_d = parseInt(month);
    	     calendar_year_d = parseInt(year);
		      spot_d = markierung; 
		      feld_d = datum;
    }
    
    var i   = 0;
    
    var days         = getDaysInMonth(month+1,year);
    var firstOfMonth = new Date(year, month, 1);
     
	// Konvertierung in deutsche Wochentagausgabe !!
    var help = firstOfMonth.getDay();
   
    
    if (help == 0) { 
        
         startingPos = help+6;
        
    } 
    else { 
    
         startingPos = help-1;
    }

    days += startingPos;

//    alert("help="+help+" startingPos="+startingPos+" days="+days);

	// MAKE BEGINNING NON-DATE BUTTONS BLANK

    for ( i = 0; i < startingPos; i++) {
        datum[i]= '&nbsp;';
    }

   // SET VALUES FOR DAYS OF THE MONTH

    for (i = startingPos; i < days; i++) {
        datum[i]= i-startingPos+1;
    }

   for (i=days; i<42; i++)  {
        datum[i]= '&nbsp;';
   }

   if (which == 'a') {
            startingPos_a = startingPos; 
   }
   else { 
           startingPos_d = startingPos;
   }


   for (i=0; i<42; i++) {
   
	markierung[i] = 'nofocus';
   }

   var a = new Date();
   var lauf_monat_a = a.getMonth();
   var focusDay_a = a.getDate();
   var laufJahr_a = a.getFullYear();

   var d = new Date();
   d.setTime( d.getTime()  + 86400000 );
   var lauf_monat_d = d.getMonth();
   var focusDay_d = d.getDate();
   var laufJahr_d = d.getFullYear();
   
  with (oInput) {

   if ((eval("calendar_month_" + which) == month) && (eval("calendar_year_" + which) == year)) {
               if ( which == 'a' ) { 
		    if ( !focusDay_a ) {
           
		             focusDay_a = parseInt(JArrivee.options[JArrivee.selectedIndex].value);
 		    }

		    if (month == lauf_monat_a && year ==laufJahr_d) {markierung[focusDay_a+startingPos-1] = 'focus';}

		    currentDay_a = focusDay; 

                    if ( day != 0 ) {
	                  if ( (zugang_a ==1 || month != lauf_monat_a) && zuruecksetzen == 0)   { markierung[focusDay+startingPos-1] = 'green';}
                    }
		}
 		else { 
		    if ( !focusDay_d ) {

		             focusDay_d = parseInt(JDepart.options[JDepart.selectedIndex].value);
		       
		    }
         
		    if ( month == lauf_monat_d && year == laufJahr_d) {markierung[focusDay_d+startingPos-1] = 'focus';}
		    currentDay_d = focusDay; 
 
                    if ( day != 0 ) {
	                 if ( (zugang_d == 1 || month != lauf_monat_d) && zuruecksetzen == 0) {   markierung[focusDay+startingPos-1] = 'rot';}
                    }
              
		}
	}
	else { 
 		if (which == 'a') { currentDay_a = -7; }
 		else { currentDay_d = -7; }
	}

    }  //  end with
    
      if ( zugang_a == 0 && which == 'a' ) { zugang_a = 1; }     
      if ( zugang_d == 0 && which == 'd' ) { zugang_d = 1; }     

}

// GET NUMBER OF DAYS IN MONTH
function getDaysInMonth(month,year)  {
    var days;
    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
        month==10 || month==12)  days=31;
    else if (month==4 || month==6 || month==9 || month==11) days=30;
    else if (month==2)  {
        if (isLeapYear(year)) {
            days=29;
        }
        else {
            days=28;
        }
    }
    return (days);
}

// CHECK TO SEE IF YEAR IS A LEAP YEAR
function isLeapYear (Year) {
    if (((Year % 4)==0) && ((Year % 100)!=0) || ((Year % 400)==0)) {
        return (true);
    }
    else {
        return (false);
    }
}

// SET FORM FIELD VALUE TO THE DATE SELECTED
function returnDate(inDay, which)
{   

 var day   = inDay;
    var month = eval("calendar_month_" + which);
  //  month++;

    var year  = eval("calendar_year_" + which);

    var days         = getDaysInMonth(month,year+lauf_year);

    if (which == 'a') {

	activeDay_a = inDay;
	        if (currentDay_a != activeDay_a) { spot_a[currentDay_a+startingPos_a-1] = 'focus'; }
		for (i=0; i<42; i++) {
			if (spot_a[i] != 'focus') {spot_a[i] = 'nofocus';}
			else {activeDay = i; }
		}

                setDays(document.Reservation.JArrivee, month, year);

  		document.Reservation.JArrivee.selectedIndex=inDay-1;
		document.Reservation.MArrivee.selectedIndex=month;
		document.Reservation.AArrivee.selectedIndex=year-lauf_year;

	}
	else {
		activeDay_d = inDay;
		if (currentDay_d != activeDay_d) { spot_d[currentDay_d+startingPos_d-1] = 'focus'; }

		for (i=0; i<42; i++) {
			if (spot_d[i] != 'focus') {
			    spot_d[i] = 'nofocus';
			}
			else {
			    activeDay = i;
			}
		}

               setDays(document.Reservation.JDepart, month, year);

  	       document.Reservation.JDepart.selectedIndex=inDay-1;
	       document.Reservation.MDepart.selectedIndex=month;
	       document.Reservation.ADepart.selectedIndex=year-lauf_year;

      
      }
        getnumberofdays()

        checkDate(document.Reservation);
        setDate();


 var nav = navigator.appVersion;
// alert(navigator.userAgent);
   if (window.opera ) {
         if ( parseInt(nav.substr(0,1))>4 ) {
      
  //              alert("OPERA >= 6.05");
              calendar = window.open('./hrsparser?%%cd_hv["COMPLETE"]&%%cd_hv_name["NEXT_PAGE"]=79','cal','WIDTH=390,HEIGHT=260');
              calendar.location = calendar.location;

         }
         else {
     //            alert("OPERA 6.01 oder 7.01");
            if ( navigator.userAgent.indexOf('Opera 6.0') != -1){
  //                alert("opera 6.01");
   
      
                   opera6_01(mal_opera,loc);
  
                    mal_opera++;  
  
            }
            else {
//               alert("opera7.0");

               calendar.location.reload(loc);
           }
     }
   }
   else {
            
            calendar.location = calendar.location;
  }
}
   function opera6_01(v1, l) {

        var cal = "cal";
        var cal_neu = cal.concat(v1);

        calendar.close();
        calendar=window.open(l, cal_neu, 'WIDTH=390,HEIGHT=260');

       window.calendar.focus();
 }

