$(document).ready(function(){
	//*****Replace align attribute with class**************************************************//
	$('img[align="left"]').addClass('left').removeAttr('align');
	$('img[align="right"]').addClass('right').removeAttr('align');
	$('img[align="middle"]').addClass('middle').removeAttr('align');
	//**************************************************Replace target attribute with class**************************************************//
	$('a[target]').addClass('newwindow').removeAttr('target');
	//*****The following function make it possible to have web standard popups**************************************************//
	$('a.newwindow').click(function() {
		window.open($(this).attr('href'));
    return false;
	});
	//*****Add swfobject flash call**************************************************//
	if ($("#flashcontent").length > 0) {
	  var so = new SWFObject('swf/header-home-flash.swf', 'flash', '950', '233', '8', '#fff');
	  so.addParam('wmode', 'transparent');
	  so.write('flashcontent');
  }
	//*****Clear search text out search box on focus**************************************************//
	$('#txtSearch').focus(function() {
    if ($(this).val() == 'SEARCH') {
      $(this).val('');
	  }
  });
	//*****Extend the footer to the bottom of the page**************************************************//
  var footerHeight = $('#footer').height();
  var pageHeight = $(document.body).height();
  var windowHeight = $(document).height();
  var difference = windowHeight - pageHeight;
  $('#footer').height(footerHeight + difference)
	//*****Add starting number to second column list**************************************************//
  $('.right ol').attr('start','35');

//*****Replace target attribute with class**************************************************//
function replaceTarget() {
  if (!document.getElementsByTagName('a')) return false;
  $('a[target]').addClass('newwindow').removeAttr('target');
}
//*****Mask function from http://digitalbush.com/projects/masked-input-plugin**************************************************//
function mask() {
  if ($('#formValidate, #frmLogin').length < 1) return false;
  $('.zip').mask('99999');
  $('.phone').mask('(999) 999-9999');
  $('.date').mask('99/99/9999');
  $('.ssn').mask('9999');
  $('.year').mask('9999');
}
//*****jQuery form validation from http://bassistance.de/jquery-plugins/jquery-plugin-validation/**************************************************//
function validate() {
  if ($('#formValidate, #frmLogin').length < 1) return false;
  jQuery.validator.messages.email = "Enter a valid address.";
  $('#formValidate, #frmLogin').validate({
    highlight: function(element, errorClass) {//add the error class to the label and element
      if ($(element).is(':radio')) {
        $(element).parent().prev().addClass(errorClass);
      } else {
        $(element).addClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
      }
    },
    unhighlight: function(element, errorClass) {//remove error class from label and element once valid
      if ($(element).is(':radio')) {
        $(element).parent().prev().removeClass(errorClass);
        $(element).parent().prev().addClass('valid');
      } else {
        $(element).removeClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
      }
    },
    success: function(label) {//add the green box and checkmark for valid fields
      if (label.prev().is(':checkbox')) {
        label.next().addClass("valid");
        label.addClass("valid").text("✓");
      } else {
        label.prev().addClass("valid");
        label.addClass("valid").text("✓");
      }
    },
    onfocusout: false,
    onkeyup: false,
    onclick: false,
    //do not use the title attribute as the error message
    ignoreTitle: true,
    //make the error text wrapped in a span
    errorElement: 'span',
    //javascript instead of class specified rules
    rules: {
      'rdoLiveWith[]': 'required'
    },
    //messages other than the default 'X'
    messages: {
      'rdoLiveWith[]': '<br />X Please select one of the options below<br />'
    },
    // the errorPlacement has to take the h2 of radio buttons into account
    errorPlacement: function(error, element) {
      if ( element.is(':radio') )
        error.appendTo( element.parent().prev() );
      else
        error.insertAfter(element.closest('strong'));
    }
  });
}
//*****jQuery clear value from july 21st 2009 comment on http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click*****//
function clearDefaultValue() {
  if ($('input').length < 1) return false;
  $(':input').focus(function() {
    if($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  }).blur(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });
  $('#btnNext').click(function() {
    $(':input').each(function (i) {
      if($(this).val() == $(this).attr('title')) {
        $(this).val('');
      }
    });
  });
}
function populateDefaultValues(){
  $(':input').each(function(){
    if($(this).val() == ''){
      $(this).val($(this).attr('title'));      
    }
  });
}

//*****misc verify functions*****//
function verify() {
  $('.formVerify li:odd').addClass('odd');
  $('.formVerify li:even').addClass('even');
}

  /* Form Validation */
  replaceTarget();
  mask();
  validate();
  clearDefaultValue();
  populateDefaultValues();
  verify();
});
