$(document).ready(function() {

	jQuery.getScript("http://www.ktmltd.ru.swteh.ru/metas/files/js/ui/js/jquery-ui-1.8.2.custom.min.js"); 

	function rand(m,n){
    m = parseInt(m);
    n = parseInt(n);
    return Math.floor( Math.random() * (n - m + 1) ) + m;
	};
	
	var one = rand(1, 30);
	var two = rand(1, 30);
	var cap = one + ' + ' + two + ' = ';
	var ndata = {'res':one + two};
	$('#result').html(cap);
	$.ajax({
				type: "POST",
				data: ndata,
				url: "/metas/plugin/sendmail/php/connector/captcha.php",
				beforeSend: function(){
				},
				success: function(answer){
				$('#key').html(answer);
				$('#ans_res').val('');
				}
		});


	$('#submitpl').click(function() {	
		var name = $('#name').val();
		var tele = $('#tele').val();
		var mail = $('#mail').val();
		var also = $('#also').val();
		var capth = $('#ans_res').val();
		var ndata = {'res':capth};
		var key = $('#key').html();		
		var data = {"name":name, "tele":tele, "mail":mail, "also":also};
		$.ajax({
				type: "POST",
				data: ndata,
				url: "/metas/plugin/sendmail/php/connector/captcha.php",
				beforeSend: function(){
				},
				success: function(answer){
				if (name != '' && tele != '' && key == answer)
					{
						$.ajax({
							type: "POST",
							data: data,
							url: "/metas/plugin/sendmail/php/connector/sendmail.php",
							beforeSend: function(){
							},
							success: function(answer){
							$('#name').val('');
							$('#tele').val('');
							$('#mail').val('');
							$('#also').val('');
							$('#submitpl').animate({ 
								backgroundColor: "#7AF73B"
								}, 1500 );
							$('#submitpl').animate({ 
								backgroundColor: "#FFF"
								}, 1500 );
							$('#submitpl').val('Сообщение отправлено');
								var one = rand(1, 30);
								var two = rand(1, 30);
								var cap = one + ' + ' + two + ' = ';
								var ndata = {'res':one + two};
								$('#result').html(cap);
								$.ajax({
									type: "POST",
									data: ndata,
									url: "/metas/plugin/sendmail/php/connector/captcha.php",
									beforeSend: function(){
									},
									success: function(answer){
									$('#key').html(answer);
									$('#ans_res').val('');
									}
							});
							}
						});
					} else
					{
						if (name == '')
							{
							$('#name').animate({ 
								backgroundColor: "#fc7e7e"
								}, 1500 );
							$('#name').animate({ 
								backgroundColor: "#FFF"
								}, 1500 );
							}
						if (tele == '')
							{
							$('#tele').animate({ 
								backgroundColor: "#fc7e7e"
								}, 1500 );
							$('#tele').animate({ 
								backgroundColor: "#FFF"
								}, 1500 );
							}

						if (key != answer)
							{
							$('#ans_res').animate({ 
								backgroundColor: "#fc7e7e"
								}, 1500 );
							$('#ans_res').animate({ 
								backgroundColor: "#FFF"
								}, 1500 );
							}


					}
				}
		});	return false;
	});

	
});
