var ok = 1, place = "", msg = "Certaines informations sont manquantes ou erronées : \n";

function popup_erreur(message) {
	var wh=window.open("formulairechangementMDR.php","Erreurs","width=400,height=400");
	return;
}

function verif_em(email) {
   var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
   return (reg.exec(email)!=null)
}

function verif_dte(dte) {
   var reg = /^[0-3][0-9][/-][0-1][0-9][/-][20][0-9]{2}$/
   return (reg.exec(dte)!=null)
}

function verif_cp(cp,ps) {
	var reg1 = /^[0-9]{4}$/;
	var reg2 = /^[0-9]{5}$/;
	if (ps=="France")
		return (reg2.exec(cp)!=null);
	if ((ps=="Belgique") || (ps=="Suisse"))
		return (reg1.exec(cp)!=null);
}

function vide() {
	for (var i=0; i<document.forms[0].length ; i++) {
		if ((document.forms[0][i].type=="text") || (document.forms[0][i].type=="textarea") || (document.forms[0][i].type=="hidden"))
			document.forms[0][i].value="";
		if (document.forms[0][i].type=="select-one")
			document.forms[0][i].value=document.forms[0][i].options[0].value;
	}
	visi(false,false,false);
}

function visi(t,a,d) {
	var nouv_theme = document.getElementById('nouvtheme').style;
	var nouv_adr = document.getElementById('nouvadr').style;
	var date_nouvadr = document.getElementById('datenouvadr').style;
	var date_fin = document.getElementById('fin').style;
	(t) ? nouv_theme.display='block' : nouv_theme.display='none';
	(a) ? nouv_adr.display='block' : nouv_adr.display='none';
	(d) ? date_nouvadr.visibility='visible' : date_nouvadr.visibility='hidden';
	date_fin.visibility=date_nouvadr.visibility;
}

function init() {
	var civ = document.formul.civ.value;
	var ps = document.formul.ps.value;
	var ps2 = document.formul.ps2.value;
	var th = document.formul.th.value;
	if (civ!="")
		for (var i=0;i<document.formul.civilite.length;i++)
			if (document.formul.civilite.options[i].value==civ)
				document.formul.civilite.options[i].selected=true;
	if (ps!="")
		for (var i=0;i<document.formul.pays.length;i++)
			if (document.formul.pays.options[i].value==ps)
				document.formul.pays.options[i].selected=true;
	if (ps2!="")
		for (var i=0;i<document.formul.pays2.length;i++)
			if (document.formul.pays2.options[i].value==ps2)
				document.formul.pays2.options[i].selected=true;
	if (th!="")
		for (var i=0;i<document.formul.theme.length;i++)
			if (document.formul.theme.options[i].value==th) {
				document.formul.theme.options[i].selected=true;
				changetype(document.formul.theme);
			}
}

function changetype(s) {
	switch (s.value) 	{
		case '1' :
			visi(false,true, false);
			break;
		case '2' :
			visi(false,true, true);
			break;
		case '3' :
			visi(false,false,false);
			break;
		case '4' :
			visi(false,false,false);
			break;
		case '5' :
			visi(false,false,false);
			break;
		case '6' :
			visi(false,false,false);
			break;
		case '7' :
			visi(false,false,false);
			break;
		case '8' :
			visi(true, false, false);
			break;
		default:
			visi(false, false, false);
	}
}

function verif_champ(champ, message) {
	champ.value = champ.value.replace(/\"/g,"\'");
	if ((champ.value + "")=="") {
		msg += "- "+message+"\n";
		if (place=="") {
	 		place= "formul."+champ.name+".focus()";
		}
		ok = 0;
	}
	return ok;
}

function verification(frm) {

	var theme = frm.theme;

	if (ok) {

	ok=0;
	for (var i=0;i<theme.options.length;i++) {
		if ((theme.options[i].selected==true) && (theme.options[i].value!="0")) {
			ok=1;
			var theme_choisi = theme.options[i].value;
		}
	}
	if (ok==0) 
		msg += "- Choisissez un thème.\n";

	if (theme_choisi=="8") {
		verif_champ(frm.autretheme,"Indiquez votre nouveau thème.");
	}

frm.numero.value = frm.numero.value.replace(/\"/g,"\'");
frm.adresse2.value = frm.adresse2.value.replace(/\"/g,"\'");
frm.adresse22.value = frm.adresse22.value.replace(/\"/g,"\'");

		verif_champ(frm.message,"Indiquez votre message.");
		verif_champ(frm.numero,"Indiquez votre numéro d'abonné.");
		verif_champ(frm.nom,"Indiquez votre nom.");
		verif_champ(frm.prenom,"Indiquez votre prénom.");
		verif_champ(frm.adresse1,"Indiquez votre adresse.");
		verif_champ(frm.codepostal,"Indiquez votre code postal.");
		if (!verif_cp(frm.codepostal.value+"", frm.pays.value)) {
			msg += "- Code postal incorrect.\n";
			frm.codepostal.value="";
			if (place=="") {
				place = frm.name + ".codepostal.focus()";
			}
			ok=0;
		}
		verif_champ(frm.ville,"Indiquez votre ville.");
		verif_champ(frm.email,"Indiquez votre e-mail.");
		if (!verif_em(frm.email.value+"")) {
			msg += "- E-mail incorrect.\n";
			frm.email.value="";
			if (place=="") {
				place = frm.name + ".email.focus()";
			}
			ok=0;
		}
		if ((theme_choisi=="1") || (theme_choisi=="2")) {
			verif_champ(frm.dte_debut,"Indiquez la date de début du changement.");
			if (theme_choisi=="2")
				verif_champ(frm.dte_fin,"Indiquez la date de fin du changement.");
			verif_champ(frm.adresse12,"Indiquez votre nouvelle adresse.");
			verif_champ(frm.codepostal2,"Indiquez votre nouveau code postal.");
			if (!verif_cp(frm.codepostal2.value+"", frm.pays2.value)) {
				msg += "- Nouveau code postal incorrect.\n";
				frm.codepostal2.value="";
				if (place=="") {
					place = frm.name + ".codepostal2.focus()";
				}
				ok=0;
			}
			verif_champ(frm.ville2,"Indiquez votre nouvelle ville.");
			verif_champ(frm.email2,"Indiquez votre nouvel e-mail.");
			if (!verif_em(frm.email2.value+"", frm.pays2.value)) {
				msg += "- Nouvel e-mail incorrect.\n";
				frm.email2.value="";
				if (place=="") {
					place = frm.name + ".email2.focus()";
				}
				ok=0;
			}
		}
	}


	if (ok==1) {
		frm.inscrit.value=1;
		frm.submit();
	} else {
		alert(msg);
		eval(place);
		ok=1;
		place="";
		msg = "Certaines informations sont manquantes ou erronées : \n";
	}

}
