
function toggle(el, height){
	el.setStyle('overflow', 'hidden');
	el.setStyle('zoom', '1');
	if(height){
		el.removeClass('hide');
		el.tween('height', height);
	} else {
		el.tween('height', height);
		hideCompletely.delay(500, el);
	}
}

function hideCompletely(){
	this.addClass('hide');
}

var initFormEvents = function (pIdentifiantFormulaire) {
	if ($('devis'+pIdentifiantFormulaire)) {
		$('devis'+pIdentifiantFormulaire).addEvent('click', function(){
			$('btn'+pIdentifiantFormulaire).value = 'devis';
		})
	}
	if ($('rappel'+pIdentifiantFormulaire)) {
		$('rappel'+pIdentifiantFormulaire).addEvent('click', function(){
			$('btn'+pIdentifiantFormulaire).value = 'rappel';
		})
	}
	if ($('geoloc'+pIdentifiantFormulaire)) {
		$('geoloc'+pIdentifiantFormulaire).addEvent('click', function(){
			$('btn'+pIdentifiantFormulaire).value = 'geoloc';
		})
	}
	if ($('formTarification'+pIdentifiantFormulaire)) {
		$('formTarification'+pIdentifiantFormulaire).addEvent('submit', function (e) {
			// pour connaitre sur quel bouton l'internaute a cliqué
			//$('btn'+pIdentifiantFormulaire).value = (e.event.explicitOriginalTarget||document.activeElement).name;
			new Event(e).stop();
			new Request({
				url: $('formTarification'+pIdentifiantFormulaire).get('action'),
				data: $('formTarification'+pIdentifiantFormulaire),
				onRequest: function () {
					$('error').set('html', '');
				},
				onSuccess: function(txt){
					var oJson = JSON.decode(txt);
					// il y a des erreurs
					if(oJson.error) {
						var sError = '<ul>';
						for(var key in oJson.error) {
							sError += '<li>'+eval('oJson.error.'+key)+'</li>';
						}
						sError += '</ul>';
						$('error').set('html', sError);
						TB_show ('', '#TB_inline?height=480&width=552&inlineId=popup_error', false);
					}
					// il faut envoyer le formulaire à CMA
					else {
						if (oJson.urlDelegationPopup) {
							TB_show ('', oJson.urlDelegationPopup, false);
							setTimeout(function(){
								new Request({
									url: oJson.urlDelegationRequest,
									onSuccess: function(txt){
										var oJson = JSON.decode(txt);
										
										if(oJson.error) {
											var sError = '<ul>';
											sError += '<li>'+oJson.error+'</li>';
											sError += '</ul>';
											$('error').set('html', sError);
											TB_show ('', '#TB_inline?height=480&width=552&inlineId=popup_error', false);
										}else{
											if(oJson.url){
												TB_show ('', oJson.url, false);				
											}
										}
									},
									onFailure: function(xhr){
										console.log(xhr);
									}
								}).send();
								
							}, 2000);
							
						}
						else {
							if (oJson.urlPopup) {
								window.location.href = oJson.urlPopup;
							}
						}
					}
				},
				onFailure: function(xhr){
					/*alert('Une erreur s\'est produite lors de l\'envoi du formulaire');
					console.log(xhr);*/
				}
			}).send();
			return false;
		});
	}
}
