function onlyDigit(input) {  
   input.value = input.value.replace(/[^\d]/g, '');
};   
function getRandomInt(min, max) {
	return Math.floor(Math.random() * (max - min + 1)) + min;
}

function showDialog(title, message) {
	$("#lastDialog").html(message);
	$('#lastDialog').dialog({
   	zIndex: 1010,
		modal: true,
      resizable: false,
		title: title,
      buttons: {
      	"Закрыть": function() {
				document.getElementById('con_request').reset();
		   	$(this).dialog("close");
      	}
		}
	});
}

function showResponseAction(resp) {
	$('#loader').hide();
   if (resp.respStatus == "true") {
      showDialog(resp.respTitle, resp.respText);
   } else {
      showDialog(resp.respTitle, resp.respText);
      res = false;
   }
}

$(document).ready(function() {
	var bonmax = $('.bonus  > li').length;
	var ran_el = getRandomInt(0, bonmax - 1);
	var error = '';		

   $('.left_list > li').click(function() {
      if ($(this).hasClass('active')) {
         return false;
      } else {
      	$('.left_list  > li').removeClass('active'); 
			$(this).addClass('active');
			$('.page-txt').html($(this).find('div').html());
			if ($(this).parent().hasClass('dop')) {
				$('.left_list  > li').find('p:first').hide();
				$(this).find('p').show();
			}

			if (typeof($(this).attr('id')) != "undefined") {
				$('.rate').hide();
				$('.' + $(this).attr('id')).show();
			} else {
				$('.rate').hide();
			}
      }
		var bonmax = $('.bonus  > li').length;
		var ran_el = getRandomInt(0, bonmax - 1);
		$('.bonus  > li').removeClass('active').find('p').hide();
		$('.bonus  > li:eq('+ ran_el + ')').addClass('active').find('p').show();
      return false;
	});

   $('.bonus  > li:eq('+ ran_el + ')').addClass('active').find('p').show();
   $('.bonus  > li').click(function() {
      if ($(this).hasClass('active')) {
         return false;
      } else {
      	$('.bonus  > li').removeClass('active').find('p').hide(); 
			$(this).addClass('active').find('p').show();  
      }
      return false;
   });

   $('.navigate').click(function() {
      if ($(this).hasClass('active')) {
         return false;
      } else {
      	$('.navigate').removeClass('active');
			$(this).addClass('active');
			$('.tabs-content').hide();
			$('.sidebar > .module-left').hide();
			$('.sidebar > .module-right').hide();
			$('.tabs-body-in2 > .tabs-content').hide();
			$('.' + 	$(this).find('a').attr('id')).show();
      }
      return false;
   });

	if ($('#con_request').length > 0) {
		$('#name, #phone, #email').live('keydown', function() {
			if ($(this). val() == '') {
				$(this).addClass('error');
			} else {
				$(this).removeClass('error');
			}
		})

		$('#con_request_b').live('click', function() {
		   error = '';
			if ($('#name').val() == '') {
				error = error + ' - Контактное лицо <br />';
				$('#name').addClass('error');
				$('#name').focus();
			}
			if ($('#phone').val() == '') {
				error = error + ' - Телефон для связи <br />';
				$('#phone').addClass('error');
				$('#phone').focus();
			}
			if ($('#email').val() == '') {
				error = error + ' - Электронная почта <br />';
				$('#email').addClass('error');
				$('#email').focus();
			}
			if (error == '') {
				$('#loader').show();
	         $.ajax({
   	         url: 'engine.php',
      	      type: "POST",
         	   dataType : "json",
            	data: ({
	               company : $("#company").val(),
   	            name : $("#name").val(),
      	         phone : $("#phone").val(),
         	      email : $("#email").val(),
            	   comment : $("#comment").val()
	            }),
   	         success: showResponseAction
      	   });
			} else {
				showDialog("Заполните обязательные поля:", error);
			}
			return false;
		})		
	}

})
