$(document).ready(function(){

// Regular Expression to test whether the value is valid

$.tools.validator.fn("[type=phone]", "Please supply a valid phone format", function(input, value) { 
	
	//return /^(\(?\d\d\d\)?)?( |-|\.)?\d\d\d( |-|\.)?\d{4,4}(( |-|\.)?[ext\.]+ ?\d+)?$/.test(value)
	
	var result = true;
	
	if(value !=""){	
      result= /^(\(?\d\d\d\)?)?( |-|\.)?\d\d\d( |-|\.)?\d{4,4}(( |-|\.)?[ext\.]+ ?\d+)?$/.test(value);
	}
	return result;
	
});

//This section of code adds some custom functionality to the validator tool. It makes it so it diplays the validation errors in an idependent div. NICE!
$.tools.validator.addEffect("wall", function(errors, event) {
	// get the message wall
	var wall = $(this.getConf().container).fadeIn();
	// remove all existing messages
	wall.find("p").remove();
	// add new ones
	$.each(errors, function(index, error) {
		
		wall.append(
			'<p><strong>'+error.input.attr("name")+ "</strong> " +error.messages+ '</p>'
		);		
	});
// the effect does nothing when all inputs are valid	
}, function(inputs)  {

});


$("#contact-form").validator({
   effect: 'wall', 
   container: '#comment_form_log',
   // do not validate inputs when they are edited
   errorInputEvent: null
// custom form submission logic  
}).submit(function(e)  { 
   // when data is valid 
   if (!e.isDefaultPrevented()) {
      // tell user that everything is OK
     
	   var datastr = $("#contact-form").serialize();
	   
       var loading_msg = "<p>Your message is being sent  <img src='/assets/img/ajax-loader_s.gif'/> </p>";
	   
	   $("#comment_form_log").html(loading_msg)
						     .fadeIn("slow")
							 .animate({opacity: 1.0}, 1500, function(){
								  //Force a delay of 1.5 secs, before sending email request to the server
								  send(datastr);
								  }); //end of func

	}//end of if stat
	
	// prevent the form data being submitted to the server
	e.preventDefault();
	
	}); //end of submit func
	
});	//End of doc ready

function send(datastr){
	
	 
	 $.ajax({
		type:"POST",
		url:"/assets/serv/conf_email.php",
		data:datastr,
		cashe:false,
		success:function(data){	
		$("#contact-form, .contact-form-wrap .form").hide();
		$(".contact-form-wrap .thank-you-message").show();
		$(':input','#contact-form')
	   .not(':button, :submit, :reset, :hidden')
	   .val('')
	   .removeAttr('checked')
	   .removeAttr('selected');	
		
		$("#comment_form_log").
		html("<p>"+data+"</p>").
		fadeIn("slow").
		delay(2000).fadeOut();	
		
		}
	  });
	
	}
