function isValidEmailAddress(emailAddress) {
	// http://www.reynoldsftw.com/2009/03/live-email-validation-with-jquery/
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

$(document).ready(function(){
	// Hide the honeypot field
	$('#form-hpx').hide();
	
	var pathname = window.location.pathname;
	
	if ( (!$.browser.msie) || ($.browser.msie && $.browser.version >= 7) ) {
		
		$('#home .sub-panel div h3:gt(0)').siblings().hide();
		$('#home .sub-panel div h3').wrapInner('<a href=""></a>');
		$('#home .sub-panel div h3:gt(0) a').addClass('hidden');
		$('#home .sub-panel div h3:eq(0) a').addClass('shown');
		$('#home .sub-panel div h3 a').click(function() {
			if ($(this).hasClass('hidden')) {
				$('#home .sub-panel div h3').siblings().slideUp();
				$('#home .sub-panel div h3 a').removeClass('shown').addClass('hidden');
				$(this).removeClass('hidden').addClass('shown');
				$(this).parent('h3').siblings().slideDown();
			}
			return false;		
		});
		
	}
	$('input:submit').addClass('submit');
	var search_input = $('#global li input').val();
	$('#global li input').focus(function() {
		if ($(this).val() == search_input) $(this).val('');
	}).blur(function() {
		if ($(this).val() == '') $(this).val(search_input);
	});
	$('.light-lunch').each(function(){
		$(this).children('h1:eq(1)').before('<ul id="ll-nav"></ul>');
		$(this).children('h2').each(function(i) {
			var link_title = $(this).text();
			link_title = $.trim(link_title);
			$('#ll-nav').append('<li><a href="'+pathname+'#ll-heading-'+i+'">'+link_title+'</a></li> ');
			$(this).attr('id','ll-heading-'+i);
		});
	});
	

	// hide show 'How did you hear about us'
	$("form#contact_us_form #hear_about_us[value!='Other']").parent().next().hide();
	$("form#contact_us_form #hear_about_us").bind("change", function(){
		if($(this).val()=='Other'){
			$(this).parent().next().show();
		}
		else{
			$(this).parent().next().hide();
		}
	});
	// change label of hear about us 'other' 
	// COMMENTED OUT AS CAUSES IE JS ERROR, AND APPEARS TO DO NOTHING...
    //$("form#contact_us_form #hear_about_us_other").parent().children().html('Please Specify');
	
	// process required fields
	$("p.required input, p.required select").bind("blur", function(){
		var f_val = $(this).val();
		var f_id = $(this).attr('id');
		if (f_id == 'email') {
			if (isValidEmailAddress(f_val) == false){
				$(this).addClass('required');
				$(this).parent('p').children().addClass('required');
			}
			else {
				$(this).removeClass('required');
				$(this).parent('p').children().removeClass('required');
			}			
		} else if (f_val.length == 0) {
			$(this).addClass('required');
			$(this).parent('p').children().addClass('required');			
		} else {
			$(this).removeClass('required');
			$(this).parent('p').children().removeClass('required');
		}		
		var missed_q = $("p.missed").size();
		var hit_q = $("p.hit").size();
		var required_q = $('p.required:not(:has(select))').size(); 
		if (hit_q == required_q) $('input[type="submit"]').removeAttr('disabled'); 		
	});
	
	// external links
	$('a.external').attr("target","_blank");
	// document links
	$('a.doc_link').attr("target","_blank");
	
	// New Join clinks form changes
    $('#directory-information').hide();
    $('#add-directory').click(function() { $('#directory-information').toggle(); });
    if ($('#add-directory').is(':checked')) { $('#directory-information').show(); } // Init on page load
    
    $('#individual').click(function() {
      if ($(this).is(':checked')) {
        $('#organisation').val('').attr('disabled', 'disabled'); 
      } else {
        $('#organisation').removeAttr('disabled');
      }
    });
    if ($('#individual').is(':checked')) { $('#organisation').removeAttr('disabled'); } // Init on page load
    
});

