tmt_globalPatterns.tel = new RegExp("^\\(?(00?(-| )?|\\+?)([0-9]{2,3}\\)?(-| )?((-| )?[0-9]+)+)(-| )?[0-9]{2}$");
var nbImgPromo = 0;
var iShow = 0;
var iShowMoto = 0;


$(document).ready(function() {
	$('#essaiMotoForm').submit(function(){
		if(!tmt_validateForm(this))
		{
			return false;
		}
		$.post(j_basepath+'index.php?module=accueil&action=accueilFo_inscription',
			{zNom : $('#nom').val(),
			zPrenom : $('#prenom').val(),
			zMail : $('#email').val(),
			zTelephone : $('#telephone').val(),
			iMoto : $('#idMoto').val()},function(json){
				json = eval('(' + json + ')');
				$('#messageRetour').text(json.msg);
				$('#messageRetour').css('padding','10px 0 0 35px');
				$('#messageRetour').show();
				$('#nom').val('');
				$('#prenom').val('');
				$('#email').val('');
				$('#telephone').val('');
		});
		return false;
	});

	// affichage d'image sur la formulaire d'inscription
	nbImgMotos = $('#moto_select').children().length;
	showImageMoto(iShowMoto);

	$('#moto_select').change(function(){
		var elSel = $(this).children();
		var h = $(this).val();
		var iImg = '#moto_' + h;
		var optMoto = '#optMoto' + h;
		iShowMoto = $(elSel).index($(optMoto)[0]) ;
		$('#idMoto').val(h);
		showImageMoto(iShowMoto);
	});


	// affichae des images sur la promotion home
	nbImgPromo = $('#selectPromo').children().length;
	elem = $('#selectPromo').children();
	showImage(iShow, elem);

	$('li#liGauche a').click(function(){
		if(iShow != 0)
			iShow--;

		showImage(iShow, elem);
	});

	$('li#liDroite a').click(function(){
		if(iShow < nbImgPromo-1)
			iShow++;
		showImage(iShow, elem);
	});

	$('#selectPromo').change(function(){
		var k = $(this).val();
		var idOpt = '#opt' + k;
		iShow = $(elem).index($(idOpt)[0]);
		showImage(iShow, elem);
	});
});

function showImageMoto(iMoto)
{
	for(k=0; k<nbImgMotos; k++){
		if(iMoto != k)
		{
			next = $('#imgSelectMoto img').get(k);
			$(next).hide();
		}else{
			first = $('#imgSelectMoto img').get(k);
			$(first).show();
		}
	}

}

function showImage(iShow,elem)
{
	for(i=0; i<nbImgPromo; i++)
	{
		if(i != iShow)
		{
			next = $('li#liCentre a img').get(i);
			$(next).hide();
			imgId = $(next).attr('id');
			var iDiv = '#div_' + imgId;
			$(iDiv).hide();
		}else{
			first = $('li#liCentre a img').get(iShow);
			imgId = $(first).attr('id');
			var iDiv = 'div#div_' + imgId;
			$(iDiv).show();
			$(first).show();
			el = $(elem).get(iShow);
			$(el).attr('selected', 'selected');
		}
	}
}
