$(document).ready(function(){
	$("form#submit").submit(function() {
 
	// we want to store the values from the form input box, then send via ajax below
	var name1    = $('#name1').attr('value');
	var phone    = $('#phone').attr('value');
	var dak      = $('#dak').attr('value');	
	var cmpy      = $('#cmpy').attr('value');
	var findus      = $('#findus').attr('value');
	var message  = $('#message').attr('value');


if(name1 == '' ) {
	$('#error').html("Enter your name.");
	$('#error').fadeIn();
	$('#success').hide();
	$('#name1').focus();
 } 
 else if(phone == '' ) {
	$('#error').html("Enter your phone number.");
	$('#error').fadeIn();
	$('#success').hide();
	$('#phone').focus();
 } 
else if(dak == '' ) {
	$('#error').html("Enter your email.");
	$('#error').fadeIn();
	$('#success').hide();
	$('#dak').focus();
} 

 else if(dak == '' || !isValidEmailAddress(dak) ) {
	$('#error').html("Enter your valid email address");
	$('#error').fadeIn();
	$('#success').hide();
	$('#dak').focus();
 } 
else if(cmpy == '' ) {
	$('#error').html("Enter your company name.");
	$('#error').fadeIn();
	$('#success').hide();
	$('#phone').focus();
 } 
else if(findus == '' ) {
	$('#error').html("Enter how did you find us?");
	$('#error').fadeIn();
	$('#success').hide();
	$('#phone').focus();
 } 

else if(message == '' ) {
	$('#error').html("Enter your message for us.");
	$('#error').fadeIn();
	$('#success').hide();
	$('#message').focus();
 } 

 else {
	 $('#loader').show();
	 
		$.ajax({
			type: "POST",
			url: "js/ajax.php",
			//data: " name ="+ name, 
			// please don't give a space
			data: "name1="+name1+"&phone="+phone+"&dak="+dak+"&cmpy="+cmpy+"&findus="+findus+"&message="+message, 
			
			success: function(data){
				
				if (data=='ok') {
				$('#error').hide();
				 $('#loader').fadeOut();
				$('#success').fadeIn();
				$('form').clearForm();
				} else {
					
				alert("Some kind of error: "+data);
				
				}

				
			}
		});
 }		
	return false;
	});
});

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);
}


$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};