function get_licence()
 {
  document.licence_form.submit();
 }

function is_int(s)
 {
  if ((s == '0') ||
      (s == '1') ||
      (s == '2') ||
      (s == '3') ||
      (s == '4') ||
      (s == '5') ||
      (s == '6') ||
      (s == '7') ||
      (s == '8') ||
      (s == '9')) { return 1; } else { return 0; }
 }

function check_new_licence_format(licence)
 {

  var s = '';
  for (i = 0; i <= licence.length-1; i++)
   {
    var n = licence.charCodeAt(i);
    if (((n >= 65) && (n <= 90)) || ((n >= 48) && (n <= 57)))
     {
      s = s + licence.charAt(i);
     }
   }
  if (s.length != 6) { return licence; }

  if ((is_int(s.charAt(4)) == 0) && (is_int(s.charAt(5)) == 1))
   {
    licence = s.charAt(0) + s.charAt(1) + '-' + s.charAt(2) + s.charAt(3) + s.charAt(4) + '-' + s.charAt(5);
   }

  return licence;
 }


function check_licencex(e)
 {
  var key = window.event ? e.keyCode : e.which;
  var keychar = String.fromCharCode(key);
  if (key == 8) { return; }
  if (key == 13) {  $('.zoeken').click(); }

  var licence = document.licence_form.licence.value;
  licence = licence.toUpperCase();

  s = ''; m = 0;
  for (i = 0; i <= licence.length-1; i++)
   {
    n = licence.charCodeAt(i);
    if (((n >= 65) && (n <= 90)) || ((n >= 48) && (n <= 57)))
     {
      s = s + licence.charAt(i);
      m = m + 1;
      if (m == 2) { s = s + '-'; }
      if (m == 4) { s = s + '-'; }
     }
   }

  s = check_new_licence_format(s);
  document.licence_form.licence.value = s;
 }

//***
function insert_char(s, behind_char_pos, ch)
 {
  var s1 = s.substr(0, behind_char_pos)
  var s2 = s.substr(behind_char_pos);

  return s1 + ch + s2;
 }


function check_licence(e)
 {
  var key = window.event ? e.keyCode : e.which;
  if (key == 8) { return; }
  if (key == 13) { $('.zoeken').click(); }

  var licence = $('#kenteken').val();

  //IE: 189, FF: 109
  if ((key == 189) || (key == 109))
   {
    var dash_count = 0;
    for (i = 0; i <= licence.length-1; i++)
     {
      n = licence.charCodeAt(i);
      if (n == 45)
       {
        dash_count++;
       }
     }
    if (dash_count <= 2)
     {
      return;
     }
   }

  licence = licence.toUpperCase();

  licence_format = ''; licence2 = '';
  for (i = 0; i <= licence.length-1; i++)
   {
    n = licence.charCodeAt(i);

    // A-Z
    if ((n >= 65) && (n <= 90))
     {
      licence_format = licence_format + 'A';
      licence2 = licence2 + licence.charAt(i);
     }

    // 0-9
    if ((n >= 48) && (n <= 57))
     {
      licence_format = licence_format + '0';
      licence2 = licence2 + licence.charAt(i);
     }
   }

  //alert(licence_format);

  licence = licence2.substr(0, 6);

  if (licence_format.length <= 2)
   {
    $('#kenteken').val(licence);
    return;
   }

  if (licence_format.length <= 4)
   {
    licence = insert_char(licence, 2, '-');
    $('#kenteken').val(licence);
    return;
   }

  if (
      (licence_format == '0AAA00')
     )
   {
    licence = insert_char(licence, 1, '-');
    licence = insert_char(licence, 5, '-');
    $('#kenteken').val(licence);
    return;
   }

  if (
      (licence_format == '00AAA0')
     )
   {
    licence = insert_char(licence, 2, '-');
    licence = insert_char(licence, 6, '-');
    $('#kenteken').val(licence);
    return;
   }

  licence = insert_char(licence, 2, '-');
  licence = insert_char(licence, 5, '-');

  $('#kenteken').val(licence);
  return;
 }
 
