// modal.js

$(document).ready(function() {
	
	// select all the a tags with name equal to modal
	$('a[name=modal]').click(function(e) {
		
		// cancel the link behavior
		e.preventDefault();
		
		// grab the a tag
		var id = $(this).attr("href");
		
		//get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		
		// set the height and width of mask to fill up the whole screen
		$("#mask").css({'width': maskWidth, 'height':maskHeight});
		
		// transition effect
		$("#mask").fadeIn(1000);
		$("#mask").fadeTo("slow", 0.8);
		
		// Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
		
		// set the popup window to center
		$(id).css("top", winH/2 - $(id).height()/2);
		$(id).css("left", winW/2 - $(id).width()/2);
		
		//transition effect
		$(id).fadeIn(2000);
	
	});

	// if close button is clicked
	$(".window .close").click(function(e) {
		// cancel link behavior
		e.preventDefault();
		$("#mask, .window").hide();
	 });
	
	// if mask is clicked:
	$("#mask").click(function() {
		$(this).hide();
		$(".window").hide();
	});
	
	
	
	// submit the form
	$(".error").hide();
		
		$(".button").click(function() {	
			// validate and process form here
			$(".error").hide();
				var name = $("input#name").val();
					if (name == "") {
					$("label#name_error").show();
					$("input#name").focus();
					return false;
					}
				var email = $("input#email").val();
					if (email == "") {
					$("label#email_error").show();
					$("input#email").focus();
					return false;
					}
				var message = $("textarea#message").val();
					if(message == "") {
					$("label#message_error").show();
					$("textarea#message").focus();
					return false;
					}
				var phone = $("input#phone").val();
				var regarding = $("select#regarding").val();
				
				
				
				
				if (phone == "") {
					var dataString = "name=" + name + "&email=" + email + "&regarding=" + regarding + "&message=" + message;
				} 
				else {
					var dataString = "name=" + name + "&email=" + email + "&phone=" + phone + "&regarding=" + regarding + "&message=" + message;
				};
				
				
				//var dataString = "name=" + name + "&email=" + email + "&phone=" + phone + "&regarding=" + regarding + "&message=" + message;
					
				//alert(dataString); 
				
				$.ajax({
					type:"POST",
					url: "../includes/processmail.php",
					data: dataString,
					success: function() {
						$(".contact-form").html("<div id='response'></div>");
						$("#response").html("<h2>Thank You</h2>")
							.hide()
							.fadeIn(2000);
					}
				});
			
			return false;
		});	
	
});