

/* -----------------------------------------------------------------------------
	Soumission du formulaire
------------------------------------------------------------------------------*/

	// Change le status d'un champ
	function switchStatus(selecteur, status){
		if (status == 'n') {
			$(selecteur).parent('p').removeClass('good').addClass('error');
			$(selecteur).parent('div.p').removeClass('good').addClass('error').addClass('error-bad');
		}
		else
		{
			$(selecteur).parent('p').removeClass('error').addClass('good');
			$(selecteur).parent('div.p').removeClass('error').removeClass('error-bad').addClass('good');
		}
	}

	function countErrors(selector)
	{
		var nbErrors = $(selector).find('p.error:visible').length + $(selector).find('div.error:visible').length;
		return nbErrors;
	}

	// Validation d'un champ date en trois parties DD MM AAAA
	function isDD(val){
		if (isNaN(val) || val == 0 ||val > 31 || val.length != 2) {
			return false;
		}
		return true;
	}

	function isMM(val){
		if (isNaN(val) || val == 0 || val > 12 || val.length != 2) {
			return false;
		}
		return true;
	}

	function isYYYY(val){
		if (isNaN(val) || val < 1900 || val > 2010 || val.length != 4) {
			return false;
		}
		return true;
	}

	function validDate(selecteur){
		/*
		if ($(selecteur).children('input[name=dd]').val().length == 1) {
			$(selecteur).children('input[name=dd]').val('0'+$(selecteur).children('input[name=dd]').val());
		}
		if ($(selecteur).children('input[name=mm]').val().length == 1) {
			$(selecteur).children('input[name=mm]').val('0'+$(selecteur).children('input[name=mm]').val());
		}
		*/
		if (
			isDD($(selecteur).children('input[name=dd]').val())
			&& isMM($(selecteur).children('input[name=mm]').val())
			&& isYYYY($(selecteur).children('input[name=yyyy]').val())
			)
		{
			switchStatus(selecteur.children(), 'o');
		}
		else
		{
			switchStatus(selecteur.children(), 'n');
		}
	}

	function submitForm($thisForm){
		// Champs et Select
		$thisForm.find('input:visible, textarea:visible, select:visible').each(function(index) {
				$(this).focus().blur();
		});
		// Checkboxes
		$thisForm.find('input.obligatoire_checkbox:visible').each(function(index) {
			var thisName = $(this).attr('name');
			var isChecked  = $('input[name='+thisName+']:checked').length;
			if (isChecked == 1) {switchStatus(this, 'o');}
			else {switchStatus(this, 'n');}
		});
		// Boutons radio
		$thisForm.find('input.obligatoire_radio:visible').each(function(index) {
			var thisName = $(this).attr('name');
			if ($('input[name='+thisName+']:checked').length==1) {switchStatus(this, 'o');}
			else {switchStatus(this, 'n');}
		});

		// reste-t-il des erreurs ?
		$thisForm.find('div.error').addClass('error-bad');
		if (countErrors($thisForm) == 0) {
			$thisForm.children('input[type=submit]').addClass('activated');
			// ajax submit !
			if ($thisForm.find('input[name=ajaxAction]').length >= 1) {
				$.post("controller/ajaxActions.php", {
					'ajaxAction': $thisForm.find('input[name=ajaxAction]').val(),
					'serialised': $thisForm.serialize()
				},
				function(data){
					if (data.erreur == '0') {
						$thisForm.slideUp();
					}
					if (data.redir == '1') {
						location.href = data.redirUrl;
					}
					else
					{
						Zee.Modal.close();
						window.setTimeout(function() {
							Zee.Modal.create(data.html.titre,data.html.content);
						}, 500);
					}
				}, "json");
			}
			// Submit
			else
			{
				$thisForm.submit();
			}
		}
		else
		{
			$thisForm.find('p.error:first').children('input:first').focus();
		}
	}


	$('A.submitter').live('click', function() {
		submitForm($('form.standardForm:first'));
		return false;
	});

	$('form.standardForm input[type=submit]').live('click', function() {
		submitForm($(this).parents('form'));
		return false;
	});

/* -----------------------------------------------------------------------------
	Types de champs
------------------------------------------------------------------------------*/
	// Code promo / propriétaire
	$('#code-input').live('keyup blur', function() {
		var thisCode = $(this).val();
		var thisPrice = $('p.discount-code').attr('rel');
		if (thisCode.length == 6) {
			$('p.discount-code').removeClass('error');
			$.post("controller/ajaxActions.php", {
				'ajaxAction': 'code_check',
				'price': thisPrice,
				'code': thisCode
			},
			function(data){
				if (data.erreur == '0') {
					$('p.discount-code').addClass('good');
					$('#reduction_val').html('-'+data.remise+ ' €');
					$('#prix_total').html(data.prix_total+' €');
					$('span.statut-code').html('code valide').show();
				}
				else {
					$('p.discount-code').removeClass('good').addClass('error');
					$('#reduction_val').html('--');
					$('#prix_total').html(thisPrice+' €');
					$('span.statut-code').html(data.message).show();
				}
			}, "json");
		}
		else
		{
			$('span.statut-code').html();
			$('p.discount-code').removeClass('good');
			$('#reduction_val').html('--');
			$('#prix_total').html(thisPrice+' €');
		}
	});

/*
	function checkCode(val){
		if (val.length == 6) {
			return true;
		}
		return false;
	}
*/


	// Champ mail
	$('form.standardForm input.obligatoire_mail:visible').live('keyup blur', function() {
		if (!checkEmail($(this).val())) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});

	// Champ obligatoire
	$('form.standardForm input.obligatoire:visible, form.standardForm textarea.obligatoire:visible').live('keyup blur', function() {
		if ($(this).val() == '' || $(this).val() == null) {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});


	$('form.standardForm select.obligatoire:visible').live('keyup blur change', function() {
		var thisVal = $(this).children('option:selected').val();
		if (thisVal == '' || thisVal == null || thisVal == 'Séléctionner') {
			switchStatus(this, 'n');
		}
		else
		{
			switchStatus(this, 'o');
		}
	});

	// Checkbox obligatoire
	$('form.standardForm input.obligatoire_checkbox:visible').live('click', function() {
		var thisName = $(this).attr('name');
		var isChecked  = $('input[name='+thisName+']:checked').length;
		if (isChecked == 1) {
			switchStatus(this, 'o');
		}
		else
		{
			switchStatus(this, 'n');
		}
	});

	// Radio obligatoire
	$('form.standardForm input.obligatoire_radio:visible').live('click', function() {
		var thisName = $(this).attr('name');
		if ($('input[name='+thisName+']:checked').length==1) {
			switchStatus(this, 'o');
		}
		else
		{
			switchStatus(this, 'n');
		}
	});


	// Champ numérique
	$('form.standardForm input.obligatoire_numerique, form.standardForm input.numerique').live('keydown', function(e) {
		var key = e.charCode || e.keyCode || 0;
		var thisVal = $(this).attr('value');
		if (key == 38)
		{
			$(this).attr('value',parseInt(thisVal)+1);
		}
		else if (key == 40)
		{
			var newVal = parseInt(thisVal)-1;
			if (newVal < 1) {newVal = 1;}
			$(this).attr('value',newVal);
		}
		// autorise backspace, tab, delete, fleches, chiffres, pavé numérique, controle
		if (
			key == 8 ||
			key == 9 ||
//			key == 17 ||	// Controle
			key == 46 ||
			(key >= 37 && key <= 40) ||
			(key >= 48 && key <= 57) ||
			(key >= 96 && key <= 105))
		{
			return true;
		}
		return false;
	}).live('keyup blur change', function() {
		if ($(this).val() == '' || $(this).val() == null || isNaN($(this).val())) {
			switchStatus(this, 'n');
		}
		else
		{
			if ($(this).attr('name')=='dd'|| $(this).attr('name')=='mm' || $(this).attr('name')=='yyyy') {
				validDate($(this).parent('p'));
			}
			else if ($(this).attr('id')=='zip' && $(this).val().substring(0,2) == '75') {
				$('#city').val('Paris');
			}
			else if ($(this).attr('id')=='zip' && $(this).val().length==5) {
				$.getJSON("/api/getVilleForCp.js", {cp: $(this).val()}, function(ville){
					if (ville.length == 1 && ville[0].nom)
					{
						$('#city').val(ville[0].nom);
						switchStatus(this, 'o');
					}
				});
			}
			else
			{
				switchStatus(this, 'o');
			}
		}

	});


	$('form.standardForm input.obligatoire_dd').live('change', function() {
		console.log('changed');
		var thisVal = $(this).attr('value');
		if (isNaN(thisVal) || thisVal > 31 || thisVal.length != 2) {
			console.log('bad');
			switchStatus(this, 'n');
		}
	});


/* -----------------------------------------------------------------------------
	Rollover sur un champ qui a une définition
------------------------------------------------------------------------------*/
$('fieldset.has_definition').mouseenter(function() {
	console.log('enter');
	$(this).children('div[class^=definition]').show();
 }).mouseleave(function() {
	console.log('leave');
	$(this).children('div[class^=definition]').hide();
 });

