$(function() {

	// load the modal window
	$('a.modal').click(function(){

		// scroll to top
		$('html, body').animate({scrollTop:0}, 'fast');

		// before showing the modal window, reset the form incase of previous use.
		$('.success, .error').hide();
		$('form#contactForm').show();
		
		// Reset all the default values in the form fields
		$('#tellafriendname').val('');
		$('#tellafriendemail').val('');
		$('#emailfriend').val('');
		$('#tellafriendsubject').val('');
		$('#tellafriendmessage').val('I am loving this! Check it out on The Sauce Suppliers website.  www.thesaucesuppliers.com');

		//show the mask and contact divs
		$('#mask').show().fadeTo('', 0.7);
		$('div#popupContact').fadeIn();

		// stop the modal link from doing its default action
		return false;
	});

	// close the modal window is close div or mask div are clicked.
	$('div#close, div#mask').click(function() {
		$('div#popupContact, div#mask').stop().fadeOut('slow');

	});

	/*$('#contactForm input').focus(function() {
		$(this).val(' ');
	});
	
	$('#contactForm textarea').focus(function() {
        $(this).val('');
    });*/

	// when the Submit button is clicked...
	$('input#submit').click(function() {
	$('.error').hide().remove();
		//Inputed Strings
		var tellafriendname = $('#tellafriendname').val();
		var	tellafriendemail = $('#tellafriendemail').val();
		var	emailfriend = $('#emailfriend').val();
		var	tellafriendsubject = $('#tellafriendsubject').val();
		var tellafriendmessage = $('#tellafriendmessage').val();
		var productlink = $('#productlink').val();
		var productname = $('#productname').val();
		
	
		//Error Count
		var error_count = 0;
		
		//Regex Strings
		var email_regex = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
		
			//Test Username
			/*if(!username_regex.test(name)) {
				$('#contact_header').after('<p class=error>Invalid username entered!</p>');
				error_count += 1;
			}*/
			
			if(jQuery.trim(tellafriendname) === "") {
				$('#contact_header').after('<p class=error>Please enter you name!</p>');
				error_count += 1;
			}
			
			//Test Email
			if(!email_regex.test(tellafriendemail)) {
				$('#contact_header').after('<p class=error>Invalid email entered!</p>');
				error_count += 1;
			}
			
			if(!email_regex.test(emailfriend)) {
				$('#contact_header').after('<p class=error>Invalid friends email entered!</p>');
				error_count += 1;
			}
			
			
			//No Errors?
			if(error_count === 0) {
				$.ajax({
					type: "POST",
					url: "http://www.thesaucesuppliers.com/wp-content/themes/shape/send.php",
					/*data: "tellafriendname=" + tellafriendname + "&tellafriendemail=" + tellafriendemail + "&emailfriend=" + emailfriend + "&tellafriendsubject=" + tellafriendsubject + "&tellafriendmessage=" + tellafriendmessage,*/
					data: "tellafriendname=" + tellafriendname + "&tellafriendemail=" + tellafriendemail + "&emailfriend=" + emailfriend + "&tellafriendsubject=" + tellafriendsubject + 
					"&tellafriendmessage=" + tellafriendmessage + "&productlink=" + productlink + "&productname=" + productname,					
					error: function() {
						$('.error').hide();
						$('#sendError').slideDown('slow');
					},
					success: function () {
						$('.error').hide();
						$('.success').slideDown('slow');
						$('form#contactForm').fadeOut('slow');
					}				
				});	
			}
			
			else {
                $('.error').show();
            }
			
		return false;
	});
	
});
