function flecheHaut(id)	{
    if (document.all)
        $(id).firstChild.className="";
    else      	
        $(id).firstChild.nextSibling.setAttribute("class","");
}

function flecheBas(id)	{
    if (document.all)
        $(id).firstChild.className="current";
    else
        $(id).firstChild.nextSibling.setAttribute("class","current");
}

function flecheHaut2(id) {
	$(id).className="";
}

function flecheBas2(id) {
	$(id).className="current";
}

/* 
   string 	nom		: debut du nom du formulaire 
   int 		id		: numero de la fenetre du formulaire
   bool		erreur	: spécifie s'il y a une erreur ou non dans le formulaire
*/

function afficheForm(nom,id,erreur) {
	var elt = $(nom+id);
	if(elt.style.display == "none") {
		elt.style.display = "block";	//	on montre
		flecheBas('form_t_'+id);
		if (!erreur)	{
			i=1;
			while (document.getElementById(nom+i))	{
				if (i!=id)	{
					$(nom+i).style.display="none";
					flecheHaut('form_t_'+i);
				} else
					flecheBas('form_t_'+i);
				i++;
			}
		}
	} else {
		elt.style.display = "none";	//	on cache
		flecheHaut('form_t_'+id);
	}
}

function afficheForm2(nom,id,erreur) {
	var elt = $(nom+id);
	etape = id;
	if(elt.style.display == "none") {
		elt.style.display = "block";	//	on montre
		flecheBas2('form_t_'+id);
		if (!erreur)	{
			i=1;
			while (document.getElementById(nom+i))	{
				if (i!=id)	{
					$(nom+i).style.display="none";
					flecheHaut2('form_t_'+i);
				} else
					flecheBas2('form_t_'+i);
				i++;
			}
		}
	} else {
		elt.style.display = "none";	//	on cache
		flecheHaut2('form_t_'+id);
	}
}

/*function afficheFormSalaire(id, erreur) {
	var elt = $('salaire_r_'+id);
	if(elt.style.display == "none") {
		elt.style.display = "block";
		if (!erreur)	{
			for (i=1; i<5; i++)	{
				if (i!=id)
					$('salaire_r_'+i).style.display="none";
			}
		}
	}
	else {
		elt.style.display = "none";
	}
}*/

function proposeChoixLocalite(obj) {
	var suffix = '';
	if(proposeChoixLocalite.arguments.length == 2) suffix = proposeChoixLocalite.arguments[1];
	clearProppositionNPA();
	if(obj.value.length == 0) {
		$('proposition_localite'+suffix).style.display = 'none';
	}
	else {
		new Ajax.Updater('proposition_localite'+suffix,'ajax/proposition_localite.php?nom='+obj.value+'&suffix='+suffix, {
			method: 'get',
			evalScripts: true,
			onComplete: function (req) {
				if(req.responseText != "") {
					$('proposition_localite'+suffix).style.display = 'block';
					setTimeout('clearProppositionLocalite(\''+suffix+'\')', 30000);
				}
				else {
					clearProppositionLocalite(suffix);
				}
			}
		});
	}
}

function proposeChoixLocalite2(obj, id_canton) {
	var suffix = '';
	clearProppositionNPA();
	if(obj.value.length == 0) {
		$('proposition_localite'+suffix).style.display = 'none';
	}
	else {
		new Ajax.Updater('proposition_localite'+suffix,'ajax/proposition_localite.php?nom='+obj.value+'&id_canton='+id_canton+'&suffix='+suffix, {
			method: 'get',
			evalScripts: true,
			onComplete: function (req) {
				if(req.responseText != "") {
					$('proposition_localite'+suffix).style.display = 'block';
					setTimeout('clearProppositionLocalite(\''+suffix+'\')', 30000);
				}
				else {
					clearProppositionLocalite(suffix);
				}
			}
		});
	}
}


function proposeChoixLocalite_fr(obj) {
	var suffix = '';
	if(proposeChoixLocalite_fr.arguments.length == 2) suffix = proposeChoixLocalite_fr.arguments[1];
	clearProppositionNPA();
	if(obj.value.length == 0) {
		$('proposition_localite'+suffix).style.display = 'none';
	}
	else {
		new Ajax.Updater('proposition_localite'+suffix,'ajax/proposition_localite_fr.php?nom='+obj.value+'&suffix='+suffix, {
			method: 'get',
			evalScripts: true,
			onComplete: function (req) {
				if(req.responseText != "") {
					$('proposition_localite'+suffix).style.display = 'block';
					setTimeout('clearProppositionLocalite(\''+suffix+'\')', 30000);
				}
				else {
					clearProppositionLocalite(suffix);
				}
			}
		});
	}
}
function setLocalite(id, val) {
	var suffix = '';
	if(setLocalite.arguments.length == 3) suffix = setLocalite.arguments[2];
	$('id_localite'+suffix).value = id;
	$('nom_localite'+suffix).value = val;	
	clearProppositionLocalite(suffix);
}
function clearProppositionLocalite() {
	var suffix = '';
	if(clearProppositionLocalite.arguments.length == 1) suffix = clearProppositionLocalite.arguments[0];
	if($('proposition_localite'+suffix)) {
		$('proposition_localite'+suffix).style.display = 'none';
	}
}
function proposeChoixNPA(obj) {
	clearProppositionLocalite();
	if(obj.value.length == 0) {
		$('proposition_npa').style.display = 'none';
	}
	else {
		new Ajax.Updater('proposition_npa','ajax/proposition_npa.php?nom='+obj.value, {
			method: 'get',
			evalScripts: true,
			onComplete: function (req) {
				if(req.responseText != "") {
					$('proposition_npa').style.display = 'block';
					setTimeout('clearProppositionNPA()', 30000);
				}
				else {
					clearProppositionNPA();
				}
			}
		});
	}
}
function setNPA(val) {
	$('npa').value = val;	
	clearProppositionNPA();
}
function clearProppositionNPA() {
	if($('proposition_npa')) {
		$('proposition_npa').style.display = 'none';
	}
}

function proposeChoixNPA2(obj, id_canton) {
	clearProppositionLocalite();
	if(obj.value.length == 0) {
		$('proposition_npa').style.display = 'none';
	}
	else {
		new Ajax.Updater('proposition_npa','ajax/proposition_npa2.php?nom='+obj.value+'&id_canton='+id_canton, {
			method: 'get',
			evalScripts: true,
			onComplete: function (req) {
				if(req.responseText != "") {
					$('proposition_npa').style.display = 'block';
					setTimeout('clearProppositionNPA()', 30000);
				}
				else {
					clearProppositionNPA();
				}
			}
		});
	}
}
function proposeChoixNPA2_fr(obj) {
	clearProppositionLocalite();
	if(obj.value.length == 0) {
		$('proposition_npa').style.display = 'none';
	}
	else {
		new Ajax.Updater('proposition_npa','ajax/proposition_npa2_fr.php?nom='+obj.value, {
			method: 'get',
			evalScripts: true,
			onComplete: function (req) {
				if(req.responseText != "") {
					$('proposition_npa').style.display = 'block';
					setTimeout('clearProppositionNPA()', 30000);
				}
				else {
					clearProppositionNPA();
				}
			}
		});
	}
}
function setLocalite2(id, val) {
	$('id_localite').value = id;
	$('npa').value = val;
	clearProppositionNPA();
}

function afficheFaq(id) {
	var elt = $('faq_r_'+id);
	if(elt.style.display == "none") {
		elt.style.display = "";
	}
	else {
		elt.style.display = "none";
	}
}

function afficheLexique(id) {
	var elt = $('lexique_d_'+id);
	if(elt.style.display == "none") {
		elt.style.display = "";
	}
	else {
		elt.style.display = "none";
	}
}

function lexiqueChangeStatut() {
	var a = 0;
	if($('frm_lexique').elements['application'])
		a = $('frm_lexique').elements['application'].value;
	new Ajax.Updater('lexique_theme','ajax/lexique_lstbox_theme.php?id_rubrique='+$('frm_lexique').elements['id_rubrique'].value+'&application='+a, {
		method: 'get',
		evalScripts: true
	});
	new Ajax.Updater('lexique_liste','ajax/lexique_liste.php?id_rubrique='+$('frm_lexique').elements['id_rubrique'].value+'&lettre='+$('frm_lexique').elements['lettre'].value+'&id_theme='+$('frm_lexique').elements['id_theme'].value+'&application='+a, {
		method: 'get',
		evalScripts: true
	});
}

function lexiqueChangeRubrique() {
	var a = 0;
	if($('frm_lexique').elements['application'])
		a = $('frm_lexique').elements['application'].value;
	new Ajax.Updater('lexique_liste','ajax/lexique_liste.php?id_rubrique='+$('frm_lexique').elements['id_rubrique'].value+'&lettre='+$('frm_lexique').elements['lettre'].value+'&id_theme='+$('frm_lexique').elements['id_theme'].value+'&application='+a, {
		method: 'get',
		evalScripts: true
	});
}

function lexiqueChangeTheme() {
	var a = 0;
	if($('frm_lexique').elements['application'])
		a = $('frm_lexique').elements['application'].value;
	new Ajax.Updater('lexique_liste','ajax/lexique_liste.php?id_rubrique='+$('frm_lexique').elements['id_rubrique'].value+'&lettre='+$('frm_lexique').elements['lettre'].value+'&id_theme='+$('frm_lexique').elements['id_theme'].value+'&application='+a, {
		method: 'get',
		evalScripts: true
	});
}

function lexiqueChangeLettre(l) {
	var a = 0;
	if($('frm_lexique').elements['application'])
		a = $('frm_lexique').elements['application'].value;
	$('frm_lexique').elements['lettre'].value = l;
	new Ajax.Updater('lexique_liste','ajax/lexique_liste.php?id_rubrique='+$('frm_lexique').elements['id_rubrique'].value+'&lettre='+l+'&id_theme='+$('frm_lexique').elements['id_theme'].value+'&application='+a, {
		method: 'get',
		evalScripts: true
	});
}

function lexiqueChangePage(start, l, idt, a, r) {
	new Ajax.Updater('lexique_liste','ajax/lexique_liste.php?id_rubrique='+r+'&lettre='+l+'&id_theme='+idt+'&application='+a+'&start='+start, {
		method: 'get',
		evalScripts: true
	});
}

function pop(str,titre,largeur,hauteur){
	window.open(str,titre,"toolbar=no, location=no, directories=no, status=no, scrollbars=yes, resizable=no, copyhistory=no,width="+largeur+",height="+hauteur)
}

function identification()	{
	var url = 'login.php';
	var params = "email="+$('ac_email').value+"&password="+$('ac_password').value;
    var myAjax = new Ajax.Request
	(url, 
        {
		  asynchronous:true,
          method: 'post',
		  postBody: params,
          onLoading: function (xhr)
            { // Après appel méthode open
              // (début de la requête Ajax)
               // Element.show('id_chargement');
            },
          onSuccess: function (xhr)
            {
				new Ajax.Updater(
								 "left_compte",
								 "inclusion/colonne_gauche_compte.inc.php",
								 {evalScripts:true}
								 );
            },
		  onFailure: function (xhr)
		  	{
				new Ajax.Updater(
								 "left_compte",
								 "inclusion/colonne_gauche_compte.inc.php"
								 );
			},
		  /*onException: function (xhr, e)
		  	{
				$('left_compte').innerHTML = "Exception : "+e;
			},*/
          onComplete: function(req)
            { // Toujours cappelé en fin de requête
				//Element.hide('id_chargement');
				if(req.responseText == "0") {
					display_message_infos("Nom d'utilisateur ou mot de passe inccorrect");
				}
				if(req.responseText == "-1") {
					display_message_infos("Cet identifiant est connecté, vous ne pouvez pas vous connecter plusieurs fois en même temps");
				}
            }
        }
	);
}

function recupAide(id,event,type)	{
// type = lexique | aide
	if (typeof id != 'string')	{
		var url = '/ajax/aide.php';
		var params = "id="+id+"&type="+type;
		var myAjax = new Ajax.Request
		(url, 
			{
			  asynchronous:false,
			  method: 'post',
			  postBody: params,
			  onSuccess: function(xhr)	{
				$('aide_text').innerHTML = xhr.responseText;
			  },
			  onException: function (xhr, e)	{
				$('aide_text').innerHTML = "Exception : "+e;
			  }
	//		  onComplete: function()	{}
			}
		);
	} else 
		$('aide_text').innerHTML = "<p>"+id+"</p>";
	afficherAide(event);
}

function afficherAide(event)	{
		var hauteur = $("aide").offsetHeight;
		var x=(document.body.scrollLeft || document.documentElement.scrollLeft) - $('container').offsetLeft + event.clientX	-30;
		var y=(document.body.scrollTop || document.documentElement.scrollTop) + event.clientY-hauteur-10;
		$("aide").style.left = x+"px";
		$("aide").style.top = y+"px";
		$("aide").style.visibility = 'visible';
}

function enleverAide()	{
	$("aide").style.visibility = 'hidden';
}
/* FIN AIDE */

function format_montant(valeur,decimal,separateur) {
	// formate un chiffre avec 'decimal' chiffres après la virgule et un separateur
	var deci=Math.round( Math.pow(10,decimal)*(Math.abs(valeur)-Math.floor(Math.abs(valeur)))) ; 
	var val=Math.floor(Math.abs(valeur));
	if ((decimal==0)||(deci==Math.pow(10,decimal))) {val=Math.floor(Math.abs(valeur)); deci=0;}
	var val_format=val+"";
	var nb=val_format.length;
	for (var i=1;i<4;i++) {
		if (val>=Math.pow(10,(3*i))) {
			val_format=val_format.substring(0,nb-(3*i))+separateur+val_format.substring(nb-(3*i));
		}
	}
	if (decimal>0) {
		var decim=""; 
		for (var j=0;j<(decimal-deci.toString().length);j++) {decim+="0";}
		deci=decim+deci.toString();
		val_format=val_format+"."+deci;
	}
	if (parseFloat(valeur)<0) {val_format="-"+val_format;}
	return val_format;
}

/*Vérification du mail*/
function mailCheck(str)	{
	var reg = /^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/;
	return (reg.test(str));
}

function identification_e2() {
	$('msg_err_ident').style.display = 'none';
	$('msg_err_ident2').style.display = 'none';
	new Ajax.Request('ajax/identification.php', {
		method: 'post',
		postBody: 'login='+$('clogin').value+'&password='+$('cpassword').value,
		onComplete: function(req) {
			if(req.responseText == "0") {
				$('msg_err_ident').style.display = '';
			}
			else if(req.responseText == "-1") {
				$('msg_err_ident2').style.display = '';
			}
			else {
				eval(req.responseText);
				$('frm_ident').style.display = 'none';
				$('ident_ok').style.display = 'block';
				$('bloc_inscription').style.display = 'none';
				new Ajax.Updater("left_compte", "inclusion/colonne_gauche_compte.inc.php", {
								 evalScripts: true
				});
			}
		}			 
	});
}


function email_compte_existe(email)
{
	var existe = false;
    new Ajax.Request('ajax/email_compte_existe.php', {
		method: 'post',
		postBody: 'email='+email,
		asynchronous: false,
		onComplete: function(req) {
			if(req.responseText == "1")
                existe = true;
		}
	});
	return existe;
}


function valider_form_devis() {
	var i, j;
	var ok = true;
	if(etape == 1) {
		
		if($('frm_devis').elements['id_produit[]'].length > 1) {
			var chk = false;
			for(i = 0 ; i < $('frm_devis').elements['id_produit[]'].length ; i++) {
				if($('frm_devis').elements['id_produit[]'][i].checked) {
					chk = true;
					break;
				}
			}
			if(!chk) {
				ok = false;
				$('err_produit').style.color = 'red';
			}
			else {
				$('err_produit').style.color = '';
			}
		}
		else {
			if(!$('frm_devis').elements['id_produit[]'].checked) {
				ok = false;
				$('err_produit').style.color = 'red';
			}
			else {
				$('err_produit').style.color = '';
			}
		}
						
		if(ok) {
			$('err_'+etape).hide();
			$('etape'+etape).hide();
			etape++;
			$('etape'+etape).show();
			$('bloc_etape'+etape).show();
		}
		else {
			$('err_'+etape).show();
			$('etape'+etape).show();
		}
		
	}
	else if(etape == 2)	{
		
		if($('cpt_nom').value == "") {
			ok = false;
			$('err_nom').style.color = 'red';
		}
		else {
			$('err_nom').style.color = '';
		}
		if($('cpt_prenom').value == "") {
			ok = false;
			$('err_prenom').style.color = 'red';
		}
		else {
			$('err_prenom').style.color = '';
		}
		if($('cpt_adresse').value == "") {
			ok = false;
			$('err_adresse').style.color = 'red';
		}
		else {
			$('err_adresse').style.color = '';
		}
		if($('cpt_code_postal').value == "" || $('cpt_ville').value == "") {
			ok = false;
			$('err_cp_ville').style.color = 'red';
		}
		else {
			$('err_cp_ville').style.color = '';
		}
		if($('cpt_pays').value == "") {
			ok = false;
			$('err_pays').style.color = 'red';
		}
		else {
			$('err_pays').style.color = '';
		}
		if(!mailCheck($('cpt_email').value)) {
			ok = false;
			$('err_email').style.color = 'red';
		}
		else {
			$('err_email').style.color = '';
		}
		if($('cpt_tel_fixe').value == "") {
			ok = false;
			$('err_tel_fixe').style.color = 'red';
		}
		else {
			$('err_tel_fixe').style.color = '';
		}
		/*if($('cpt_tel_mobile').value == "") {
			ok = false;
			$('err_tel_mobile').style.color = 'red';
		}
		else {
			$('err_tel_mobile').style.color = '';
		}*/
		
				
		if(facultatif() && ok) {
			$('err_'+etape).hide();
			$('etape'+etape).hide();
			etape++;
			$('etape'+etape).show();
			$('bloc_etape'+etape).show();
		}
		else {
			$('err_'+etape).show();
			$('etape'+etape).show();
		}
		
	}
	else if(etape == 3) {
	
		if(!$('frm_devis').elements['mode_com'][0].checked && !$('frm_devis').elements['mode_com'][1].checked && !$('frm_devis').elements['mode_com'][2].checked) {
			ok = false;
			$('err_mode_com').style.color = 'red';
		}
		else {
			$('err_mode_com').style.color = '';
		}
				
		if(ok) {
			$('err_'+etape).hide();
			$('etape'+etape).hide();
			etape++;
			$('etape'+etape).show();
			$('bloc_etape'+etape).show();
		}
		else {
			$('err_'+etape).show();
			$('etape'+etape).show();
		}
		
	}
	else if(etape == 4) {
		if($('adresse_mail') != null)
		{
		    if(!mailCheck($('adresse_mail').value))
		    {
                ok = false;
                $('err_login').style.display = 'none';
                $('err_login_invalide').style.display = 'inline';
            }
            else
            {
                $('err_login_invalide').style.display = 'none';
                if(email_compte_existe($('adresse_mail').value))
    		    {
                    ok = false;
                    $('err_login').style.display = 'inline';
                }
                else
                    $('err_login').style.display = 'none';
            }
            
            if($('mdp').value == "" && $('confirm_mdp').value == "")
            {
                ok = false;
                $('err_mdp_vide').style.display = 'inline';
                $('err_mdp_diff').style.display = 'none';
            }
            else
            {
                $('err_mdp_vide').style.display = 'none';
                if($('mdp').value != $('confirm_mdp').value)
                {
                    ok = false;
                    $('err_mdp_diff').style.display = 'inline';
                }
                else
                    $('err_mdp_diff').style.display = 'none';
            }
                
        }
		
		if(!$('autorisation_com').checked) {
			ok = false;	
			$('err_autorisation_com').style.color = 'red';
			$('err_'+etape).innerHTML = "Vous devez autoriser Expatwire à communiquer ces informations, pour pouvoir continuer";
		}
		else {
			$('err_autorisation_com').style.color = '';
			$('err_'+etape).innerHTML = "";
		}
				
		if (ok) {
			for (i=1; i<=4; i++)	{	// on ferme tout
				$('etape'+i).style.display="none";
				$('err_'+i).hide();
			}
		
			$('frm_devis').submit();
		}
		else {
			$('err_'+etape).show();
		}
	}
}



function display_message_infos(msg) {
	var elt = $('message-infos');
	$('message-infos-contenu').innerHTML = "<h4>" + msg + "</h4>";
	
	elt.style.top = (document.documentElement.scrollTop + (getWindowHeight() / 2) - (elt.offsetHeight / 2) - 100)+"px";
	elt.style.left = (document.documentElement.scrollLeft + (getWindowWidth() / 2) - (elt.offsetWidth / 2) + 20)+"px";
	elt.style.visibility = "visible";
	
	// Pour IE : on n'affiche plus les listbox
	if(navigator.appVersion.indexOf("MSIE") != -1) {
		var sEls = document.getElementsByTagName("select");
		for(var i = 0 ; i < sEls.length ; i++) {
			sEls[i].style.visibility = 'hidden';
		}
	}
	
	setTimeout('clear_message_infos()', 3000);
}
function clear_message_infos() {
	var elt = $('message-infos');

	// Pour IE
	if(navigator.appVersion.indexOf("MSIE") != -1) {
		var sEls = document.getElementsByTagName("select");
		for(var i = 0 ; i < sEls.length ; i++) {
			sEls[i].style.visibility = 'visible';
		}
	}

	elt.style.visibility = "hidden";
	$('message-infos-contenu').innerHTML = "";
}


function getWindowHeight() {
    var windowHeight=0;
    if (typeof(window.innerHeight)=='number') {
        windowHeight=window.innerHeight;
    }
    else {
     if (document.documentElement&&
       document.documentElement.clientHeight) {
         windowHeight = document.documentElement.clientHeight;
    }
    else {
     if (document.body&&document.body.clientHeight) {
         windowHeight=document.body.clientHeight;
      }
     }
    }
    return windowHeight;
}


function getWindowWidth() {
    var windowWidth=0;
    if (typeof(window.innerWidth)=='number') {
        windowWidth=window.innerWidth;
    }
    else {
     if (document.documentElement&&
       document.documentElement.clientWidth) {
         windowWidth = document.documentElement.clientWidth;
    }
    else {
     if (document.body&&document.body.clientWidth) {
         windowWidth=document.body.clientWidth;
      }
     }
    }
    return windowWidth;
}

function affiche_localite() {
	new Ajax.Updater('tableau_liste_localite', 'ajax/carte_tableau_liste_localite_selection.inc.php?id_localite='+$('id_localite').value, {
		method: 'get',
		evalScripts: true
	});
	$('id_localite').value = "";
	$('nom_localite').value = "";
}

function afficher_actus(mois)
{
    new Ajax.Updater('actus_du_mois','ajax/actus_du_mois.inc.php?mois='+mois, {
		method: 'get',
		evalScripts: true
	});
}
