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);
}
function isValidAmount(string) {
	// http://regexlib.com/REDetails.aspx?regexp_id=131
	var pattern = new RegExp(/^\d+\.?\d{0,2}$/i);
	return pattern.test(string);
}

$(document).ready(function(){

	var options = {
		newsList: "#home-ticker-tape",
		startDelay: 10
	}
	if ($('#home-ticker-tape').length) $().newsTicker(options);
	
	// homepage gubbinry
	$('#home-products section').removeClass('prefix_2 alpha').addClass('hasjs').before('<ul id="home_products_nav" class="grid_2 alpha"></ul>').children('article').each(function(i) {
		$(this).attr('id','product_'+i).attr('name','product_'+i);
		var title = $(this).children('div').children('h1').text();
		$('#home_products_nav').append('<li><a href="#product_'+i+'">'+title+'</a></li>');		
	});
	$('#home_products_nav li:first').addClass('active');
	$('#home-products article:gt(0)').hide();
	$('#home-products article').fitted({ 'status': true, 'title': true });
	
	$('#home_products_nav a').click(function() {
		$('#home_products_nav li').removeClass('active');
		var destination = $(this).attr('href');
		$(this).parent('li').addClass('active');
		$('#home-products article').hide();
		$('#home-products article'+destination).show();
		return false;
	}).hover(function() {
		$(this).parent('li').addClass('hover');
	},function() {
		$(this).parent('li').removeClass('hover');
	});
	
	$('#content a:has(img)').addClass('hasImg');
	$('#content-inner form fieldset:not(.noshade) p:odd').addClass('odd');
	$('a[rel="_blank"]').attr('target','_blank');
	
	
	$(".datepicker").datepicker(
		{ dateFormat: 'dd-mm-yy' }
	);
	// hide helptext for date since this is only needed for non-JavaScript
	$(".datepicker").parent().children('span').remove();
	
	// toggle help text
	$('form#crm span.help:not(:has(a))').addClass('js-help');
	$('form#crm span.js-help').hide();
	$("p input, p textarea, p select").bind("focus", function(){
		$(this).siblings('span.js-help').fadeIn('fast');
	}).blur(function() {
		$(this).siblings('span.js-help').hide();
	});
	
	// apply focus to first element
	$("form#crm input:first").focus();
	
	// validation
	$("p input, p textarea").bind("blur", function(){
		var f_val = $(this).val();
		var f_id = $(this).attr('id');
		if (f_id == 'PHONE1') {
			if (isValidAmount(f_val) == true && f_val > 0) $(this).parent('p').removeClass('missed').addClass('hit');
			else $(this).parent('p').removeClass('hit').addClass('missed');
		} else if (f_id == 'EMAIL') {
			if (isValidEmailAddress(f_val) == false) $(this).parent('p').removeClass('hit').addClass('missed');
			else $(this).parent('p').removeClass('missed').addClass('hit');					
		} else {
			$(this).parent('p').addClass('hit').removeClass('missed');
		}		
		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'); 		
	});	
	// validation
	$("p.required input, p.required textarea").bind("blur", function(){
		var f_val = $(this).val();
		var f_id = $(this).attr('id');
		if (f_val.length == 0)
			$(this).parent('p').removeClass('hit').addClass('missed');				
	});
	
	
	// hide show 'How did you hear about us'
	$("form#crm #SOURCE[value!='Other']").parent().next().hide();
	$("form#crm #SOURCE").bind("change", function(){
		if($(this).val()=='Other'){
			$(this).parent().next().show();
		}
		else{
			$(this).parent().next().hide();
		}
	});
	// change label of hear about us 'other' 
	$("form#crm #SOURCE_OTHER").parent().children().html('Please Specify');		
	

});
