// JavaScript Document


$(document).ready(function() {

/* 
Karl Swedberg's js class technique
via http://www.tvidesign.co.uk/blog/improve-your-jquery-25-excellent-tips.aspx
*/

$('html').addClass('js');


/* Cache selectors */
var fName = $('#name');
var fEmail = $('#email');
var fTwit = $('#twitter');
var fForm = $('#form-mailing-list');

/* Save origninal values */
var origName = fName.attr('value');
var origEmail = fEmail.attr('value');
var origTwit = fTwit.attr('value');
			 
fName.live("click",function () {
	
	if ($(this).attr('class') != 'changed') {
		$(this).attr('value','').addClass("changed");
	}
		
	return false;
});

fName.focus(function () {
	
	if ($(this).attr('class') != 'changed') {
		$(this).attr('value','').addClass("changed");
	}
		
	return false;
});

fEmail.live("click",function () {
	
	if ($(this).attr('class') != 'changed') {
		$(this).attr('value','').addClass("changed");
	}	
		
	return false;
});

fEmail.focus(function () {
	
	if ($(this).attr('class') != 'changed') {
		$(this).attr('value','').addClass("changed");
	}
		
	return false;
});

fTwit.live("click",function () {
	
	if ($(this).attr('class') != 'changed') {
		$(this).attr('value','').addClass("changed");
	}	
		
	return false;
});

fTwit.focus(function () {
	
	if ($(this).attr('class') != 'changed') {
		$(this).attr('value','').addClass("changed");
	}
		
	return false;
});


fName.blur(function () {
	
	if ($(this).attr('value') == '') {
		$(this).attr('value',origName).removeClass("changed");
	}
		
	return false;
});

fEmail.blur(function () {
	
	if ($(this).attr('value') == '') {
		$(this).attr('value',origEmail).removeClass("changed");
	}
		
	return false;
});

fTwit.blur(function () {
	
	if ($(this).attr('value') == '') {
		$(this).attr('value',origTwit).removeClass("changed");
	}
		
	return false;
});

var f = 0;

fForm.submit(function () {
	
	f = 0;
	message = '';
	
	if (fName.attr('value') == origName && fEmail.attr('value') != origEmail) {
		message = 'Woops! You forgot to put in your name.';
		f= 1;
	}
	
	if (fName.attr('value') != origName && fEmail.attr('value') == origEmail) {
		message = 'Woops! You forgot to put in your email.';
		f= 1;
	}
	
	if (fName.attr('value') == origName && fEmail.attr('value') == origEmail) {
		message = 'Woops! You forgot to put in your name and email.';
		f= 1;
	}
	
	if (message == '') {
		// check if the email address is valid
		if (fEmail.attr('value').indexOf('.') == -1 || fEmail.attr('value').indexOf('@') == -1) {
			message = "Woops! You've given us an email that isn't valid.";
			f = 1;
		}
	}
	
	if (fTwit.attr('value') == origTwit && f == 0) {
		fTwit.attr('value') = '';
	}
	
	if (f == 1) {
		alert(message);
		return false;	
	}
});


});

