$(function(){
  //Update subcategorii
  $("#categoriiF").change(function(){
	if($(this).val() != ''){
		$('.isloading').fadeIn();
		$(this).removeClass('eroareF');
		$('#eroareScat').slideUp('slow');
		$.getJSON("ajax.php",{sub_id: $(this).val(), box: 'adauga'}, function(data){
		  var options = '';
		  for (var i = 0; i < data.length; i++) {
			options += '<option value="' + data[i].id + '">' + data[i].nume + '</option>';
		  }
		  $("#subcategoriiF").html(options).removeClass('eroareF');
		  $('#eroareSsub').slideUp('slow');
		  $('.isloading').fadeOut();
		})
	}
  })
  //Removing error class
  $('.form input,.form select,.form textarea').keyup(function(){
		var field_name = $(this).attr('name');
		if($(this).hasClass('eroareF')){
			$(this).removeClass('eroareF'); //remove error from field
			$('#eroareS'+field_name).hide(); //remove error span
		}
  });
  //Cod reciproc
  $('.cod_reciproc').click(function(){
  		$(this).focus().select();								
  });
  //Update caractere ramase
  var charLimit = 300;
  function count(){
	    var text = $('#descriereF').val();
  		var charLength = text.length;
		if(charLength > charLimit){
			$('#descriereF').val(text.substr(0,charLimit));
			return false;	
		} else {
			$('#descriereFspan b').html((charLimit-charLength));
			return true;
		}  
  }
  $('#descriereF').keypress(function(){count();}).keyup(function(){count();}).keydown(function(){count();});
  //Update cod securitate
  $('#codsecuritateFrefresh').click(function(){
		var rand = Math.ceil(100*Math.random());
  		$('#codsecuritateFspan img').attr({"src":"cod-securitate.jpg?rand="+rand});								
  });
})

