$(function() {
	$('.error').hide();
	$('input.text-input').css({backgroundColor:"#FFFFFF"});
	$('input.text-input').focus(function(){
		$(this).css({backgroundColor:"#FFDDAA"});
	});
	$('input.text-input').blur(function(){
		$(this).css({backgroundColor:"#FFFFFF"});
	});
	    jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
	    phone_number = phone_number.replace(/\s+/g, ""); 
		return this.optional(element) || phone_number.length > 9 &&
			phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
	}, "Please specify a valid phone number");

$("#contact_form form").validate({
	rules: {
		name: {
			required: true,
			minlength: 5
		},
		email: {
			required: true,
			email: true
		},
		phone: {
			phoneUS: true
		},
		fileToUpload: {
			accept: "doc|pdf|rtf|txt|xls|docx|xlsx"
		}
	},
	messages: {
		name: {
			required: "Please enter your name.",
			minlength: jQuery.format("Name must be at least {0} characters.")
		},
		email: {
			required: "We need your email address to contact you.",
			email: "Please enter a valid email address."
		},
		phone: {
			phoneUS: "Please enter a valid US phone number."
		},
		fileToUpload: {
			accept: "The accepted file types are doc, pdf, rtf, txt, xls, docx & xlsx"
		}
	},
	errorPlacement: function(error, element) {
		error.appendTo( ("#contact_error") );
	},
	submitHandler: function(form) {
		var name = $("input#name").val();
		var email = $("input#email").val();
		var phone = $("input#phone").val();
		var zip = $("input#zip").val();
		var job = $("select#job").val();
		var comments = $("textarea#comments").val();
		var filePresent = $("input#fileToUpload").val();
		
		if(filePresent != "") {
			//$("div#contact_error").html("<p style='color:#d7deaf;font-weight:bold;width:100%;text-align:right;'>Uploading&hellip;</p>");
			$("div#contact_error")
			.ajaxStart(function(){
				$(this).html("<p style='color:#d7deaf;font-weight:normal;width:100%;text-align:right;line-height: 32px'><img src='images/ajax-loader.gif' alt='' />Loading resume&hellip;</p>");
			})
			.ajaxComplete(function(){
			});

			$.ajaxFileUpload
			(
				{
					type: "POST",
					url:'bin/process-file.php',
					secureuri:false,
					fileElementId:'fileToUpload',
					dataType: 'json',
					success: function (data, status)
					{
						if(typeof(data.error) != 'undefined')
						{
							if(data.error != '')
							{
								var errorMessage = data.error;
								$("div#contact_error").html("<p class='upload-error'>" + errorMessage + "</p>");
								
							}else
							{
								var pathToFile = data.msg;
								var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&job=' + job + '&comments=' + comments + "&pathToFile=" + pathToFile;
								
								$("div#contact_error")
								.ajaxStart(function(){
									$(this).html("<p style='color:#d7deaf;font-weight:normal;width:100%;text-align:right;line-height: 32px'><img src='images/ajax-loader.gif' alt='' />Sending form&hellip;</p>");
								})
								.ajaxComplete(function(){
									$(this).html("");
								});
								
								
								$.ajax({
									type: "POST",
									url: "bin/process.php",
									data: dataString,
									success: function() {
										if( $("#question_4_button").attr("disabled") == true ) {
											$('.panel').html("<img src=\"images/newguywithphone.png\" class=\"result-image\" alt=\"Another guy with a phone.\" /><h2 class=\"result\">Thank you for contacting us!</h2><p class=\"result\"><a href='http://www.jobster.com/outreach/careers/Microsoft-Entertainment-and-Devices/jobs?location=&amp;owner=&amp;keywords=seattle&amp;i=Amx0UbWKwg%3D%3D' title='Potential Matches at Jobster.com'>See all South Lake Union jobs</a>.</p>");
										}
										$('#contact_form').html("<div id='message'></div>");
										$('#message').html("<h2>Contact Form Submitted!</h2>")
											.append("<p>We will be in touch soon.</p>")
											.hide()
											.fadeIn(1500, function() {
												$('#message').append("<img id='checkmark' src='images/check.png' />");
											});
										setTimeout(function() {
											$('div#hide_button').click();
										}, 3500);
									}
								});  //end ajax
								
								
							}
						}
					},
					error: function (data, status, e)
					{
						// alert(e);
					}
				}
			); // end ajaxFileUpload
			
		} else { // filePresent is empty
			
			var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&job=' + job + '&comments=' + comments;
			
			$.ajax({
				type: "POST",
				url: "bin/process.php",
				data: dataString,
				success: function() {
					if( $("#question_4_button").attr("disabled") == true ) {
						$('.panel').html("<img src=\"images/newguywithphone.png\" class=\"result-image\" alt=\"Another guy with a phone.\" /><h2 class=\"result\">Thank you for contacting us!</h2><p class=\"result\"><a href='http://www.jobster.com/outreach/careers/Microsoft-Entertainment-and-Devices/jobs?location=&amp;owner=&amp;keywords=seattle&amp;i=Amx0UbWKwg%3D%3D' title='Potential Matches at Jobster.com'>See all South Lake Union jobs</a>.</p>");
					}
					$('#contact_form').html("<div id='message'></div>");
					$('#message').html("<h2>Contact Form Submitted!</h2>")
						.append("<p>We will be in touch soon.</p>")
						.hide()
						.fadeIn(1500, function() {
							$('#message').append("<img id='checkmark' src='images/check.png' />");
						});
					setTimeout(function() {
						$('div#hide_button').click();
					}, 3500);
				}
			});  //end ajax
		}; // end filePresent if

	}
});

});
