$(document).ready(function(){
    $("#form-share").validate( {
		rules: {
			from_name : {
				required:true,
				minlength:2
			},
			from_email : {
				required:true,
				email:true
			},
			to_name: {
				required:true,
				minlength:2
			},
			to_email : {
				required:true,
				email:true
			}
		},
		
		messages : {
			from_name: {
				required: "We need your name to let your friend know who this is from.",
				minlength: jQuery.format("At least {0} characters required!")
			},
			from_email : {
				required:"We need your email to let your friend know who this is from.",
				email:"Your email address must be in the format of name@domain.com"
			},
			to_name: {
				required:"We need your friend's name so we know who to send it to.",
				minlength:jQuery.format("At least {0} characters required!")
			},
			to_email : {
				required:"We need your friend's email so we know who to send this to.",
				email:"Your friend's email address must be in the format of name@domain.com"
			}
		},
		submitHandler: function(form) {
			
			// organize the fields
			// disable text fields
			$('.text').attr('disabled', 'true');
			
			// show loading sign
			$('#loading').show();
			
			
			// start AJAX
			$.ajax({
				
				// this is the PHP file that handles server-side validation and sendmail
				url:'process.php',
				
				// GET method
				type:'GET', 
				
				// data passed to PHP
				data:'from_name='+form.from_name.value + '&from_email=' + form.from_email.value + '&to_name=' + form.to_name.value + '&to_email=' + form.to_email.value + '&message=' + form.message.value, 
				dataType:"text", 
				// DO NOT CAHCE
				cache:false, 
				
				// SUCCESS!
				success: function (html) {
					// if process.php returns 1/true (sendmail success)
					if ( html == 1) {
						// hide the form
						$('#main p').hide('fast');
						$('#loading').hide();
						// reset form
						$('.text').removeAttr('disabled');
						$('.text').val("");
						// show success message
						$('#done').html('<p style="margin-bottom:50px;">Thank you for being brave enough to share Spook House Dave! with your friend. Please pass it along to more of your friends!</p>').fadeIn('slow');
					// if FAILED
					} else {
						alert('Sorry, unexpected error. Please try again later.' + html);
					}
				}
				
			});
			
			// cancel default behaviour
			return false;
			
			
		}
				
				
	})
});