$(document).ready(function(){
	// Redirects
	$('#donate').click(function(){
		window.open('https://secure.yourpatriot.com/ou/fomh/donate.aspx');
	});
	$('#volunteer').click(function(){
		window.open('https://secure.yourpatriot.com/ou/fomh/volunteersignup.aspx');
	});
	$('#faq').click(function(){
		window.location.href = '/FAQ.aspx';
	});
	$('#connect').click(function(){
		window.location.href = '/Connect.aspx';
	});
	$('#contact').click(function(){
		window.location.href = '/Contact.aspx';
	});
	$('#talkback').click(function(){
		window.location.href = '/Talk_Back.aspx';
	});

	// Social
	$('#twitter').click(function(){
		window.open('http://twitter.com/hillrep');
	});
	$('#facebook').click(function(){
		window.open('http://www.facebook.com/matthewlistens');
	});
	$('#youtube').click(function(){
		window.open('http://www.youtube.com/MatthewListens');
	});



	// Newsletter
	$('#signup').click(function()  {
		if (isValidEmailAddress($('#email').val()))  {
			$.post(
				'/Domains/hill/files/newsletterhandler.php',
				'email=' + $('#email').val(),
				function(data)  {
					$('#email').removeClass('error');
					$('#email').addClass('success');
					$('#email').val('Success!');
					setTimeout(function() {
						$('#email').val('Email Address');
						$('#email').removeClass('success');
					}, 3000);
				}
			);
		}
		else  {
			$('#email').addClass('error');
		}
		return false;
	});

	$('#email').focus(function()  {
		if(this.value == this.defaultValue)  {
			this.select();
		}
	});


	// Talk back
	$('#tbname').focus(function()  {
		if(this.value == this.defaultValue)  {
			this.select();
		}
	});
	$('#tbemail').focus(function()  {
		if(this.value == this.defaultValue)  {
			this.select();
		}
	});
	$('#tbcomment').focus(function()  {
		if(this.value == this.defaultValue)  {
			this.select();
		}
	});
	$('#tbsubmit').click(function()  {
		if (!$('#tbname').val() || $('#tbname').val() == 'Your Name')  {
			$('#tbmessage').html('Your name is required.');
			setTimeout(function() {
				$('#tbmessage').html('');
			}, 3000);
			return false;
		}
		if (!isValidEmailAddress($('#tbemail').val()))  {
			$('#tbmessage').html('A valid email address is required.');
			setTimeout(function() {
				$('#tbmessage').html('');
			}, 3000);
			return false;
		}
		if (!$('#tbcomment').val() || $('#tbcomment').val() == 'Your comment...')  {
			$('#tbmessage').html('A comment is required.');
			setTimeout(function() {
				$('#tbmessage').html('');
			}, 3000);
			return false;
		}

		$.post(
			'/Domains/hill/files/talkback.php',
			{
				name: $('#tbname').val(),
				email: $('#tbemail').val(),
				message: $('#tbcomment').val()
			},
			function(data)  {
				$('#tbmessage').html('Your feedback has been sent. Thank you.');
				$('#tbname').val('Your Name');
				$('#tbemail').val('Email Address')
				$('#tbcomment').val('Your comment...');
				setTimeout(function() {
					$('#tbmessage').html('');
				}, 3000);
			}
		);
		return false;
	});
});

function isValidEmailAddress(emailAddress) {
	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);
}
