
function CAccount(ID, Name, Price, PayWays, Actie)
{
  this.ID    = ID;
  this.Name  = Name;
  this.Price = Price;
  this.Ways  = PayWays;
  this.Actie = Actie;
}

function CPayWay(ID, Name, Extra, Times, NoActie)
{
  this.ID    = ID;
  this.Name  = Name;
  this.Extra = Extra-0;
  this.Times = Times;
  this.Actie = NoActie;
}

function CPayTime(ID, Name, Pay)
{
  this.ID    = ID;
  this.Name  = Name;
  this.Pay   = Pay;
}

function CActie(ID, Name, Type)
{
  this.ID    = ID;
  this.Name  = Name;
  this.Type  = Type;
}

function CAccounts()
{
  this.Accounts   = new Array();
  this.PayWays    = new Array();
  this.PayTimes   = new Array();
  this.Acties     = new Array();
  this.CurAcc     = null;

  //---
  this.Add = function(ID, Name, Price, PayWays, Actie)
  {
    this.Accounts[ID] = new CAccount(ID, Name, Price, PayWays, Actie);
  }

  //---
  this.AddPayWay = function(ID, Name, Extra, Times, Actie)
  {
    this.PayWays[ID] = new CPayWay(ID, Name, Extra, Times, Actie);
  }

  //---
  this.AddTime = function(ID, Name, Pay)
  {
    this.PayTimes[ID] = new CPayTime(ID, Name, Pay);
  }

  //---
  this.AddActie = function(ID, Name, Type)
  {
    this.Acties[ID] = new CActie(ID, Name, Type);
  }

  //---
  this.FillList = function()
  {
    var i, sel = document.getElementById('Account');
    if(sel)
    {
      var grp = -1;
      var s, opt = null;

      sel.options.length = 0;
      if(typeof(UserID) == 'undefined')
      {
        sel.options[0] = new Option('', '*', true, false);

        for(i in this.Accounts)
        {
          if(opt == null || Math.round(this.Accounts[i].ID/10000) != grp)
          {
            if(opt) sel.appendChild(opt);
            opt = document.createElement('optgroup');
            opt.label = '----------';
            grp = Math.round(this.Accounts[i].ID/10000);
          }
          s = document.createElement('option');
          s.value = i;
          s.innerHTML  = this.Accounts[i].Name;
          opt.appendChild(s);
        }
        if(opt) sel.appendChild(opt);
      }
      else
      {
        if(this.Accounts[60010]) sel.options[0] = new Option('', '*', true, false);
        for(i in this.Accounts)
        {
          s = document.createElement('option');
          s.value = i;
          s.innerHTML  = this.Accounts[i].Name;
          sel.appendChild(s);
        }
      }
      sel.disabled = false;
    }
  }

  //---
  this.RefillList = function(Nl)
  {
    var sel = document.getElementById('Account');
    var per = document.getElementById('PayTime');
    var way = document.getElementById('PayWay');
    var act = document.getElementById('Actie');

    if(sel)
    {
      // Reset other selections
      var Account = sel.value;
      var Time    = per.value;
      var Way     = way.value;
      var Actie   = act.value;

      this.FillList();

      if((Account != '*') && (typeof(this.Accounts[Account]) == 'undefined'))
      {
        for(i in this.Accounts)
        {
          Account = i;
          break;
        }
        Panel(5);
      }

      this.SelAccount(Account, Time, Way, Actie);
    }
  }

  //---
  this.SelAccount = function(Account, Time, Way, Actie)
  {
    var i;
    var Price = '...';//<br/><br/>';
    this.CurAcc = null;
    var sel = document.getElementById('Account');
    var per = document.getElementById('PayTime');
    var way = document.getElementById('PayWay');
    var act = document.getElementById('Actie');

    // Reset other selections
    var PayTime = per.value;
    var PayWay  = way.value;
    var PayAct  = act.value;

    if(Account && Time && Way)
    {
      sel.value = Account;
      PayTime = Time;
      PayWay  = Way;
      PayAct  = Actie;
    }

    per.options.length = 0;
    way.options.length = 0;
    act.options.length = 0;
    per.disabled = true;
    way.disabled = true;
    act.disabled = true;

    if(sel && per && way && act && sel.value!='*')
    {
      this.CurAcc = this.Accounts[sel.value];
    }
    if(this.CurAcc)
    {
      // Valid account selected
      // Setup payways
      if(this.CurAcc.Ways)
      {
        if(this.CurAcc.Ways.length > 1) way.options[0] = new Option('', '*', true, false);
        for(i in this.PayWays)
        {
          if(this.CurAcc.Ways.indexOf(i) != -1)
          {
            way.options[way.options.length] = new Option(this.PayWays[i].Name, i, false, false);
          }
        }
        way.disabled = (this.CurAcc.Ways.length == 1);
      }
      if(way.options.length > 1) way.value = PayWay;
      else PayWay = way.value;

        //alert(this.CurAcc.Price);

      // Setup paytimes
      if(this.PayWays[way.value])
      {
        /*
        if(this.PayWays[way.value].length > 1)
                                      per.options[0] = new Option('', '*', true, false);
        */
        for(i in this.PayTimes)
        {
          if(this.CurAcc.Price[i] &&
             (this.PayWays[way.value].Times.indexOf(i) != -1))
          {
            per.options[per.options.length] = new Option(this.PayTimes[i].Name, i, false, false);
          }
        }

        per.disabled = false;
        if(per.options.length > 1) per.value = PayTime;
        else PayTime = per.value;
      }

      // Setup actie
      if(this.CurAcc.Actie &&
         this.PayWays[way.value] &&
         (per.value != '') &&
         (per.value != '*'))
      {
        act.options[0] = new Option('No offer available', '-', false, false);

        for(i in this.Acties)
        {
          if((this.CurAcc.Actie.indexOf(i) != -1) &&
              per.value.length &&
             (this.Acties[i].Type.indexOf(per.value) != -1))
          {
            if(!this.PayWays[way.value] ||
               (this.PayWays[way.value].Actie.indexOf(i) == -1))
            {
              act.options[act.options.length] = new Option(this.Acties[i].Name, i, false, false);
            }
          }
        }
        if(act.options.length > 1)
        {
          act.options[0] = new Option('', '@', false, false);
          act.options[act.options.length] = new Option('I don`t want to use an offer', '--', false, false);
          act.value = PayAct=='-' ? '@' : PayAct;
        }
        act.disabled = false;
      }
      else
      {
        act.options[0] = new Option('choose another combination', '*', false, false);
      }

      var Extra = '';
      if(this.PayWays[way.value] && (this.PayWays[way.value].Extra > 0))
      {
        Extra = '<span style="font-size:10px"> + &euro; '+this.PayWays[way.value].Extra.toFixed(2)+' per payment for administrative costs</span>';
      }

      if(this.CurAcc.Price[per.value])
      {
        Price = '&euro; '+this.CurAcc.Price[per.value].toFixed(2);
        if(per.value == '1')  Price += ' '+Extra;//+'<br/><br/>';
        else                  Price += ' per month <span style="font-size:10px">'+this.PayTimes[per.value].Pay+'</span>'+Extra;//+'<br/><br/>';
      }
    }

    var el = document.getElementById('dom_actie');
    var al = document.getElementById('ShowActieLink');
    var ad = document.getElementById('dom_actie_ena');
    if(el && al)
    {
      if(act.value.indexOf('d') == -1)
      {
        DomainChange();
        CheckedResult = -2;
        el.style.display = '';
        al.style.display = 'none';
        if(ad) ad.style.display = 'none';
      }
      else
      {
        if(CheckedResult == -2) CheckedResult = -1;
        el.style.display = 'none';
        al.style.display = '';
        if(ad) ad.style.display = '';
      }
    }

    var el = document.getElementById('Price');
    if(el) el.innerHTML = Price;

    return Price.substring(0,3) != '...';
  }
}

//-----------------------------------------------------------------------------

var Accounts = new CAccounts();
var NlAcc    = new CAccounts();
var IntAcc   = new CAccounts();
