jQuery(document).ready(function() {
	jQuery('#submit').click(function () {
		var page_title = jQuery('input[name=page_title]');
		var page_url = jQuery('input[name=page_url]');
		var yourname = jQuery('input[name=yourname]');
		var youremail  = jQuery('input[name=youremail]');
		var recipientemail = jQuery('input[name=recipientemail]');
		var note = jQuery('textarea[name=note]');
		var cc = jQuery('#cc:checked[name=cc]');

		if (yourname.val()=='' || yourname.val()=='Your Name') {
			yourname.addClass('error').fadeIn("slow");
			yourname.focus();
			return false;
		} else yourname.removeClass('error');

		if (youremail.val()=='' || youremail.val()=='Your Email Address') {
			youremail.addClass('error').fadeIn("slow");
			youremail.focus();
			return false;
		} else {
			if (!isValidEmailAddress(youremail.val())) {
				youremail.addClass('error').fadeIn("slow");
				youremail.focus();
				return false;
			} else {youremail.removeClass('error');}
		}

		if (recipientemail.val()=='' || recipientemail.val()=='Recipient\'s Email Address') {
			recipientemail.addClass('error').fadeIn("slow");
			recipientemail.focus();
			return false;
		} else {
			if (!isValidEmailAddress(recipientemail.val())) {
				recipientemail.addClass('error').fadeIn("slow");
				recipientemail.focus();
				return false;
			} else {recipientemail.removeClass('error');}
		}

		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);
		}

		//organize the data properly
		var data = 'page_title=' + page_title.val() + '&page_url=' + encodeURIComponent(page_url.val()) + '&yourname=' + yourname.val() + '&youremail=' + youremail.val() + '&recipientemail=' + recipientemail.val() + '&cc=' + cc.val() + '&note=' + encodeURIComponent(note.val());

		//disabled all the text fields
		jQuery('.text').attr('disabled','true');
		
		//show the loading sign
		jQuery('.loading').show();
		
		//start the ajax
		jQuery.ajax({
			//this is the php file that processes the data and send mail
			url: "http://www.mclaughlinstern.com/email_page.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {
				//if process.php returned 1/true (send mail success)
				//alert(html);
				if (html==1) {
					//hide the form
					jQuery('#email-form').slideUp('medium');

					//show the success message
					jQuery('#message_sent').slideDown('slow');

					//setTimeout('closeDOMWindow', 2000);

					_gaq.push(['_trackEvent', 'EmailPage', 'Sent', '{title} | 38.107.179.240']);

				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');		
			}
		});
		
		//cancel the submit button default behaviours
		return false;
	});
});
