/*----------------------------------*/
/*-------------- MISC --------------*/
/*----------------------------------*/

function focussel(field_id)
{
    document.getElementById(field_id).select();
    document.getElementById(field_id).focus();
}

function check(field_id, field_name, field_yours, appendix)
{
  if (!document.getElementById(field_id)) return false;
  if (document.getElementById(field_id).value.length == 0)
  {
    var msg = 'U dient ';
    if (field_yours) msg += 'uw'; else msg += 'een';
    msg += ' ' + field_name + ' in te voeren.';
    if (appendix && appendix.length > 0) msg += '\n\n' + appendix;
    alert(msg);
    focussel(field_id);
    return false;
  }
  else return true;
}

/*----------------------------------*/
/*---------- USER ACCOUNT ----------*/
/*----------------------------------*/

function check_login()
{
  return (check('user', 'gebruikersnaam', true) && check('key', 'wachtwoord', true));
}

function check_registration()
{
  if (check('contactperson', 'contactpersoon') && check('email', 'e-mail adres', true) && check('phone', 'telefoonnummer', true) && check('name', 'bedrijfsnaam', true) && check('address', 'postadres', true) && check('postalcode', 'postcode', true) && check('city', 'plaatsnaam', true) && check('domain', 'domeinnaam', true) && check('key', 'berichtsleutel', true))
  {
    if (!document.getElementById('trial_ok').checked) { alert('U dient akkoord te gaan met het trial abonnement.'); return false; }
    if (!document.getElementById('terms_ok').checked) { alert('U dient akkoord te gaan met de Algemene Voorwaarden.'); return false; }
    hide_form();
  }
  else return false;
}

function check_newpass()
{
  return (check('email', 'e-mail adres', true) && check('key', 'berichtsleutel', true));
}

function check_profile()
{
  return (check('contactperson', 'contactpersoon', true) && check('email', 'e-mail adres', true) && check('telephone', 'telefoonnummer', true) && check('name', 'bedrijfsnaam', true) && check('address', 'adres', true) && check('postalcode', 'postcode', true) && check('city', 'woonplaats', true));
}

function check_password()
{
  if (document.getElementById('oldpass').value.length == 0)
  {
    alert('U dient uw huidige wachtwoord in te voeren.');
    focussel('oldpass');
    return false;
  }
  if (document.getElementById('newpass1').value.length == 0)
  {
    alert('U dient een nieuw wachtwoord in te voeren.');
    focussel('newpass1');
    return false;
  }
  if (document.getElementById('newpass2').value.length == 0)
  {
    alert('U dient uw nieuwe wachtwoord te bevestigen.');
    focussel('newpass2');
    return false;
  }
  return true;
}

/*----------------------------------*/
/*------------ REACTIONS -----------*/
/*----------------------------------*/

function check_reaction_search()
{
  if (document.getElementById('keyword') && document.getElementById('keyword').value.length > 0 && document.getElementById('keyword').value.length < 3)
  {
    alert('Zoektermen met minder dan 3 tekens worden genegeerd.');
    focussel('keyword');
    return false;
  }
  else return true;
}

function check_reaction_new()
{
  return (check('name', 'naam', true) && check('subject', 'onderwerp') && check('body', 'bericht') && check('key', 'berichtsleutel', true));
}

function check_reaction_removalrequest()
{
  return (check('message', 'bericht', false, 'Wij hopen dat u in dit bericht aan zou willen geven\nwaarom deze reactie verwijderd zou moeten worden.'), check('key', 'berichtsleutel', true));
}

function reaction_section_change()
{
  if (document.getElementById('keyword') && document.getElementById('keyword').value.length > 0 && document.getElementById('keyword').value.length < 3)
  {
    alert('Zoektermen met minder dan 3 tekens worden genegeerd.');
  }
  document.getElementById('search_form').submit();
}

/*----------------------------------*/
/*-------- CONTACT MESSAGE ---------*/
/*----------------------------------*/

function check_contactmessage()
{
  return (check('name', 'naam', true) && check('email', 'e-mail adres', true) && check('subject', 'onderwerp') && check('message', 'bericht') && check('key', 'berichtsleutel', true));
}

/*----------------------------------*/
/*------------- SEARCH -------------*/
/*----------------------------------*/

function check_search()
{
  if (document.getElementById('keyword').value.length == 0)
  {
    alert('U dient een zoekterm te specificeren.');
    focussel('keyword');
    return false;
  }
  else
  if (document.getElementById('keyword').value.length < 5)
  {
    alert('Uw zoekterm dient uit minimaal 5 tekens te bestaan.');
    focussel('keyword');
    return false;
  }
  else return true;
}

function exec_search()
{
  document.getElementById('search_form').submit();
}

function search_section_change()
{
  if (document.getElementById('keyword').value.length == 0)
  {
    focussel('keyword');
  }
  else
  {
    if (check_search()) exec_search();
  }
}

function go_to(page)
{
  var item_count = document.getElementById('start').childNodes.length;

  if (page > 0 && page <= item_count)
  {
    if (document.getElementById('start').value == page)
    {
      alert('U bevindt zich al op deze pagina.');
    }
    else
    {
      document.getElementById('start').childNodes[page - 1].selected = true;
      exec_search();
    }
  }
  else alert('Pagina bestaat niet.');
}

function get_current_page()
{
  var x = document.getElementById('start');
  for (var y = 0; y < x.childNodes.length; y++)
  {
    if (x.childNodes[y].selected) return (y + 1);
  }
  return 0;
}

function first()
{
  go_to(1);
}

function prior()
{
  go_to(get_current_page() - 1);
}

function next()
{
  go_to(get_current_page() + 1);
}

function last()
{
  go_to(document.getElementById('start').childNodes.length);
}

function limit_change()
{
  document.getElementById('start').value = "1";
  exec_search();
}

/*----------------------------------*/
/*-------------- SHOP --------------*/
/*----------------------------------*/

function set_select_param(link_id, product_id, property_id)
{
  var x = document.getElementById(link_id + product_id);
  var q = document.getElementById('Q' + property_id);
  if (!x || !q) return;
  var y = x.href.lastIndexOf(property_id + '=');
  if (y <= 0) return;
  property_id = property_id + '';
  y += property_id.length + 1;
  var new_link = x.href.substr(0, y);
  new_link += q.value;
  y = x.href.indexOf('&', y);
  if (y > 0) new_link += x.href.substr(y);
  x.href = new_link;
}

function set_select_params(product_id, property_id)
{
  set_select_param('B', product_id, property_id);
  set_select_param('O', product_id, property_id);
  set_select_param('I', product_id, property_id);
}

function set_multiselect_param(link_id, product_id, property_id)
{
  var x = document.getElementById(link_id + product_id);
  var q = document.getElementById('Q' + property_id);
  if (!x || !q) return;
  var y = x.href.lastIndexOf(property_id + '=');
  var not_present = (y <= 0);
  if (not_present && !q.checked) return;
  if (!not_present)
  {
    if (q.checked)
    { 
      property_id = property_id + '';
      y += property_id.length + 1;
      var new_link = x.href.substr(0, y);
      new_link += '1';
    }
    else
    {
      var first_param = (x.href.charAt(y - 1) == '?');
      if (first_param) var new_link = x.href.substr(0, y); else var new_link = x.href.substr(0, y - 1);
    }
    y = x.href.indexOf('&', y);
    if (y > 0)
    {
      if (!first_param)
        new_link += x.href.substr(y);
          else new_link += x.href.substr(y + 1);
    }
  }
  else
  {
    if (x.href.charAt(x.href.length - 1) != '?')
      var new_link = x.href + '&' + property_id + '=1';
        else var new_link = x.href + property_id + '=1';
  }

  x.href = new_link;
}

function set_multiselect_params(product_id, property_id)
{
  set_multiselect_param('B', product_id, property_id);
  set_multiselect_param('O', product_id, property_id);
  set_multiselect_param('I', product_id, property_id);
}

function check_shop_login()
{
  if (document.getElementById('new_user') && document.getElementById('new_user').checked) return true; else
  if (document.getElementById('reg_user') && document.getElementById('reg_user').checked)
  {
    return check_login();
  }
  else
  {
    alert('U dient een keuze te maken.');
    return false;
  }
}

function check_shop_deliver()
{
  if (document.getElementById('same_address') && document.getElementById('same_address').checked) return true; else
  if (document.getElementById('new_address') && document.getElementById('new_address').checked)
  {
    return (check('name', 'naam', true) && check('address', 'bezorgadres', true) && check('postalcode', 'postcode', true) && check('city', 'bezorgplaats', true));
  }
  else
  {
    alert('U dient een keuze te maken.');
    return false;
  }
}

function check_shop_payment()
{
  var inputs = document.getElementsByTagName('INPUT');
  for (var x = 0; x < inputs.length; x++)
  {
    if (inputs[x].type == 'radio' && inputs[x].checked) return true;
  }
  alert('U dient een keuze te maken.');
  return false;
}

function check_shop_control()
{
  if (!document.getElementById('agree1') || !document.getElementById('agree1').checked)
  {
    alert('U dient aan te geven alle gegevens naar waarheid te hebben ingevoerd. U doet dit door het betreffende keuzeveldje onderaan deze pagina aan te vinken.');
    return false;
  }
  if (!document.getElementById('agree2') || !document.getElementById('agree2').checked)
  {
    alert('U dient aan te geven akkoord te gaan met onze Algemene Voorwaarden. U doet dit door het betreffende keuzeveldje onderaan deze pagina aan te vinken.');
    return false;
  }
  document.getElementById('button_div').style.display = 'none';
  var p = document.createElement('P');
  p.style.fontWeight = 'bold';
  p.appendChild(document.createTextNode('Een moment geduld s.v.p.'));
  alert(document.getElementById('shop_form').innerHTML);
  alert(document.getElementById('shop_form').childNodes.length);
  alert(document.getElementById('shop_form').childNodes[0].innerHTML);
  document.getElementById('shop_form').childNodes[0].appendChild(p);
  return true;
}

function shop_control_bonus()
{
  if (document.getElementById('shop_form').action.indexOf('/_verzenden') != -1) document.getElementById('shop_form').action = document.getElementById('shop_form').action.replace('/_verzenden', '');
  if (document.getElementById('shop_form').action.indexOf('#') == -1) document.getElementById('shop_form').action = document.getElementById('shop_form').action + '#bonus';
  document.getElementById('shop_form').submit();
}

/*----------------------------------*/
/*---- THE END - HAVE A NICE DAY ---*/
/*----------------------------------*/


