// Variables globales
TITREERREUR = "Une erreur est survenue";

function postContact(adress) {

	if (($("#contactFormRappel").val() === 1)
			&& ($("#contactFormPhone").val() == "")) {
		alert("Merci de préciser un numéro de téléphone pour pouvoir être recontacté par téléphone.");
	} else {

		sentData = preparePost("send");

		$.post(adress, {
			formulaire : sentData
		}, function(data) {
			manageCallback(data);
		});
	}

}

function preparePost(action) {
	contactFields = new Array("contactFormNom", "contactFormPrenom",
			"contactFormSociete", "contactFormAdresse", "contactFormZipcode",
			"contactFormVille", "contactFormEmail", "contactFormPhone",
			"contactFormMessage", "contactFormRappel", "contactFormNewsletter",
			"monCaptcha-input", "monCaptcha-id");

	contactFieldsLength = contactFields.length;

	// Creation des donnees AJAX
	if (action === "send") {
		monReturn = "";

		for (i = 0; i < contactFieldsLength; i++) {

			switch (contactFields[i]) {
			case 'contactFormRappel':
			case 'contactFormNewsletter':
				monReturn += contactFields[i] + "SEPARE_VALEURS"
						+ $("#" + contactFields[i] + ":checked").val()
						+ "SEPARE_CHAMPS";
				break;

			default:
				monReturn += contactFields[i] + "SEPARE_VALEURS"
						+ $("#" + contactFields[i]).val() + "SEPARE_CHAMPS";
				break;
			}
		}

		return monReturn;
	}
	// Remise a zero des champs de saisie
	else {
		clearForm($("#contactForm"));
	}
}

function clearForm(form) {
	// iterate over all of the inputs for the form
	// element that was passed in
	$(':input', form).each(function() {
		var type = this.type;
		var tag = this.tagName.toLowerCase(); // normalize case
			// it's ok to reset the value attr of text inputs,
			// password inputs, and textareas
			if (type == 'text' || type == 'password' || tag == 'textarea')
				this.value = "";
			// checkboxes and radios need to have their checked state cleared
			// but should *not* have their 'value' changed
			else if (type == 'checkbox' || type == 'radio')
				this.checked = false;
			// select elements need to have their 'selectedIndex' property set
			// to -1
			// (this works for both single and multiple select elements)
			else if (tag == 'select')
				this.selectedIndex = -1;
		});
	toggleError('');
};

function manageCallback(data) {
	var emptyCallback = "L'intégrité des données n'a pas pu être vérifiée. Veuillez recommencer. Si le problème persiste, contactez votre support.";

	if (data != "") {
		if (data.search(/ERROR_DETECTED/) != -1) {
			data = data.split('ERROR_DETECTED');
			toggleError(data[0]);
		} else if (data.search(/SUCCESS_DETECTED/) != -1) {
			data = data.split('SUCCESS_DETECTED');
			clearForm($("#contactForm"));
			$("#osx-modal-data-osx1").html(data[0]);
		} else if (data.search(/CAPTCHA_ERROR/) != -1) {
			data = data.split('CAPTCHA_ERROR');
			$("#divCaptcha").replaceWith(data[0]);
		} else {
			alert(data);
		}
	}
	// Data = vide, une erreur s est produite
	else {
		alert(emptyCallback);
	}
}

function toggleError(chaine) {
	if (chaine != '') {
		$("#errorForm").html(chaine);
		$("#introForm").hide();
		$("#errorForm").fadeIn();
	} else {
		$("#errorForm").hide();
		$("#introForm").fadeIn();
	}
}

function addContactForm(adress) {
	$.get(adress, {}, function(data) {
		$('<div id="ajaxForm"></div>').appendTo($('#ajaxFormContainer'));
		$('#ajaxForm').html(data);
	});
}
