$(function(){
  //todo: Проверка формы купона. Надо определиться как она будет выглядеть для пользователя.

  $('#js-couponToggleLink').click(function(e){
    e.preventDefault();

    $(e.target).toggleClass('hidecoup').toggleClass('showcoup');
    $('#coup1').toggleClass('hideText');
  });
  
  var $couponInputs = $('.js-markTextInput');
  var inputsStartValues = {
  	f_name: 'Ваша фамилия',
  	s_name: 'Имя',
  	l_name: 'И ещё отчество, пожалуйста',
  	auto: 'Какой у вас автомобиль?'
  };
  
  var isFormCorrect = function(){
  	var isCorrect = true;
  	
  	$couponInputs.each(function(){
  		if(
  			jQuery.trim($(this).val()) == '' ||
  			$(this).val() == inputsStartValues[$(this).attr('name')]
  		){
  			isCorrect = false;
  		}
  	});
  	
  	return isCorrect;
  }
  
  setInterval(function(){
  	if(isFormCorrect()){
  		$('#getCoup').addClass('linkReady');
  		$('#js-errorBox').addClass('hideText');
  	} else {
  		$('#getCoup').removeClass('linkReady');
  	}
  }, 200);
  
  $couponInputs.each(function(){
  	if(jQuery.trim($(this).val()) == ''){
  		$(this).val(inputsStartValues[$(this).attr('name')]);
  	} else if($(this).val() != inputsStartValues[$(this).attr('name')]){
  		$(this).addClass('fill');
  	}
  });
  
  $couponInputs.bind('blur', function(){
  	if(!jQuery.trim($(this).val())){
  		$(this)
  			.val(inputsStartValues[$(this).attr('name')])
  			.removeClass('fill');
  		
  	}
  });
  
   $couponInputs.bind('focus', function(){
  	if($(this).val() == inputsStartValues[$(this).attr('name')]){
  		$(this)
  			.val('')
  			.addClass('fill');
  	}
  });
  
  $('#formCoupon').submit(function(e){
  	if(isFormCorrect()){
  		return true;
  	} else {
  		$('#js-errorBox').removeClass('hideText');
  		return false;
  	}
  });
});




