// verifie la validite d'un email
function validemail(champ)
{
	
 test = document.formulaire.elements[""+champ+""].value;
 valeur = test.indexOf("@");
 valeur2 = test.lastIndexOf(".");
 valeur3 = valeur2 + 1;
 longueur = test.length;
 ext = test.substring(valeur3,longueur);
 ok=0;
 temp = ext.length;
 if ( temp==2 || temp==3 ) 
    { ok=1; }
 else
    { ok=0; }
 if ( valeur2 == (valeur + 1) )
     { ok=0; }
 if (valeur==0)
     { ok=0; }
 if (valeur==-1 || valeur2==-1 || ok==0)
    { alert("L'email que vous avez rentrée n'est pas valide.\nVeuillez la ressaisir s'il vous plaît.");
      return false; }
 else
    { return true; }   
    
}

function valider_radio_texte() 
{		
		var tab = valider_radio_texte.arguments;
		taille = tab.length;				
		test_all = 0;		
		for (i=0;i<taille;i++) {
				valid = 0;
				for (j=0;j<3;j++) {
						var str_option = "document.formulaire."+tab[i]+"[j].checked";				
						if (eval(str_option)) { 										
									valid = 1;									
						}				
				}
				if ( valid == 0 ) {
						alert("Vous devez choisir une proposition pour le champ : "+tab[i]+" s'il vous plait");				
						return false;
				}				
		}
		if (test_all == 0) {
				return true;
		}
}

function valider_radio() 
{		
		var tab = valider_radio.arguments;
		taille = tab.length;		
		test_all = 0;		
		for (i=0;i<taille;i++) {
				valid = 0;
				for (j=0;j<10;j++) {
						var str_option = "document.formulaire."+tab[i]+"[j].checked";				
						if (eval(str_option)) { 										
									valid = 1;
						}				
				}
				if ( valid == 0 ) {
						alert("Merci de choisir une proposition pour le champ : "+get_trad_lib(tab[i]));				
						test_all = 1;
						return false;
				}				
		}
		if (test_all == 0) {
				return true;
		}
}

function get_trad_lib(lib) 
{
		switch(lib)
		{
				case "general" :
						return("Appréciation générale");
						break;
				case "accueil" :
						return("Accueil du personnel de bord");
						break;
				case "decoration" :
						return("Décoration du navire");
						break;
				case "ambiance" :
						return("Ambiance");
						break;
				case "cadeau_bienvenue" :
						return("Cadeau de bienvenue (chocolats)");
						break;
				case "info_anim" :
						return("Information sur les animations à bord");
						break;
			}
}

// verifie les champs obligatoire d'un formulaire
valider_formulaire = function () {
	var tab = valider_formulaire.arguments;

	taille = tab.length;

	for (i=0;i<taille;i++) {
		if (tab[i]=="email") {
			if (!validemail(tab[i])) {
				return 0; 
			}
		}else{
			if ((document.formulaire.elements[tab[i]].value=="") || (document.formulaire.elements[tab[i]].value==" ")) {
				str_temp = tab[i].replace(/_/g," ");
				alert("Le champ "+str_temp+" est obligatoire.\nVeuillez le ressaisir s'il vous plaît.");
				return 0; 
			}
		}
	}
	
	document.formulaire.submit();
}

function check_date(str_date) { // Vérifie la validité d'une date
		parties = str_date.split("/");
		jour = parseFloat(str_date.substring(0, 2));
		mois = parseFloat(str_date.substring(3, 5));
		annee = parseFloat(str_date.substring(6, 10));

		if ( (jour < 1 || jour > 31) || ( mois < 1 || mois > 12 ) || ( annee < 2006 || annee > 2008 ) || ( str_date == "" ) || ( isNaN(jour) || isNaN(mois) || isNaN(annee) ) || ( jour == "" || mois == "" || annee == "" ) ) {
				alert("Format de date de traversée incorrect");
				return false;
		}
	else {
			return true;
	}
}

// verifie les champs obligatoire d'un formulaire
valider_form_enquete = function () {
	var tab = valider_form_enquete.arguments;

	taille = tab.length;

	for (i=0;i<taille;i++) {
		if (tab[i]=="email") {
			if (!validemail(tab[i])) {
				return 0; 
			}
		}else if (!valider_radio(tab[i])) {
				return 0; 
			}else{
			if ((document.formulaire.elements[tab[i]].value=="") || (document.formulaire.elements[tab[i]].value==" ")) {
				str_temp = tab[i].replace(/_/g," ");
				alert("Le champ "+str_temp+" est obligatoire.\nVeuillez le ressaisir s'il vous plaît.");
				return 0; 
			}
		}
	}
	
	document.formulaire.submit();
}

function empechechar()
{
	if( event.keyCode < 48 || event.keyCode > 57 )
 	{ 
 		alert( "Ce champ doit être numérique, il ne peut contenir aucun autre type de caratères." );
   		event.returnValue = false;
   	}
}

// fonction qui ouvre la popup de réservation
function openRes(param) {
	window.open("","Reservation",'width=300,height=300,scrollbars=yes');
}

// navigation de niveau 4
function validNav4() {
	if (document.form_nav.url.value!=0) {
		location.href=document.form_nav.url.value;
	}
}

// ouvre un zoom sur une photo
function openPhoto(id_photo) {
	window.open("popup/visu.php?id="+id_photo,"Photo",'width=500,height=400,scrollbars=yes');
}

// ferme la popup de zoom
function fermer() {
	window.close();	
}

// ouvre la reservation
function openReservation(num,nom) {
	var url_resa = document.getElementById("url_moteur_resa").value;	
	location.href = "rappel.php?iframeSrc="+url_resa+"reserverRecall;jsessionid=0000sQ-_QY_P_U87zG8LL4eavzp:-1%3FnoDossier="+num+"%26nomPassager="+nom;
}

// redimentionne l'Iframe
function getDocHeight(doc) {
var docHt = 0, sh, oh;
if (doc.height) docHt = doc.height;
else if (doc.body) {
if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
if (sh && oh) docHt = Math.max(sh, oh);
}
return docHt;
}

function setIframeHeight(nom) {
//	document.getElementById(nom).height=800;
	var iframeWin = window.frames[nom];
	var iframeEl = document.getElementById? document.getElementById(nom): document.all? document.all[nom]: null;
	if ( iframeEl && iframeWin ) {
	iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous
	var docHt = getDocHeight(iframeWin.document);
	// need to add to height to be sure it will all show
	if (docHt) iframeEl.style.height = docHt + 30 + "px";
	}
}

// redimentionne l'Iframe
function setIframeNormal(nom) {
	document.getElementById(nom).height=509;
}

function showFaq(nom) {
	if (document.getElementById(nom).style.display=="none") {
		document.getElementById(nom).style.display = "block";
	}else{
		document.getElementById(nom).style.display = "none";	
	}
}

function valider_enquete() {
		//if ( valider_radio('general','accueil','decoration','ambiance','cadeau_bienvenue','info_anim','magie','animateur','art','petit_cadeau','originalite','qualite','gout','creches','groupe','anim_musicale') ) {				
		if ( valider_radio('general','accueil','decoration','ambiance','cadeau_bienvenue','info_anim') ) {				
				//if ( valider_radio_texte('enfants','animations') ) {			
				if (check_date(document.formulaire.elements["date_traversee"].value)) {
						valider_formulaire('nom_navire','civ','nom','prenom','email','adresse','cp','ville');
				}
		}
}
