$(document).ready(function() {
	
	if (!Modernizr.input.placeholder) {
		$('.form-contact input.type-text').each(function() {
			if($(this).val() == '') $(this).val($(this).prev('label').html());
		});
		
		
		$('.form-contact input.type-text').focus(function() {
			if($(this).val() == $(this).prev('label').html()) $(this).val('');
		}).blur(function() {
			if($(this).val() == '') $(this).val($(this).prev('label').html());
		});
	}
	
	$('input.mail').click(function(ev) {
		ev.preventDefault();

		var pName = $('input[name=name]').val(),
			pVerb = $('input[name=verb]').val(),
			pAdj = $('input[name=adjective]').val(),
			pTalk = $('.radio-collection input:radio:checked').val(),
			pEmail = $('input[name=email]').val(),
			pPhone = $('input[name=phone]').val(),
			pPref = $('input[name=preference]').val(),
			pSignOff = $('input[name=sign-off]').val();
		
		/*	The Error messages 
			add new conditional statement for each required input
			css array should be input ID;
			errors should be the message.
		*/
		var errors	= new Array(),
			css = new Array(),
			i = 0;
		
		
		if(pName.length < 3) {
			errors[i] = 'your name';
			css[i] = 'name';
			i++;
		}
		
		if(pEmail.length < 5) {
			errors[i] = 'your email address';	
			css[i] = 'email';
			i++;		
		} else if(!checkEmail(pEmail)) {
			errors[i] = 'a valid email address';
			css[i] = 'email';
			i++;
		}
		

		if(errors.length == 0) {
			$.ajax({
				async: false,
				url: "/v6/includes/mailer.php",
				type: "POST",
				data: { 'name' : pName, 'verb' : pVerb, 'adjective' : pAdj, 'talk' : pTalk, 'email' : pEmail, 'phone' : pPhone, 'preference' : pPref, 'sign-off' : pSignOff },
				success: function(data) {
					$('#mail-msg').html(data);
				}
			});
		} else {
			var msg;
			if(errors.length == 1) {
				msg = 'Please enter ' + errors[0] + '.';
			} else {
				msg = "We'll want to write back; please enter ";
				for(var i=0; i < errors.length; i++) {
					if(i == (errors.length - 1)) {
						msg += errors[i] + '.';
					} else if(i == (errors.length - 2)) {
						msg += errors[i] + ' and ';
					} else {
						msg += errors[i] + ', ';
					}
				}
			}
			
			for(var i=0; i < css.length; i++){
				$('#'+css[i]).addClass('rqd-err');
			};
			
			$('#mail-msg').html('<p>'+msg+'</p>');
		}
	});
	
	$('.form-contact input#name').blur(function() { if($(this).val().length >= 3) $(this).removeClass('rqd-err'); });
	$('.form-contact input#email').blur(function() { if($(this).val().length >= 5 && checkEmail($(this).val())) $(this).removeClass('rqd-err'); });
	
	if($('body').hasClass('how-to-connect')) {
		
		$('form .type-radio').after('<span class="radio-check">&nbsp;</span>').hide();
		$('.radio-collection').delegate('.radio-check', 'click', function() {
			$('.radio-check.selected').removeClass('selected').prev('input').attr('checked', false);
			$(this).addClass('selected').prev('input').attr('checked', true);
		});
	}
	
});

function checkEmail(emailAdd) {
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAdd)) {
		return (true);
	} else {
		return (false);
	}
}
