$.elementReady("form-registration-errors", function() {
  /* removes error div on register page if empty */
     if ($('#form-registration-errors').text().length > 0 ) {$('#form-registration-errors').show(); }
});


$.elementReady("login-screen-container", function() {

/* Login landing */
/* shows remember me box on login page - must be hidden if js is disabled */
   $("#login-screen .rememberme_hidden_div").css('display','block');

/* removes error div if no error  */
   if ($('div.error').text().length =='0') {$('div.error').hide();};
});


$(document).ready(function(){

/* Global */

/* hides pagination if only one page with results is available */
   if ($('div.pagination-index p a').length == 0 ) {$('div.pagination').hide();}
   else {   if ( $('div.pagination-next a').attr('href') == "" ) {$('div.pagination-next').remove();}
            if ( $('div.pagination-prev a').attr('href') == "" ) {$('div.pagination-prev').remove();}
        }

/* opens in new window all links that has a class = new_window */
/* used on newsfeed */
  $('a.new_window').attr('target','_blank');

/* print this page  */
   $('a.print_page').click(function(){
       window.print();
   });



/* CIO Barometer */
/* cio barometer - removes next months question form if there is no question - */
   if ($('input#nextmonths_question').val() == '0' ) $('div.answer').remove();

/* CIO Barometer - rmeoves tip text after submiting answer for next month's question */
if ( $('input#thankyou_page').val() == '1' ) $('p.tip_text').remove();


/*Registration */
/*registration scripts */
  $('#form-registration-box input').attr('size','40');
  $('div.password_input input:first').after('<br />');
  $('div.checkbox_input label').hide();
  $('input#sq_commit_button').val("");


/* Events */
/* removes margin class from links on images listing */
   var alinks_counter = 0;
   $('div.events-pictures-listing a').each(function(){
       alinks_counter++;
       if ( alinks_counter%3 == 0){
          $(this).removeClass('events-picture-margin');
       };
   });

/* Blog article */
/* adding a comment on Blog Article page - pre-filling fields */

 /* prefills email address field */
   if ($('input#user_email')){
      $('#metadata_field_text_888_value').val($('input#user_email').val());
   }
 /* prefills company field */

   if ($('input#user_company')){
      $('#metadata_field_text_887_value').val($('input#user_company').val());
   }

 /* prefills name field */
   var comment_name = $('input#user_firstname').val() +" "+ $('input#user_lastname').val();

   if ( $('input#user_firstname').length > 0){
       $('#metadata_field_text_892_value').val(comment_name);
   }





/* Contact Us page - pre-filling fields */

 /* prefills email address field */
   if ($('input#contact_email')){
      $('#q462_q2').val($('input#contact_email').val());
   }
 /* prefills name field */
   var contact_name = $('input#contact_firstname').val() +" "+ $('input#contact_lastname').val();

   if ($('input#contact_firstname').length > 0){
       $('#q462_q1').val(contact_name);
   }

/* events - related videos last entry removes dotted border */
 $('#related_box div.rhs-item:last').addClass('rhs-item-last');


/* Registration - username metadata field */

   $('#user_0_233').blur(function(){
      $('#metadata_field_text_974_value').val($(this).val());
   });

/* writes user email to user username field while editing user details - in simple edit mode */
$('div.email_edit input').blur(function(){
  $('div.username_edit input').val($('div.email_edit input').val());

});


/* events page - members login header */
  if ( $('input#loggedin').val() == 'yes' ) {
    $('h1.h1-events').addClass('events-loggedin');  
  }



$('div#form-leave-comment-box').each(function(){scrollDown();});
$('div#next-question-form-box').each(function(){scrollDown();});
$('div#register-event-form-box').each(function(){scrollDown();});


});

/* function that scrolls down to the footer */
function scrollDown(){
   $('form:not([action*="site-search"])').each(function(){
      var oldaction = $(this).attr('action');
      $(this).attr('action',oldaction + '#footer');
   });
}

