$(function() {
	 $("#send").click(function() {
	 	var user = $("#user").val();
	 	var mail = $("#mail").val();
	 	var phonecode = $("#phonecode").val();
	 	var phonenumber = $("#phonenumber").val();
	 	var phonecode1 = $("#phonecode1").val();
	 	var phonenumber1 = $("#phonenumber1").val();
	 	var text = $("#text").val();
	 	var cap = $("#cap").val();
	 	var capcha = $("#capcha").val();
	 	
	 	if(user==''){
	 		$("#user").focus();
		    alert('Вы забыли представиться.');
		    return false;
	 	}
	 	if(mail==''){
	 		$("#mail").focus();
		    alert('Вы забыли ввести E-mail');
		    return false;
	 	}else{
	 		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);
	     	if(!pattern.test(mail)) {
	   		$("#mail").focus();
	   		alert('Введите корректный E-mail.');
	     	return false;
	     	}
	 	}
	 	if(phonecode==''){
	 		$("#phonecode").focus();
		    alert('Вы забыли ввести код номера');
		    return false;
	 	}else{
	 		var pattern = new RegExp(/^([-0-9 ()]+)$/i);
	     	if(!pattern.test(phonecode)) {
	   		$("#phonecode").focus();
	   		alert('Введите корректный код номера.');
	     	return false;
	     	}
	 	}
	 	if(phonenumber==''){
	 		$("#phonenumber").focus();
		    alert('Вы забыли ввести номер телефона');
		    return false;
	 	}else{
	 		var pattern = new RegExp(/^([-0-9 ()]+)$/i);
	     	if(!pattern.test(phonenumber)) {
	   		$("#phonenumber").focus();
	   		alert('Введите корректный номер телефона.');
	     	return false;
	     	}
	 	}
	 	if(phonecode1!=''){
	 		var pattern = new RegExp(/^([-0-9 ()]+)$/i);
	     	if(!pattern.test(phonecode1)) {
	   		$("#phonecode1").focus();
	   		alert('Введите корректный код номера.');
	     	return false;
	     	}
	 	}
	 	if(phonecode1!=''&&phonenumber1==''){
	 		var pattern = new RegExp(/^([-0-9 ()]+)$/i);
	     	if(!pattern.test(phonenumber1)) {
	   		$("#phonenumber1").focus();
	   		alert('Введите корректный номер телефона.');
	     	return false;
	     	}
	 	}
	 	if(phonecode1!=''&&phonenumber1!=''){
	 		var pattern = new RegExp(/^([-0-9 ()]+)$/i);
	     	if(!pattern.test(phonenumber1)) {
	   		$("#phonenumber1").focus();
	   		alert('Введите корректный номер телефона.');
	     	return false;
	     	}
	 	}
	 	
	 	
	 	
	 	if(text==''){
	 		$("#text").focus();
		    alert('Вы забыли ввести текст заявки');
		    return false;
	 	}
	 	if(capcha!=cap){
	 		$("#capcha").focus();
			   alert('Введите код защиты');
			   return false;
	 	}
	 	
	 	var dataString = 'user='+user+'&mail='+mail+'&phone='+phone+'&text='+text+'&act=send';
	 	$.ajax({
		      type: "POST",
		      url: "/jquery/events/mail.php",
		      data: dataString,
		      success: function(data) {
		       if(data == 'true')
		        {
		        	alert('Ваше письмо отправлено!');
		  
		        	window.location.href="";
		        }
		      }
		     });
	 	
	 });
});
