/* Mail form */

function showPreloader() {
	$('#submit').hide();
	$("#hide").show();
}

function hidePreloader() {
	$('#hide').hide();
	$('#submit').show();
}

function IsEmail(email) {
	var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(regex.test(email)) 
		return true;
	else
		return false;
}

$(document).ready(function() {
	$('#nav a[@class=none]').hover(function() {
		$(this).animate({ color: '#e4e4e4' },{ duration: 250, queue: false });
		},
		function() {
			$(this).animate({ color: '#efefef'},{ duration: 250 });
	});
	
	hidePreloader();
	
	var options = {
		target: '#result',
		url: 'mail.php?a=1',
		resetForm: 'true',
		beforeSubmit: showPreloader,
		success: hidePreloader
	};
	$('#mailForm').ajaxForm(options);

	var txtDefaults = {};
	txtDefaults.name	= 'Namn';
	txtDefaults.mail	= 'E-mail';
	txtDefaults.phone	= 'Telefon';
	txtDefaults.company	= 'Företag';



		$('input[type=text]').focus(function() {
			if ( $(this).attr('name') in txtDefaults ) {
				for ( var defs in txtDefaults ) {
					if ( $(this).val() == txtDefaults[defs] ) {
						$(this).val('');
						return;
					}
				}
			}
		});

		$('input[@type=text]').blur(function() {
			if ( $(this).val() == '' ) {
				$(this).val( txtDefaults[ $(this).attr('name') ] );
			}
		});
		
		$('textarea').focus(function() {
			if($(this).html() == 'Message') {
				$(this).html('');
			}
		});
		
		$('textarea').blur(function() {
			if($(this).html() == '') {
				$(this).html('Message');
			}
		});
		
		var validateUsername = $('#error');

		$('input[name=mail]').keyup(function() {
			var t = this;
			if (this.value != this.lastValue) {
				this.timer = setTimeout(function() {
					if (IsEmail(t.value)) {
						validateUsername.addClass('valid');
					}
					else {
						validateUsername.addClass('error');
				    };
				}, 200);

				this.lastValue = this.value;
			}
		});
});
