$(document).ready(function(){
	jQuery.validator.addMethod("yourName", function(value, element) {
    	return this.optional(element) || value.match(/^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/);
	});
	
	jQuery.validator.addMethod("phoneAll", function(phone_number, element) {
    	phone_number = phone_number.replace(/\s+/g, "");
		return this.optional(element) || phone_number.match(/^[1-9]+(([\.\- ][0-9 ])?[0-9])*$/);
	});
	
	$(".download").click(function(){
		$("#blind").css({"opacity": 0.8}).fadeIn(750);
		$("#download").append('<form id="downloadForm" method="post" action="download.php">To download a free copy of this report, simply fill out the information below:<br /><br /><dl><dt><span style="color: #F00">*</span> Your Name:</dt><dd><input name="name" type="text" /></dd><dt><span style="color: #F00">*</span> Company:</dt><dd><input name="company" type="text" /></dd><dt><span style="color: #F00">*</span> Your E-Mail:</dt><dd><input name="email" type="text" /></dd><dt><span style="color: #F00">*</span> Phone:</dt><dd><input name="phone" type="text" /></dd></dl><div class="cancel">Cancel</div><input id="downloadButton" type="submit" value="Download"></form>').slideDown(1250);
		
		$(".cancel").click(function(){
			$("#download").slideUp(750);
			$("#blind").fadeOut(1250);
			$("#download").children().remove();
		});
		
		$("#downloadForm").validate({
			rules: {
				name: {
					required: true,
					yourName: true
				},
				company: "required",
				email: {
					required: true,
					email: true
				},
				phone: {
					required: true,
					phoneAll: true
					}
				},
			messages: {
				name: "Enter your name.",
				company: "Enter company name.",
				email: "Enter valid e-mail address.",
				phone: "Enter valid phone number."
			},
			submitHandler: function(form){
				$(form).ajaxSubmit({
					success: function(){
						$("#download").slideUp(750);
						$("#blind").fadeOut(1250);
						$("#download").children().remove();
						window.location.href = "http://www.revisions-editorial.com/media/portfolio/The-Key-is-the-Written-Word.pdf";
					}
				});
			}
		});
	});
});
