//  Created to handle all SB sign ups - Some work to continue with
// Am I in here?

//$( function() {
$(document).ready(function(){
	
	var at = (parseFloat( $().jquery ) > 1.2) ? '' : '@';
	var emailFilter = new RegExp(/^[^@]+@[^@.]+\.[^@]*\w\w$/);
	var illegalChars= new RegExp(/[\(\)\<\>\,\;\:\&\\"\[\]]/);

	//error check the whole form
	if ($('#llh2011TestingField') ) {
	
		$.validator.addMethod( "notPrompt", 
			function(value, element) {
				var isValid = false;
				var prompt = $(element).attr("prompt");
				
				if( prompt != value){
					isValid = true;
				}
		
				return(isValid);
			}, "Invalid"
		);
		
		$('.dataSubmit').click( function () {
			$('#sendmail').submit();
			return false;
		});
		
		var validator = $('#sendmail').validate( {
			errorPlacement: function(error, element) {
				error.appendTo( element.parent("div").children("span") );
				
			},
			
			onblur: false,
			focusInvalid: false, // Focus the last active or first invalid element on submit via validator.focusInvalid().
			onfocusout: false, // Validate elements (except checkboxes/radio buttons) on blur. If nothing is entered, all rules are skipped, except when the field was already marked as invalid.
	
			submitHandler: function( form ) { // careful not to cause a loop with submit handler...		
					form.submit();
					return false;		
			},
			messages: {
				'Title'	: 'Required',
				'email' : {
						required :	'Required',
						email:		'Invalid'
						},
				'given_name' : {
						required : 'Required',
						minlength : 'Invalid'
						},
				'family_name' : {
						required : 'Required',
						minlength : 'Invalid'
						}		
				
			},
			rules: {
				'Title'	: 'required',
				'email' : {
						required : true,
						email : true
						},
				'given_name' : {
						required : true, 
						minlength : 2,
						notPrompt : true
						},
				'family_name' : {
						required : true, 
						minlength : 3,
						notPrompt : true
						}		
			}
			
		 });
		
	}else {
		$('#sendmail').submit(function() {
			errorMsg = '';
			errorMsg += (($('select['+at+'name=title]', this).val()=='')?'Please select your title\n':'');
			errorMsg += (($('input['+at+'name=family_name]', this).val()=='') || ($('input['+at+'name=family_name]', this).val()=='Surname')?'Please enter your surname\n':'');
			errorMsg += (($('input['+at+'name=email]', this).val()=='') || ($('input['+at+'name=email]', this).val()=='Email') || ($('input['+at+'name=email]', this).val().match(illegalChars)) || (!$('input['+at+'name=email]', this).val().match(emailFilter))?'Please enter a valid e-mail address.\nAddress must be in the format user@domain.com':'');
			
			if (errorMsg != '') {
				alert(errorMsg);
				return false;
			} else {
				return true;
			}
		});	
	}
	
	//default wording on fields
	$('.defaultInput').each( function(i,n) {
		$(n).val( (($(n).val()=='') ? $(n).attr('prompt') : $(n).val()) ).focus( function() {
			if( $( this).val()==$(this).attr('prompt')) {
				$( this).val('').removeClass('defaultInput');
			}
		} ).blur( function() {
			if( $( this).val()=='') {
				$( this).val( $(this).attr('prompt') ).addClass('defaultInput');
			}
		} );
	} );
	
});


// Function to accomodate BFB who had a lovely open window function that is awful and will be altered!!!
function openwin(url) {
	window.open(url,'mywindow','width=770,height=300, scrollbars=yes');
}
