/*
 * db.js
 * JavaScript Funktionen fuer die Bus- und Bahnintegration.
 *
 */


/** TODO: location change for show and hide function in calendar functionality*/

/** END TODO */


function addDepartureParams(url, ci, st, hn, pc, arrIndex) {
  var newUrl=url;
  var kws = "";

  if (url != "") {
    /* Falls die Methode schon mal aufgerufen wurde loeschen wir alles hinter dem
       Parameter depSt */
    var reg = eval("/&depSt=.*$/");
    newUrl = newUrl.replace(reg,"");
    
    if (st != null && st != "") {
      newUrl = newUrl + "&depSt=" + st;
    }
    if (hn != null && hn != "") {
      newUrl = newUrl + "&depHn=" + hn;
    }
    if (pc != null && pc != "") {
      newUrl = newUrl + "&depPc=" + pc;
    }
    if (ci != null && ci != "") {
      newUrl = newUrl + "&depCi=" + ci;
    }
    if (arrIndex != null && arrIndex != "") {
      newUrl = newUrl + "&arrIndex=" + arrIndex;
    }
  }

  if (document.forms[0]){
    if (document.forms[0].kw) {
      kws=document.forms[0].kw.value;
      if (kws != null && kws != "") {
        newUrl = newUrl + "&kw=" + kws
      }
    }	
  }
  location.href = newUrl;
}
 
function getjsDate(type) {
    var ndate = new Date();
    var t = ndate.getDate();
    var m = 1+(1*ndate.getMonth());
    var g = "g"+ndate.getYear();
    var j = g.substring(3,5);
    if(t<10) t = "0"+t;
    if(m<10) m = "0"+m;
    if(j<10) j = "0"+1*j;
    if(type=="full") {var date = t+"."+m+"."+j; return date;}
    else {var date = new Array(t,m,j); return date;}
}

function checkWeekday(count,returnDateOnly) {
  var getcDate = eval("document.getElementById('datum').value.toLowerCase();");
  if(getcDate == ""){var nix = 0;} else
  if((getcDate.length > 2) || (getcDate.indexOf("+")!= -1)) {
    changeDate(count,returnDateOnly);
  }
  else {doWeekday(getcDate,returnDateOnly);}
}

function checkWeekdayEs(count,returnDateOnly,sel) {
  var getcDate;
  
  if (sel == "start") {
    getcDate = eval("document.getElementById('datum').value.toLowerCase();");
  } else {
    getcDate = eval("document.getElementById('datum').value.toLowerCase();");
  }
  
  if(getcDate == ""){var nix = 0;} else
  if((getcDate.length > 2) || (getcDate.indexOf("+")!= -1)) {
    changeDateEs(count,returnDateOnly,sel);
  }
  else {doWeekdayEs(getcDate,returnDateOnly,sel);}
}

function useOutwardDate(wDay) {
  var getcDate = eval("document.getElementById('datum').value.toLowerCase();");
  var reg = eval("/^ *("+wDay+")\, */");
  var cDate = getcDate.replace(reg,"");
  var t = (cDate.substring(0,cDate.indexOf(".")));
  var m = (cDate.substring((cDate.indexOf(".")+1),cDate.lastIndexOf(".")));
  var j = (cDate.substring((cDate.lastIndexOf(".")+1),cDate.length));
  var date = new Array(t,m,j);
  return date;
}

function doWeekday(getcDate,returnDateOnly){
  var wDayOri = eval("document.getElementById('weekdays').value;");
  var wDay = eval("document.getElementById('weekdays').value.toLowerCase();");
  if(returnDateOnly == "yes") {
    var dateField = getjsDate('field');
  }
  else {
    var dateField = getjsDate('field');
  }

  var reg = eval("/^ *("+wDay+") */");
  var test = null;//getcDate.match(reg);
  if (test!=null) {
    var days = getDaysSince1980((1*("20"+dateField[2])),(1*dateField[1]),(1*dateField[0]));
    var cwd = (wDay.substr((3*(1*days%7)),2));
    var nwd = test[0];
    var nwdidx  = (wDay.indexOf(test[0])/3);
    var cwdidx  = (wDay.indexOf(cwd)/3);
    if(nwdidx < cwdidx) {nwdidx = nwdidx + 7 - cwdidx;}
    else {nwdidx = nwdidx - cwdidx;}
    var newDay = nwdidx;
    var gDate = gregDate(1*newDay+1*(getDaysSince1980((1*("20"+dateField[2])),(1*dateField[1]),(1*dateField[0]))));
    var days = (1*+newDay+1*getDaysSince1980((1*("20"+dateField[2])),(1*dateField[1]),(1*dateField[0])));
    setNewDate(gDate,getcDate,wDayOri,days);
  }
}

function doWeekdayEs(getcDate,returnDateOnly,sel){
  var wDayOri = eval("document.getElementById('weekdays').value;");
  var wDay = eval("document.getElementById('datum').value.toLowerCase();");
  if(returnDateOnly == "yes") {
    var dateField = getjsDate('field');
  }
  else {
    var dateField = getjsDate('field');
  }

  var reg = eval("/^ *("+wDay+") */");
  var test = null;//getcDate.match(reg);
  if (test!=null) {
    var days = getDaysSince1980((1*("20"+dateField[2])),(1*dateField[1]),(1*dateField[0]));
    var cwd = (wDay.substr((3*(1*days%7)),2));
    var nwd = test[0];
    var nwdidx  = (wDay.indexOf(test[0])/3);
    var cwdidx  = (wDay.indexOf(cwd)/3);
    if(nwdidx < cwdidx) {nwdidx = nwdidx + 7 - cwdidx;}
    else {nwdidx = nwdidx - cwdidx;}
    var newDay = nwdidx;
    var gDate = gregDate(1*newDay+1*(getDaysSince1980((1*("20"+dateField[2])),(1*dateField[1]),(1*dateField[0]))));
    var days = (1*+newDay+1*getDaysSince1980((1*("20"+dateField[2])),(1*dateField[1]),(1*dateField[0])));
    setNewDateEs(gDate,getcDate,wDayOri,days,sel);
  }
}

function changeDate(count,returnDateOnly,id) {
  if (!id) id = "datum";
  var wDay = $('#weekdays').val();
  var getcDate = $("#"+id).val();
  var reg = eval("/^ *("+wDay+")\, */");
  var cDate = getcDate.replace(reg,"");
  var ctg = 1*(cDate.substring(0,cDate.indexOf(".")));
  var cmt = 1*(cDate.substring((cDate.indexOf(".")+1),cDate.lastIndexOf(".")));
  var cjr = (cDate.substring((cDate.lastIndexOf(".")+1),cDate.length));
  if(ctg=="" || cmt=="" || cjr=="") {/* NIX */}
  else {
    if(cjr.length==4){cjr = cjr.substring(2,4);};
      var gDate = gregDate(1*count+1*(getDaysSince1980((1*("20"+cjr)),cmt,ctg)));
      var days = (1*count+1*(getDaysSince1980((1*("20"+cjr)),cmt,ctg)));
      setNewDate(gDate,cDate,wDay,days, id);
    }
}

function setNewDate(gDate,cDate,wDay,days,id) {
  if (!id) id = "datum"; 
  var nwd = (wDay.substr((3*(1*days%7)),2));
  if(gDate[0]<10){gDate[0]="0"+gDate[0];}
  if(gDate[1]<10){gDate[1]="0"+gDate[1];}
  var nDate = nwd+", "+gDate[0]+"."+gDate[1]+"."+gDate[2];
  if(nDate.indexOf("NaN")!=-1) var nDate = cDate;
  $('#' + id).val(nDate);
}


function changeDateEs(count,returnDateOnly,sel) {
  var wDay = $('#weekdays').val(); 
  var getcDate;
  if (sel == "start") {
	  getcDate = $('#datum').val();
  } else {
	  getcDate = eval("document.forms[0].datum2.value;");
  }
  var reg = eval("/^ *("+wDay+")\, */");
  var cDate = getcDate.replace(reg,"");
  var ctg = 1*(cDate.substring(0,cDate.indexOf(".")));
  var cmt = 1*(cDate.substring((cDate.indexOf(".")+1),cDate.lastIndexOf(".")));
  var cjr = (cDate.substring((cDate.lastIndexOf(".")+1),cDate.length));
  if(ctg=="" || cmt=="" || cjr=="") {/* NIX */}
  else {
    if(cjr.length==4){cjr = cjr.substring(2,4);};
      var gDate = gregDate(1*count+1*(getDaysSince1980((1*("20"+cjr)),cmt,ctg)));
      var days = (1*count+1*(getDaysSince1980((1*("20"+cjr)),cmt,ctg)));
      setNewDateEs(gDate,cDate,wDay,days,sel);
    }
}

function setNewDateEs(gDate,cDate,wDay,days,sel) {
  var nwd = (wDay.substr((3*(1*days%7)),2));
  if(gDate[0]<10){gDate[0]="0"+gDate[0];}
  if(gDate[1]<10){gDate[1]="0"+gDate[1];}
  var nDate = nwd+", "+gDate[0]+"."+gDate[1]+"."+gDate[2];
  if(nDate.indexOf("NaN")!=-1) var nDate = cDate;
  if (sel == "start") {
	  $('#datum').val(nDate);
  } else {
	  eval("document.forms[0].datum2.value =\""+nDate+"\";");
  }
}

function getDaysSince1980(y, m, d) {
  var daysInMonth = new Array(0,31,59,90,120,151,181,212,243,273,304,334);
  var returnDays = (y-1980)*365;
  returnDays    += (y-1980+3)/4;
  returnDays    += daysInMonth[m-1];
  if(m>2 && isLeapYear(y)==true) returnDays ++;
  returnDays += d;
  returnDays = parseInt(returnDays);
  return returnDays;
}

function isLeapYear(y){if((y%4)==0 && (y%100)!=0 || (y%400)==0) return true;}

function gregDate(daysSince1980){
  var daysSince1980 = parseInt(daysSince1980);
  var daysInMonth = new Array(0,31,59,90,120,151,181,212,243,273,304,334);
  var daysInYear = 366;
  var years      = 0;
  var february_29;
  var n = 11;
  if(daysSince1980 < 0) daysSince1980 = 0;
  while(daysSince1980> daysInYear){
    years ++;
    daysSince1980 -= daysInYear;
    if((years%4)==0) {daysInYear = 366;} else {daysInYear = 365;}
  }
  if((years%4)==0 && daysSince1980>=60) {february_29 = true; daysSince1980 --;} 
  else {february_29 = false;}

  while(n>0 && daysInMonth[n]>=daysSince1980) n--;
  if(february_29==true && daysSince1980 == 59) daysSince1980 ++;
  var d = (daysSince1980-daysInMonth[n]);
  var m = n+1;
  var ty = (years + 1980).toString();
  var y = ty.substring(2,4);
  var gDate = new Array(d,m,y);
  return gDate;
}

/*** Calendar functions ***/
function CalendarRange(returnWindow, returnForm, returnTextField, instancename, startday, startmonth, startyear, howmanydays) {
   //  methods
   //  ~~~
   this.setHowManyDays         = CalendarRange_setHowManyDays;
   this.setHowManyMonths       = CalendarRange_setHowManyMonths;
   this.setFullMonthsOnly      = CalendarRange_setFullMonthsOnly;
   this.setScrollable          = CalendarRange_setScrollable;
   this.nextMonth              = CalendarRange_nextMonth;
   this.prevMonth              = CalendarRange_prevMonth;
   this.draw                   = CalendarRange_draw;
   this.setDate                = CalendarRange_setDate;
   this.getStartDateFromString = CalendarRange_getStartDateFromString;
   this.getStartDateFrom       = CalendarRange_getStartDateFrom;
   this.getEndDateFromString   = CalendarRange_getEndDateFromString;
   this.getEndDateFrom         = CalendarRange_getEndDateFrom;
   this.parseUserDateInput     = CalendarRange_parseUserDateInput;
   this.refReturnField         = CalendarRange_refReturnField;
   this.setDate2               = CalendarRange_setDate2;
   //  fields
   //  ~~~

   this.returnWindow = returnWindow;
   this.name = instancename;
   this.returnForm = returnForm;
   this.returnTextfield = returnTextField;

   parameters= String(window.location.search).split("&");
   for(i= 1; i<parameters.length; i++) {
        if(parameters[i].indexOf("type=")==0) {
             this.returnTextfield = parameters[i].substr(5)
        }
        if(parameters[i].indexOf("formname=")==0) {
             this.returnForm = parameters[i].substr(9);
        }
   }

   this.startday   = startmonth*1;
   this.startmonth = startmonth-1;
   this.startyear  = startyear*1;
   // check start date parameters - default: today
   if ((!this.startday) || (!this.startmonth) || (!this.startyear)) {
      var tempstartdate = new Date();
      this.startday = tempstartdate.getDate();
      this.startmonth = tempstartdate.getMonth();
      this.startyear = tempstartdate.getFullYear();
   }

   this.endday     = null;
   this.endmonth   = null;
   this.endyear    = null;

   this.lastdrawstartday   = null;
   this.lastdrawstartmonth = null;
   this.lastdrawstartyear  = null;
   this.lastdrawendday     = null;
   this.lastdrawendmonth   = null;
   this.lastdrawendyear    = null;

   this.howmanydays = 1;
   this.howmanymonths = 1;
   this.howmanymonthsmax = 4;

   this.selectedDay = this.name+"_df_"+this.startyear+"/"+this.startmonth+"/"+this.startday;
   this.multipleSelect = false;

   this.fullMonthsOnly = true;
   this.oneMonthOnly = true;
   this.scrollable = false;

   this.monthsTexts = new Array();
   this.weekdaysTexts = new Array();
   this.prevMonthHTML = "&lt;";
   this.nextMonthHTML = "&gt;";

   this.closeOnSetDate = false;
   this.showShortYear = false;


   //  initializations
   //  ~~~
   this.setHowManyDays(howmanydays);

   // calc enddate
   var tempdate = new Date(this.startyear, this.startmonth, this.startday);
   var daycount = 0;
   while (daycount <= this.howmanydays) {
      daycount++;
      tempdate.setDate(tempdate.getDate()+1);
   }
   this.endday = tempdate.getDate();
   this.endmonth = tempdate.getMonth();
   this.endyear = tempdate.getFullYear();

   // try to auto-detect parent window (which ultimately holds the inputfield)
   // ~~
   if (this.returnWindow == null) {
     if (window.opener != null) {
       this.returnWindow = "window.opener.document";
     } else {
       this.returnWindow = "window.document";
     }
   }
   
   this.date2 = "false";
   
}

function CalendarRange_setDate2(dateTwo) {
   this.date2 = dateTwo;
}

function CalendarRange_prevMonth() {
   this.startmonth--;
   this.selectedDay = null;
   this.setHowManyDays(90);
   this.draw();
}

function CalendarRange_nextMonth() {
   this.startmonth++;
   this.selectedDay = null;
   this.setHowManyDays(90);
   this.draw();
}

function CalendarRange_setFullMonthsOnly(truefalse) {
  this.fullMonthsOnly = truefalse;
  // recalculate enddate
  this.setHowManyDays(90);
}

function CalendarRange_setScrollable(truefalse) {
   this.scrollable = truefalse;
}

function CalendarRange_setHowManyDays(howmanydays) {
   if (this.fullMonthsOnly == true) {
      // show only full months
      this.startday = 1;
      var tempstartdate = new Date(this.startyear, this.startmonth, this.startday);
      this.howmanydays = 0;
      var savemonth = -1;
      var monthcount = 0;

      while (monthcount <= this.howmanymonths) {
         this.howmanydays++;
         if (tempstartdate.getMonth() != savemonth) {
            savemonth = tempstartdate.getMonth();
            monthcount++;
         }
         tempstartdate.setDate(tempstartdate.getDate()+1);
      }
      tempstartdate.setDate(tempstartdate.getDate()-2);
      this.endyear = tempstartdate.getFullYear();
      this.endmonth= tempstartdate.getMonth();
      this.endday  = tempstartdate.getDate();

   } else {
      // show minimal 14 days and max 4 months
      if (howmanydays < 14) {
         howmanydays = 14;
      }
      this.howmanydays = howmanydays;
      var tempstartdate = new Date(this.startyear, this.startmonth, this.startday);
      var tempenddate   = new Date(this.endyear, this.endmonth, this.endday);
      var prevmonth = tempstartdate.getMonth();
      var savemonth = -1;
      var monthcount = 0;

      if ((tempstartdate != null) && (tempenddate != null)) {
         while ((tempstartdate.getTime() < tempenddate.getTime()) && (howmanydays >= 0) && (tempstartdate.getMonth()-prevmonth < this.howmanymonths))
         {
            tempstartdate.setDate(tempstartdate.getDate()+1);
            howmanydays--;
         }
         while (monthcount <= this.howmanymonths) {
            if (tempstartdate.getMonth() != savemonth) {
               savemonth = tempstartdate.getMonth();
               monthcount++;
            }
           tempstartdate.setDate(tempstartdate.getDate()-1);
         }
         // change enddate to new enddate according to howmanydays
         if (tempstartdate.getTime() <= tempenddate.getTime()) {
            this.endyear = tempstartdate.getFullYear();
            this.endmonth= tempstartdate.getMonth();
            this.endday  = tempstartdate.getDate();
         }
      }
   }
}

function CalendarRange_setHowManyMonths(howmanymonths) {
   if (howmanymonths > this.howmanymonthsmax) {
      howmanymonths = this.howmanymonthsmax;
   }
   this.howmanymonths = howmanymonths;
}

function CalendarRange_getStartDateFrom(ioField) {
   eval("userInput = "+this.returnWindow+"."+this.returnForm+"."+ioField+".value;");
   this.getStartDateFromString(userInput);
}

function CalendarRange_getStartDateFromString(datestring) {
   var startdate = this.parseUserDateInput(datestring);
   this.startday   = startdate.getDate();
   this.startmonth = startdate.getMonth();
   this.startyear  = startdate.getFullYear();

   var enddate = new Date(this.endyear,this.endmonth,this.endday);
   if ((startdate != null) && (enddate != null)) {
      var daycount = 0;
      while (startdate.getTime() <= enddate.getTime()) {
        startdate.setDate(startdate.getDate()+1);
        daycount++;
      }
      // enddate after startdate
      if (daycount == 0) {
         this.endyear = this.startyear;
         this.endmonth= this.startmonth;
         this.endyear = this.startyear;
         this.setHowManyDays(1);
      } else {
         this.setHowManyDays(daycount);
      }
   }
}

function CalendarRange_getEndDateFrom(ioField) {
   eval("userInput = "+this.returnWindow+"."+this.returnForm+"."+ioField+".value;");
   this.getEndDateFromString(userInput);
}


function CalendarRange_getEndDateFromString(datestring) {
   var enddate = this.parseUserDateInput(datestring);
   this.endday   = enddate.getDate();
   this.endmonth = enddate.getMonth();
   this.endyear  = enddate.getFullYear();

   var startdate = new Date(this.startyear,this.startmonth,this.startday);
   if (startdate != null && enddate != null) {
      var daycount = 0;
      while (startdate.getTime() <= enddate.getTime()) {
        startdate.setDate(startdate.getDate()+1);
        daycount++;
      }
      // enddate after startdate
      if (daycount == 0) {
         this.startyear = this.endyear;
         this.startmonth= this.endmonth;
         this.startyear = this.endyear;
         this.setHowManyDays(1);
      } else {
         this.setHowManyDays(daycount);
      }
   }
}


function CalendarRange_parseUserDateInput(userInput){
   wDay = this.weekdaysTexts.join("|");
   reg_exp = eval("/^ *("+wDay+")\, */");
   clearedUserInput = userInput.replace(reg_exp,"");
   userInput_day = (clearedUserInput.substring(0,clearedUserInput.indexOf(".")));
   userInput_month = (clearedUserInput.substring((clearedUserInput.indexOf(".")+1),clearedUserInput.lastIndexOf(".")));
   userInput_year = (clearedUserInput.substring((clearedUserInput.lastIndexOf(".")+1),clearedUserInput.length));
   userInput_day *= 1; userInput_month *= 1; userInput_year *= 1;
   // attention: userInput_month is 1-12-ranged!
   // ~~~
   if(userInput_month!="") {
     userInput_month -= 1;
     if(userInput_month<0) {
       userInput_month = 11;
     } else if(userInput_month>11) {
       userInput_month = 0;
     }
   }
   if(userInput_year!="") {
     if(userInput_year<100) {
       if(userInput_year<50){
          userInput_year+=2000;
        } else {
          userInput_year+=1900;
        }
     } else if (userInput_year < 1000) {
       if(userInput_year<200){
          userInput_year+=1900;
        } else {
          userInput_year+=1000;
        }
     }
   }
   return new Date(userInput_year, userInput_month, userInput_day);
}

function CalendarRange_refReturnField() {
   return this.returnWindow+"."+this.returnForm+"."+this.returnTextfield;
}

//
// This function toggles the given date's style
//
function CalendarRange_setDate(cell) {
   // set the colors by style
   if (this.multipleSelect == true) {
      var tempday = document.getElementById(cell);
      tempday.className = (tempday.className == "active") ? "enabled" : "active";
   } else {
      // select only ONE day
      if (this.selectedDay != null) {
        tempday = document.getElementById(this.selectedDay);
        tempday.className = (tempday.className == "active") ? "enabled" : "active";
      }
      this.selectedDay = cell;
      tempday = document.getElementById(cell);
      tempday.className = (tempday.className == "active") ? "enabled" : "active";

      date = cell.substring((this.name.length)+4,cell.length);
      temp = date.split("/");
      year = temp[0];
      month = temp[1];
      day = temp[2];
      //  implicit typecasting...
      day *= 1; month *= 1; year *= 1;

      // prepare for display
      month = month + 1;
      if (day < 10) {
        day = "0"+day;
      }
      if (month < 10) {
        month = "0"+month;
      }

      // has to be after the previous step for interpreting purposes
      if (this.showShortYear == true) {
         year -= 2000;
         if (year < 10) {
           year = "0"+year;
         }
      }
      date = day+"."+month+"."+year;

      // calculate weekday & begin week with monday
      weekday = (new Date(year,month-1,day)).getDay()-2;
      if (weekday == -1) weekday = 6;
      if (weekday == -2) weekday = 5;

	  if (this.date2 == "true") {	  
	      document.forms[0].datum2.value = this.weekdaysTexts[weekday]+", " + date;
	  } else if (document.getElementById('det_busbahn_datum')){
		  document.getElementById('det_busbahn_datum').value = this.weekdaysTexts[weekday]+", " + date;
	  } else {
		  document.getElementById('datum').value = this.weekdaysTexts[weekday]+", " + date;
	  }
      //hsb
      //temp = this.refReturnField()+"datum.value = this.weekdaysTexts[weekday]+\", \"+date;";
      //eval(temp);

      if (this.closeOnSetDate == true) {
        if (window.opener) {
          window.opener.focus();
          window.close();
        }
      }
   }
}


//
// This function fills the calendar table with the days of
// the selected month and year.
//
function CalendarRange_draw() {
   // saving some time - only redraw if inputs have changed
   if ((this.lastdrawstartday   != this.startday) ||
       (this.lastdrawstartmonth != this.startmonth) ||
       (this.lastdrawstartyear  != this.startyear) ||
       (this.lastdrawendday     != this.endday) ||
       (this.lastdrawendmonth   != this.endmonth) ||
       (this.lastdrawendyear    != this.endyear))
   {
      this.lastdrawstartday   = this.startday;
      this.lastdrawstartmonth = this.startmonth;
      this.lastdrawstartyear  = this.startyear;
      this.lastdrawendday     = this.endday;
      this.lastdrawendmonth   = this.endmonth;
      this.lastdrawendyear    = this.endyear;

      var div = document.getElementById(this.name);

      // create table if it does not already exist
      var table = document.getElementById(this.name+"_table");
      if (table == null) {
         table = document.createElement("TABLE");
         div.insertBefore(table,div.firstChild);
         table.setAttribute("cellSpacing", "0");
         table.style.width = "100%";
         table.id = this.name+"_table";
      }

      // Recycling: remove complete table body...it's recreated => fast delete
      var tbody = document.getElementById(this.name+"_tbody");
      if (tbody != null) {
           tbody.parentNode.removeChild(tbody);
      }

      // (re-)create tbody
      tbody = document.createElement("TBODY");
      table.appendChild(tbody);
      tbody.id = this.name+"_tbody";
      // update header and status texts
      tempdate = new Date(this.startyear,this.startmonth,this.startday);

         // show month name above weekdays
         if (this.oneMonthOnly == true) {
            // create row for month name
            current_row = document.createElement("TR");
            if (this.scrollable == true) {
               mycurrent_cell= document.createElement("TH");
               mycurrent_cell.innerHTML=this.prevMonthHTML;
               mycurrent_cell.className = "enabled";
               mycurrent_cell.setAttribute("style","cursor:pointer");
               mycurrent_cell.id = this.name+"_heading_months_lt";
               mycurrent_cell.onclick = function() {
                  var calid = this.id.substring(0,this.id.indexOf("_heading_months_lt"));
                  var test = eval('calid')+".prevMonth();";
                  eval(test);
               }
               current_row.appendChild(mycurrent_cell);
            }
            mycurrent_cell= document.createElement("TH");
            mycurrent_cell.colSpan = 5;
            mycurrent_cell.innerHTML = this.monthsTexts[tempdate.getMonth()]+"&nbsp;"+tempdate.getFullYear();
          //mycurrent_cell.setAttribute("align","center");
            mycurrent_cell.textAlign="center";
            mycurrent_cell.id = this.name+"_heading_months"+tempdate.getMonth();
            if (this.multipleSelect == true) {
               mycurrent_cell.className = "enabled";
               mycurrent_cell.onclick = function() {
                  var calid = this.id.substring(0,this.id.indexOf("_heading_months"));
                  var month = this.id.substring(this.id.indexOf("_heading_months")+15,this.id.length);
                  var test = eval('calid')+".selectMonth(month);";
                  eval(test);
               }
            } else {
               mycurrent_cell.className = "disabled";
            }
            current_row.appendChild(mycurrent_cell);

            if (this.scrollable == true) {
               mycurrent_cell = document.createElement("TH");
               mycurrent_cell.id = this.name+"_heading_months_gt";
               mycurrent_cell.innerHTML=this.nextMonthHTML;
               mycurrent_cell.setAttribute("style","cursor:pointer",0);
               mycurrent_cell.className = "enabled";
               mycurrent_cell.onclick = function() {
                  var calid = this.id.substring(0,this.id.indexOf("_heading_months_gt"));
                  var test = eval('calid')+".nextMonth();";
                  eval(test);
               }
               current_row.appendChild(mycurrent_cell);
            }

            tbody.appendChild(current_row);
         }

         // write weekday names in first (or second) row
         var row = document.createElement("TR");

         for (d = 0 ; d < 7 ; d++) {
            var cell = document.createElement("TH");
            cell.id = this.name+"_heading_"+d;
            //cell.innerHTML = "<b>"+this.weekdaysTexts[d]+"</b>";
            cell.innerHTML = this.weekdaysTexts[d];
            if (this.multipleSelect == true) {
               cell.className = "enabled";
               cell.onclick = function() {
                  var calid = this.id.substring(0,this.id.indexOf("_heading_"));
                  var day = this.id.substring(this.id.indexOf("_heading_")+9,this.id.length);
                  var test = eval('calid')+".selectDays(day);";
                  eval(test);
               }
            } else {
               cell.className = "disabled";
            }
            row.appendChild(cell);
         }

         tbody.appendChild(row);

        // Calculate skip between first table cell and first one with content (which
        // weekday does the calendar start with)
        // if it is 0 (sunday) set to 6 as the week begins on monday
        daystoskip = (tempdate.getDay()-1 < 0) ? 6 : tempdate.getDay()-1;

        var daysdrawn = 0;
        var newmonth = false;
        var oldtempcolspan = 0;
        var colspan = 1;

        var w = -1;
        var newWeekNeeded = true;
        while (newWeekNeeded == true) {
           w++;

           if ((tempdate.getDate() == 1 && newmonth == true) || (daysdrawn == 0)) {
              // a new month begins...

              // which weekday is the current date (e.g. 1st April 2005 is a Friday (5))?
              // if it is 0 (sunday) set to 6 as the week begins on monday
              daystoskip = (tempdate.getDay()-1 < 0) ? 6 : tempdate.getDay()-1;


                 if (this.oneMonthOnly == false) {
                    // create row for month name
                    current_row = document.createElement("TR");
                    mycurrent_cell= document.createElement("TD");
                    mycurrent_cell.colSpan = 7;
                    mycurrent_cell.innerHTML = "<b>"+this.monthsTexts[tempdate.getMonth()]+" - "+tempdate.getFullYear()+"</b>";
                    mycurrent_cell.id = this.name+"_heading_months"+tempdate.getMonth();
                    if (this.multipleSelect == true) {
                       mycurrent_cell.className = "enabled";
                       mycurrent_cell.setAttribute("align", "center",0);
                       mycurrent_cell.setAttribute("style","cursor:pointer",0);
                       mycurrent_cell.onclick = function() {
                          var calid = this.id.substring(0,this.id.indexOf("_heading_months"));
                          var month = this.id.substring(this.id.indexOf("_heading_months")+15,this.id.length);
                          var test = eval('calid')+".selectMonth(month);";
                          eval(test);
                       }
                    } else {
                       mycurrent_cell.className = "disabled";
                       mycurrent_cell.setAttribute("align", "center",0);
                    }
                    current_row.appendChild(mycurrent_cell);

                    tbody.appendChild(current_row);
                   }

            }

            current_row = document.getElementById(this.name+"_row_"+w);
            if (current_row == null) {
                current_row = document.createElement("TR");
                current_row.id = this.name+"_row_"+w;
                tbody.appendChild(current_row);
            }
               // draws week rows
            for(var d = 0; d < 7; d++) {

                 if ((tempdate.getDate() == 1 && newmonth == false) && (daysdrawn != 0)) {
                    newmonth = true;
                    // append remaining empty cells
                    for (var e = d; e < 7; e++) {
                       cell = document.createElement("TD");
                       cell.className = "disabled";
                       cell.setAttribute("align","center",0);
                       cell.innerHTML = "&nbsp;";
                       if (current_row != null) {
                          current_row.appendChild(cell);
                       } else {
//                          cell.className = "active";
                       }
                    }
                    break;
                 }

              cell = document.createElement("TD");

              if(daystoskip <= 0 && daysdrawn < this.howmanydays) {
                newmonth = false;
                // Change table cells id to represent current displayed date
                cell.id = this.name+"_df_"+tempdate.getFullYear()+"/"+(tempdate.getMonth())+"/"+tempdate.getDate(); //+"-"+((w*7)+d+1);
                // increase already drawn days by one
                daysdrawn += 1;

                cell.innerHTML = tempdate.getDate();
                cell.className = "enabled";
                cell.setAttribute("align","center",0);
                cell.setAttribute("style","cursor:pointer",0);
                tempdate.setDate(tempdate.getDate()+1);
                cell.onclick = function(){
                   var calid = this.id.substring(0,this.id.indexOf("_df"));
                   var test = eval('calid')+".setDate(this.id);";
                   eval(test);
                };

                //  deactivate cell
              } else {
                cell.className = "disabled";
                cell.innerHTML = "&nbsp;";
                cell.onclick = null;
                daystoskip -= 1;
              }

              current_row.appendChild(cell);
            }
            var tempenddate   = new Date(this.endyear, this.endmonth, this.endday);
            if ((tempdate > tempenddate) || (daysdrawn >= this.howmanydays)) {
              newWeekNeeded = false;
            }

        }


    // auto-correction of div size
    document.getElementById(this.name).style.width = "auto";
   }
   // select initial date
   tempday = document.getElementById(this.selectedDay);
   if (tempday != null) {
      tempday.className = "active";
   }
} // - END function Calendar_draw();

function isLeapYear(y){if((y%4)==0 && (y%100)!=0 || (y%400)==0) return true;}


