// Begin BRS Formulator (version 5.8.10)
$(function(){
	$('.formulate').each(function(){
		// Remove accessibility labels
		$('label').not('.form_help').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> ');
			}
			
			// Position Help Labels
			if ( $(this).siblings('label[for='+$id+']').hasClass('form_help') ) {
				var help = $('label[for='+$id+'].form_help');
				help.css({display:'none'});
				$(this).focus(function(){
					var fieldPos = $(this).position();
					help.css({ position:'absolute',top:fieldPos.top+'px',left:(fieldPos.left + $(this).outerWidth() + 4)+'px' });
					help.fadeIn();
				});
				$(this).blur(function(){ help.fadeOut(); });
			}
		});
		
		// 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
	$('input[class="button"],.btn').hover(
		function() { $(this).addClass('hover'); },
		function() { $(this).removeClass('hover'); }
	);
	
	// Highlight labels when form has class of hilite
	$('form.hilite input, form.hilite textarea').each(function(){ 
		var hilite = $(this).attr('id');
		$(this).focus(function(){ $('label[for='+hilite+']').not('.form_help').addClass('hilite'); });
		$(this).blur(function(){ $('label[for='+hilite+']').not('.form_help').removeClass('hilite'); });
	});

});



function validatePhoneNumber(strPhone){
	var t = strPhone.replace(/[^\d]/g, "");
	if(t.length == 10) return true;
	return false;
}



// Begin BRS Validation Script (version 5.8.10)
function validateForm(event) {
	var a,e,e2,good;
	var i,inputs,input,value;
	
	good = true;
	
	// clean up any old notifications
	$('.red_alert', event.target ).removeClass('red_alert');
	
	// 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));
		
		errorFound = false;
		
		// 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 ) errorFound = true;
		
		// check for minimum length
		if(input.attr('minlength') && input.attr('minlength') < value.length) errorFound = true;
		
		// validate phone numbers
		if(input.hasClass('phone')) {
			if(!validatePhoneNumber(value)) errorFound = true;
		}
		
		if(errorFound) {
			good = false;
			// apply styles
			input.addClass('red_alert');
			$('label[for='+input.attr('id')+']').addClass('red_alert');	
			// Jump to first alert
			// window.location = '#'+$('.red_alert:first').attr('for');
		}
	}

	if(good) {
		checkForm();
		
		if($(event.target).hasClass('ajax')){
			event.preventDefault();
			var thedata = $(event.target).serializeArray();
			var theurl = $(event.target).attr('action');
			$.post(theurl, thedata, function(data) { 
				$(event.target).hide();
				var thanks = $(event.target).siblings('.thanks');
				$('.thank_you',thanks).replaceWith('<strong>Thank you, '+ $('input[name=name_field]',event.target).val() +'.</strong>');
				//alert('Thank you, '+ $('input[name=name_field]',event.target).val() +'\nYour request has been sent');
				thanks.show();
			} );
		}
	} else {
		alert ("Please fill in all required fields.");
		event.preventDefault();
	}

}

// 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);
});
