	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}
    
	$(function () {

		// INNERFADE	
		$('#home_gallery').innerfade({ 
			speed: 'slow', 
			timeout: 3000, 
			type: 'sequence', 
			containerheight: '244px'		
		});
	
		$("#btn_make_appt").click(function(){
		
			$("#modal-dialog").modal({
				onOpen: function(dialog){
					dialog.overlay.fadeIn('fast', function(){
						dialog.container.slideDown('slow', function(){
							dialog.data.fadeIn('slow');
						});
					});
				},
				onClose: function (dialog){
					dialog.data.fadeOut('fast', function () {
						dialog.container.slideUp('slow', function () {
							dialog.overlay.fadeOut('slow', function () {
								$.modal.close();
							});
						});
					});
				}
				
				
				
			});
		});
		
		$('.error').hide();
		$('.val_error').hide().slideDown(500);
		
		$('.contact-loading').hide();	
		$('.textbox').css({backgroundColor:"#FFFFFF"});
		$('.textbox').focus(function(){
			$(this).css({backgroundColor:"#E1FFDF"});
		});
		$('.textbox').blur(function(){
			$(this).css({backgroundColor:"#FFFFFF"});
		});		
		
		$('#contact-us').submit( function(){
			
			$('.error').hide();
			$('.contact-loading').hide();	
			
		// Validation
		
			var name = $("input#name").val();
			if (name == "") {
				$("span#name_error").show();
				$("input#name").focus();
				return false;
			}
			var email = $("input#email").val();
				if (email == "") {
				$("span#email_error").show();
				$("input#email").focus();
				return false;
			}				
			if(!isValidEmailAddress(email)) {
				$("span#invalid_email").show();
				$("input#email").focus();
				return false;
			} 		
			var loc = $("select#location").val();
			if (loc == "") {
				$("span#loc_error").show();
				$("input#loc").focus();
				return false;
			}
			
		// Processing
	
			$.ajax({
				url: '/sendEmail',
				data: $('#modal-dialog form').serialize() + '&action=send',
				type: 'post',
				cache: false,
				dataType: 'html',
				success: function () {
					$('#modal-dialog #contact-us').fadeOut(500, function () {
							$('#modal-dialog').html('<h3 class="center">Thank you!<br/>Your appointment request has been completed.</h3><div class="seperator"></div><center>Someone will be contacting you shortly to confirm your appointment. You will then be given a password to access your online paperwork in the "Patients" section.</center>');
							$("#simplemodal-container").animate({ height: 140 }, 650);
					});
				}
			});

		//stay on the page
		return false;
		});	
		
				
    });
	

