function trim(value) {
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  return value;
}
$(document).ready(function(){
  
  $('.bubblepopup').CreateBubblePopup({
    innerHtml: '',
    themeName: 'grey',
    themePath: '/js/jquerybubblepopup/jquerybubblepopup-theme',
    position:  'top',
    width: '400px',          
    innerHtmlStyle: {'text-align': 'left'},
    dropShadow: ($.browser.msie && $.browser.version.slice(0,1) < 9) ? false : true
  });
  $('.bubblepopup').mouseover(function(){
      var tooltip = $(this);
      tooltip.SetBubblePopupInnerHtml(tooltip.attr('alt'));
  });
  /*
   * Mainmenu behaviour
   */
  var current_item = $(".current");
  var sub_menu = $("#sub_menu");

  // Initiate backgroundimages for current page
  if(current_item.index($("#main_menu ul li")) == 0)
  {
    // Current page is homepage
    current_item.css("background", "url(/images/tab_home.png)");
    sub_menu.css("background", "url(/images/menu_bar_home.png)");
  }
  else
  {
    // Current page is regular page
    current_item.css("background", "url(/images/tab_reg.png)");
    sub_menu.css("background", "url(/images/menu_bar_reg.png)");
  }
  var menuitem = $("#main_menu ul li").mouseover(function(){
    if(!$(this).attr("class"))
    {
      if(menuitem.index(this) == 0)
      {
        $(this).css("background", "url(/images/tab_home_hover.png)");
        sub_menu.css("background", "url(/images/menu_bar_home.png)");

      }
      else
      {
        if ($(this).find('a').html() != 'Downloads') {
          $(this).css("background", "url(/images/tab_reg_hover.png)");
        }        
      }
    }
  });

  $("#main_menu ul li").mouseout(function(){
    if(current_item.index($("#main_menu ul li")) == 0)
    {
      sub_menu.css("background", "url(/images/menu_bar_home.png)");
    }
    else
    {
      sub_menu.css("background", "url(/images/menu_bar_reg.png)");
    }
    if(!$(this).attr("class"))
    {
      $(this).css("background", "none");
    }
  });

  /*
   * Submenu behaviour
   */
  $("#sub_menu ul li").mouseover(function(){
    $(this).css("background", "#ACB8C4");
  });
  $("#sub_menu ul li").mouseout(function(){
    $(this).css("background", "none");
  });

  /*
   * Tagcloud
   */
  $('.word').click(function(){
    var tag = trim($(this).html().replace(/&nbsp;/gi, ''));
    window.location='/tag/' + tag;
  });
  /*
   * Searchbox behaviour
   */
  $(".search_input_header").focus(function(){
    if($(this).val() == "Zoeken")
    {
      $(this).val("");
    }
  });

  $(".search_input_header").blur(function(){
    if($(this).val() == "")
    {
      $(this).val("Zoeken");
    }
  });
  $(".footer_search input").focus(function(){
    if($(this).val() == "Zoeken")
    {
      $(this).val("");
    }
  });

  $(".footer_search input").blur(function(){
    if($(this).val() == "")
    {
      $(this).val("Zoeken");
    }
  });
  /*
   * Word counter
   */
  $('#agendaform_short_description').parent().find('label').append('<br />200 karakters');
  $('#agendaform_short_description').keyup(function(){
    var cur_length = $(this).val().length;
    var length_left = 200 - cur_length;
    
    $(this).parent().find('label').html('Korte omschrijving *<br />' + length_left + ' karakters');

    if (length_left < 1)
    {
      $(this).val($(this).val().substring(0, 200));
      $(this).parent().find('label').html('Korte omschrijving *<br />0 karakters');
    }
  });

  /*
   * Word counter
   */
  $('#saleslinkform_description').parent().find('label').append('<br />200 karakters');
  $('#saleslinkform_description').keyup(function(){
    var cur_length = $(this).val().length;
    var length_left = 200 - cur_length;

    $(this).parent().find('label').html('Omschrijving *<br />' + length_left + ' karakters');

    if (length_left < 1)
    {
      $(this).val($(this).val().substring(0, 200));
      $(this).parent().find('label').html('Omschrijving *<br />0 karakters');
    }
  });

  /*
   * Word counter
   */
  $('#jobform_description').parent().find('label').append('<br />200 karakters');
  $('#jobform_description').keyup(function(){
    var cur_length = $(this).val().length;
    var length_left = 200 - cur_length;

    $(this).parent().find('label').html('Omschrijving *<br />' + length_left + ' karakters');

    if (length_left < 1)
    {
      $(this).val($(this).val().substring(0, 200));
      $(this).parent().find('label').html('Omschrijving *<br />0 karakters');
    }
  });

  /*
   * Fix captcha name
   */
  $('input.captcha').each(function(){
    var formname = $(this).parent().parent().parent().attr('name');
    $(this).attr('name', formname + '[captcha]');
  });

  /*
   * Saleslinks form frontend
   */
  $('#saleslinkform_section option').each(function(){
    if($(this).html() == 'Saleslinks')
    {
      $(this).attr('selected', 'selected');
      var section = $(this).val();
      $.ajax({
        url: '/showcategories/' + section,
        success: function(data){
          $('#saleslinkform_category').html(data);
        }
      });
    }
  });
  /*
   * Article rating
   */
  $("#article_rating_slider").slider({
    range: "max",
    min: 1,
    max: 10,
    value: 1,
    slide: function(event, ui) {
      $("#articlerateform_rating").val(ui.value);
      $("#article_rating").html(ui.value);
    }
  });
  
  if ($('#articlerateform_rating').length > 0)
  {
    $("#articlerateform_rating").val($("#article_rating_slider").slider("value"));
  }

  /*
   * Article formats
   */
  $('.article_container p').removeAttr('style');
  $('.article_container p').removeAttr('class');
  $('.article_container span').removeAttr('style');
  $('.article_container span').removeAttr('class');

  /*
   *
   */
  $('#jobform_billing_address_copy').change(function(){
    if ($(this).attr('checked'))
    {
      $('#jobform_billing_address').val($('#jobform_address').val());
      $('#jobform_billing_number').val($('#jobform_number').val());
      $('#jobform_billing_zipcode').val($('#jobform_zipcode').val());
      $('#jobform_billing_city').val($('#jobform_city').val());
    }
    else
    {
      $('#jobform_billing_address').val('');
      $('#jobform_billing_number').val('');
      $('#jobform_billing_zipcode').val('');
      $('#jobform_billing_city').val('');
    }
  });

  $('#searchresult ul > li > ul').eq(0).show();

  $('#searchresult ul > li').click(function(){
    $('#searchresult ul > li > ul').hide();
    $(this).find('ul').show();
    $('#searchresult ul > li').css('fontWeight', 'normal');
    $(this).css('fontWeight', 'bold');
  });

  $('#saleslink_overview > ul > li').click(function(){
    $('#saleslink_overview > ul > li > ul').slideUp();
    $(this).find('ul').slideDown();
  });

  $('#saform_training_0').click(function(){
    $('#saform_message').hide();
  });
  $('#saform_training_1').click(function(){
    $('#saform_message').hide();
  });
  $('#saform_training_2').click(function(){
    $('#saform_message').hide();
  });
  $('#saform_training_3').click(function(){
    $('#saform_message').hide();
  });
  $('#saform_training_4').click(function(){
    $('#saform_message').hide();
  });
  $('#saform_training_5').click(function(){
    $('#saform_message').show();
  });
});

  function checkForm(myForm) {
          var errorMessage = 'Het formulier kon niet worden verzonden om de volgende reden(en):\n\n';
          var errorFound = false;

           setHiddenBorder('field1337TD');
          if (myForm.field1337 != null) {
                  if (myForm.field1337.value == '') {
                          errorMessage = errorMessage + '- Geen waarde ingevuld voor E-mailadres.\n';
                          errorFound = true;
                          setRedBorder('field1337TD');
                  } else {
                          emailAddress = myForm.field1337.value;
                          if (emailAddress.search(/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&''*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i) == -1) {
                                  errorMessage = errorMessage + '- Ongeldig waarde ingevuld voor E-mailadres.\n';
                                  setRedBorder('field1337TD');
                                  errorFound = true;
                          }
                  }
          }
          if (myForm.field1338 != null) {
                  if (myForm.field1338.value == '') {
                          errorMessage = errorMessage + '- Geen waarde ingevuld voor Voornaam.\n';
                                  setRedBorder('field1338TD');
                          errorFound = true;
                  }
                   else {setHiddenBorder('field1338TD');}
          }
          if (myForm.field1340 != null) {
                  if (myForm.field1340.value == '') {
                          errorMessage = errorMessage + '- Geen waarde ingevuld voor Achternaam.\n';
                                  setRedBorder('field1340TD');
                          errorFound = true;
                  }
                   else {setHiddenBorder('field1340TD');}
          }
                   setHiddenBorder('field1351TD');
           if (myForm.field1351 != null) {
                   if (myForm.field1351.value == '') {
                   }
                   else {
                           number = myForm.field1351.value;
                           var re = new RegExp('[^0-9]', 'g');number = number.replace(re, '');myForm.field1351.value = number;
                           if (number.length < 10) {errorFound = true;
                           errorMessage = errorMessage + '- Geen geldig telefoon nummer ingevuld voor Ja, benader mij via dit telefoonnummer:\n';
                           setRedBorder('field1351TD');
                           } else 			 if (number.length > 12) {errorMessage = errorMessage + '- Geen geldig telefoon nummer ingevuld voor Ja, benader mij via dit telefoonnummer:\n';
                           setRedBorder('field1351TD');
                           } else 			 if ((number.length == 10) && (number.indexOf('0') != 0)) {errorFound = true;
                           setRedBorder('field1351TD');
                           errorMessage = errorMessage + '- Geen geldig telefoon nummer ingevuld voor Ja, benader mij via dit telefoonnummer:\n';
                           } else 			 if ((number.length == 11) && (number.indexOf('31') != 0)) {errorFound = true;
                           setRedBorder('field1351TD');
                           errorMessage = errorMessage + '- Geen geldig telefoon nummer ingevuld voor Ja, benader mij via dit telefoonnummer:\n';
                           } else 			 if ((number.length == 12) && (number.indexOf('310') != 0)) {errorFound = true;
                           setRedBorder('field1351TD');
                           errorMessage = errorMessage + '- Geen geldig telefoon nummer ingevuld voor Ja, benader mij via dit telefoonnummer:\n';
                           }}
                   }
          if ($('#field9999').val() == '1'){
            var field1352Value = '';
            for (i = 0; i < 9; i++) {
               if (document.getElementById('field1352_'+ i).checked) {
               if (field1352Value == '')
                       field1352Value = document.getElementById('field1352_'+i).value;
               else		 field1352Value += '|,|' + document.getElementById('field1352_'+ i).value;
              }
            }
            document.getElementById('field1352').value = field1352Value;
          }
          if (errorFound) {
                  alert(errorMessage);
                  return false;
          }
          if (!errorFound){
            $.cookie('subscribed', 'yes', {path: '/'});
            $.cookie('firstname', myForm.field1338.value, {path: '/'});
            $.cookie('lastname', myForm.field1340.value, {path: '/'});
            $.cookie('email', myForm.field1337.value, {path: '/'});
            
          }
          
          return true;
  }
