$(document).ready(function(){
	

   //submission scripts
  $('.contactForm').submit( function(){
		//statements to validate the form	
		var emailfilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var datefilter = /^((0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2})*$/;
		var email = document.getElementById('e-mail');
		var fail = false; // start with a no fail state and if anything is wrong, set the fail flag 
		
		var name = document.cform.name.value;
		var email1 = document.cform.email1.value;
		var email2 = document.cform.email2.value;
		var phone = document.cform.phone.value;
		var date = document.cform.date.value;
		var referredby = document.cform.referredby.selectedIndex;
		var message = document.cform.message.value;
		
		// NAME -- check to see if name is blank 
		if (name == "") {
			$('.name-missing').show();
			fail = true;
		} else {
			$('.name-missing').hide();
		}	
		
		// EMAIL -- first check to see if the email fields both match - essential to get a correct reply email or lead is lost if no phone number 
		//redo, reverse
		if (emailfilter.test(email1)) {
			$('.email-missing').hide();//yep, real email format
			if(email1 == email2){
				$('.email-nomatch').hide();
			}
			else{
				$('.email-nomatch').show();
				fail = true;
			}
		}
		else 
		{
			$('.email-missing').show();//nope
			$('.email-nomatch').show();
			fail = true;
		}
		
		    
		// PHONE -- validate if not empty -> clear out any spacer characters, such as parentheses, dashes, spaces, and dots. 
		if(phone != "") {// there is something in the phone field 
			
			//strip out acceptable non-numeric characters
			var strippedPhone = phone.replace(/[\(\)\.\-\ ]/g, '');
			/*alert(parseInt(strippedPhone));
			alert( "test: " + /\D/.test(strippedPhone) );
			alert(strippedPhone.length);			
			*/
			// check for all integers and that the length is 10 digits
			if ( !(/\D/.test(strippedPhone) )  && (strippedPhone.length == 10) ) {
			   $('.phone-wrong').hide();	
			} 
			else 
			{
				$('.phone-wrong').show();
				fail = true;
			}
				
		}
		else
		{
			$('.phone-wrong').hide();
		}//end phone
		
		
	
		
		// Wedding date
		if (date != "") 
		{
			if (datefilter.test(date)) 
			{
				$('.date-wrong').hide();
			}
			else
			{
				$('.date-wrong').show();
				fail = true;
			}
		}
		else
		{
			$('.date-wrong').hide();
		}  // end date
		
		
		// REFERRED BY
		// make sure that this field is selected, 0 is the non-selected state
		if (referredby == 0)
		{
			$('.none-selected').show();
			fail = true;
		}
		else
		{
			$('.none-selected').hide();
		} //end referredby
		
		
		// MESSAGE
		
		if (message == "") 
		{
			$('.message-missing').show();
			fail = true;
		} 
		else 
		{
			$('.message-missing').hide();
		}		
		
		
		// SUBMIT THE FORM
		
		if (fail)
		{
			return false;
		} 
		else
		{
			//hide the form
			$('.contactForm').hide();
		
			//show the loading bar
			$('.loader').append($('.bar'));
			$('.bar').css({display:'block'});
		
			//send the ajax request
			$.post('mail.php',{name:$('#name').val(),
							  email1:$('#email1').val(),
							  phone:$('#phone').val(),
							  date:$('#date').val(),
							  referredby:$('#referredby').val(),
							  message:$('#message').val()},
		
			//return the data
			function(data){
			  //hide the graphic
			  $('.bar').css({display:'none'});
			  
			  
			  if(jQuery.trim(data) == "success")
			  { 
			  		$('.email-error').hide();
					$('#messageSent').show();
					 
			
			  }
				else
				{ 
					$('.contactForm').show();
					$('.email-error').show();
					alert(data);
				}
			}, "text");
			
			//waits 2000, then closes the form and fades out
			//setTimeout('$("#backgroundPopup").fadeOut("slow"); $("#contactForm").slideUp("slow")', 2000);
			
			//stay on the page
			return false;
		} 
  });
	//only need force for IE6  
//	$("#backgroundPopup").css({  
//		"height": document.documentElement.clientHeight 
//	});  
});