﻿Spif.DOMEvents.attach(
  window,
  'load',
  function()
  {
    Vragenlijst.init();    
  }
);

function activateCheckbox(value, id, el)
{
  document.getElementById(id).value = value;
  var p = el.parentNode.parentNode;
  var col = p.getElementsByTagName('img');
  
  for (var i = 0; i < col.length; i++)
  {
    col[i].src = '/images/verzuimcheck/checkbox-inactive.gif';
  }
  el.firstChild.src = '/images/verzuimcheck/checkbox-active.gif';
  
}


var Vragenlijst =
{
  vragenlijst: null, 
  glasspane: null,
  currentVragenlijst: null,
  counter: 'vragenlijstCounter',
  submit: 'themaVragenControlSubmit',
  forward: 'themaVragenControlForward',
  maxPage: null,
  init: function()
  {
    this.currentVragenlijst = document.getElementById(this.counter).innerHTML;
    this.maxPage = document.getElementById('vragenlijstCounterMax').innerHTML;
    
    this.vragenlijst = document.getElementById('overlayVragenlijst');
    this.glasspane = document.getElementById('glasspane');           
  },
  moveForward: function()
  {    
    var el = document.getElementById('vragenlijst-'+this.currentVragenlijst);
    this.changeVisibility(el); 
    
    this.currentVragenlijst++;
    el = document.getElementById('vragenlijst-'+this.currentVragenlijst);
    this.changeVisibility(el);
    this.changeButtons();
    this.setCounter();
  },
  moveBack: function()
  {    
    var el = document.getElementById('vragenlijst-'+this.currentVragenlijst);
    this.changeVisibility(el);    
    this.currentVragenlijst--;
    el = document.getElementById('vragenlijst-'+this.currentVragenlijst);
    this.changeVisibility(el);
    this.changeButtons();
    this.setCounter();
  },
  changeButtons: function()
  {
    var el = document.getElementById('themaVragenControl');
    if (this.currentVragenlijst == 1)
    {
      if (Spif.ClassNameAbstraction.contains(el, 'controlSubmit'))
        Spif.ClassNameAbstraction.replace(el, 'controlSubmit', 'controlFirst');
      else if (Spif.ClassNameAbstraction.contains(el, 'controlDefault'))
        Spif.ClassNameAbstraction.replace(el, 'controlDefault', 'controlFirst');
    }
    else if (this.currentVragenlijst == this.maxPage)
    {      
      if (Spif.ClassNameAbstraction.contains(el, 'controlDefault'))
        Spif.ClassNameAbstraction.replace(el, 'controlDefault', 'controlSubmit');
      else if(Spif.ClassNameAbstraction.contains(el, 'controlFirst'))
        Spif.ClassNameAbstraction.replace(el, 'controlFirst', 'controlSubmit');    
    }
    else
    {
      if (Spif.ClassNameAbstraction.contains(el, 'controlSubmit'))
        Spif.ClassNameAbstraction.replace(el, 'controlSubmit', 'controlDefault');
      else if(Spif.ClassNameAbstraction.contains(el, 'controlFirst'))
        Spif.ClassNameAbstraction.replace(el, 'controlFirst', 'controlDefault');
    }
  },
  changeVisibility: function(el)
  {
    if (Spif.ClassNameAbstraction.contains(el, 'visible'))
      Spif.ClassNameAbstraction.replace(el, 'visible', 'hidden'); 
    else
      Spif.ClassNameAbstraction.replace(el, 'hidden', 'visible'); 
  },
  setCounter: function()
  {    
    document.getElementById(this.counter).innerHTML = this.currentVragenlijst;
  },
  show: function()
  {        
    document.body.appendChild(this.glasspane);          
    document.body.appendChild(this.vragenlijst);
    
    Spif.ClassNameAbstraction.replace(this.glasspane, 'hidden', 'shown'); 
    Spif.ClassNameAbstraction.replace(this.vragenlijst, 'hidden', 'shown'); 
    
    if(Spif.IsIE && !Spif.IsIE7)
      document.scrollTo(0,0);
  },
  hide: function()
  {
    Spif.ClassNameAbstraction.replace(this.glasspane, 'shown', 'hidden'); 
    Spif.ClassNameAbstraction.replace(this.vragenlijst, 'shown', 'hidden'); 
  },
  submit: function()
  {
    var el = document.getElementById('vragenlijstForm');
    var coll = el.getElementsByTagName("input");
    var giveError = false;
    for (var i = 0; i < coll.length; i++)
    {
     for (var j = 0; j < coll[i].parentNode.parentNode.childNodes.length; j++)
      {
        var changeEl = coll[i].parentNode.parentNode.childNodes[j];
        if (changeEl.className == 'themaVragenTableTDFirst')
        {
          changeEl.style.color = 'Black';
          break;
        }
      }       
     if (coll[i].type == 'hidden' && coll[i].value == '')
      {
        giveError = true;           
        for (var j = 0; j < coll[i].parentNode.parentNode.childNodes.length; j++)
        {
        
          var changeEl = coll[i].parentNode.parentNode.childNodes[j];
          if (changeEl.className == 'themaVragenTableTDFirst')
          {
            changeEl.style.color = 'Red';
            break;
          }
        }       
      }
    }
    if (giveError == false)
    { 
      el.submit();
    }
    else
    {
      alert('Niet alle vragen zijn ingevuld');
    }
  }
};

