$(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();
		$('form.contact_blurb').show();
		
		// Reset all the default values in the form fields
		$('#username').val('');
		$('#useremail').val('');
		$('#comment').val('');
		$('#tel').val('');


		//show the mask and contact divs

		$('#mask').css("width",$(window).width());
		$('#mask').css("height",$(window).height());
		//alert ($(window).height());
		$('#mask').show().fadeTo('', 0.7);
		$('div#contact').fadeIn();
		 $("body").css({ overflow: 'hidden' })
		 $('select').hide();

		// 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#contact, div#mask').stop().fadeOut('slow');
		 $('select').show();
		$("body").css({ overflow: 'inherit' }) 

	});

//	$('#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();
	$('#contactForm .error').hide();
	
		//Inputed Strings
		var name = jQuery.trim($('#username').val()),
		comment = jQuery.trim($('#comment').val()),
		email = jQuery.trim($("#useremail").val()),
		from = jQuery.trim($("#from").val()),
		restid = jQuery.trim($("#rest-id").val());
		subscribe = $("#subscribe:checked").length;

		//Error Count
		var error_count = 0;
		
		//Regex Strings
		var username_regex = /^[a-z0-9_-]{3,16}$/,
	    emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		

		//Blank Comment?
			if(!comment) {
			$('.contact_header').after('<p class=error>Please enter your comments!</p>');
			error_count += 1;
			}
			
			
			
			if(!emailReg.test(email) || !email ) {
            $('.contact_header').after('<p class=error>Enter a valid email address!</p>');
            error_count += 1;
        	}
	

			//Test Username
			if(!name) {
				$('.contact_header').after('<p class=error>Invalid username entered!</p>');
				error_count += 1;
			}
			
	
			//url: "/wp-content/themes/sugarvine/inculdes/contact_form_php_modal/send.php",
			//
			// data: "name=" + name + "&email=" + email + "&comment=" + comment + "&restid=" + restid,
			//No Errors?
			//No Errors?
			if(error_count === 0) {
				$.ajax({
					type: "post",
					url: "/wp-content/themes/sugarvine/inculdes/contact_form_php_modal/send.php",
					data: "from=" + from + "&name=" + name + "&email=" + email + "&comment=" + comment + "&restid=" + restid+ "&subscribe=" + subscribe,
					success: function(data, textStatus, jqXHR) {
					// alert('success' + data + textStatus + jqXHR);
					$('.error').hide();
					$('.success').slideDown('slow');
					$('form#contactForm').fadeOut('slow');
					},
					error: function(jqXHR, textStatus, errorThrown) {
					$('.error').hide();
					// alert('error' + jqXHR + textStatus + errorThrown);
					$('.error').hide();
					$('.success').slideDown('slow');
					$('form#contactForm').fadeOut('slow');
					}
				});	
			}
			
			else {
                $('.error').show();
            }
			
		return false;
	});
	
});



