Date.prototype.addMonths= function(m) {
  var d = this.getDate();
  this.setMonth(this.getMonth() + m);

  if (this.getDate() < d){
    this.setDate(0);
  }

}
Date.prototype.addYears = function(y) {
  var m = this.getMonth();
  this.setFullYear(this.getFullYear() + y);

  if (m < this.getMonth()) {
    this.setDate(0);
  }
};

Date.prototype.addDays = function(d) {
  this.setDate( this.getDate() + d );
};

Date.prototype.addWeeks = function(w) {
  this.addDays(w * 7);
};

Date.prototype.msPERDAY = 1000 * 60 * 60 * 24;

Date.prototype.getDaysBetween = function(d) {
  d = d.copy();

  d.setHours(this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());

  var diff = d.getTime() - this.getTime();
  return (diff)/this.msPERDAY;
};

Date.prototype.copy = function () {
  return new Date(this.getTime());
};




var UserArrivalDateObject;
var UserDepartureDateObject;
//      alert( myUserSelectedDate.toUTCString() + "  " + myTodayDate.toUTCString() );
function checkmydate(YourFieldObject,YourDateType){

    myReturnObject = correctDate(YourFieldObject.value);


    if(YourDateType == "arrival"){

      //Das Datum liegt in der Vergangenheit. Böses Datum!
      var myTodayDate=new Date();
      var myUserSelectedDate=new Date();

      myUserSelectedDate.setFullYear(myReturnObject.year,myReturnObject.month -1 ,myReturnObject.day);
      UserArrivalDateObject = myUserSelectedDate;

      if(myUserSelectedDate < myTodayDate){
          myTodayDate.addWeeks(1);
          UserArrivalDateObject = myUserSelectedDate;
           //Nachdem wir das Datum korrigiert haben rufen wir uns nochmal selber auf damit die Formatierung paast (5.9.08 - > 05.09.2008)
           myReturnObject = correctDate(myTodayDate.getDate() + "."+ (myTodayDate.getMonth() +1) + "." + myTodayDate.getFullYear());

      }

    }
    if(YourDateType == "departure"){
         if(typeof( UserArrivalDateObject ) == 'object' ){


            // Jetzt überprüfen wir ob das departure Datum nach dem  arrival datum liegt.
            var myUserSelectedDate=new Date();
            myUserSelectedDate.setFullYear(myReturnObject.year,myReturnObject.month -1 ,myReturnObject.day);

//            alert( myUserSelectedDate.toUTCString() + "  " + UserArrivalDateObject.toUTCString() );


            var days = UserArrivalDateObject.getDaysBetween(myUserSelectedDate);
            if(days < 1){

              UserDepartureDateObject = UserArrivalDateObject.copy();
              UserDepartureDateObject.addWeeks(1);
               //Nachdem wir das Datum korrigiert haben rufen wir uns nochmal selber auf damit die Formatierung paast (5.9.08 - > 05.09.2008)
               myReturnObject = correctDate(UserDepartureDateObject.getDate() + "."+ (UserDepartureDateObject.getMonth() +1) + "." + UserDepartureDateObject.getFullYear());
//                .getFullYear() .getMonth() . getDate()
            }

         }
         //alert(UserArrivalDateObject.toUTCString());

    }


    YourFieldObject.value = myReturnObject.day+"."+myReturnObject.month+"."+myReturnObject.year;

}

function correctDate(val){
    val +="";            // int -> char
    val  = val.replace(/\s+/,'');
    val  = val.replace(/,/,'.');
    val  = val.replace(/;/,'.');
    val  = val.replace(/-/,'.');
    val = val.replace('/','.');
    val = val.replace('/','.');
    val = val.replace('/','.');
    val = val.replace('/','.');        
    
    val = OnlyThisChars(val);
            
    var myTodayDate=new Date();
    myDateArray = new Object();

    if ( val == "" ){

        myDateArray.month   = myTodayDate.getMonth() +1;
        myDateArray.day     = myTodayDate.getDate();
        myDateArray.year    = myTodayDate.getFullYear();

        return correctDate(myDateArray.day + "."+ myDateArray.month + "." + myDateArray.year);
        
        //return myDateArray;
    }
    //Es gibt keine Trennzeichen
    if (    val.indexOf(".")    == -1){
        myDay     = val.substring(0, 2);        
        myMonth = val.substring(2, 4);        
        myYear  = val.substring(4, val.length);
        
    }else{
        ourValue    = val.split(".");

           myDay     = ourValue[0] + "";
           myMonth = ourValue[1] + "";
           myYear     = ourValue[2] + "";                
    }


    if( myDay.length == 0 || parseFloat(myDay) == 0 ){
          myDay = myTodayDate.getDate()  + "";

    }
    if( myMonth.length == 0 ){
         myMonth = myTodayDate.getMonth() + 1  + "";
    }
    if(parseFloat(myMonth) > 12){
        myMonth = 12 + "";
    }
    if(parseFloat(myDay) > 31){
        myDay = 31 + "";
    }

    if( myDay.length != 2){
           myDay = "0" + myDay;
    }
    if( myMonth.length != 2){
           myMonth = "0" + myMonth;
    }
    if( isNaN(myYear) || myYear.length == 0){
        myYear = myTodayDate.getFullYear() + "";
    }
    if( myYear.length != 4){
           myYear = "20" + myYear;
    }        

//    val = myDay+"."+myMonth+"."+myYear;


    myDateArray.month = myMonth;
    myDateArray.day   = myDay;
    myDateArray.year  = myYear;

    //Wenn wir hier ankommen haben wir ein "vernüftiges" Datum
    return myDateArray;


}
function OnlyThisChars(sText){

   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   var myReturn = "";
 
   for (i = 0; i < sText.length ; i++){ 
      Char = sText.charAt(i); 
    
      if (ValidChars.indexOf(Char) == -1){
      //   IsNumber = false;
      }else{

      myReturn = myReturn + Char;
      }
   }
   
   return myReturn;
   
}

function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}
