/*
**  
*/
function DealerDemand(handler_url, button_id){
	this.handler_url=handler_url;
	this.button_id=button_id;
	this.stateVisible = false;
	this.init();
}
DealerDemand.prototype={
	init:function(){
		var o_this=this;
		$('#'+this.button_id).click(function(){o_this.send();});
	},
	showHideForm:function(){
		this.stateVisible = !this.stateVisible;
		if(this.stateVisible){
			$('#s').css('display', 'none');	
			$('#s').css({height: "0px"}).animate({
						height: "320px"}, 'slow');
		}
		else{
			$('#s').css({height: "310px"}).animate({
						height: "0px"}, 'slow', function(){$('#s').css('display', 'none')});
		}
	},
	checkFields:function(){
		var company			=$('#s-company').val();
		var city			=$('#s-city').val();
		var contactface		=$('#s-contactface').val();
		var contacts		=$('#s-contacts').val();
		var message			=$('#s-message').val();
		
		if(!company || !city || !contactface || !contacts){
			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.company		=$('#s-company').val();
		param.city			=$('#s-city').val();
		param.contactface	=$('#s-contactface').val();
		param.contacts		=$('#s-contacts').val();
		param.message		=$('#s-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(){
    	$('#s-error').hide();
    	$('#s-msg').hide();
	},
	showError:function(code){
	    this.hideError();
	    switch (code) {
	        case '1':
	            $('#s-error').html('Заполните обязательные поля').slideDown();
	            break;
	        case '0':
	            $('#s-msg').html('Спасибо! Ваша заявка отправлена и будет рассмотрена в ближайшее время').slideDown();
	            break;
			default:
	            $('#s-error').html('Не удалось распознать тип ошибки при отправке заявки').slideDown();
	            break;
	    }
	},
	hideLoader:function(){
    	$('#s-loader').hide();
	},
	showLoader:function(){
    	$('#s-loader').show();
	},
	clearFields:function(){
		$('#s-company').val('');
		$('#s-city').val('');
		$('#s-contactface').val('');
		$('#s-contacts').val('');
		$('#s-message').val('');
	}
};