/**
	Система публичной модурации объявлений
	скрипты для контрола формы
*/
var moderate = {
	ready : true,
    first_show : true,
	incorrect_id: 0,
	maxlength: 200,
	temp_comment: "",
	
	refreshCaptha: function() {
		var captcha_img = $('.incorrect_container .captcha_code_img');
		var captcha_code = $('.incorrect_container .captcha_code');
		if ( captcha_img !== null && captcha_code !== null )
		{
			captcha_code.val('');
			captcha_img.attr("src", "/service/captcha/?r="+Math.random());
		}
	},

	hideForm: function() {
		$('#incorrect_form .incorrect_reason option:first').attr('selected', 'yes');
		$('#incorrect_container_'+this.incorrect_id).slideUp('fast');
		this.incorrect_id = 0;
	},
	
	showForm: function(id) {		
		if ( !this.ready )
		{
			alert("В данный момент происходит обработка, попробуйте позднее");
			return false;
		}
		
		this.hideForm();
		
		if ( this.incorrect_id == id || $('#incorrect_container_'+id).css('display') != 'none' ) {
			return;
		}
		
		this.incorrect_id = id;
		
		var el = $('#incorrect_container_'+this.incorrect_id);		
		
		el.append($('#incorrect_form'));
		$('#incorrect_form').show();
        if ( !this.first_show )
		    this.refreshCaptha();

        this.first_show = false;

		$('#incorrect_container_'+this.incorrect_id).slideDown('fast');
		return false;
	},
	
	incorrectAdv: function()
	{	
		if (!this.ready) {
			alert("В данный момент происходит обработка, попробуйте позднее");
			return false;
		}
		
		this.ready = false;
		$('.incorrect_container .send_incorrect').attr('disabled', 'true');
		var self = this;
		$.ajax({				
			type: 'POST',
			dataType: 'json',
			data: {
				action: 'incorrect',
				reason: $('.incorrect_container .incorrect_reason').val(),
				id: this.incorrect_id,
				comment: $('.incorrect_container .incorrect_comments').val(),
				captcha_code: $('.incorrect_container .captcha_code').val()
			},
			success: function(data) {
				self.ready = true;
				$('.incorrect_container .send_incorrect').removeAttr('disabled');
				if ( data.status === 0 )
				{					
					alert(data.success);
					self.hideForm();
					$('#incorrect_form .incorrect_reason option:first').attr('selected', 'yes');
				}
				else
				{
					alert(data.error);
					self.refreshCaptha();
				}
			},
			error: function() {
				$('.incorrect_container .send_incorrect').removeAttr('disabled');
				alert("Ошибка");
				self.ready = true;
				self.refreshCaptha();
			}
		});
	},
	
	onchange_incorrect_reason: function()
	{		
		var incorrect_reason = $('.incorrect_container .incorrect_reason');
		if ( incorrect_reason.val() == -1 ) {
			$('.incorrect_container .incorrect_comments').show();
		} else {
			$('.incorrect_container .incorrect_comments').hide();
		}
	}
};

