// Fonction qui change de class
function ChangeClass(objet, myClass){ 
  objet.className = myClass;
}


// Fonction de redirection
function LmUp(path){
	location.href = path;
}

// Preload d'image et swap
function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// Afficher/cacher un calque
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

// Ouverture de popup
function ouvrefenetre(adresse, hauteur, largeur) {
	toto = window.open( adresse, "param", "scrollbars=0,toolbar=0,menubar=no,resizable=0,top=5,left=5,height="+hauteur+",width="+largeur );
	toto.focus();
}

// Fonction pour savoir si une checkbox est selectionné.
function AnySelected(champs){
    for (i=0; i<champs.length; i++) {
        if (champs[i].checked) return true;
    }
    return false;
}


////////////////////////////
//                        //
//      FORMULAIRES       //
//                        //
////////////////////////////

// Fonction de validité de mail.
function testMail(champ) {
	var car = 'non';
	for (var i = 0; i < champ.value.length; i++) {
		var ch = champ.value.substring(i, i + 1);
		if (ch == '@') {
			for (var j = i+1; j < champ.value.length; j++) {
				var ch2 = champ.value.substring(j, j + 1);
				if (ch2 == '.') {car='oui';}
				}
			}
		}
	if (car!='oui') {
		alert("Votre email semble être erroné."); champ.focus(); return 'non';
	} return 'ok';
}

// Fonction test de champ vide.
function testVide(champ){
	if (champ.value.length == 0){
		champ.focus();
		return 'non';
    }
    if (champ.value.length > 0){
        var tout_espaces='oui';
    	for (var i = 0; i < champ.value.length; i++){
        	if (champ.value.substring(i, i + 1) != ' '){ tout_espaces='non'; }
      	}
      	if (tout_espaces=='oui'){
			champ.focus();
			return 'non';
      	}
    } return 'ok';
}

// Fonction pour verifier le numero de telephone.
function verifTel(telephone){
	if (telephone.value != ''){
		var reg_tel = /^[0-9]{2}(\s[0-9]{2}){4}$/
		if (!(reg_tel.exec(telephone.value)!=null)){
			alert("Votre numéro de téléphone n'est pas au bon format.");
			telephone.focus(); // on selectionne le champ concerné.
			return 'non';
		}
		else {
			return 'ok';
		}
	}
	else {
		alert("Vous n'avez pas indiqué votre téléphone.");
		telephone.focus(); // on selectionne le champ concerné.
		return 'non';
	}
}

// Verification du projet et de ses coordonnees contact.
function verifProj(form) {
	if (testVide(form.Dep) != 'ok') {alert("Le département du projet est obligatoire."); return 'non'}
	if (testVide(form.Resultat) != 'ok') {alert("La commune du projet est obligatoire."); return 'non'}
	if (testVide(form.Orientation) != 'ok') {alert("L'orientation du projet est obligatoire."); return 'non'}
	if (testVide(form.Dimension) != 'ok') {alert("La surface du projet est obligatoire."); return 'non'}
	if (testVide(form.Delais) != 'ok') {alert("L'échéance du projet est obligatoire."); return 'non'}
	return 'ok';
} 
function valProj() {
	if (verifProj(document.Projet)=='ok') {
		document.Projet.but.disabled = true;
		document.Projet.but.value = 'patientez...';
		document.Projet.submit();
	}
}
function verifCont(form) {
	if (testVide(form.Nom) != 'ok') {alert("Votre nom est obligatoire.");return 'non'}
	if (testVide(form.Prenom) != 'ok') {alert("Votre prénom est obligatoire.");return 'non'}
	if (verifTel(form.Tel1) != 'ok') {return 'non'}
	if (testVide(form.Email) != 'ok') {alert("Votre email est obligatoire.");return 'non'}
	if (testMail(form.Email) != 'ok') {return 'non'}
	return 'ok';
} 
function valCont() {
	if (verifCont(document.Contact)=='ok') {
		document.Contact.but.disabled = true;
		document.Contact.but.value = 'patientez...';
		document.Contact.submit();
	}
}

// Verification du formulaire de question.
function verifQuest(form) {
	if (testVide(form.Nom) != 'ok') {alert("Les champs marqués d'une * sont obligatoires.");return 'non'}
	if (testVide(form.Prenom) != 'ok') {alert("Les champs marqués d'une * sont obligatoires.");return 'non'}
	if (testVide(form.Adresse) != 'ok') {alert("Les champs marqués d'une * sont obligatoires.");return 'non'}
	if (testVide(form.Cp) != 'ok') {alert("Les champs marqués d'une * sont obligatoires.");return 'non'}
	if (testVide(form.Ville) != 'ok') {alert("Les champs marqués d'une * sont obligatoires.");return 'non'}
	if (verifTel(form.Tel1) != 'ok') {return 'non'}
	if (testVide(form.Email) != 'ok') {alert("Les champs marqués d'une * sont obligatoires.");return 'non'}
	if (testMail(form.Email) != 'ok') {return 'non'}
	return 'ok';
}
function valQuest() {
	if (verifQuest(document.Contacts)=='ok') {
		document.Contacts.but.disabled = true;
		document.Contacts.but.value = 'patientez...';
		document.Contacts.submit();
	}
}

// Verification du formulaire de callback.
function verifCallB(form) {
	if (testVide(form.Nom) != 'ok') {alert("Les champs marqués d'une * sont obligatoires.");return 'non'}
	if (verifTel(form.Tel1) != 'ok') {return 'non'}
	return 'ok';
}
function valCallB() {
	if (verifCallB(document.Contacts)=='ok') {
		document.Contacts.submit();
	}
}


////////////////////////////
//                        //
//         AJAX           //
//                        //
////////////////////////////

function file(fichier){
	//On teste le navigateur
	if ( window.XMLHttpRequest ){ // On teste si le navigateur est Firefox
		xhr_object = new XMLHttpRequest();
	}
	else if ( window.ActiveXObject ){ // Sinon, on teste si c'est IE
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else{
		return false;
	}
	xhr_object.open( "GET", fichier, false );
	xhr_object.send(null);
	if ( xhr_object.readyState == 4 ){
		return xhr_object.responseText;
	}
	else{
		return false;
	}
}
