/* Select stations from history-list */
function useHistory(varselect,currentLocation,selectLocType){
    var index  = eval("document.forms[0]."+ varselect +"select.selectedIndex;");
    var text   = eval("document.forms[0]."+ varselect +"select.options[index].text;");
    var triple = eval("document.forms[0]."+ varselect +"select.options[index].value;");
    var type = 7;

    var tokens = triple.split("#");
    for (var index = 0; index < tokens.length; index++)
      {
      // Typ der Location //
      if (tokens[index].charAt(0) == 'A')
        {
        type = tokens[index].charAt(2);
        }
      // Ausgabename der Location //
      if (tokens[index].charAt(0) == 'O')
        {
        var length = 2;
        if (isNaN(tokens[index].charAt(1)) == false)
          {
          length = 3;
          }
        var text = tokens[index].substring(length, tokens[index].length);
        }
      }

    if(triple=="x")
      {eval("document.forms[0]."+ varselect +"select.options[0].selected=true;");}
      else
      {
       /* Set Location Name */
       eval("document.forms[0].REQ0JourneyStops"+currentLocation+"G.value = text;");
       eval("document.forms[0].REQ0JourneyStops"+currentLocation+"ID.value = triple;");
       if(type==7) var ttc = 0; else if(type==1) var ttc = 1; else
       if(type==2) var ttc = 2; else if(type==4) var ttc = 3;
       if(selectLocType == "LIST")
         eval("document.forms[0].REQ0JourneyStops"+currentLocation+"A.selectedIndex = ttc;");
       else if(selectLocType == "RADIO")
         eval("document.forms[0].REQ0JourneyStops"+currentLocation+"A[ttc].checked = true;");
         }
      /* Reset select list*/
      eval("document.forms[0]."+ varselect +"select.selectedIndex = 0;");
    }
/* Link to open a popup-window with any content */
function popUp(url,windowname,style)
    {
    var standard = "top=20,left=200,resizable=no,status=no,toolbar=no,menubar=no,location=no";
    if (style=="pro" || style=="int" || style=="sef") var size = ",scrollbars=yes,width=600,height=500";
    else if (style=="poi") var size = ",scrollbars=yes,width=500,height=480";
    else if (style=="pri") var size = ",scrollbars=yes,width=685,height=600";
    else if (style=="cms") var size = ",scrollbars=yes,width=500,height=400";
    else if (style=="loc") var size = ",scrollbars=no,width=305,height=355";
    else if (style=="qua") var size = ",scrollbars=yes,width=790,height=750";
    else if (style=="cla") var size = ",scrollbars=yes,width=550,height=485";
    else if (style=="icg") var size = ",scrollbars=yes,width=550,height=385";
    newwin = window.open(url,windowname,standard+""+size);
    newwin.focus(self);
    }
/* Show Calendar or Servicedays in Popup */
function openCalendar(url,windowname,journeystatus){
    var standard = "scrollbars=no,resizable=no,status=no,toolbar=no,menubar=no,location=no";
    if (windowname=="Verkehrstage")
    {var size = ",width=320,height=420";} else {var size = ",width=320,height=360";}
    newwin = window.open(url,windowname+""+journeystatus,standard+""+size);
    //newwin.opener = self;
    }

/* Set Date by JavaScript */
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,journeyType,returnDateOnly){
    var getcDate = eval("document.formular.REQ"+journeyType+"JourneyDate.value.toLowerCase();");
    if(getcDate == ""){var nix = 0;} else
    if((getcDate.length > 2) || (getcDate.indexOf("+")!= -1)) /* Abfrage auf '+' benötigt ??? */
      {changeDate(count,journeyType,returnDateOnly);}
    else
      {doWeekday(journeyType,getcDate,returnDateOnly);}
    }

function useOutwardDate(wDay)
   {
   var getcDate = eval("document.formular.REQ0JourneyDate.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);
   var date = cdate.split(".");
   return date;
   }

function doWeekday(journeyType,getcDate,returnDateOnly){
   var wDayOri = eval("document.formular.wDayExt"+journeyType+".value;");
   var wDay = eval("document.formular.wDayExt"+journeyType+".value.toLowerCase();");
   if(journeyType == 0 && returnDateOnly == "yes")
      {var dateField = getjsDate('field');}
   else if(journeyType == 1 && returnDateOnly == "yes")
      {var dateField = useOutwardDate(wDay);}
   else /* if(journeyType == 1 && returnDateOnly == "no") */
      {var dateField = getjsDate('field');}

   var reg = eval("/^ *("+wDay+") */");
   var test = 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(journeyType,gDate,getcDate,wDayOri,days);
      }
   }

/* Get and Calculate new date */
function changeDate(count,journeyType,returnDateOnly){
    var wDay = eval("document.formular.wDayExt"+journeyType+".value;");
    var getcDate = eval("document.formular.REQ"+journeyType+"JourneyDate.value;");
    var reg = eval("/^ *("+wDay+")\, */");
    if(getcDate=="" && journeyType==1 && returnDateOnly=="no")  {var getcDate = getjsDate('full');}
    if(getcDate=="" && journeyType==1 && returnDateOnly=="yes") {var getcDate = eval("document.formular.REQ0JourneyDate.value;");}
    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(journeyType,gDate,cDate,wDay,days);
       }
    }

function setNewDate(journeyType,gDate,cDate,wDay,days)
   {
   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;
   eval("document.formular.REQ"+journeyType+"JourneyDate.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;
   }

/* get current time and calculate new time */
function changeTime(count,journeyType)
    {
    var getTime = eval("document.formular.REQ"+journeyType+"JourneyTime.value;");
    var jsTime = getTime.split(":");
    var jsHours = jsTime[0];
    var jsMinutes = jsTime[1];
    var jsNewHours = (1*jsHours) + (1*count);
    if(jsNewHours >= 24)
        jsNewHours -= 24;
    else if(jsNewHours <0)
        jsNewHours += 24;
    var newTime = "\""+jsNewHours +":"+jsMinutes+"\"";
    var output = "document.formular.REQ"+journeyType+"JourneyTime.value ="+newTime;
    eval(output);
    }

