// Begin BRS Formulator (version 7.1.09)

$(function(){
	$('.jFormScript.formulate').each(function(){
		// Complete Text Fields and Text Areas with Labels and OverLabels
		$textField = $('input[type=text],textarea').not('.manual_override');
		$textField.each(function() {
			$label = $(this).attr('title');
			$id = $(this).attr('id');
			$type = $(this).attr('type');
			
			if ($(this).hasClass('overlabel') && $(this).hasClass('required')) {
				$(this).wrap('<span class="input_wrapper"></span>');
				$(this).before('<label for="'+$id+'" title="'+$label+'" class="'+$type+' overlabel required">'+$label+'&nbsp;<span class="required">*</span></label> ');
			}
			else if ($(this).hasClass('overlabel')) {
				$(this).wrap('<span class="input_wrapper"></span>');
				$(this).before('<label for="'+$id+'" title="'+$label+'" class="'+$type+' overlabel">'+$label+'</label> ');
			}
			else if ($(this).hasClass('required')) {
				$(this).before('<label for="'+$id+'" title="'+$label+'" class="'+$type+' required">'+$label+'&nbsp;<span class="required">*</span></label> ');
			}
			else {
				$(this).before('<label for="'+$id+'" title="'+$label+'" class="'+$type+'">'+$label+'</label> ');
			}
		});
		
		// Complete Checkboxes and Radio Buttons with Labels
		$radioCheck = $('input[type=checkbox],input[type=radio]').not('.manual_override');
		$radioCheck.each(function(){
			$label = $(this).attr('title');
			$id = $(this).attr('id');
			$type = $(this).attr('type');
			$value = $(this).attr('value');
			if ($label == '') $label = $value;
			
			if ($(this).hasClass('required')) {
				$(this).after('<label for="'+$id+'" title="This field is required." class="'+$type+' required">'+$label+'&nbsp;<span class="required">*</span></label> ');			
			}
			else {
				$(this).after('<label for="'+$id+'" title="'+$label+'" class="'+$type+'">'+$label+'</label> ');
			}
		});	
	});
	
	// Create Tab Order
	$('input, textarea, select, button').not('[type=hidden]').each(function(i){
		i += 1;
		$(this).attr({tabindex:i});
		$(this).not('[type=checkbox],[type=radio]').attr({name:$(this).attr('id')});
	});
	
	// Assign classes to input types to assist older browsers
	$('input[type=checkbox]').addClass('checkbox');
	$('input[type=radio]').addClass('radio');
	$('input[type=text]').addClass('text');
	$('input[type=submit]').addClass('button');
});

// Begin BRS Validation Script (version 02.23.09)

	function validateForm(event) {
		var a,e,e2,good,msg;
		var i,inputs,input,value;
		
		good = true;
		msg = "";
		
		// check the text fields, radio buttons and select boxes
		inputs = $('input.required, select.required', event.target );
		for(i=0; i<inputs.length; i++) {
			input = $(inputs.get(i));
			if (input.attr('type') == 'radio') {
				value = $('input[name=' + input.attr('name') + ']:checked').val() ? "1" : "";
			} else {
				value = input.val();
			}
			if(value.length == 0 ) {
				if(msg.length) msg = msg + "\n";
				if(input.attr('requirednote').length)
					msg = msg + "\t" + input.attr('requirednote');
				else 
					msg = msg + "\tThis field is required.";
				good = false;
			}
			if(input.attr('minlength') && input.attr('minlength') < value.length) {
				if(msg.length) msg = msg + "\n";
				if(input.attr('requirednote').length)
					msg = msg + "\t" + input.attr('requirednote');
				else 
					msg = msg + "\tThis field is required.";
				msg = msg + " Must be at least " + input.attr('minlength') + " characters.";
				good = false;
			}

		}

		if(!good) {
			alert ("The following fields are required:\n" + msg);
			event.preventDefault();
		}
		
		checkForm();
	}

function checkForm() {
		$('#accesskey').val('j' + $('#accesskey').val() );
		$('#accesskey2').val('j' + $('#accesskey2').val() );
}

// Begin OverLabel Scripts

function initOverLabels () {
  var labels, id, input;

  // Set focus and blur handlers to hide and show 
  // LABELs with 'overlabel' class names.
  labels = $('label.overlabel');
  for (var i = 0; i < labels.length; i++) {
		var theLabel = $(labels.get(i));
		
      // Skip labels that do not have a named association
      // with another field.
      id = theLabel.attr('for');
			input = $('#' + id);
      if (input.length != 1) {
        continue;
      }

      // Change the applied class to hover the label 
      // over the form field.
      theLabel.addClass( 'overlabel-apply' );

      // Hide any fields having an initial value.
      if (input.val() !== '') {
        hideLabel(input.attr('id'), true);
      }

			//alert(field.id);
      // Set handlers to show and hide labels.
      input.focus(function () {
        hideLabel($(this).attr('id'), true);
      } );
      input.blur(function () {
        if ($(this).val() == '') {
          hideLabel($(this).attr('id'), false);
        }
      } );

      // Handle clicks to LABEL elements (for Safari).
      theLabel.click(function () {
        var id, field;
        id = $(this).attr('for');
        if (id && (field = $('#' + id))) {
          field.focus();
        }
      });

  }
};

function hideLabel (field_id, hide) {
  var field_for;
  var labels = $('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = $(labels[i]).attr('for'); 
    if (field_for == field_id) {
      $(labels[i]).css('text-indent', (hide) ? '-1000px' : '0px' );
      //labels[i].className = 'overlabel-marker'; `BKS
      return true;
    }
  }
}

function setRequiredFields(fieldarray) {
	var i;
	for(i=0; i<fieldarray.length; i++) {
		$('#' + fieldarray[i][0]).attr('requirednote', fieldarray[i][1]);
	}
}

$(document).ready(function() {
	setTimeout(initOverLabels, 50);
	
	// Create cross browser button hover `BKS
	$('input[class="button"]').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
});

