function ObjectBlocImmoContactForm(form_element, element_id)
{
	this.form = $(form_element);
	this.elementId = element_id;
	
	this.mandatories = new Array();
	
	this.errorPanel = null;
	
	this.init = function()
	{		
		this.populateMandatories();
		this.bindSubmit();
		Form.focusFirstElement(this.form);
	}
	
	this.populateMandatories = function()
	{
		var ref = this;
		this.form.getElementsByClassName('mandatory').each(function(elem)
		{
			ref.mandatories.push(elem);
		});
	}
	
	
	this.bindSubmit = function()
	{
		Event.observe(this.form, 'submit', this.onSubmit.bindAsEventListener(this));
	}
	
	this.onSubmit = function(event)
	{
		Event.stop(event);
		if(this.checkUserInput())
		{
			var params = Form.serialize(this.form, true);
			
			new Ajax.Request('launchAjaxScript.php?script_name=ObjectBlocImmoContactFormProcessDatas',
			{
				parameters:params,
				onSuccess:this.onDatasProcessed.bind(this)
			})
		}
		
		return false;
	}
	
	this.onDatasProcessed = function()
	{
		this.form.hide();
		var div = $(document.createElement('div')).addClassName('success');
		div.innerHTML = 'Votre demande a bien été enregistrée, nous vous contacterons dans les plus brefs délais !';
		this.form.up().insertBefore(div, this.form);
	}
	
	this.checkUserInput = function()
	{
		var ref = this;
		var error = false;
	
		this.mandatories.each(function(elem)
		{			
			if(!ref.checkMandatory(elem))
			{
				elem.addClassName('error');
				error = true;
			}
			else
				elem.removeClassName('error');
		})
		if(error)
		{			
			this.showErrorPanel();
			this.errorPanel.scrollTo();
		}
		else
			this.hideErrorPanel();
		
		return !error;
	}
	
	this.showErrorPanel = function()
	{
		if(this.errorPanel)
		{
			this.errorPanel.show();
			return;
		}
		
		this.errorPanel = $(document.createElement('div'));
		this.errorPanel.innerHTML = 'Attention, les champs marqués en rouge n\'ont pas été correctement saisis !';
		this.errorPanel.addClassName('error_panel');		
		this.form.insertBefore(this.errorPanel, this.form.down());
	}
	
	this.hideErrorPanel = function()
	{
		if(this.errorPanel)
			this.errorPanel.hide();
	}
	
	this.checkMandatory = function(element)
	{
		var input;
		var textarea;
		if(element.tagName != 'INPUT' && element.tagName != 'TEXTAREA')
		{
			input = element.down('input');			
			if(!input)
			{
				var textarea = element.down('textarea');				
			}
		}	
		else
			input = element;	
		
		if(input)
		{
			return input.value.length ? true:false;
		}		
		else if(textarea)
		{
			return textarea.value.length ? true:false;
		}
		else
			return true; //Nothing to check
	}
	
	this.init();
} 