/*
**  
*/
function FeedBack(handler_url, button_id){
	this.handler_url=handler_url;
	this.button_id=button_id;
	this.stateVisible = false;
	this.init();
}
FeedBack.prototype={
	init:function(){
		var o_this=this;
		this.noactionForm();
		$('#'+this.button_id).click(function(){o_this.send();});
		$('#fb-name').focus(function(){o_this.actionForm();});
		$('#fb-email').focus(function(){o_this.actionForm();});
		$('#fb-phone').focus(function(){o_this.actionForm();});
		$('#fb-category').focus(function(){o_this.actionForm();});
		$('#fb-message').focus(function(){o_this.actionForm();});
		$('#'+this.button_id).focus(function(){o_this.actionForm();});
		$('#question_a').click(function(){o_this.showHideForm();});
	},
	showHideForm:function(){
		this.stateVisible = !this.stateVisible;
		if ($('#fb').css('display')=='none') this.stateVisible = true;
		if(this.stateVisible){
			$('#fb').css('display', 'none');	
			$('#fb').css({height: "0px"}).animate({
						height: "320px"}, 'slow');
			$('#fb1').css('display', 'none');
		}
		else{
			//$('#fb').css({height: "310px"}).animate({height: "0px"}, 'slow', function(){$('#fb').css('display', 'none')});
			$('#fb').css('display', 'none');
		}
	},
	checkFields:function(){
		var name=$('#fb-name').val();
		var emailphone=$('#fb-email').val();
		var category=$('#fb-category option:selected').attr('value');
		var message=$('#fb-message').val();
		if(!name || !emailphone || category=='0' || !message){
			this.showError('1');	
			return false;
		}
		return true;
	},
	send:function(){
		$("#"+this.button_id).attr("disabled","disabled");
		if(!this.checkFields()){
			$("#"+this.button_id).removeAttr("disabled");
			return;
		}
		this.hideError();
		this.showLoader();
		var param = new Object();
		param.name=$('#fb-name').val();
		param.email=$('#fb-email').val();
		param.category=$('#fb-category option:selected').attr('value');
		param.phone=$('#fb-phone').val();
		param.message=$('#fb-message').val();
		
		var o_this=this;
		$.post(this.handler_url, param, function(xml){o_this.onLoadSend(xml);}, 'xml');
	},
	onLoadSend:function(xml){
		this.hideLoader();
		var type=$('type', xml);
		if($(type).text()=='error'){
			this.showError($(type).attr('value'));
		}
		else if($(type).text()=='access'){
			this.showError('0');
			this.clearFields();
		}
		$("#"+this.button_id).removeAttr("disabled");
	},
	hideError:function(){
    	$('#fb-error').hide();
    	$('#fb-msg').hide();
	},
	showError:function(code){
	    this.hideError();
	    switch (code) {
	        case '1':
	            $('#fb-error').html('Заполните обязательные поля').slideDown();
	            break;
	        case '2':
	            $('#fb-error').html('Введите e-mail правильного формата').slideDown();
	            break;
	        case '3':
	            $('#fb-error').html('Вы ввели несуществующий e-mail').slideDown();
	            break;
	        case '0':
	            $('#fb-msg').html('Спасибо! Ваш вопрос отправлен и будет рассмотрен в ближайшее время').slideDown();
	            break;
			default:
	            $('#fb-error').html('Не удалось распознать тип ошибки').slideDown();
	            break;
	    }
	},
	hideLoader:function(){
    	$('#fb-loader').hide();
	},
	showLoader:function(){
    	$('#fb-loader').show();
	},
	clearFields:function(){
		$('#fb-name').val('');
		$('#fb-email').val('');
		$('#fb-phone').val('');
		$('#fb-category option:selected').removeAttr('selected');
		$('#fb-message').val('');
	},
	actionForm:function(){
		//$('#fb').fadeTo("slow", 1.00);
	},
	noactionForm:function(){
		//$('#fb').fadeTo("slow", 0.40);
	}
};