
  var target;
  var depCreated = false;
  var onlyDirect = false;

  function adaptTerminals(startDest) {
    var countryCode = document.getElementById("country"+startDest).value;
    countryCode = window["country"+startDest+"_"+countryCode];
    var terminalBox = document.getElementById("terminal"+startDest);
    var selectedTerminal = terminalBox.value;
    var nrOfOptions = terminalBox.length;
    var newNrOfOptions = window["terminal"+startDest+"_"+countryCode] + 1;
    
    // remove old options
    var boxLength = terminalBox.length;
    for (var i = 1; i < boxLength; i++ ) {
       terminalBox.remove(1);
    }
    
    // add options
    if(newNrOfOptions < 1)
      return;
    if(startDest == "from")
      var currentArray = terminalValuefrom;
    else if(startDest == "to")
      var currentArray = terminalValueto;
    else   
      var currentArray = terminalValue;
    for(var i = 0; i<newNrOfOptions; i++)
      {
      currentOption = document.createElement("option");
      currentOption.value = currentArray[countryCode][i];
      currentOption.text = window["terminalName_"+currentArray[countryCode][i]];
      if (selectedTerminal == currentOption.value) {
         currentOption.selected= true;
      }
      optionIndex = null;
      if (document.all)
        optionIndex = terminalBox.length;
      terminalBox.add(currentOption,optionIndex);
      }
  }

  function updateDest(targetList){
     if(!($('terminalfrom')))
         return;
      if(!(onlyDirect))
         return;
      target = targetList;
      var noChangesChecked = true;
      if (document.getElementById("noChanges")) {
         if (document.getElementById("noChanges").checked == false) {
            noChangesChecked = false;
         }
      }
      if ((target.indexOf("to") > -1) && (noChangesChecked)) {
         var dep = $F('terminalfrom');
      }
      else {
         var dep = "";
      }
      // adapt 'to' list via Ajax
  		var myAjax = new Ajax.Request(
  			baseUrlHelp+"tpl=getTerminals&dep="+dep,
  			{
  				method: 'get',
  				onComplete: updateDestCountry
  			});
  }

  function updateDep(targetList){
      if (depCreated == true) {
         return;
      }
      target = targetList;
      
      // adapt 'from' list via Ajax
  		var myAjax = new Ajax.Request(
  			baseUrlHelp+"tpl=getTerminals",
  			{
  				method: 'get',
  				onComplete: updateDestCountry
  			});
  }

  function updateDestCountry(originalRequest){

   var selectedCountry = $F('countryto');
   delete returnJourney;
   returnJourney = new Array();
   var xmlCountry = originalRequest.responseXML;
   var countriesTmp = xmlCountry.getElementsByTagName('country');
   var countries = new Array();
   for(var a = 0; a < countriesTmp.length; a++)
     {
     countries[a] = new Object();
     countries[a]["value"] = countriesTmp[a].getElementsByTagName('value')[0].firstChild.data;
     countries[a]["countryValue"] = countriesTmp[a].getElementsByTagName('value')[0].firstChild.data;
     countries[a]["text"] = countriesTmp[a].getElementsByTagName('text')[0].firstChild.data;
     countries[a]["terminals"] = countriesTmp[a].getElementsByTagName('term');
     }
   destcountry = new Array();
   currentCountryList = new Array();
   for(var i=0; i< countries.length;i++)
    {
    destcountry[i] = document.createElement("option");
    var countryXML = countries[i];
    var text = countries[i]["text"];
    var value = countries[i]["countryValue"];
    var currentNode;
    /*
    for(var j=0; j< countryXML.childNodes.length;j++)
      {
      currentNode = countryXML.childNodes[j];
      if (currentNode.nodeName == "text")
       {
         text = currentNode.firstChild.data;
       }
      else if (currentNode.nodeName == "value")
        {
         value = currentNode.firstChild.data;
         countries[i]["countryValue"] = value;
        }
      }
      */
    destcountry[i].text = text;
    destcountry[i].value = value;
    if((target == "to") && (value == selectedCountry)){
       destcountry[i].selected= true;
    }
    window["country"+target+"_"+value] = i;
    }

    // jetzt country liste updaten
    destCountrySelect = document.getElementById("country"+target);
    var countryLength = destCountrySelect.length;
    for (var i = 0; i < countryLength; i++) {
       destCountrySelect.remove(0);
    }
    for (var i = 0; i < destcountry.length; i++)
      {
      optionIndex = null;
      if (document.all)
        optionIndex = destCountrySelect.length;
      destCountrySelect.add(destcountry[i],optionIndex);
      }
    
    // terminal listen je country
   if (target == "from") {
      terminalValuefrom = new Array();
      currentArray = terminalValuefrom;
   }
   else {
      terminalValueto = new Array();
      currentArray = terminalValueto;
   }
   var currentTerminalName;
   for(var i=0; i< countries.length;i++)
    {
    var terminals = countries[i].terminals;
    currentArray[i] = new Array();
    window["terminal"+target+"_"+i] = terminals.length-1;
    for(var j=0;j<terminals.length;j++)
      {
      var currentTerm = terminals[j];
      currentTerminalName = "";
      for(var k = 0; k < currentTerm.childNodes.length; k++)
        {
        var currentNode = currentTerm.childNodes[k];
        if(currentNode.firstChild){
          if (currentNode.nodeName == "value")
            {
            currentArray[i][j] = currentNode.firstChild.data.escapeHTML();
            var currentTerminalId = currentArray[i][j];
            }
          else if (currentNode.nodeName == "text")
            {
            currentTerminalName = currentNode.firstChild.data.escapeHTML();
            }
          }
        }
      if(countries[i]["countryValue"] == "00")
        {
        for(var k = 0; k < currentTerm.attributes.length; k++)
          {
          var currentAttribute = currentTerm.attributes[k];
          if((currentAttribute.nodeName == "ret") && (currentAttribute.nodeValue == "yes"))
            returnJourney.push(currentTerminalId);
          }
        // setze Terminal Name (nötig, falls Terminal nicht in dep-Liste enthalten)
        if(window["terminalName_"+currentTerminalId])
          {}
        else
          {
          window["terminalName_"+currentTerminalId] = currentTerminalName;
          }
        }
      }
    }
  adaptTerminals(target);
  if (depCreated != true) {
     depCreated = true;
     var tempOnlyDirect = onlyDirect;
     onlyDirect = true;
     updateDest('to');
     onlyDirect = tempOnlyDirect;
  }
  }

  function checkLocation(loc,param){

    var currentElement = document.getElementById("postalCode"+loc);
    if ((currentElement) && (currentElement.value != ""))
        {
        var currentCountry = document.getElementById("countryAdd"+loc);
        if ((currentCountry) && (currentCountry.value != ""))
          {
          document.getElementById("REQ0JourneyStops"+param+"0G").value = currentCountry.value+"-"+currentElement.value;
          document.getElementById("REQ0JourneyStops"+param+"0A").value = 2;
          return true;
          }
        else if((currentElement) && (window["redArrow"+loc]))
           {
           var currentTD = document.getElementById("TDPostCountry"+loc);
           currentTD.innerHTML = redArrow+currentTD.innerHTML;
           document.getElementById("errormessage"+loc).innerHTML = redArrowText;
           window["redArrow"+loc] = false;
           return false;
           }
        else if(currentElement)
           return false;
        }
    var currentElement = document.getElementById("terminal"+loc);
    if (currentElement)
      {
      var currentSelection = currentElement.value;
      if(currentSelection != "")
        {
        document.getElementById("REQ0JourneyStops"+param+"0G").value = currentSelection;
        document.getElementById("REQ0JourneyStops"+param+"0A").value = 1;
        }
      }
    return true;
  }


  function checkForm(){
    // if postalCode and countryAdd are given. submit them, otherwise look if a terminal is selected

    if((typeof(envForm) != "undefined") && (envForm == "quick"))  {
      window.open("about:blank","journeyplanner","width=885,height=590,menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,toolbar=no");
      return true;
    }
    var checkFrom = checkLocation("from","S");
    var checkTo = checkLocation("to","Z");
    if(checkFrom && checkTo){
      return true;
      }
    else
      return false;
  }

  function enableVia(viaNr){
    var checked = document.getElementById("viaTerminalCheck"+viaNr).checked;
    if (checked == false)
      {
      document.getElementById("viaTerminal"+viaNr).disabled = true;
      currentViaTerminal = document.getElementById("viaTerminal"+viaNr).value;
      document.getElementById("viaTerminal"+viaNr).value = "";
      return;
      }
    else
      {
      document.getElementById("viaTerminal"+viaNr).disabled = false;
      if(typeof(currentViaTerminal) != "undefined")
        document.getElementById("viaTerminal"+viaNr).value = currentViaTerminal;
      }
  }

  function updateVia(viaNr){
    var currentViaTerminal = document.getElementById("viaTerminal"+viaNr).value;
    document.getElementById("REQ0JourneyStops"+viaNr+".0G").value = currentViaTerminal;

  }

  function disableAddrInput(loc){
    if(document.getElementById("postalCode"+loc))
      {
      document.getElementById("postalCode"+loc).disabled = true;
      document.getElementById("countryAdd"+loc).disabled = true;
      }
  }

  function disableTerminalInput(loc){
    if (document.getElementById("postalCode"+loc).value != "") {
       document.getElementById("country"+loc).disabled = true;
       document.getElementById("terminal"+loc).disabled = true;
    }
    else {
       if (loc == "from") 
          var param = 'S';
       else
          var param = 'Z';
       document.getElementById("REQ0JourneyStops"+param+"0G").value = "";
    }
  }

  function toggleOnlyDirect(){
     var checkboxOnlyDirect = document.getElementById("noChanges");
     var tempOnlyDirect = checkboxOnlyDirect.checked;
     if (tempOnlyDirect) {
        onlyDirect = true;
        updateDest('to');
     }
     else {
        updateDest('to');
        onlyDirect = false;
     }
     var checkboxVias = document.getElementById("viaChanges");
     if(document.getElementById("viaTerminalCheck1") != null)
       {
       if(checkboxVias.checked) {
         document.getElementById("viaTerminalCheck1").disabled = false;
         document.getElementById("viaTerminalCheck2").disabled = false;
         document.getElementById("viasActivated").disabled = false;
         if(document.getElementById("viaTerminalCheck1").checked)
           document.getElementById("viaTerminal1").disabled = false;
         if(document.getElementById("viaTerminalCheck2").checked)
           document.getElementById("viaTerminal2").disabled = false;
         }
       else {
         document.getElementById("viasActivated").disabled = true;
         document.getElementById("viaTerminalCheck1").disabled = true;
         document.getElementById("viaTerminalCheck2").disabled = true;
         document.getElementById("viaTerminal1").disabled = true;
         document.getElementById("viaTerminal2").disabled = true;
         }
       }
  }

  function checkSelection(){
     if (!(document.getElementById("noChanges").checked)) 
        return;
     if (document.getElementById("terminalfrom").value == "") 
        return;
     onlyDirect = true;
     updateDest('to');
  }

  function checkStatus(){
     var terminalIdFrom = document.getElementById("REQ0JourneyStopsS0G").value;
     var terminalIdTo = document.getElementById("REQ0JourneyStopsZ0G").value;
     if ((!(isNaN(terminalIdFrom))) && (terminalIdFrom > 0)) {
         document.getElementById("terminalfrom").value = terminalIdFrom;
         var country = Math.floor(terminalIdFrom/100000);
         document.getElementById("countryfrom").value = country;
         adaptTerminals("from");
     }
     else if(document.getElementById("REQ0JourneyStopsS0ID").value != "")
       {
       var terminalIdFrom = document.getElementById("REQ0JourneyStopsS0ID").value;
       var termIdFromSplit = terminalIdFrom.split("@");
       for(var i = 0; i < termIdFromSplit.length; i++)
         {
         if(termIdFromSplit[i].substr(0,2) == "L=")
           {
           var terminalFrom = termIdFromSplit[i].substr(2);
           // remove '0' at beginning
           var subStrStart = 0;
           while(terminalFrom.charAt(subStrStart) == '0')
             subStrStart++;
           terminalFrom = terminalFrom.substr(subStrStart);
           document.getElementById("terminalfrom").value = terminalFrom;
           var country = Math.floor(terminalFrom/100000);
           document.getElementById("countryfrom").value = country;
           adaptTerminals("from");
           break;
           }
         }
       }
     else if((document.getElementById("REQ0JourneyStopsS0G").value == "") && (terminalfrom == ""))
     {
       if(countryfrom != "")
         document.getElementById("countryfrom").value = countryfrom;
       else
         document.getElementById("countryfrom").value = "80";
       adaptTerminals("from");
       document.getElementById("terminalfrom").selectedIndex = 0;
       if(document.getElementById("countryAddfrom"))
         {
         document.getElementById("countryAddfrom").value = "D";
         document.getElementById("postalCodefrom").value = "";
         }
     }
     if ((!(isNaN(terminalIdTo))) && (terminalIdTo > 0)) {
         document.getElementById("terminalto").value = terminalIdTo;
         var country = Math.floor(terminalIdTo/100000);
         document.getElementById("countryto").value = country;
         adaptTerminals("to");
     }
     else if(document.getElementById("REQ0JourneyStopsZ0ID").value != "")
       {
       var terminalIdTo = document.getElementById("REQ0JourneyStopsZ0ID").value;
       var termIdToSplit = terminalIdTo.split("@");
       for(var i = 0; i < termIdToSplit.length; i++)
         {
         if(termIdToSplit[i].substr(0,2) == "L=")
           {
           var terminalTo = termIdToSplit[i].substr(2);
           // remove '0' at beginning
           var subStrStart = 0;
           while(terminalTo.charAt(subStrStart) == '0')
             subStrStart++;
           terminalTo = terminalTo.substr(subStrStart);
           document.getElementById("terminalto").value = terminalTo;
           var country = Math.floor(terminalTo/100000);
           document.getElementById("countryto").value = country;
           adaptTerminals("to");
           break;
           }
         }
       }
     else if((document.getElementById("REQ0JourneyStopsZ0G").value == "") && (terminalto == ""))
     {
       if(countryto != "")
         document.getElementById("countryto").value = countryto;
       else
       document.getElementById("countryto").value = "80";
       adaptTerminals("to");
       document.getElementById("terminalto").selectedIndex = 0;
       if(document.getElementById("countryAddto"))
         {
         document.getElementById("countryAddto").value = "D";
         document.getElementById("postalCodeto").value = "";
         }
     }
     checkSelection();
     if (document.getElementById("REQ0JourneyStopsS0A").value == 1) {
        disableAddrInput("from");
     }
     else if (document.getElementById("REQ0JourneyStopsS0A").value == 2) {
        disableTerminalInput("from");
     }
     if (document.getElementById("REQ0JourneyStopsZ0A").value == 1) {
        disableAddrInput("to");
     }
     else if (document.getElementById("REQ0JourneyStopsZ0A").value == 2) {
        disableTerminalInput("to");
     }
     if ((document.getElementById("countryfrom").value == "00") && (document.getElementById("terminalfrom").value == "")) {
        document.getElementById("countryfrom").value = 80;
        adaptTerminals("from");
     }
     if ((document.getElementById("countryto").value == "00") && (document.getElementById("terminalto").value == "")) {
        document.getElementById("countryto").value = 80;
        adaptTerminals("to");
     }
     if ((document.getElementById("countryAddfrom")) && (document.getElementById("countryAddfrom").value == "")) {
        document.getElementById("countryAddfrom").value = "D";
     }
     if ((document.getElementById("countryAddto")) && (document.getElementById("countryAddto").value == "")) {
        document.getElementById("countryAddto").value = "D";
     }
  }

  function selectTerminalDeparture(terminalId) {
    errorTerminalNotFound = "nicht als Startterminal";
    if(document.getElementById("terminal"))
      selectTerminal(terminalId,"");
    else
      selectTerminal(terminalId,"from");
  }

  function selectTerminalDestination(terminalId) {
    errorTerminalNotFound = "mit den aktuellen Einstellugen nicht als Zielterminal";
    selectTerminal(terminalId,"to");
  }

  function selectTerminal(terminalId, depDest) {
    if(typeof(terminalId) == "number")
      terminalId = terminalId.toString();
    var countrySelection = $("country"+depDest);
    // check if the country must be selected first
    var selectCountry = true;
    if(countrySelection.value == "00")
      selectCountry = false;
    else if(countrySelection.value == terminalId.substring(0,2))
      selectCountry = false;
    if(selectCountry) 
      {
      var newCountryCode = terminalId.substring(0,2);
      countrySelection.value = newCountryCode;  
      // if the selected value hasn't changed, there is no option with the desired value
      if((countrySelection.value != newCountryCode) && (typeof(envForm) == "undefined"))
        {
        //alert("Das Terminal "+ terminalId+ " kann "+errorTerminalNotFound+" gewählt werden");
        $("errorTerminal"+depDest).style.display = "block";
        return;
        }
      adaptTerminals(depDest);
      if(typeof(envForm) == "undefined")
        disableAddrInput(depDest);
      }
    // select terminal
    var terminalSelection = $("terminal"+depDest);
    terminalSelection.value = terminalId;
    if((terminalSelection.value != terminalId) && (typeof(envForm) == "undefined"))
      {
      //alert("Das Terminal "+ terminalId+ " kann "+errorTerminalNotFound+" gewählt werden");
      $("errorTerminal"+depDest).style.display = "block";
      return;
      }
    if(depDest == "from")
      updateDest('to');
  }


  var FPCreated = false;
  
  function getToday(){
     today = new Date();
     todayDay = today.getDate();
     todayMonth = today.getMonth()+1;
     todayYear = today.getFullYear();
  }

   function getFP(){
       if (FPCreated == true) {
          return;
       }
       FPCreated = true;
       // get FP  via Ajax
       var myAjax = new Ajax.Request(
          baseUrlQuery,
          {
             method: 'get',
             onComplete: updateFP
          });
   }

   function updateFP(originalRequest){
     var xmlResponse = originalRequest.responseXML;
     var beginFPObject = xmlResponse.getElementsByTagName('begin');
     var endFPObject = xmlResponse.getElementsByTagName('end');
     var todayFPObject = xmlResponse.getElementsByTagName('today');
     var beginFP = beginFPObject[0].firstChild.data;
     var endFP = endFPObject[0].firstChild.data;
     var todayFP = todayFPObject[0].firstChild.data;
     beginFPYear = 2000+beginFP % 100;
     beginFPMonth = Math.floor((beginFP % 10000)/100);
     beginFPDay = Math.floor(beginFP/10000);
     endFPYear = 2000+endFP % 100;
     endFPMonth = Math.floor((endFP % 10000)/100);
     endFPDay = Math.floor(endFP/10000);
     todayFPYear = 2000+todayFP % 100;
     todayFPMonth = Math.floor((todayFP % 10000)/100);
     todayFPDay = Math.floor(todayFP/10000);

     beginMonth = beginFPYear*100 + beginFPMonth;
     beginDay = beginFPDay;
     endMonth = endFPYear*100 + endFPMonth;
     endDay = endFPDay;

     // is today in FP?
     if(!(dateInFP(todayDay,todayMonth,todayYear))){
      todayYear = todayFPYear;
      todayMonth = todayFPMonth;
      todayDay = todayFPDay;
      document.getElementById("REQ0JourneyDate").value=todayDay+"."+(todayMonth)+"."+todayYear;
     }

     // year and month
     yearMonthList = document.getElementById("journeyMonth");
     yearMonthList.innerHTML = "";
     for(var currentTP = beginFPYear; currentTP <= endFPYear; currentTP++)
       {
       for(var currentMonth = 1; currentMonth <= 12; currentMonth++)
         {
         if(((currentTP > beginFPYear) || (currentMonth >= beginFPMonth)) && ((currentTP < endFPYear) || (currentMonth <= endFPMonth)))
           {
           var option = document.createElement("option");
           option.text = month[currentMonth - 1]+" "+currentTP;
           option.value = currentTP*100 + currentMonth;
           if ((todayYear == currentTP) && (todayMonth == currentMonth))
            option.selected = true;
           optionIndex = null;
           if (document.all)
             optionIndex = yearMonthList.length;
           yearMonthList.add(option, optionIndex);
           }
         }
       }
      

     // day
     dayList = document.getElementById("journeyDay");
     dayList.options[todayDay-1].selected = true;
     if((todayYear == beginFPYear) && (todayMonth == beginFPMonth)){
       for(var i = 1; i< beginFPDay;i++)
         dayList.remove(0);
     }
     if((todayYear == endFPYear) && (todayMonth == endFPMonth)){
       for(var i = endFPDay+1; i<= 31;i++)
         dayList.remove(endFPDay);
     }

     // set sisabled ranges in calendar
     cal.addDisabledDates2(null, beginFPMonth+"/"+beginFPDay+"/"+beginFPYear);
     cal.addDisabledDates2(endFPMonth+"/"+endFPDay+"/"+endFPYear, null);



   }

   function dateInFP(day,month,year){
     if(FPCreated == false)
       return false;
     if ((year < beginFPYear) || (year > endFPYear))
       return false;
     if (year == beginFPYear){
      if(month < beginFPMonth)
       return false;
      if((month == beginFPMonth) && (day < beginFPDay))
       return false;
      return true;
     }
     else if(year == endFPYear){
       if (month > endFPMonth)
         return false;
       if ((month == endFPMonth) && (day > endFPDay))
         return false;
       return true;
     }
     return true;
   }

   function updateDate(){
     var selectedDay = dayList.value;
     var selectedMonth = document.getElementById("journeyMonth").value;
     var currentMonth = selectedMonth % 100;
     var currentYear = (selectedMonth - currentMonth) / 100;
     document.getElementById("REQ0JourneyDate").value = selectedDay+"."+currentMonth+"."+currentYear;
   }

   function updateMonth(){
     var selectedMonth = document.getElementById("journeyMonth").value;
     var currentMonth = selectedMonth % 100;
     var currentYear = (selectedMonth - currentMonth) / 100;
     if(beginMonth == selectedMonth)
       setFirstDay(beginDay);  
     else
       setFirstDay(1);
     if(endMonth == selectedMonth)
       setLastDay(endDay);
     else
       {
       if(currentMonth == 2)
         {
         if(currentYear % 4 == 0)
           setLastDay(29);
         else
           setLastDay(28);
         }
       else
         {
         if(((currentMonth <= 7) && (currentMonth % 2 == 0)) || ((currentMonth > 7) && (currentMonth % 2 == 1)))
           setLastDay(30);
         else
           setLastDay(31);
         } 
       }  
     updateDate();  
   }

   function setDate(year, month, day) {
     if(month < 10) 
       month = "0"+month;

     var yearMonth = year+""+month;
     document.getElementById("journeyMonth").value = yearMonth;
     if(document.getElementById("journeyMonth").value != yearMonth)
       return;
    updateMonth();
    dayList.value = day; 
    updateDate();
   }


   function updateJourneyDate(){
     if(FPCreated == false)
       return;
     var dateField = document.getElementById("REQ0JourneyDate");
     dateField.value = dayList.value +"."+ monthList.value+"."+yearList.value;
   }

   function updateSingleMonth(){
     if(FPCreated == false)
       return;
     todayMonth = document.getElementById("journeySingleMonth").value
     if((todayYear == beginFPYear) && (todayMonth == beginFPMonth))
       setFirstDay(beginFPDay);  
     else
       setFirstDay(1);
     if((todayYear == endFPYear) && (todayMonth == endFPMonth))
       setLastDay(endFPDay);
     else
       {
       if(todayMonth == 2)
         {
         if(todayYear % 4 == 0)
           setLastDay(29);
         else
           setLastDay(28);
         }
       else
         {
         if(((todayMonth <= 7) && (todayMonth % 2 == 0)) || ((todayMonth > 7) && (todayMonth % 2 == 1)))
           setLastDay(30);
         else
           setLastDay(31);
         } 
       }  
     updateJourneyDate();  
   }

   function updateYear(){
    todayYear = yearList.value;
    if(todayYear == beginFPYear)
     setFirstMonth(beginFPMonth); 
    else
     setFirstMonth(1);
    if(todayYear == endFPYear)
     setLastMonth(endFPMonth); 
    else
     setLastMonth(12);
    updateSingleMonth();
   }

  function setFirstMonth(firstMonth){
   firstMonth = parseInt(firstMonth);
   var currentFirst = monthList.options[0].value;
   if(currentFirst < firstMonth){
      // remove options
      for(i= currentFirst ;i < firstMonth; i++)
        {
        monthList.options[0] = null;
        }
   }
   else if(currentFirst > firstMonth){
     // add options
     for(var i = firstMonth ;i < currentFirst; i++)
       {
       var tempIndex = currentFirst-i;
       currentOption = document.createElement("option");
       currentOption.text = month[tempIndex-1];
       currentOption.value = tempIndex;
       var nextOption = monthList.options[0];
       if (document.all)
         nextOption = 0;
       monthList.add(currentOption, nextOption);
       }
   }
  }

  function setLastMonth(lastMonth){
    var currentLast = monthList.options[monthList.length-1].value;
    if(currentLast < lastMonth)
      {
        // add options
        for(var i = currentLast ;i < lastMonth; i++)
          {
          var optionIndex = null;
          tempIndex = parseInt(i)+1;
          currentOption = document.createElement("option");
          currentOption.text = month[i];
          currentOption.value = tempIndex;
          if (document.all)
            optionIndex = monthList.length;
          monthList.add(currentOption, optionIndex);
          }
      }
    else if(currentLast > lastMonth)
      {
      // remove options
      for(i= lastMonth ;i < currentLast; i++)
        {
        monthList.options[monthList.length-1] = null;
        }
      }
  }

  function setFirstDay(firstDay){
    firstDay = parseInt(firstDay);
    var currentFirst = dayList.options[0].value;
    if(currentFirst < firstDay)
      {
      // remove options
      for(i= currentFirst ;i < firstDay; i++)
        {
        dayList.options[0] = null;
        }
      }
    else if(currentFirst > firstDay)
      {
        // add options
        for(var i = firstDay ;i < currentFirst; i++)
          {
          var tempIndex = currentFirst-i;
          currentOption = document.createElement("option");
          currentOption.text = tempIndex;
          currentOption.value = tempIndex;
          var nextOption = dayList.options[0];
          if (document.all)
            nextOption = 0;
          dayList.add(currentOption, nextOption);
          }
      }
  }

  function setLastDay(lastDay){
    var currentLast = dayList.options[dayList.length-1].value;
    if(currentLast < lastDay)
      {
        // add options
        for(var i = currentLast ;i < lastDay; i++)
          {
          var optionIndex = null;
          tempIndex = parseInt(i)+1;
          currentOption = document.createElement("option");
          currentOption.text = tempIndex;
          currentOption.value = tempIndex;
          if (document.all)
            optionIndex = dayList.length;
          dayList.add(currentOption, optionIndex);
          }
      }
    else if(currentLast > lastDay)
      {
      // remove options
      for(i= lastDay ;i < currentLast; i++)
        {
        dayList.options[dayList.length-1] = null;
        }
      }
  }
