//*****The following function make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  if (!document.getElementsByTagName('a')) return false;
  $('a.newwindow').click(function() {
		window.open($(this).attr('href'));
    return false;
	});
}
//*****Mask function from http://digitalbush.com/projects/masked-input-plugin**************************************************//
function mask() {
  $('#txtZip').mask('99999');
  $('#txtPhone').mask('(999) 999-9999');
  $('#txtDOB').mask('99/99/9999');
  $('#txtSSN').mask('999-99-9999');
}
//*****jQuery form validation from http://bassistance.de/jquery-plugins/jquery-plugin-validation/**************************************************//
function validate() {
  $('#formId').validate({
    //add the error class to the label and element
    highlight: function(element, errorClass) {
		  if ($(element).is(':radio')) {
		    $(element).parent().prev().addClass(errorClass);
		  } else {
        $(element).addClass(errorClass);
        $(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
      }
    },
    //remove error class from label and element once valid
    unhighlight: function(element, errorClass) {
		  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);
      }
    },
    //add the green box and checkmark for valid fields
    success: function(label) {
		  if (label.prev().is(':checkbox')) {
        label.next().addClass("valid");
        label.addClass("valid").text("✓");
      } else {
        label.prev().addClass("valid");
        label.addClass("valid").text("✓");
      }
    },
    //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: {
      'HalfNunc[]': 'required'
    },
    //messages other than the default 'X'
    messages: {
		  'HalfNunc[]': '<br />X Please select one of the options below'
		},
    // 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);
		}
  });
}
//*****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() {
  $(':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 form functions*****//
function formStuff() {
  function cloneTableRow() {
    $('.addALine').click(function() {
      //the row to clone
      var $row = $(this).closest('tr').prev();
      //the cloned row
      var $clonedRow = $row.clone();
      //the amount of rows to append to end of the id and name
      var index = $(this).closest('tr').siblings().length + 1;
      $clonedRow.find('*').andSelf().filter('[id]').each( function(){
        id = this.id.slice(0,this.id.length - 1);
        this.id = id + index;
      });
      $clonedRow.find('*').andSelf().filter('[name]').each( function(){
        name = this.name.slice(0,this.name.length - 1);
        this.name = name + index;
      });
      $clonedRow.find('*').andSelf().filter('[value]').each( function(){
        this.value = '';
      });
      $clonedRow.find('span').remove();
      $clonedRow.find('input').removeAttr('class');
      $clonedRow.find('input').removeAttr('checked');
      $clonedRow.find('select').removeAttr('class');
      $clonedRow.insertAfter($row);
      $clonedRow.find('*').andSelf().filter('input').eq(0).focus();
      return false;
    });
  }
  cloneTableRow();
}
//*****misc verify functions*****//
function verify() {
  $('.formVerify li:odd').addClass('odd');
  $('.formVerify li:even').addClass('even');
}
//*****Load all functions**************************************************//
$(document).ready(function(){
  strictNewWindow();
  mask();
  validate();
  clearDefaultValue();
  populateDefaultValues();
  formStuff();
  verify();
});