$(function() {
	$('.question').each(function(i) {
		$('.question:eq('+i+') :radio').click(function() {
			$.scrollTo( $('.arrow:eq('+i+')') , 500,  {offset:19}  );
		});
		$('.question:eq('+i+') select').change(function() {
			$.scrollTo( $('.arrow:eq('+i+')') , 500,  {offset:19}  );
		});
		$('.question:eq('+i+') :text:first, .question:eq('+i+') textarea').focus(function() {
			$.scrollTo( $('.arrow:eq('+(i-1)+')') , 500,  {offset:19}  );
		});
	});
	$('label a').click(function() {
		if ($(this).prev().attr('checked')==false) {
			$(this).prev().attr('checked','checked');
		} else {
			$(this).prev().attr('checked',false);
		}
		return false;
	});

	$('#phone').keyup(function() {
		var phone = $(this).val();
		var Reg2 = /(^0207|02\d|08[0-9]{2}|01[0-9]{3}|07[0-9]{3})([0-9]{3})([0-9 ]+$)/i;
		var correct = phone.replace(/\D/g,'');
			correct = correct.replace(Reg2,"$1 $2 $3");
			correct = correct.replace(/^00(\d\d)(\d)(.*$)/,"+$1 ($2) $3");
		$(this).val(correct);
	});

	$('#postcode').keyup(function() {
		var postcode = $(this).val();
		var postcodeReg2 = /(^[A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2}$)/i;
		var correct = postcode.replace(postcodeReg2,"$1 $2");
		$(this).val(correct.toUpperCase());
	});

	$('form').submit(function() {
		$(':text').css('border','1px solid #E6E6E6');
		var errors = '';
		var msg = '';
		
		if ($('textarea:first').val().length < 3) { 
			errors += 'textarea,';
			msg += ' - A brief description of your project is required.\n';
		}
		
		if ($('#name').val().match(/^\w+/i)==null) { 
			errors += '#name,';
			msg += ' - Your full name is required.\n';
		}
		
		if ($('#email').val().match(/^[a-z0-9\-\_\.]+@[a-z0-9\-\_\.]+\.[a-z\.]+$/i)==null) { 
			errors += '#email,';
			msg += ' - A working email address is required.\n';
		}
		
		if ($('#phone').val().match(/^[\+|0][0-9 ]+/i)==null) { 
			errors += '#phone,';
			msg += ' - A full phone number is required.\n';
		}
		
/*
		if ($('#postcode').val().match(/\b[A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {0,2}[0-9][ABD-HJLN-UW-Z]{2}\b/i)==null) { 
			errors += '#postcode,';
			msg += ' - A valid UK postcode is required.\n';
		}
*/
		
		if (errors!='') {
			$(errors).css('border','1px solid red');
			alert('Please correct the following errors and try again:\n\n'+msg);
			pageTracker._trackPageview(document.location.href+'/js-errors');
			return false;
		}
	});
});	