// Identique fonction trim() PHP
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

/* ************************************************ */
/* Fonctions génériques de validation de formulaire */
/* ************************************************ */

// Verifie la validite d'un email
function valide_mail_generique ( nameForm, champ ) {
	
	var email = document.forms[nameForm].elements[champ];
		
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if ( !filter.test(email.value) ) {
		
	 	alert(get_trad_champ(champ));
		email.select();
		return false;
	}
	else {
		return true;
	}
}

// Verifie les champs obligatoire d'un formulaire
valider_formulaire_generique = function () {
	var tab = valider_formulaire_generique.arguments;

	taille = tab.length;

	if ( taille > 0 ) {

		nameForm = tab[0];
		
		for ( i = 1; i < taille; i++ ) {
			
			if ( tab[i] == "email" || tab[i] == "votre_email" || tab[i] == "ami_email" ) {
				if ( !valide_mail_generique( nameForm, tab[i] ) ) {
					return false; 
				}
			}
			else {
				if ( ( document.forms[nameForm].elements[tab[i]].value.trim() == "" ) ) {				
					alert ( get_trad_champ( tab[i] ) );				
					document.forms[nameForm].elements[tab[i]].focus();
					return false; 
				}
			}
		}
		
		return true;
		
	}
	else {
		return false;
	}
}

/* ************************************************** */
/* / Fonctions génériques de validation de formulaire */
/* ************************************************** */



/* ***************** */
/* Fonctions locales */
/* ***************** */

// Validation du moteur de recherche
function validerSearchCamping() {	
	if ( valider_formulaire_generique('formSearchCamping', 'destination' ) ) {				
		document.forms['formSearchCamping'].submit();					
	}
	else {
		return false;
	}		
}

// MAJ des listes Regions dans le formulaire de contact
function getRegionFromDestination(){	
	$.ajax({
		type: "POST",
		data: "destination_souhaitee="+document.getElementById('destination_souhaitee').value,
		async: true,
		url:  "ajax/ajax_maj_region.php",		
		success: function(data){			
			var tab = data.split("|");	
			var err = 0;
			
			document.getElementById('div_site').style.display = "none";	
			// On fait le travail ici
			if ( document.getElementById('destination_souhaitee').value.trim() != "" ) {
				document.getElementById('div_region').style.display = "block";
				document.getElementById('div_region').innerHTML = tab[1];
			}
			else {
				document.getElementById('div_region').style.display = "none";				
			}
			
		} });
	
}

// MAJ des listes Campings dans le formulaire de contact
function getSiteFromRegion(){	
	$.ajax({
		type: "POST",
		data: "destination_souhaitee="+document.getElementById('destination_souhaitee').value+"&region_souhaitee="+document.getElementById('region_souhaitee').value,
		async: true,
		url:  "ajax/ajax_maj_site.php",		
		success: function(data){			
			var tab = data.split("|");	
			var err = 0;			
			if ( document.getElementById('region_souhaitee').value.trim() != "" ) {
				document.getElementById('div_site').style.display = "block";
				document.getElementById('div_site').innerHTML = tab[1];
			}
			else {
				document.getElementById('div_site').style.display = "none";	
			}
			
		} });
	
}

// Formulaire de contact 
function validCaptchaContact(){	
	$.ajax({
		type: "POST",
		data: "captcha="+document.getElementById('strcode').value,
		async: true,
		url:  "ajax/ajax_verif_captcha.php",		
		success: function(data){
			
			var tab = data.split("|");	
			var err = 0;
			
			if(tab[1] == "OK"){								
				validFormContact();
			}else{
				alert(get_trad_champ('bad_captcha'));		
				document.forms['formContact'].elements['strcode'].select();		
				return false;
			}
		} });
	
}

function validFormContact() {
	if ( valider_formulaire_generique('formContact', 'nom',
																		'prenom', 'telephone', 'email', 'comment_connu',  'message' ) ) {					
		document.forms['formContact'].submit();		
	}																		
	else {
		return false;
	}
}

// Inscription Newsletter
function validformNewsletter() {
	if ( valider_formulaire_generique('formNewsletter', 'email' ) ) {					
		document.forms['formNewsletter'].submit();		
	}																		
	else {
		return false;
	}
}


// Recherche geo
function gotoRegion() {
	window.location.href = document.getElementById('directRegion').value;
}


// Espace Envoi à un ami
function validAjaxFormAmi(){	
	$.ajax({
		type: "POST",
		data: "urlAmi="+document.getElementById('urlAmi').value+"&votre_nom="+document.getElementById('votre_nom').value+"&votre_email="+document.getElementById('votre_email').value+"&ami_nom="+document.getElementById('ami_nom').value+"&ami_email="+document.getElementById('ami_email').value,
		async: true,
		url:  "ajax/ajax_recommander_ami.php",		
		success: function(data){
			
			var tab = data.split("|");	
			var err = 0;
			
			document.getElementById('recommanderAmiPopin').innerHTML = tab[1];
			
		} });
	
}

function validFormAmi() {
	if ( valider_formulaire_generique('formAmi', 'votre_nom', 'votre_email', 'ami_nom', 'ami_email' ) ) {					
		validAjaxFormAmi();		
	}																		
	else {
		return false;
	}
}

function validAjaxFormAmiSearch(idCamping){	
	$.ajax({
		type: "POST",
		data: "urlAmi="+document.forms['formAmi'+idCamping].elements['urlAmi'].value+"&votre_nom="+document.forms['formAmi'+idCamping].elements['votre_nom'].value+"&votre_email="+document.forms['formAmi'+idCamping].elements['votre_email'].value+"&ami_nom="+document.forms['formAmi'+idCamping].elements['ami_nom'].value+"&ami_email="+document.forms['formAmi'+idCamping].elements['ami_email'].value,
		async: true,
		url:  "ajax/ajax_recommander_ami.php",		
		success: function(data){
			
			var tab = data.split("|");	
			var err = 0;
			
			document.getElementById('recommanderAmiPopinSearch'+idCamping).innerHTML = tab[1];
			
		} });
	
}

function validFormAmiSearch(idCamping) {
	if ( valider_formulaire_generique('formAmi'+idCamping, 'votre_nom', 'votre_email', 'ami_nom', 'ami_email' ) ) {					
		validAjaxFormAmiSearch(idCamping);		
	}																		
	else {
		return false;
	}
}


// Espace Mon projet
function getDivSelectProjetStep1(idDiv) {
	
	// On efface les trois
	document.getElementById('des').style.display = "none";
	document.getElementById('reg').style.display = "none";
	document.getElementById('vil').style.display = "none";
	
	if ( idDiv != '' ) {
		document.getElementById(idDiv).style.display = "block";
	}
	
}

function validFormProjetStep1() {		
	document.forms['formProjetStep1'].submit();			
}

function validFormProjetStep2() {		
	document.forms['formProjetStep2'].submit();			
}

function validFormProjetStep3() {		
	document.forms['formProjetStep3'].submit();			
}

function validFormProjetStep4() {
	if ( valider_formulaire_generique('formProjetStep4', 'nom', 'prenom', 'telephone', 'email', 'projet' ) ) {					
		document.forms['formProjetStep4'].submit();		
	}																		
	else {
		return false;
	}
}

/* ******************* */
/* / Fonctions locales */
/* ******************* */
