$(document).ready(function(){
  var nav_buttons;
  $("#navigation_button_bar img").each(
    function(index) {

      var original_src = $(this).attr("src");
      $(this).attr("original_src", original_src);
    }
  );

  //Rollover navigation images
  $("#navigation_button_bar img").hover(function(){
    if($(this).attr("class") != "disabled") {
      var src = $(this).attr("src");

      var firstPart = src.substring(0,src.length - 12);
      var lastPart = src.substr(src.length - 12 ,12);

      if(lastPart == "inactive.gif") {
        src = firstPart + "active.gif";
      }

      $(this).attr("src", src);
    }
  },function(){
      $(this).attr("src", $(this).attr("original_src"));
  });

  $("#navigation_button_bar img.disabled").parent().click(function(){return false;});

  //put a focus on textboxes
  $("input:text,input:password").focus(function(){
    $(this).addClass("focus");
  }).blur(function(){
    $(this).removeClass("focus");
  });

  $('form').attr("autocomplete","off");

  $(":input:visible:enabled:not([readonly]):first").focus(); //Focus first visible element

  $('.numeric').numeric();
  $('.alpha').alpha({allow:".- "});
  //$('.alphanumeric').alphanumeric();
  $('.date').numeric({allow:"-"});

  $('.help').cluetip({
    width: 350,
    attribute: 'longdesc',
    dropShadow: true,
    dropShadowSteps: 4,
    cluetipClass: 'pensionplanner',
    sticky: true,
    activation: 'click',
    closePosition: 'title',
    closeText: 'x'
  });

  $('.help_link').cluetip({
    width: 350,
    dropShadow: true,
    dropShadowSteps: 4,
    cluetipClass: 'pensionplanner',
    sticky: true,
    activation: 'click',
    closePosition: 'title',
    closeText: 'x'
  });

  $('.help').hover(function() {
    $(this).attr('src','img/icons/help_active.gif');
  }, function() {
    $(this).attr('src','img/icons/help.gif');
  });

  jQuery.validator.addMethod("dateNL", function(value, element) {
    var test = this.required(element) || /^\d\d?\-\d\d?\-\d\d\d\d$/.test(value);
    var year = value.substr(6 ,4);
    var month = value.substr(3 ,2);
    var day = value.substr(0 ,2);
    var today = new Date();

    test = (year*10000 + month*100 + day*1 < today.getDate());
    test = (year > 1993 && year <= today.getFullYear()) && month <= 12 && day <= 31 && month > 0 && day > 0;
    return test;
  });

  jQuery.validator.addMethod("checkAges", function( value, element, param ) {
    if($(element).is(":visible")) {

      var ages = param.split(",");
      var lft_1 = $(ages[0]).val() * 1;
      var lft_2 = $(ages[1]).val() * 1;

      return this.required(element) || lft_2 > lft_1;
    } else {
      return true;
    }
  });

  jQuery.validator.addMethod("maxValueDependent", function( value, element, param ) {
    var max = $(param).val();
    max = max * 1;
    return this.required(element) || value <= max;
  });

  $('body').click(function() {
    $("#cluetip-close:visible").click();
  });

  $("#cluetip").click(function() {
    return false;
  });

  $("#footer a").attr("target","_blank");

});//end domready

function intval(v) {
  v = parseInt(v,10);
  return isNaN(v) ? 0 : v;
}

