// Begin BRS Formulator (version 2.2.10)
$(function(){
	$('.formulate').each(function(){
		// Remove accessibility labels
		$('label').remove();
			   
		// 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).parent('form').hasClass('overlabel') && $(this).hasClass('required')) {
				$(this).wrap('<span class="input_wrapper"></span>');
				$(this).before('<label for="'+$id+'" title="'+$label+'" class="'+$type+' required">'+$label+'&nbsp;<span class="required">*</span></label> ');
			}
			else if ($(this).parent('form').hasClass('overlabel')) {
				$(this).wrap('<span class="input_wrapper"></span>');
				$(this).before('<label for="'+$id+'" title="'+$label+'" class="'+$type+'">'+$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');
	
	// Create cross browser button hover `BKS
	$('input[class="button"]').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
});


// Begin BRS Validation Script (version 2.2.10)
function validateForm(event) {
	var a,e,e2,good;
	var i,inputs,input,value;
	
	good = true;
	
	// 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));
		
		// check for radio selection
		if (input.attr('type') == 'radio') { value = $('input[name=' + input.attr('name') + ']:checked').val() ? "1" : ""; }
		else { value = input.val(); }
		
		// check for any input value
		if(value.length == 0 ) {
			good = false;
			input.addClass('red_alert');
			$('label[for='+input.attr('id')+']').addClass('red_alert');
		}
		
		// check for minimum length
		if(input.attr('minlength') && input.attr('minlength') < value.length) {
			good = false;
			// apply styles
			input.addClass('red_alert');
			$('label[for='+input.attr('id')+']').addClass('red_alert');
		}
	}

	if(!good) {
		alert ("Please fill in all required fields.");
		event.preventDefault();
	}
	
	checkForm();
}

// Spam prevention
function checkForm() {
		$('#accesskey').val('j' + $('#accesskey').val() );
		$('#accesskey2').val('j' + $('#accesskey2').val() );
}

// Begin OverLabel Scripts (version 2.2.10)
function initOverLabels () {
	var labels, id, input;
	
	// Set focus and blur handlers to hide and show for overlabel forms
	labels = $('form.overlabel label');
	for (var i = 0; i < labels.length; i++) {
		var theLabel = $(labels.get(i));
		
		// Skip labels not associated with a field.
		id = theLabel.attr('for');
		input = $('#' + id);
		if (input.length != 1) { continue; }
		
		// Apply overlabel class to label.
		theLabel.addClass('overlabel');
		
		// Hide any fields having an initial value.
		if (input.val() !== '') { hideLabel(input.attr('id'), true); }
		
		// Set handlers to show and hide labels. [jq_1.4]
		input.focusin(function() { hideLabel($(this).attr('id'), true);  });
		input.focusout(function() { if ($(this).val() == '') { hideLabel($(this).attr('id'), false); } });
	}
};

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() {
	if($('form').hasClass('overlabel')){ setTimeout(initOverLabels, 50); }
	
	$('form.validate').submit(validateForm);
});
