/* ----- Racourci ----- */
function get(e){
	return document.getElementById(e);
}

/* ----- Popup image Zone admin ----- */
function popupCentre(source,largeur,hauteur){
  var x=(screen.width-largeur)/2;
  var y=(screen.height-hauteur)/2;
  window.open(source,'','top='+y+',left='+x+',width='+largeur+',height='+hauteur+',location=no,status=no,scrollbars=no');
}

/* ----- Deplacement d'une div dans une autre horizontalement ----- */
/*
function move(direction,num,max,divid,leftarrowid,rightarrowid,decalage){
	var left = get(divid).style.left;
	var longueur = left.length - 2; 
	if (direction == 'droite'){
		if (num < (max-1)){
			get(divid).style.left = parseFloat(left.substring(0,longueur)) - decalage + 'px';
			get(leftarrowid).style.visibility = 'visible';
			if (num == (max-4)){
				get(rightarrowid).style.visibility = 'hidden';
			}
			return (num+1);
		}
	}
	if (direction == 'gauche'){
		if (num > 0){
			get(divid).style.left = parseFloat(left.substring(0,longueur)) + decalage + 'px';
			get(rightarrowid).style.visibility = 'visible';
			if (num == 1){
				get(leftarrowid).style.visibility = 'hidden';
			}
			return (num-1);
		}
	}
	return num;
}
*/
/* --------------------- DIAPORAMA --------------------- */
function lancetiming(ms){
	try{
		timing(ms,images);
	}
	catch(e){
		// On arrive ici si la page n'est pas celle du diaporama.
	}
}

/* ----- Chargement / affichage image ----- */
function display(current){
	current = current + 1;
	if (current > (images.length - 1)){current = 0;}
	get('diapo').style.visibility = 'hidden';
	get('diapo').innerHTML = '<img src = "'+images[current]+'" width = "'+largeur[current]+'" height = "'+hauteur[current]+'" alt = "" title = "" onload = "this.style.visibility = \'visible\';fade_in(\'agence_left\',0);"/>';
	
	return (current);
}

/* ----- Timing permanent récursif ----- */
var mto;
var t=0;
function timing(ms,tab){
	current = display(current);
	clearTimeout(mto);
	mto = setTimeout(function(){timing(ms,tab);},ms);
}

/* ---- Charge une photo dans une div ----- */
function majPhoto(psource,pwidth,pheight){
	get('thephoto').style.visibility = 'hidden';
	//get('portfolio_zoom').style.background = 'background:url(\'images/portfolio/loading.gif\') no-repeat center center;';
	get('thephoto').innerHTML = '<table cellspacing = "0" width = "100%"><tr><td valign = "middle" align = "center" style = "height:432px;"><img src = "'+psource+'" width = "'+pwidth+'" height = "'+pheight+'" alt = "" title = "" onload = "get(\'thephoto\').style.visibility = \'visible\';fade_in(\'portfolio_zoom\',0);"/></td></tr></table>';
}

/* ----- Deplacement d'une div dans une autre verticalement ----- */
function movev(direction,num,max,divid,leftarrowid,rightarrowid,decalage){
	var top = get(divid).style.top;
	var longueur = top.length - 2; 
	if (direction == 'bas'){
		if (num < (max)){
			get(divid).style.top = parseFloat(top.substring(0,longueur)) - decalage + 'px';
			get(leftarrowid).style.visibility = 'visible';
			if (num == (max-1)){
				get(rightarrowid).style.visibility = 'hidden';
			}
			return (num+1);
		}
	}
	if (direction == 'haut'){
		if (num > 0){
			get(divid).style.top = parseFloat(top.substring(0,longueur)) + decalage + 'px';
			get(rightarrowid).style.visibility = 'visible';
			if (num == 1){
				get(leftarrowid).style.visibility = 'hidden';
			}
			return (num-1);
		}
	}
	return num;
}

/* ----- Deplacement de DEUX div dans une autre verticalement ----- */
function movev2(direction,num,max,divid,divid2,leftarrowid,rightarrowid,decalage){
	var top = get(divid).style.top;
	var longueur = top.length - 2; 
	var top2 = get(divid2).style.top;
	var longueur2 = top2.length - 2;
	if (direction == 'bas'){
		if (num < (max)){
			get(divid).style.top = parseFloat(top.substring(0,longueur)) - decalage + 'px';
			get(divid2).style.top = parseFloat(top2.substring(0,longueur2)) - decalage + 'px';
			get(leftarrowid).style.visibility = 'visible';
			if (num == (max-1)){
				get(rightarrowid).style.visibility = 'hidden';
			}
			return (num+1);
		}
	}
	if (direction == 'haut'){
		if (num > 0){
			get(divid).style.top = parseFloat(top.substring(0,longueur)) + decalage + 'px';
			get(divid2).style.top = parseFloat(top2.substring(0,longueur2)) + decalage + 'px';
			get(rightarrowid).style.visibility = 'visible';
			if (num == 1){
				get(leftarrowid).style.visibility = 'hidden';
			}
			return (num-1);
		}
	}
	return num;
}

/* ----- Verifie que les champs obligatoires sont remplis dans le formulaire de contact ----- */
function checkForm(langue,total){
	var nom = get('cnom').value.length;
	var prenom = get('cprenom').value.length;
	var email = get('cemail').value.length;
	var msg = get('cmsg').value.length;
	
	if (nom>0 && prenom>0 && email>0 && msg>0 && total>=4){
		get('valide').value = 1;
		document.forms['contact'].submit();
	}else{
		if (langue == 'fr') alert('Tous les champs doivent être remplis. Le champ téléphone est optionnel.');
		else alert('All fields are required. The telephone field is optional.');
	}
}

/* ----- Bascule une fausse checkbox ----- */
function toggle(imgid,hiddenid){
	var val = get(hiddenid).value;
	// Activé => desactivé
	if (val == 1){
		get(imgid).src = 'images/spacer.gif';
		get(hiddenid).value = 0;
	}
	// Désactivé => activé
	else{
		get(imgid).src = 'images/contact/tick.jpg';
		get(hiddenid).value = 1;
	}
}

function checkRecForm(langue){
	var nom = get('rec_nom').value.length;
	var prenom = get('rec_prenom').value.length;
	var telfixe = get('rec_tel').value.length;
	var email = get('rec_email').value.length;
	var cv = get('rec_cv').value.length;
	var lm = get('rec_lm').value.length;
	
	if (nom>0 && prenom>0 &&telfixe>0 && email>0 && cv>0 && lm>0){
		get('valid').value = 1;
		document.apply.submit();
	}else{
		if (langue == 'fr') alert('Tous les champs doivent être remplis.');
		else alert('All fields are required.');
	}
}

function checkNL(langue){
	var nl_email = get('nl_email').value.length;
	var nl_code = get('nl_code').value.length;
	if (nl_email>0 && nl_code == 4){
		get('valid').value = 1;
		document.forms['nl_suscribe'].submit();
	}else{
		if (langue == 'fr') alert('Merci de renseigner votre adresse email, et de recopier le code anti-spam dans le champ à sa droite.');
		else alert('Please fill in the email address field, and copy the anti-spam code in the field on the right.');
	}
}

/* ----- ------------------------------------------------------------------------------------------- ----- */

function initContinent(nbc,ids,noms,ini){
	get('continent').innerHTML = '';
	if (nbc != 0){
		for(var i=0;i<nbc;i++){
			opt = new Option(noms[i],ids[i]);
			get('continent').options[get('continent').options.length] = opt;
		}
		if (ini == 1) get('continent').options.selectedIndex = 0;
		
	}else{
		opt = new Option(nothing('c'),-1);
		get('continent').options[get('continent').options.length] = opt;
	}
}

function initPays(nbp,ids,noms,fks,ini){
	get('pays').innerHTML = '';
	if (nbp != 0){
		var current_c = get('continent').options[get('continent').options.selectedIndex].value;
		var total = 0;
		for(var i=0;i<nbp;i++){
			if (fks[i] == current_c){
				opt = new Option(noms[i],ids[i]);
				get('pays').options[get('pays').options.length] = opt;
				total = total + 1;
			}
		}
		if (total == 0){
			opt = new Option(nothing('p'),-1);
			get('pays').options[get('pays').options.length] = opt;
		}
		if (ini == 1) get('pays').options.selectedIndex = 0;
	}else{
		opt = new Option(nothing('p'),-1);
		get('pays').options[get('pays').options.length] = opt;
	}
}

function initVille(nbv,ids,noms,fks,ini){
	get('ville').innerHTML = '';
	if (nbv != 0){
		var current_p = get('pays').options[get('pays').options.selectedIndex].value;
		var total = 0;
		for(var i=0;i<nbv;i++){
			if (fks[i] == current_p){
				opt = new Option(noms[i],ids[i]);
				get('ville').options[get('ville').options.length] = opt;
				total = total + 1;
			}
		}
		if (total == 0){
			opt = new Option(nothing('v'),-1);
			get('ville').options[get('ville').options.length] = opt;
		}
		if (total > 0){
			majPoints();
		}
		if (ini == 1) get('pays').options.selectedIndex = 0;
	}else{
		opt = new Option(nothing('v'),-1);
		get('ville').options[get('ville').options.length] = opt;
	}
}

function nothing(typ){
	switch(typ){
		case 'c':
			if (langue == 'fr') return 'Aucun continent';
			else return 'No continent';
		break;
		case 'p':
			if (langue == 'fr') return 'Aucun pays';
			else return 'No country';
		break;
		case 'v':
			if (langue == 'fr') return 'Aucune ville';
			else return 'No city';
		break;
		default:
			return ''; 
		break;
	}
}

function majPoints(){
	var ville = get('ville').options[get('ville').options.selectedIndex].value;
	// On recharge le contenu de la div pour la ville selectionnée
	get('ptdesc').innerHTML = loadFile('loadpoints.php?ville='+ville);
	// On recharge le lancement de scroll
	startminiscroll('sconteneur','scontenu','bottomarrow','sscrollbg');
}

function loadFile(fichier)
{
    if(window.XMLHttpRequest) obj = new XMLHttpRequest(); //Pour Firefox, Opera,...
    else if(window.ActiveXObject) obj = new ActiveXObject("Microsoft.XMLHTTP"); //Pour Internet Explorer 
    else return(false);
    
    if (obj.overrideMimeType) obj.overrideMimeType("text/xml"); //Évite un bug de Safari
   
    obj.open("GET", fichier, false);
    obj.send(null);
   
    if(obj.readyState == 4) return(obj.responseText);
    else return(false);
}

function checkLogin(langue){
	var log = get('dident').value.length;
	var pass = get('dpassword').value.length;
	if (log>0 && pass>0){
		get('filled').value = 1;
		document.forms['login'].submit();
	}else{
		if (langue == 'fr') alert('Tous les champs doivent être remplis.');
		else alert('All fields are required.');
	}
}

function checkLost(langue){
	var em = get('demail').value.length;
	if (em>0){
		get('filled2').value = 1;
		document.forms['lost'].submit();
	}else{
		if (langue == 'fr') alert('Merci de renseigner votre adresse e-mail.');
		else alert('Thank you to fill the email adress field.');
	}
}

function checkDis(langue,tot){
	var dismsg = get('dismsg').value.length;
	if (dismsg>0 && tot>0){
		get('filled3').value = 1;
		document.forms['discontact'].submit();
	}else{
		if (langue == 'fr') alert('Merci de remplir le champ destiné à votre message.');
		else alert('Please fill in the message field.');
	}
}

function checkModif(langue,pass){
	var nom = get('dch_nom').value.length;
	var prenom = get('dch_prenom').value.length;
	var soc = get('dch_soc').value.length;
	var fct = get('dch_fct').value.length;
	var pays = get('dch_pays').value.length;
	var ville = get('dch_ville').value.length;
	var email = get('dch_email').value.length;
	var tel = get('dch_tel').value.length;
	var mdp1 = get('dch_mdp1').value;
	var mdp2 = get('dch_mdp2').value;
	
	if (nom>0 && prenom>0 && soc>0 && fct>0 && pays>0 && ville>0 && email>0 && tel>0){
		if (pass >= 2){
			get('changedpass').value = 1;
		}
		if ((pass < 2) || ((pass >= 2) && (mdp1 == mdp2) && mdp1.length >= 6 && mdp2.length >= 6)){
			get('changing').value = 1;
			document.forms['changeinfos'].submit();
		}else{
			if (mdp1.length < 6 || mdp2.length < 6){
				if (langue == 'fr') alert('Votre mot de passe doit faire au minimum 6 caractères.');
				else alert('Your password has to be at least 6 characters long.');
			}
			else{
				if (langue == 'fr') alert('Les 2 mots de passe saisis ne sont pas identiques.');
				else alert('Passwords you typed are not the sames.');
			}
		}
	}else{
		if (langue == 'fr') alert('Merci de remplir les 8 premiers champs (changer de mot de passe est optionnel).');
		else alert('Please fill in the 8 first fields (changing password is not required).');
	}
}
function checkDevenir(langue,total){
	var nom = get('devnom').value.length;
	var prenom = get('devprenom').value.length;
	var societe = get('devsociete').value.length;
	var fonction = get('devfonction').value.length;
	var pays = get('devpays').value.length;
	var ville = get('devville').value.length;
	var email = get('devemail').value.length;
	var tel = get('devtel').value.length;
	var msg = get('devmsg').value.length;
	
	if (nom>0 && prenom>0 &&tel>0 && email>0 && msg>0 && societe>0 && fonction>0 && pays>0 && ville>0 && total>8){
		get('valide').value = 1;
		document.forms['devenirdistrib'].submit();
	}else{
		if (langue == 'fr') alert('Tous les champs doivent être remplis.');
		else alert('All fields are required.');
	}
}

function test(){
	try{alert(get('itemnum2').offsetHeight);}
	catch(e){}
}
