
/* -----------------------------------------------------------------------------
	Soumission du formulaire
------------------------------------------------------------------------------*/

	// Change le status d'un champ
	function switchStatus(selecteur, status){
		if (status == 'n') {
			$(selecteur).parent('p').removeClass('good').addClass('error');
		}
		else
		{
			$(selecteur).parent('p').removeClass('error').addClass('good');
		}
	}

	function countErrors(selector)
	{
		var nbErrors = $(selector).find('p.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');}
		});

		$thisForm.find('input.obligatoire_checkboxes:visible').each(function(index) {
			var thisRel = $(this).attr('rel');
			var isChecked  = $('input[rel='+thisRel+']:checked').length;
			if (isChecked == 0) {switchStatus(this, 'n');}
			else {switchStatus(this, 'o');}
		});

		// 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 ?
		if (countErrors($thisForm) == 0) {
			// 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();
						$thisForm.remove('.erreur');		// modif pour sophia (pas de modale)
					}
					if (data.redir == '1') {
						location.href = data.redirUrl;
					}
					else if (data.replace == '1') {
						$(data.replace_selector).html(data.replace_content);
					}
					else
					{
						var tmp = $('<p class="erreur">'+data.html.content+'</p>');	// modif pour sophia (pas de modale)
						$thisForm.append( tmp );
//						Zee.Modal.create(data.html.titre,'<p>'+data.html.content+'</p>');
					}
				}, "json");
			}
			// Submit
			else
			{
				$thisForm.find('input[type=submit]').css('opacity','0.5').unbind('click');
				$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
------------------------------------------------------------------------------*/

	// 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');
		}
	});

	$('form.standardForm select.obligatoire_ddmm:visible').live('keyup blur change', function() {
		var thisParent = $(this).parent('p');

		var thisDD = thisParent.children('select[name*=dd]').val();
		var thisMM = thisParent.children('select[name*=mm]').val();
		if (!isDD(thisDD) || !isMM(thisMM)) {
			switchStatus(thisParent.children('select:first'), 'n');
		}
		else
		{
			switchStatus(thisParent.children('select:first'), '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');
		}
	});

	// Checkboxes obligatoire
	$('form.standardForm input.obligatoire_checkboxes:visible').live('click', function() {
		var thisRel = $(this).attr('rel');
		var isChecked  = $('input[rel='+thisRel+']:checked').length;
		if (isChecked == 0) {switchStatus(this, 'n');}
		else {switchStatus(this, 'o');}
	});

	// 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() {
		var thisVal = $(this).attr('value');
		if (isNaN(thisVal) || thisVal > 31 || thisVal.length != 2) {
			switchStatus(this, 'n');
		}
	});


