getSelectedValue=function(selectBox)
{
  if(selectBox.selectedIndex >= 0)
    return selectBox.options[selectBox.selectedIndex].value;
}
selectValue=function(selectBox,valueToSelect)
{
  for(i = 0; i < selectBox.options.length; i++)
  {
    if(selectBox.options[i].value == valueToSelect)
    {
        selectBox.selectedIndex = i;
        break;
    }
  }
}
Controller = Class.create();
Controller.prototype =
{
    initialize: function(site,lang,myForm,containerElement,sumElement,currency,formTemplate)
    {
        this.container = containerElement;
        this.site = site;
        this.lang = lang;
        this.formTemplate = formTemplate;
        this.sumElement = sumElement;
        this.currency = currency;
        this.form = myForm;
        this.cardIndexCounter = 1;
        this.editors = $H();
        this.cardTypes = $H();
        this.calendar = new CalendarAPI(site,lang,'liftkort');

        this.cardOrderInitFunc = null;
        var d = new Date();
        this.birthdateYearLimits = $H({min: (d.getFullYear() - 100), max: d.getFullYear()});
        
        this.existingCardsToLoad = $H();
    },
    getSite: function()
    {
      return this.site;
    },
    getLang: function()
    {
      return this.lang;
    },
    init: function(initStratedgy)
    {
        initStratedgy(this);
    },
    setCardOrderInitFunction: function(fn)
    {
      this.cardOrderInitFunc = fn;
    },
    setCardTypes: function(ct)
    {
        this.cardTypes = ct;
    },
    setFirstDate: function(orderIndex,dateValue)
    {
        var e = this.editors[orderIndex];

        if(e)
          e.setFirstDate(dateValue);
    },
    newCardOrder: function()
    {
       var coe = new CardOrderEditor(this.container,this.cardIndexCounter++,this,this.formTemplate);
       this.editors[coe.getId()] = coe;
       coe.create();

       if(this.cardOrderInitFunc)
           this.cardOrderInitFunc(coe);

       return coe;
    },
    removeCardOrder: function(id)
    {
        var e = this.editors[id];
        if(e != null)
        {
            e.destroy();
            delete this.editors[id];
        }
    },
    getCardOrder: function(id)
    {
        return this.model.getCardOrder(id);
    },
    getCardTypes: function()
    {
      return this.cardTypes;
    },
    isDate: function(d)
    {
      return this.calendar.isDate(d);
    },
    validateFirstDate: function(d,isLoad)
    {
      if(isLoad)
        return this.calendar.validate(d);
      else
      {
        var opt = $H();
        opt['daysForwardKey'] = 'new_card_days_forward';
        opt['closeDaysForwardKey'] = 'new_card_close_days_forward';
        return this.calendar.validate(d,opt);
      }
    },
    getBirthDateYearMinMax: function()
    {
      return this.birthdateYearLimits;
    },
    setBirthDateYearMinMax: function(h)
    {
      this.birthdateYearLimits = h;
    },
    getBirthDateMonths: function()
    {

//      return $H({'01': 'Januari', '02': 'Februari', '03': 'Mars', '04': 'April', '05': 'Maj', '06': 'Juni', '07': 'Juli', '08': 'Augusti','09': 'September', '10': 'Oktober', '11': 'November', '12': 'December'});
      return $H({'01': this.getText('January'),
                 '02': this.getText('February'), 
                 '03': this.getText('Mars'), 
                 '04': this.getText('April'),
                 '05': this.getText('May'),
                 '06': this.getText('June'),
                 '07': this.getText('July'),
                 '08': this.getText('August'),
                 '09': this.getText('September'),
                 '10': this.getText('October'),
                 '11': this.getText('November'),
                 '12': this.getText('December')});
    },
    onStateChanged: function()
    {
        var sum = 0;
        this.editors.each(function(pair) { sum += pair.value.getPrice(); });

        this.sumElement.update(sum + " " + this.currency);
    },
    isValid: function()
    {
      // OK, några skipass ?
      if(this.editors.size() == 0)
      {
        alert(this.getText('TXTXBOKLI004','R2'));
        return false;
      }

      var valid = true;
      this.editors.each(function(pair) { if(!pair.value.isValid()) { valid = false; throw $break;};});

      if(valid)
      {
        // OK, still valid, check dates ..
        var list = new Array();
        var i = 0;
        this.editors.each(function(pair)
            {
                list[i++] = new DatePair(pair.value.getArrdate(),pair.value.getDepdate());
            });

        if(!this.calendar.isWithinSamePeriod(list))
        {
          alert(this.getText('TXTXBOKLI008','R2'));
          return false;
        }

        // OK, finally check so that not seasonpass mixes with others
        var numSeason = 0;
        var numOther = 0;
        this.editors.each(function(pair)
            {
                if(pair.value.getPeriodId() == "SA")
                   numSeason++;
                else
                   numOther++;
            });
        if(numSeason > 0 && numOther > 0)
        {
          // Nope
          alert(this.getText('TXTXBOKLI010','R2'));
          return false;
        }
        // OK, lets check that we don't have any overlapping periods with the same card when
        // loading, first we build a hash with card + period(:s)
        var ourCards = $H();

        this.editors.each(function(pair)
            {
                if(pair.value.isLoad())
                {
                    var v = ourCards[pair.value.getCardNumber()];
                    if(!v)
                        v = $A();
                    v[v.length] = {arrdate: pair.value.getArrdate(), depdate: pair.value.getDepdate()};
                    ourCards[pair.value.getCardNumber()] = v;
                }
            });
        // OK, any old cards already in basket ?
        this.existingCardsToLoad.each(function(pair)
            {
                var v = ourCards[pair.key];
                if(!v)
                    v = $A();
                for(vi = 0; vi < pair.value.length; vi++)
                    v[v.length] = {arrdate: pair.value[vi].arrdate, depdate: pair.value[vi].depdate};
                ourCards[pair.key] = v;
            });

        var invalidCardNo = this._validateIntersectingPeriods(ourCards);

        if(invalidCardNo)
        {
          alert(this.getText('TXTXBOKLI011','R2') + invalidCardNo);
          return false;
        }

        // Everything is fine,
        return true;
      }
      else
          return false;
    },
    _validateIntersectingPeriods: function(ourCards)
    {
        var me = this;
        var invalidCardNo = null;

        ourCards.each(function(pair)
                      {
                          if(me._intersectAny(pair.value))
                          {
                              invalidCardNo = pair.key;
                              throw $break;
                          }
                      });

        return invalidCardNo;
    },
    _intersectAny: function(dateList)
    {
        var others = dateList;

        for(var i = 0; i < dateList.length;i++)
        {
          for(var i2 = 0; i2 < others.length; i2++)
          {
            if(i == i2)
                continue;
                
            if(this._intersect(dateList[i].arrdate,dateList[i].depdate,others[i2].arrdate,others[i2].depdate))
                return true;
          }
        }
        return false;
    },
    _intersect: function(date11,date12,date21,date22)
    {
      return this._includes(date11,date21,date22) || this._includes(date12,date21,date22);
    },
    _includes: function(searchDate,startdate,enddate)
    {
        return searchDate <= enddate && searchDate >= startdate;
    },
    submit: function()
    {
      this.editors.each(function(pair) { pair.value.prepareSubmit(); });
       this.form.submit();
    },
    setExistingCardsToLoad: function(ectl)
    {
      this.existingCardsToLoad = ectl;
    }
};

CardOrderEditor = Class.create();
CardOrderEditor.prototype =
{
    initialize: function(parentElement,cardOrderId,ctrl,template)
    {
        this.cardOrderId = cardOrderId;
        this.parentElement = parentElement;
        this.ctrl = ctrl;
        this.template = template;
        this.days = $H();
    },
    getText: function(k,domain)
    {
      return this.ctrl.getText(k,domain);
    },
    getSite: function()
    {
      return this.ctrl.getSite();
    },
    getLang: function()
    {
      return this.ctrl.getLang();
    },
    getId: function()
    {
        return this.cardOrderId;
    },
    setLoad: function(l)
    {
        if(l)
        {
            this.newCardElement.checked = false;
            this.loadCardElement.checked = true;
        }
        else
        {
            this.newCardElement.checked = true;
            this.loadCardElement.checked = false;
        }

        this.onModeChanged(null);
    },
    isLoad: function()
    {
        return this.loadCardElement.checked;
    },
    getFirstDate: function()
    {
        return this.firstValidDateCtrl.getDate();
    },
    setFirstDate: function(fd)
    {
        this.firstValidDateCtrl.setDate(fd);
    },
    setCardType: function(ct)
    {
      this.cardTypeCtrl.setCardType(ct);
    },
    getCardType: function()
    {
      return this.cardTypeCtrl.getCardType();
    },
    getCardTypes: function()
    {
      return this.ctrl.getCardTypes();
    },
    isValidCardType: function()
    {
      return this.cardTypeCtrl.isValid();
    },
    setCardNumber: function(cardNumber)
    {
        this.cardNumberCtrl.setCardNumber(cardNumber);
    },
    getCardNumber: function()
    {
        return this.cardNumberCtrl.getCardNumber();
    },
    isCardNumber: function()
    {
        this.cardNumberCtrl.isReady();
    },
    isValidCardNumber: function()
    {
        this.cardNumberCtrl.isValid();
    },
    isValidBirthDate: function()
    {
      return this.birthDateCtrl.isValid();
    },
    isValidFirstDate: function()
    {
      return this.firstValidDateCtrl.isValid();
    },
    setFirstName: function(fn)
    {
        this.firstNameElement.value = fn;
    },
    setLastName: function(ln)
    {
        this.lastNameElement.value = ln;
    },
    setBirthdate: function(bd)
    {
      this.birthDateCtrl.setDate(bd);
    },
    setCardNo: function(cn)
    {
      this.cardNoElement.value = cn;
    },
    setRegisterUser: function(ru)
    {
      if(ru)
          this.cardRegisterUserElement.value = "register";
      else
          this.cardRegisterUserElement.value = "ignore";
    },
    setAddrlid: function(addrlid)
    {
      this.addrlidElement.value = addrlid;
    },
    getBirthdate: function()
    {
      return this.birthDateCtrl.getDate();
    },
    isBirthdate: function()
    {
      this.birthDateCtrl.isReady();
    },
    getPrice: function()
    {
      return this.daysCtrl.getPrice();
    },
    getArrdate: function()
    {
      return this.daysCtrl.getArrdate();
    },
    getDepdate: function()
    {
      return this.daysCtrl.getDepdate();
    },
    getPeriodId: function()
    {
      return this.daysCtrl.getPeriod();
    },
    isValidDays: function()
    {
      return this.daysCtrl.isValid();
    },
    setDefaultDays: function(numDays)
    {
      this.daysCtrl.setDefaultDays(numDays);
    },
    setPeriodId: function(periodId)
    {
      this.daysCtrl.setDefaultPeriodId(periodId);
    },
    create: function()
    {
        var div = document.createElement('div');
        this.view = Element.extend(div);

        var show = {cardIndex: this.cardOrderId, tabindex: this.cardOrderId};
        this.view.update(this.template.evaluate(show));

        this.parentElement.appendChild(div);

        // OK, now lets find the elements we need
        this.newCardElement = $('newCard' + this.cardOrderId);
        this.loadCardElement = $('loadCard' + this.cardOrderId);
        this.loadCardLabelElement = $('loadCardLabel' + this.cardOrderId);

        this.cardNumberCtrl = new CardNumberEditor(this,
                                                   $('cardNumberContainer' + this.cardOrderId),
                                                   $('chipId' + this.cardOrderId),
                                                   $('crc' + this.cardOrderId),
                                                   $('origin' + this.cardOrderId),
                                                   $('statusImage' + this.cardOrderId));

        this.firstValidDateCtrl = new FirstDateEditor(this,
                                                      $('firstValidDay' + this.cardOrderId));
        this.firstValidDayLabel = $('firstValidDayLabel' + this.cardOrderId);

        this.openCalendarElement = $('openCalendar' + this.cardOrderId);
        this.removeLinkElement = $('removeCard' + this.cardOrderId);

        this.firstNameElement = $('firstName' + this.cardOrderId);
        this.lastNameElement = $('lastName' + this.cardOrderId);

        this.birthDateCtrl = new DateEditor(this,
                                            $('yyyy' + this.cardOrderId),
                                            $('mm' + this.cardOrderId),
                                            $('dd' + this.cardOrderId) );
        this._initBirthDate();
//statusImageType
        this.cardTypeCtrl = new CardTypeEditor(this,
                                               $('cardType' + this.cardOrderId),
                                               $('statusImageType' + this.cardOrderId),
                                               $('cardTypeContainer' + this.cardOrderId));
//        this.cardTypeContainerElement = $('cardTypeContainer' + this.cardOrderId);

/*
        this.cardTypeElement = $('cardType' + this.cardOrderId);
        this.cardTypeContainerElement = $('cardTypeContainer' + this.cardOrderId);
        this._initCardType();
*/

        this.daysCtrl = new PeriodEditor(this,
                                         $('days' + this.cardOrderId),
                                         $('statusImageDays' + this.cardOrderId));

        this.hintCardInfo = $('hintCardInfo' + this.cardOrderId);
        this.hintOwnerInfo = $('hintOwnerInfo' + this.cardOrderId);
        this.ownerInfoContainer = $('ownerInfoContainer' + this.cardOrderId);
        this.cardTip = $('cardTip' + this.cardOrderId);
        this.cardInfoPart = $('cardInfoPart' + this.cardOrderId);

        this.cardNoElement = $('cardNo' + this.cardOrderId);
        this.cardRegisterUserElement = $('registerUser' + this.cardOrderId);
        this.addrlidElement = $('addrlid' + this.cardOrderId);

        // Install event handlers
        Event.observe(this.removeLinkElement,'click',this.ctrl.removeCardOrder.bind(this.ctrl,this.cardOrderId));
        Event.observe(this.newCardElement,'click',this.onModeChanged.bindAsEventListener(this));
        Event.observe(this.loadCardElement,'click',this.onModeChanged.bindAsEventListener(this));
//        Event.observe(this.cardTypeElement,'change',this.onCardTypeChanged.bindAsEventListener(this));

        this.openCalendarFnc = this.onOpenCalendar.bindAsEventListener(this);
        this.openHelpCardTypeFnc = this.onOpenHelp.bindAsEventListener(this,'HLP009','Cardtype');
        this.helpCardTypeElement = $('helpCardType' + this.cardOrderId);
        this.openHelpDaysFnc = this.onOpenHelp.bindAsEventListener(this,'HLP010','Days/price');
        this.helpDaysElement = $('helpDays' + this.cardOrderId);
        this.observing = false;

        Event.observe(this.firstNameElement,'blur',this.onStateChanged.bindAsEventListener(this));
        Event.observe(this.lastNameElement,'blur',this.onStateChanged.bindAsEventListener(this));

        Event.observe($('newCardLabel' + this.cardOrderId),'click',this.onOpenHelp.bindAsEventListener(this,'HLP011','New card'));
        Event.observe(this.loadCardLabelElement,'click',this.onOpenHelp.bindAsEventListener(this,'HLP012','Load card'));

        this.toMakeTransparent = [$('firstValidDay' + this.cardOrderId),
                                  $('openCalendar' + this.cardOrderId),
                                  $('cardType' + this.cardOrderId),
                                  $('days' + this.cardOrderId),
                                  $('helpCardType' + this.cardOrderId),
                                  $('helpDays' + this.cardOrderId)];

    },
    destroy: function()
    {
        this.view.remove();
    },
    prepareSubmit: function()
    {
        this.cardNumberCtrl.prepareSubmit();
        this.firstValidDateCtrl.prepareSubmit();
        this.birthDateCtrl.prepareSubmit();
        this.daysCtrl.prepareSubmit();

        // Enable form controls (so that they will be posted!)
        this.firstNameElement.enable();
        this.lastNameElement.enable();
        this.birthDateCtrl.enable();
    },
    onStateChanged: function(ctrl)
    {
      // Update form data
      if(this.isLoad())
      {
        if(this.cardNumberCtrl.isValid())
        {
           if(this.cardNumberCtrl.isOwnerInfo())
           {
               this.setFirstName(this.cardNumberCtrl.getFirstName());
               this.setLastName(this.cardNumberCtrl.getLastName());
               this.setBirthdate(this.cardNumberCtrl.getBirthdate());
           }

           this.setAddrlid(this.cardNumberCtrl.getAddrlid());
           this.setCardNo(this.cardNumberCtrl.getCardNo());
           this.setRegisterUser(this.cardNumberCtrl.isRegisterUser());
        }
      }
      else
      {
          this.setAddrlid(0);
          this.setCardNo(0);
          this.setRegisterUser(false);
      }
      // Lets refresh days
//      if(this.isValidBirthDate() && this.isValidFirstDate() && this.isValidCardType())
      this.cardTypeCtrl.refresh();
      this.daysCtrl.refresh();

      this._refreshForm();

      // OK, låt vår kotroller veta också
      if(this.ctrl && this.ctrl.onStateChanged)
          this.ctrl.onStateChanged(this);

      if(ctrl && ctrl.focus)
          ctrl.focus();

    },
    onModeChanged: function(e)
    {
      this.onStateChanged();
    },
    showError: function(ctrl,messageKey,isOwnerInfoRelated)
    {
      if(/^TXT/.test(messageKey))
          var message = this.getText(messageKey,'R2');
      else
          var message = this.getText(messageKey);

      alert(message);
      if(ctrl && ctrl.focus)
          ctrl.focus();
    },
    onCardTypeChanged: function()
    {
      this.onStateChanged();
    },
    onOpenCalendar: function(e)
    {
       // OK, open the calendar in the right way ..
       if(this.isLoad())
           IP_show(this.title,'/app/projects/common/templates/calendar/inlineMain.php?KeepThis=true&bLang='+this.ctrl.getLang()+'&bSite='+this.ctrl.getSite()+'&path=/app/projects/common/&activity=liftkort&mode=days&name=firstDate&callback=setFirstDateFromPopup&name=' + this.getId() + '&TB_iframe=true&height=400&width=300');
       else
           IP_show(this.title,'/app/projects/common/templates/calendar/inlineMain.php?KeepThis=true&bLang='+this.ctrl.getLang()+'&bSite='+this.ctrl.getSite()+'&path=/app/projects/common/&activity=liftkort&mode=days&name=firstDate&daysForwardKey=new_card_days_forward&closeDaysForwardKey=new_card_close_days_forward&callback=setFirstDateFromPopup&name=' + this.getId() + '&TB_iframe=true&height=400&width=300');

    },
    onOpenHelp: function(e,keyTXT,keyTitle)
    {
      var site = this.ctrl.getSite();
      showInlineHelp('TXT' + site.substr(0,1).toUpperCase() + keyTXT,keyTitle,site,this.ctrl.getLang());
    },
    isValid: function()
    {
      return this._validate(this.showError.bind(this));
    },
    _validateOwnerInfo: function(showValidateMessageFunc)
    {
      // OK, lets validate
      if(this.isLoad())
      {
        // OK, hur är status på kortnr ?
        if(!this.cardNumberCtrl.isValid())
        {
            if(showValidateMessageFunc)
                showValidateMessageFunc(this.cardNumberCtrl,'Specify cardnumber',true);

            return false;
        }
      }
      // Puhe namn
      if(this.firstNameElement.value.length == 0)
      {
          if(showValidateMessageFunc)
              showValidateMessageFunc(this.firstNameElement,'Specify firstname',true);

          return false;
      }

      if(this.lastNameElement.value.length == 0)
      {
          if(showValidateMessageFunc)
              showValidateMessageFunc(this.lastNameElement,'Specify lastname',true);

          return false;
      }

      if(!this.birthDateCtrl.isValid())
      {
          if(showValidateMessageFunc)
              showValidateMessageFunc(this.birthDateCtrl,'Specify birthdate',true);

          return false;
      }
      return true;
    },
    _validateCardInfo: function(showValidateMessageFunc)
    {
      // OK, första giltighetsdatum ..
      var resCode = this.ctrl.validateFirstDate(this.getFirstDate(),this.isLoad());

      switch(resCode)
      {
        case 0 : // OK
        break;

        case 1 : // Felaktigt format
        if(showValidateMessageFunc)
            showValidateMessageFunc(this.firstValidDateCtrl,'Specify first valid date',false);

        return false;
        break;

        case 2 : // Brott mot datumregler
        if(showValidateMessageFunc)
            showValidateMessageFunc(this.firstValidDateCtrl,'TXTXBOKLI005',false);

        return false;
        break;

        case 3 : // Funkade inte att kolla, hmm kommer att smälla längre fram så vi ignorerar tillsvidare
//        this.showError(this.firstValidDateCtrl,this.getText('Rider') + ' ' + this.cardOrderId + '\nFunkade inte att kolla datum');
//        return false;
        break;
      }

      if(!this.isValidCardType())
      {
        if(showValidateMessageFunc)
            showValidateMessageFunc(this.cardTypeCtrl,'Select cardtype',false);

        return false;
      }
      if(!this.isValidDays())
      {
        if(showValidateMessageFunc)
            showValidateMessageFunc(this.daysCtrl,'Select days/price',false);

        return false;
      }
      return true;
    },
    _validate: function(showValidateMessageFunc)
    {
      return this._validateOwnerInfo(showValidateMessageFunc) && this._validateCardInfo(showValidateMessageFunc);
    },
    disableForm: function()
    {
      // OK, disable form elements
      this.newCardElement.disable();
      this.loadCardElement.disable();

      this.cardNumberCtrl.disable();

      this.firstNameElement.disable();
      this.lastNameElement.disable();

      this.birthDateCtrl.disable();

      this.cardTypeCtrl.disable();
      this.firstValidDateCtrl.disable();
      this.daysCtrl.disable();

    },
    _setTip: function(t)
    {
      if(t)
          this.cardTip.update('- ' + t);
      else
          this.cardTip.update('');
    },
    showTip: function(ctrl,messageKey,isOwnerInfoRelated)
    {
      if(/^TXT/.test(messageKey))
          var message = this.getText(messageKey,"R2");
      else
          var message = this.getText(messageKey);

      this._setTip(message);

      // OK, vi hanterar pilen ...
      if(isOwnerInfoRelated)
      {
        // OK, vi tänder den vänstra pilen och disablar formulär ..
        this.hintOwnerInfo.removeClassName("white");
        this.hintOwnerInfo.addClassName("orange");

        this.hintCardInfo.removeClassName("orange");
        this.hintCardInfo.addClassName("white");
      }
      else
      {
        this.hintOwnerInfo.removeClassName("orange");
        this.hintOwnerInfo.addClassName("white");

        this.hintCardInfo.removeClassName("white");
        this.hintCardInfo.addClassName("orange");
      }
    },
    _refreshForm: function()
    {
      // OK, disable form elements
      this.newCardElement.enable();
      this.loadCardElement.enable();

      // OK, check the state
      if(this.isLoad())
      {
        this.cardNumberCtrl.show();
        this.cardNumberCtrl.enable();
        this.loadCardLabelElement.update(this.getText('Load card') + ':');

        // OK, check state ..
        var cardState = this.cardNumberCtrl.getCardState();

        switch(cardState)
        {
          case 0 :
          this.ownerInfoContainer.show();
          this.firstNameElement.disable();
          this.lastNameElement.disable();
          this.birthDateCtrl.disable();
          break;

          case 1 :
          this.ownerInfoContainer.show();
          this.firstNameElement.enable();
          this.lastNameElement.enable();
          this.birthDateCtrl.enable();
          break;

          default:
          this.ownerInfoContainer.hide();
        }
      }
      else
      {
        this.cardNumberCtrl.hide();
        this.loadCardLabelElement.update(this.getText('Load card'));

        this.ownerInfoContainer.show();
        this.firstNameElement.enable();
        this.lastNameElement.enable();

        this.birthDateCtrl.enable();
      }
      //OK, det var det nu kollar vi om "vänsterdelen"
      // är komplett, och i sådanafall så skall vi
      // "tända" höger delen, annars skall den vara
      // släckt ..
      if(this._validateOwnerInfo())
      {
        this.firstValidDateCtrl.enable();

        // Handle days ..
        if(this.cardTypeCtrl.isTypeToSelect())
            this.cardTypeCtrl.enable();
        else
            this.cardTypeCtrl.disable();

        // Handle days ..
        if(this.daysCtrl.isPeriodToSelect())
            this.daysCtrl.enable();
        else
            this.daysCtrl.disable();

        this.firstValidDayLabel.style.color = "#000000";

        this.toMakeTransparent.each(function(e)
        {
            e.style.filter = "";
            e.style.opacity = "";
        });
        if(!this.observing)
        {
            Event.observe(this.openCalendarElement,'click',this.openCalendarFnc);
            Event.observe(this.helpCardTypeElement,'click',this.openHelpCardTypeFnc);
            Event.observe(this.helpDaysElement,'click',this.openHelpDaysFnc);

            this.openCalendarElement.style.cursor = "pointer";
            this.helpCardTypeElement.style.cursor = "pointer";
            this.helpDaysElement.style.cursor = "pointer";
            this.observing = true;
        }
      }
      else
      {
        this.firstValidDateCtrl.disable();
        this.cardTypeCtrl.disable();
        this.daysCtrl.disable();

        this.firstValidDayLabel.style.color = "#999999";
        this.toMakeTransparent.each(function(e)
        {
            e.style.opacity = "0.5";
            e.style.filter = "alpha(opacity=50)";
        });

        if(this.observing)
        {
            Event.stopObserving(this.openCalendarElement,'click',this.openCalendarFnc);
            Event.stopObserving(this.helpCardTypeElement,'click',this.openHelpCardTypeFnc);
            Event.stopObserving(this.helpDaysElement,'click',this.openHelpDaysFnc);
            this.openCalendarElement.style.cursor = "default";
            this.helpCardTypeElement.style.cursor = "default";
            this.helpDaysElement.style.cursor = "default";

            this.observing = false;
        }
      }

      // OK, vi uppdaterar "tipsningen"
      if(this._validate(this.showTip.bind(this)))
      {
        this._setTip('');
        this.hintOwnerInfo.removeClassName("orange");
        this.hintOwnerInfo.addClassName("white");

        this.hintCardInfo.removeClassName("orange");
        this.hintCardInfo.addClassName("white");
      }

    },
    _initCardType: function()
    {
       // Load values from ctrl
/*       var types = this.ctrl.getCardTypes();
       if(types.size() == 1)
       {
           this.cardTypeContainerElement.hide();
           this.cardTypeElement.hide();

           // Add and select the first..
           var i = 0;
           var me = this;
           types.each(function(pair) { me.cardTypeElement.options[i++] = new Option(pair.value,pair.key); });
           this.cardTypeElement.selectedIndex = 0;
       }
       else
       {
           this.cardTypeElement.options[0] = new Option(this.getText('Cardtype'),'-');

           // Add and select the first..
           var i = 1;
           var me = this;
           types.each(function(pair) { me.cardTypeElement.options[i++] = new Option(pair.value,pair.key); });
           this.cardTypeElement.selectedIndex = 0;
       }
*/       
    },
    _initBirthDate: function()
    {
        var yearMinMax = this.ctrl.getBirthDateYearMinMax();

        var yyyy = $H();
        var i;
//        for(i = yearMinMax.min; i <= yearMinMax.max; i++)
//            yyyy[i] = i;
        for(i = yearMinMax.max; i >= yearMinMax.min; i--)
            yyyy[i] = i;

        this.birthDateCtrl.setYearOptions(yyyy,this.getText('Year'));

        var months = this.ctrl.getBirthDateMonths();
        this.birthDateCtrl.setMonthOptions(months,this.getText('Month'));

        var dayMinMax = $H({min: 1, max: 31});
        var days = $H();
        for(i = dayMinMax.min; i <= dayMinMax.max; i++)
        {
          var v = String(i);
          if(v.length == 1)
              v = '0' + v;

          days[v] = i;
        }
        this.birthDateCtrl.setDayOptions(days,this.getText('Day'));
    }
};

PeriodEditor = Class.create();
PeriodEditor.prototype =
{
    initialize: function(container,dayElement,dayStatusImageElement)
    {
        this.container = container;
        this.daysElement = dayElement;
        this.statusImage = dayStatusImageElement;
        Event.observe(this.daysElement,'change',this.onDaysChanged.bindAsEventListener(this));
        this.lastRequest = "";
        this.days = $H();
        this.showingMessage = false;
        this.defaultDays = 0;
        this.firstTime = true;
        this.setMessage(this.container.getText('Days/price'));

    },
    refresh: function()
    {
       if(this.container.isLoad())
           load = "yes";
       else
           load = "no";

       var p = $H({site: this.container.getSite() ,lang: this.container.getLang(), firstDate: this.container.getFirstDate(), birthdate: this.container.getBirthdate(), load: load, cardType: this.container.getCardType()});

       if(p.inspect() == this.lastRequest)
           return;

       this.lastRequest = p.inspect();
       if(this.container.isValidBirthDate() && this.container.isValidFirstDate() && this.container.isValidCardType())
       {
         this.container.disableForm();
         this.statusImage.show();

         new Ajax.Request('/app/projects/common/templates/skipass/api/fetchDays.php',{method: 'get',parameters: p,onSuccess: this._onFetchDaysSuccess.bind(this),onFailure: this._onFetchDaysFailed.bind(this)});
       }
       else
        this.container.onStateChanged();
    },
    isValid: function()
    {
      return this.isReady()
    },
    isPeriodToSelect: function()
    {
      return !this.showingMessage && this.daysElement.options.length > 1;
    },
    isReady: function()
    {
      var poollid = getSelectedValue(this.daysElement);
      return poollid > 0;
    },
    getSelectedPeriod: function()
    {
      var poollid = getSelectedValue(this.daysElement);
      if(poollid > 0)
          return this.days[poollid];
      else
          return false;
    },
    getPeriod: function()
    {
      var d = this.getSelectedPeriod();

      if(d)
          return d.periodId;
      else
          return "";
    },
    getPrice: function()
    {
      var d = this.getSelectedPeriod();

      if(d)
          return d.price;
      else
          return 0;
    },
    getArrdate: function()
    {
      var d = this.getSelectedPeriod();

      if(d)
          return d.arrdate;
      else
          return null;
    },
    getDepdate: function()
    {
      var d = this.getSelectedPeriod();

      if(d)
          return d.depdate;
      else
          return null;
    },
    setPeriod: function(p)
    {
      selectValue(this.daysElement,p);
    },
    setMessage: function(m)
    {
      this.showingMessage = true;

      this.daysElement.options.length = 0;
      this.daysElement.options[0] = new Option(m,0);
      this.daysElement.disable();
    },
    setDefaultDays: function(numDays)
    {
      this.defaultDays = numDays;
    },
    setDefaultPeriodId: function(periodId)
    {
      this.defaultPeriodId = periodId;
    },
    clearMessage: function()
    {
      this.showingMessage = false;
      this.daysElement.options.length = 0;
    },
    enable: function()
    {
      this.daysElement.enable();
    },
    disable: function()
    {
      this.daysElement.disable();
    },
    focus: function()
    {
      this.daysElement.focus();
    },
    prepareSubmit: function()
    {
      // Nothing
    },
    onDaysChanged: function(e)
    {
        this.container.onStateChanged();
    },
    _onFetchDaysSuccess: function(transport)
    {
      try
      {
       this.statusImage.hide();
       res = eval('(' + transport.responseText + ')');

       var selectedPeriod = this.getPeriod();
       
       this.daysElement.options.length = 0;
       this.daysElement.options[0] = new Option(this.container.getText('Days/price'),0);

       this.days = $H();
       switch(res.result)
       {
          case 0 :    // Everything OK
          this.clearMessage();
          this.daysElement.options.length = 0;
          this.daysElement.options[0] = new Option(this.container.getText('Days/price') + ' - ' + this.container.getText(res.ageGroup),0);

          if(res.days)
          {
            var days = res.days;
            var poollidToSelect = 0;

            for(i = 0; i < days.length; i++)
            {
                this.daysElement.options[i + 1] = new Option(days[i].desc,days[i].poollid);
                this.days[days[i].poollid] = days[i];

                // DO we have a period do suggest ?
                if(selectedPeriod)
                {
                  // Yes, try to preserve user selection
                  if(days[i].periodId == selectedPeriod)
                      poollidToSelect = days[i].poollid;
                }
                else
                {
                  // OK, we have no suggestion, is this the first
                  // time we have a succesful load, and do we have a default
                  // suggestion ?
                  if(this.firstTime)
                  {
                    if(this.defaultPeriodId)
                    {
                       if(days[i].periodId == this.defaultPeriodId)
                         poollidToSelect = days[i].poollid;
                    }
                    else if(this.defaultDays)
                    {
                       // Ok, then search for one ..
                       if(days[i].numDays == this.defaultDays)
                         poollidToSelect = days[i].poollid;
                    }
                  }  
                }
            }
            if(poollidToSelect > 0)
                this.setPeriod(poollidToSelect);
                
            this.firstTime = false;
          }
          else
          {
//            this.setMessage(this.container.getText('No skipass was found'));
            this.container.showError(null,'TXTXBOKLI006',false);
          }
          break;
                          //Ogiltigt första giltighetsdatum
          case 1 :
//          this.setMessage(this.container.getText('Invalid first date'));
          this.container.showError(null,'TXTXBOKLI005',false);
          break;

          case 2 :    // Everything OK, but no agegroup
//          this.setMessage(this.container.getText('No agegroup'));
          this.container.showError(null,'TXTXBOKLI007',false);
          break;

          default:
          this.container.showError(this,res.errorMessage);
        }
        this.container.onStateChanged();
//        this._refreshForm();
      }
      catch(e)
      {
          this.container.showError(this,e.message);
      }
    },
    _onFetchDaysFailed: function(transport)
    {
    }

};

FirstDateEditor = Class.create();
FirstDateEditor.prototype =
{
    initialize: function(container,firstDateElement)
    {
      this.container = container;
      this.firstDateElement = firstDateElement;

      Event.observe(this.firstDateElement,'keyup',this.onFirstDateChanged.bindAsEventListener(this));
    },
    getDate: function()
    {
        return this.firstDateElement.value;
    },
    setDate: function(fd)
    {
        this.firstDateElement.value = fd;
        this.onFirstDateChanged();
    },
    isValid: function()
    {
        var fd = this.getDate();
        return isDate(fd);
    },
    isReady: function()
    {
      var fd = this.getDate();

      return fd.length == 10;
    },
    onFirstDateChanged: function()
    {
      var fd = this.getDate();
      if(this.lastValidDateChecked == fd)
          return;

      this.lastValidDateChecked = fd;

      if(this.isReady())
      {
        if(this.firstValidDateChecker != null)
        {
            this.firstValidDateChecker.stop();
            this.firstValidDateChecker = null;
        }

        if(!this.isValid())
            this.container.showError(this,this.container.getText('Invalid first date'));
      }
      else
      {
        if(this.firstValidDateChecker == null)
            this.firstValidDateChecker = new PeriodicalExecuter(this.onFirstDateChanged.bind(this), 0.2);
      }

      this._dispatchCheck();
    },
    enable: function()
    {
        this.firstDateElement.enable();
    },
    disable: function()
    {
        this.firstDateElement.disable();
    },
    prepareSubmit: function()
    {
      // Nothing
    },
    focus: function()
    {
      this.firstDateElement.focus();
//      this.firstDateElement.select();
    },
    _dispatchCheck: function()
    {
//      if(this.isReady())
//      {
        // OK, changed
        if(this.container && this.container.onStateChanged)
          this.container.onStateChanged();
//      }
    }
};

DateEditor = Class.create();
DateEditor.prototype =
{
    initialize: function(container,birthdateYearElement,birthdateMonthElement,birthdateDayElement)
    {
        this.container = container;
        this.birthdateYearElement = birthdateYearElement;
        this.birthdateMonthElement = birthdateMonthElement;
        this.birthdateDayElement = birthdateDayElement;

        Event.observe(this.birthdateYearElement,'change',this.onBirthDateChanged.bindAsEventListener(this));
        Event.observe(this.birthdateMonthElement,'change',this.onBirthDateChanged.bindAsEventListener(this));
        Event.observe(this.birthdateDayElement,'change',this.onBirthDateChanged.bindAsEventListener(this));

        this.lastDate = "";
    },
    setDate: function(bd)
    {
      var yyyy = bd.substr(0,4);
      var mm = bd.substr(5,2);
      var dd = bd.substr(8,2);

      selectValue(this.birthdateYearElement,yyyy);
      selectValue(this.birthdateMonthElement,mm);
      selectValue(this.birthdateDayElement,dd);
      this.onBirthDateChanged();
    },
    getDate: function()
    {
      return getSelectedValue(this.birthdateYearElement) + "-" +
             getSelectedValue(this.birthdateMonthElement) + "-" +
             getSelectedValue(this.birthdateDayElement);

    },
    setYearOptions: function(h,notSelectedText)
    {
      var me = this;
      me.birthdateYearElement.options.length = 0;
      me.birthdateYearElement.options[me.birthdateYearElement.options.length] = new Option(notSelectedText,0);
      h.each( function(pair) { me.birthdateYearElement.options[me.birthdateYearElement.options.length] = new Option(pair.value,pair.key); } );
    },
    setMonthOptions: function(h,notSelectedText)
    {
      var me = this;
      me.birthdateMonthElement.options.length = 0;
      me.birthdateMonthElement.options[me.birthdateMonthElement.options.length] = new Option(notSelectedText,0);
      h.each( function(pair) { me.birthdateMonthElement.options[me.birthdateMonthElement.options.length] = new Option(pair.value,pair.key); } );
    },
    setDayOptions: function(h,notSelectedText)
    {
      var me = this;
      me.birthdateDayElement.options.length = 0;
      me.birthdateDayElement.options[me.birthdateDayElement.options.length] = new Option(notSelectedText,0);
      h.each( function(pair) { me.birthdateDayElement.options[me.birthdateDayElement.options.length] = new Option(pair.value,pair.key); } );
    },
    isReady: function()
    {
      return this.birthdateYearElement.options[this.birthdateYearElement.selectedIndex].value > 0 &&
             this.birthdateMonthElement.options[this.birthdateMonthElement.selectedIndex].value > 0 &&
             this.birthdateDayElement.options[this.birthdateDayElement.selectedIndex].value > 0;
    },
    isValid: function()
    {
      if(this.isReady())
      {
        return isDate(this.getDate());
      }
      return false;
    },
    onBirthDateChanged: function()
    {
      this._dispatchCheck();
    },
    enable: function()
    {
        this.birthdateYearElement.enable();
        this.birthdateMonthElement.enable();
        this.birthdateDayElement.enable();
    },
    disable: function()
    {
        this.birthdateYearElement.disable();
        this.birthdateMonthElement.disable();
        this.birthdateDayElement.disable();
    },
    focus: function()
    {
      if(this.birthdateYearElement.selectedIndex <= 0)
      {
          this.birthdateYearElement.focus();
          return;
      }
      if(this.birthdateMonthElement.selectedIndex <= 0)
      {
          this.birthdateMonthElement.focus();
          return;
      }
      if(this.birthdateDayElement.selectedIndex <= 0)
      {
          this.birthdateDayElement.focus();
          return;
      }


    },
    prepareSubmit: function()
    {
      // Nothing
    },
    _dispatchCheck: function()
    {
      if(this.lastDate != this.getDate())
      {
        this.lastDate = this.getDate();

        // OK, changed
        if(this.container && this.container.onStateChanged)
          this.container.onStateChanged(this);
      }
    }
};

CardNumberEditor = Class.create();
CardNumberEditor.prototype =
{
    initialize: function(container,cardNumberContainer,chipIdElement,crcElement,issuerElement,statusImageElement)
    {
        this.container = container;
        this.cardNumberContainer = cardNumberContainer;
        this.chipIdElement = chipIdElement;
        this.crcElement = crcElement;
        this.issuerElement = issuerElement;
        this.statusImageElement = statusImageElement;

        // Event handlers
        Event.observe(this.chipIdElement,'keyup',this.onCardNumberChanged.bindAsEventListener(this));
        Event.observe(this.crcElement,'keyup',this.onCardNumberChanged.bindAsEventListener(this));
        Event.observe(this.issuerElement,'keyup',this.onCardNumberChanged.bindAsEventListener(this));

        this.lastCardNumber = "";
        this.setCardNumber("");
        this.isEnabled = true;

        this.cardState = $H();
    },
    onCardNumberChanged: function(e)
    {
        var cardNumber = this.getCardNumber();

        if(cardNumber == this.lastCardNumber)
            return; // Has not changed

        this.lastCardNumber = cardNumber;

        if(this.isReady())
        {
          // Stop listening
          if(this.cardChecker)
          {
              this.cardChecker.stop();
              this.cardChecker = null;
          }
          
          // OK, dispatch check ..
          this._dispatchCardNumberCheck();
        }
        else
        {
            this._dispatchValidCheck();

            if(this.cardChecker == null)
                this.cardChecker = new PeriodicalExecuter(this.onCardNumberChanged.bind(this), 0.2);
        }
    },
    setCardNumber: function(cnr)
    {
      if(!cnr || cnr.length != 16)
      {
        this.chipIdElement.value = "";
        this.crcElement.value = "";
        this.issuerElement.value = "";
      }
      else
      {
        var chipId = cnr.substr(0,8);
        var crc = cnr.substr(9,3);
        var issuer = cnr.substr(13,3);

        this.chipIdElement.value = chipId;
        this.crcElement.value = crc;
        this.issuerElement.value = issuer;
      }
      this.onCardNumberChanged();
    },
    getCardNumber: function()
    {
      return this.chipIdElement.value + "/" + this.crcElement.value + "-" + this.issuerElement.value;
    },
    isReady: function()
    {
      var cnr = this.getCardNumber();
      return cnr.length == 16;
    },
    isValid: function()
    {
      if(this.isReady())
      {
        var card = this.cardState[this.getCardNumber()];

        if(card && (card.result == 0 || card.result == 1))
            return true;
      }

      return false;
    },
    getCardState: function()
    {
      if(this.isReady())
      {
        var card = this.cardState[this.getCardNumber()];

        if(card)
            return card.result;
      }
      return -1;
    },
    isOwnerInfo: function()
    {
      return this.getCardState() == 0;
    },
    getFirstName: function()
    {
        var card = this.cardState[this.getCardNumber()];

        return card.ownerInfo.firstName;
    },
    getLastName: function()
    {
        var card = this.cardState[this.getCardNumber()];

        return card.ownerInfo.lastName;
    },
    getBirthdate: function()
    {
        var card = this.cardState[this.getCardNumber()];

        return card.ownerInfo.birthdate;
    },
    getCardNo: function()
    {
        var card = this.cardState[this.getCardNumber()];
        if(card)
            return card.cardNo;
        else
            return 0;
    },
    getAddrlid: function()
    {
        var card = this.cardState[this.getCardNumber()];
        if(card)
            return card.ownerInfo.addrlid;
        else
            return 0;
    },
    isRegisterUser: function()
    {
        return this.getCardState() == 1;
    },
    focus: function()
    {
      if(this.isEnabled)
          this.chipIdElement.focus();
    },
    hide: function()
    {
        this.chipIdElement.hide();
        this.crcElement.hide();
        this.issuerElement.hide();
        this.cardNumberContainer.hide();
    },
    show: function()
    {
        this.chipIdElement.show();
        this.crcElement.show();
        this.issuerElement.show();
        this.cardNumberContainer.show();
        this.cardNumberContainer.style.display = "inline";
    },
    disable: function()
    {
        this.isEnabled = false;
        this.chipIdElement.disable();
        this.crcElement.disable();
        this.issuerElement.disable();
    },
    enable: function()
    {
        this.isEnabled = true;
        this.chipIdElement.enable();
        this.crcElement.enable();
        this.issuerElement.enable();
    },
    prepareSubmit: function()
    {
      // Nothing
    },
    _dispatchValidCheck: function()
    {
      if(this.isReady())
      {
         // OK, changed
         if(this.container && this.container.onStateChanged)
            this.container.onStateChanged(this);
      }
    },
    _dispatchCardNumberCheck: function()
    {
        this.container.disableForm();
        this.statusImageElement.show();

       var p = $H({site: this.container.getSite(), lang: this.container.getLang(), cardNumber: this.getCardNumber()});
       new Ajax.Request('/app/projects/common/templates/skipass/api/validateCardNumber.php',{method: 'get',parameters: p,onSuccess: this._onCardNumberCheckSuccess.bind(this),onFailure: this._onCardNumberCheckFailed.bind(this)});
    },
    _onCardNumberCheckSuccess: function(transport)
    {
      this.statusImageElement.hide();
      res = eval('(' + transport.responseText + ')');

      if(res.result >= 0)
      {
          this.cardState[this.getCardNumber()] = res;

          switch(res.result)
          {
            case 0 : // Valid number and info
            break;

            case 1 : // Valid number, no info
            break;

            case 2 : // In valid cardnumber!
            this.container.showError(this,'Invalid cardnumber');
            break;

          }
      }
      else
          this.container.showError(this,res.errorMessage);

      this._dispatchValidCheck();
    },
    _onCardNumberCheckFailed: function(transport)
    {
        this.container.showError(this.container.getText('ERR10001','R2'));
    }
};

CardTypeEditor = Class.create();
CardTypeEditor.prototype =
{
    initialize: function(container,cardTypeElement,cardTypeStatusElement,cardTypeContainerElement)
    {
        this.container = container;
        this.cardTypeElement = cardTypeElement;
        this.statusImage = cardTypeStatusElement;
        Event.observe(this.cardTypeElement,'change',this.onCardTypeChanged.bindAsEventListener(this));
        this.lastRequest = "";
        this.cardTypes = $H();
        this.showingMessage = false;
        this.setMessage(this.container.getText('Cardtype'));
        this.cardTypeContainerElement = cardTypeContainerElement;

        this.availableCardTypes = this.container.getCardTypes();
        if(this.availableCardTypes.size() == 1)
        {
            this.cardTypeContainerElement.hide();
            this.cardTypeElement.hide();

           // Add and select the first..
           var i = 0;
           var me = this;
           this.availableCardTypes.each(function(pair) { me.cardTypeElement.options[i++] = new Option(pair.value,pair.key); });
           this.cardTypeElement.selectedIndex = 0;
           this.showingMessage = false;
       }
       else
       {
           this.setMessage(this.container.getText('Cardtype'));
       }
       this.requestPending = false;
       this.firstTime = true;
       this.defaultCardType = false;

    },
    refresh: function()
    {
       // Never if we only have one card
       if(this.availableCardTypes.size() == 1)
           return;

       if(this.container.isLoad())
           load = "yes";
       else
           load = "no";

       var p = $H({site: this.container.getSite() ,lang: this.container.getLang(), firstDate: this.container.getFirstDate(), birthdate: this.container.getBirthdate(), load: load});

       if(p.inspect() == this.lastRequest)
           return;

       this.lastRequest = p.inspect();
       if(this.container.isValidBirthDate() && this.container.isValidFirstDate())
       {
         this.container.disableForm();
         this.statusImage.show();
         this.requestPending = true;

         new Ajax.Request('/app/projects/common/templates/skipass/api/fetchTypes.php',{method: 'get',parameters: p,onSuccess: this._onFetchTypesSuccess.bind(this),onFailure: this._onFetchTypesFailed.bind(this)});
       }
       else
          this.container.onStateChanged();
    },
    isValid: function()
    {
      return this.getCardType() != 0;
    },
    setCardType: function(ct)
    {
//      selectValue(this.cardTypeElement,ct);
        this.defaultCardType = ct;
    },
    getCardType: function()
    {
        if(this.requestPending)
            return 0;
        else
            return getSelectedValue(this.cardTypeElement);
    },
    setMessage: function(m)
    {
      this.showingMessage = true;

      this.cardTypeElement.options.length = 0;
      this.cardTypeElement.options[0] = new Option(m,0);
      this.cardTypeElement.disable();
    },
    clearMessage: function()
    {
      this.showingMessage = false;
      this.cardTypeElement.options.length = 0;
    },
    enable: function()
    {
      this.cardTypeElement.enable();
    },
    disable: function()
    {
      this.cardTypeElement.disable();
    },
    focus: function()
    {
      this.cardTypeElement.focus();
    },
    isTypeToSelect: function()
    {
      return !this.showingMessage && this.cardTypeElement.options.length > 0;
    },
    prepareSubmit: function()
    {
      // Nothing
    },
    onCardTypeChanged: function(e)
    {
        this.container.onStateChanged();
    },
    _onFetchTypesSuccess: function(transport)
    {
      try
      {
       this.statusImage.hide();
       this.requestPending = false;
       
       res = eval('(' + transport.responseText + ')');

       var selectedType = this.getCardType();

       this.cardTypeElement.options.length = 0;
       this.cardTypeElement.options[0] = new Option(this.container.getText('Cardtype'),0);

       switch(res.result)
       {
          case 0 :    // Everything OK
          this.clearMessage();
          this.cardTypeElement.options.length = 0;
          this.cardTypeElement.options[0] = new Option(this.container.getText('Cardtype'),0);
          
          if(res.typesAvailable)
          {
            var types = res.typesAvailable;
            var idxToSelect = 0;

            for(i = 0; i < types.length; i++)
            {
                this.cardTypeElement.options[i + 1] = new Option(types[i].value,types[i].key);

                // DO we have a period do suggest ?
                if(selectedType != 0)
                {
                  // Yes, try to preserve user selection
                  if(types[i].key == selectedType)
                      idxToSelect = i + 1;
                }
                else
                {
                    if(this.firstTime)
                    {
                        if(this.defaultCardType)
                        {
                            if(types[i].key == this.defaultCardType)
                                idxToSelect = i + 1;
                        }
                        this.firstTime = false;
                    }
                }
            }

            this.cardTypeElement.selectedIndex = idxToSelect;
          }
          else
          {
//            this.setMessage(this.container.getText('No skipass was found'));
//            this.container.showError(null,'TXTXBOKLI006',false);
          }
          break;
                          //Ogiltigt första giltighetsdatum
          case 1 :
//          this.setMessage(this.container.getText('Invalid first date'));
//          this.container.showError(null,'TXTXBOKLI005',false);
          break;

          case 2 :    // Everything OK, but no agegroup
//          this.setMessage(this.container.getText('No agegroup'));
//          this.container.showError(null,'TXTXBOKLI007',false);
          break;

          default:
//          this.container.showError(this,res.errorMessage);
        }
        this.container.onStateChanged();
//        this._refreshForm();
      }
      catch(e)
      {
          this.container.showError(this,e.message);
      }

    },
    _onFetchTypesFailed: function(transport)
    {
    }

};
