$(function() {
	
	$('label a').click(function() {
		if ($(this).prev().attr('checked')==false) {
			$(this).prev().attr('checked','checked');
		} else {
			$(this).prev().attr('checked',false);
		}
		return false;
	});

	$('[name=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);
	});

	$('[name=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());
	});

	$('[name=description]').focus(function() {
		$(this).css('height', '300px');
	});

	$('[name=description]').blur(function() {
		$(this).css('height', '100px');
	});

	$('form').submit(function() {

		$(':text').css('border','1px solid #E6E6E6');
		$('#fback').hide();
		var errors = '';
		var msg = Array();
		
		if ($('textarea:first').val().length < 3) { 
			errors += 'textarea,';
			msg.push('A brief description is required.');
		}
		
		if ($('#name').val().match(/^\w+/i)==null) { 
			errors += '#name,';
			msg.push('Full contact name is required.');
		}
		
		if ($('#email').val().match(/^[a-z0-9\-\_\.]+@[a-z0-9\-\_\.]+\.[a-z\.]+$/i)==null) { 
			errors += '#email,';
			msg.push('A working email address is required.');
		}
		
		if ($('#phone').val().match(/^[\+|0][0-9 ]+/i)==null) { 
			errors += '#phone,';
			msg.push('A full phone number is required.');
		}
		

		if ($('#postcode').val().length < 2) { 
			errors += '#postcode,';
			msg.push('A postcode is required. (Enter your country if not UK.)');
		}

		
		if (errors!='') {
			$(errors).css('border','1px solid red');
			if ($('#fback').length > 0) {
				msg = '<h4>Please correct these errors and try again</h4><ul><li>'
					  + msg.join('</li><li>') + '</li></ul>';
				$('#fback').html(msg).fadeIn('slow');
				window.scroll(0, 0);
			} else {
				msg = 'Please correct the following errors and try again:\n\n'
					  + msg.join("\n - ");
				alert(msg);
			}
			pageTracker._trackPageview(document.location.href+'/js-errors');
			return false;
		}
	});
	
	$('#mlogos tr').css('cursor','pointer').click(function() {
		document.location.href = $(this).find('a').attr('href');
	});
	
});	