jQuery(document).ready(function($) {
	
	/***  ADDS THE TITLE OF EACH FIELD AS AN INTERNAL LABEL ***/
	function formBoxes()
	{
		// for each input field with a title
		$('input[title], textarea[title]').each(function() {
			
			// if it has no value
			if($(this).val() === '' || $(this).attr('title'))
			{
				
				// add the labeled class
				$(this).addClass('labeled');
				
				// set the value to the title
				$(this).val($(this).attr('title'));
				
			}
			
			// when focused
			$(this).focus(function()
			{
				if($(this).val() === $(this).attr('title'))
				{
					$(this).removeClass('labeled').val('');
				}
			});
			
			$(this).blur(function() {
				if($(this).val() === '') {
					$(this).addClass('labeled').val($(this).attr('title'));
				}
			});
		
		});
	}
	
	formBoxes();
	
	
	$('#addmobile').click(function(){
		
		$(this).hide();
		
		$('#mobile').fadeIn(1000);
		
	});
	
	/*** qTip stuff ***/
	/* $('a.qTip').each(function()
	{
		$(this).qtip(
		{
			content: $('div#tip_' + $(this).attr('name')), // looks for div on page with is as #tip_[id]
			
			show: 'mouseover',
			
			hide: 'mouseout',
			
			position: {
				corner: {
					target: 'leftTop',
					tooltip: 'rightBottom'
				}
			},
			
			style: {
				name: 'blue',
				tip: 'rightBottom',
				width: {
					min: 225,
					max: 300
				},
				border: {
					width: 2,
					radius: 3,
					color: '#069'
				}
			}
		});
	}); */
	
	/*** Alphanumeric Masking ***/
	$('.alpha').alpha();
	$('.alphanumeric').alphanumeric();
	$('.numeric').numeric();
	$('.numeric-with-spaces').numeric({allow:" "});
	$('.numeric-date').numeric({allow:"/"});
	$('.regular').alphanumeric({allow:".,'()-& "});
	$('.alpha-with-spaces').alpha({allow:".,()-' "});
	$('.alphanumeric-with-spaces').alphanumeric({allow:" "});
	$('.email').alphanumeric({allow:".!#$%&'*+-/=?^_`{|}~@"});
	
	
	/*** Form Validation ***/
	jQuery.validator.addMethod('notselect', function(value, element) {
		
		if($(element).val() == "--select--")
			return false;
		else
			return true;
	}, 'Please make a selection');
	
	jQuery.validator.addMethod('nottitle', function(value, element) {
		
		if($(element).val() === $(element).attr("title"))
			return false;
		else
			return true;
	}, 'Please make a selection');
	
	jQuery("#form2").validate({
		
		errorElement: "p",
		
		/* success: function(label) {
			label.addClass("success");
		}, */
		
		onkeyup: true,
		
		/* highlight: function(element, errorClass) {
			$(element).fadeOut(function() {
				$(element).fadeIn();
			});
		}, */
		
		groups: {
			username: "first_name last_name"
		},
		
		rules: {
			first_name: {
				required: true,
				minlength: 2,
				nottitle: true
			},
			last_name: {
				required: true,
				minlength: 2,
				nottitle: true
			},
			phone: {
				required: true,
				minlength: 10,
				nottitle: true
			},
			mobile: {
				required: false
			}
		},
		
		messages: {
			first_name: {
				nottitle: "Your name is required",
				required: "Please enter your full name",
				minlength: "First name is too short"
			},
			last_name: {
				nottitle: "Your name is required",
				required: "Please enter your full name",
				minlength: "Last name is too short"
			},
			phone: {
				nottitle: "Phone number is required",
				required: "Please enter your phone number",
				minlength: "Phone number is too short"
			},

			N20000000zXNm: "Please accept our Privacy Policy"
		},
		
		errorPlacement: function(error, element) {
			
			if(element.attr("id") == "N20000000zXNm")
				error.appendTo(element.parent("div"));
			else
				error.appendTo(element.parent("div")/* .next("td") */);
		}
	
	}); // end validate
	
}); // end document ready


