function getPageSize(){
	if (typeof document.body.offsetWidth != 'undefined'){
		position = [document.body.offsetWidth, document.body.offsetHeight];
	
	}else if(typeof document.documentElement.clientWidth != 'undefined'){
		position = [document.documentElement.clientWidth, document.documentElement.clientHeight];

	} // end if
	
	return position;
	
} // end function getPageSize

// Placement des éléments flottants lors de l'ouverture ou du redimensionnement de la page

function moveFloatElements(){
	// espace login
	var bodyWidth = getPageSize()[0]; // largeur de la page scrollbars comprises
	var loginPositionX = 800; // on suppose que le point 0 en X correspond au à la position X du contenu
	var contentWidth = 1000; // largeur du contenu fixe du site
	var pageHeight = document.documentElement.clientHeight;
	
	window.document.getElementById("espaceLogin").style.left = (bodyWidth-contentWidth)/2 + loginPositionX - 10 + "px";
	
	// barre réseaux sociaux
	var socialNetworkHeight = window.document.getElementById("socialNetwork").offsetHeight/2;
	window.document.getElementById("socialNetwork").style.top = document.documentElement.clientHeight/2 - socialNetworkHeight + "px";
	
	// pied de page
	var headerHeight = window.document.getElementById("header").offsetHeight;
	var content = window.document.getElementById("content");
	var marginBottomPage = 10;
	var espaceOccupe = headerHeight+marginBottomPage;
	var espaceLibre = pageHeight-espaceOccupe;
	var contentHeight = content.offsetHeight-marginBottomPage;
	var minHeight = 340;
	
	if(espaceLibre > contentHeight){
		contentHeight = espaceLibre;
		
	} // end if
	
	if(contentHeight < minHeight){
		contentHeight = minHeight;
		
	} // end if
	
	content.style.height = contentHeight + "px";
	
} // end function moveFloatElements

// ----------------------------------------------------------------------------------------------------------------------------
// Image de fond du site choisie au hasard

function backgroundChange(){
	var ROW_BG = 5;
	var ROW = Math.round(Math.random()*(ROW_BG-1))+1; // on prend un chiffre au hasard entre 1 et 5
	
	window.document.body.style.background = "url(ressources/img/fonds/fond" + ROW + ".jpg) no-repeat top #BC000C";
	
} // end function backgroundChange


// ----------------------------------------------------------------------------------------------------------------------------
// Retour haut de page

var MAX_Y = 10000;
var MAX_X = 10000;
var intervalScroll;

function Deplace_Doc( x_, y_){
  window.scrollTo( x_, y_);
  return(true);

} // end function

function autoScroll(){
	intervalScroll = setInterval(scroller, 20);
	
} // end function

function scroller(){
	if(getScrollingPosition()[0] > 0){
		if(typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0){
			document.documentElement.scrollTop -= 100;
		
		}else if (typeof document.body.scrollTop != 'undefined'){
			document.body.scrollTop -= 100;
		
		} // end if
	
	}else{
		clearInterval(intervalScroll);
		
	} // end if
} // end function

// ----------------------------------------------
// gestion du rollover des boutons image

function buttonUp(object){
	var tab_temp = object.src.split(".");
	var tab_length = tab_temp.length;
	var str = "";
	
	if(tab_length > 2){
		for(var i = 0; i < tab_length-2; i++){
			str += tab_temp[i] + ".";
			
		} // end for i
	} // end if
	
	var url = str + tab_temp[tab_length-2] + "_up." + tab_temp[tab_length-1];
	
	object.src = url;
	
} // end function buttonUp

function buttonNormal(object){
	var tab_temp = object.src.split(".");
	var tab_length = tab_temp.length;
	var str = "";
	
	if(tab_length > 2){
		for(var i = 0; i < tab_length-2; i++){
			str += tab_temp[i] + ".";
			
		} // end for i
	} // end if
	
	var url = str + tab_temp[tab_length-2].substr(0, tab_temp[tab_length-2].length-3) + "." + tab_temp[tab_length-1];
	
	object.src = url;
	
} // end function buttonNormal

// -----------------------------------------------------------------------------------------------------------------------------------
// Aide

function displayHelp(div){
	window.document.getElementById(div).style.display = "block";
	
} // end function

function maskHelp(div){
	window.document.getElementById(div).style.display = "none";
	
} // end function

function footerScroll(){
		var footerBasHeight = window.document.getElementById("footerBas").offsetHeight;
		window.document.getElementById("footerBas").style.top = document.documentElement.clientHeight - footerBasHeight + "px";
}

// -----------------------------------------------------------------------------------------------------------------------------------
// Vérification du formulaire d'inscription


var registerError;

function verifRegisterForm(){
	registerError = true;
	
	getErreur(window.document.getElementById("chLogin"), "tick");
	getErreur(window.document.getElementById("chMDP"), "mdp");
	getErreur(window.document.getElementById("chConfirm"), "verif_mdp");
	getErreur(window.document.getElementById("chName"), "empty");
	getErreur(window.document.getElementById("chFName"), "empty");
	getErreur(window.document.getElementById("chAdress"), "empty");
	getErreur(window.document.getElementById("chZipCode"), "empty");
	getErreur(window.document.getElementById("chCity"), "empty");
	getErreur(window.document.getElementById("chPhone"), "empty");
	getErreur(window.document.getElementById("chMail"), "tick");
	
	if(registerError == true){
		//alert("inscription ok");
		return true;
		
	}else{
		//alert("inscription pas ok");
		return false;
	
	} // end if
} // end function verifRegisterForm

// -----------------------------------------------------------------------------------------------------------------------------------
// Vérification automatique des champs du formulaire d'inscription


function getErreur(element, method){
	switch(method){
		case "empty":
			if(element.value.length == 0){
				window.document.getElementById(element.name + "_span").style.color = "#8d1322";
				window.document.getElementById(element.name + "_span").innerHTML = "Ce champ est obligatoire.";
				element.style.border = "solid 1px #8d1322";
				window.document.getElementById(element.name + "_img").src = "ressources/img/croix.png";
				registerError = false;
				
			}else{
				window.document.getElementById(element.name + "_span").className = "spanNotErrorFormulaire";
				window.document.getElementById(element.name + "_span").innerHTML = "";
				element.style.border = "solid 1px #509f44";
				window.document.getElementById(element.name + "_img").src = "ressources/img/tick.png";
				
			} // end if
			
			break;
			
		case "login":
			if(element.value.length < 6){
				window.document.getElementById(element.name + "_span").style.color = "#8d1322";
				window.document.getElementById(element.name + "_span").innerHTML = "Votre login doit contenir au moins 6 caractères.";
				element.style.border = "solid 1px #8d1322";
				window.document.getElementById(element.name + "_img").src = "ressources/img/croix.png";
				registerError = false;
				
			}else{
				new AjaxConnector("ressources/php/isLoginExist.php", ["login=" + element.value], getErreurFromLogin, ajaxError);
				
			} // end if
			
			break;
			
		case "mdp":
			if(element.value.length < 8){
				window.document.getElementById(element.name + "_span").style.color = "#8d1322";
				window.document.getElementById(element.name + "_span").innerHTML = "Votre mot de passe doit contenir au moins 8 caractères.";
				element.style.border = "solid 1px #8d1322";
				window.document.getElementById(element.name + "_img").src = "ressources/img/croix.png";
				registerError = false;
				
			}else{
				window.document.getElementById(element.name + "_span").style.color = "#509f44";
				window.document.getElementById(element.name + "_span").innerHTML = "N'oubliez pas que plus un mot de passe est complexe plus il sera difficile pour une tiers personne de le découvrir.";
				element.style.border = "solid 1px #509f44";
				window.document.getElementById(element.name + "_img").src = "ressources/img/tick.png";
				
			} // end if
			
			break;
			
		case "verif_mdp":
			if((element.value != window.document.getElementById("chMDP").value) || (element.value.length < 1)){
				window.document.getElementById(element.name + "_span").style.color = "#8d1322";
				window.document.getElementById(element.name + "_span").innerHTML = "Ce champ doit être identique à votre mot de passe.";
				element.style.border = "solid 1px #8d1322";
				window.document.getElementById(element.name + "_img").src = "ressources/img/croix.png";
				registerError = false;
				
			}else{
				window.document.getElementById(element.name + "_span").style.color = "#509f44";
				window.document.getElementById(element.name + "_span").innerHTML = "";
				element.style.border = "solid 1px #509f44";
				window.document.getElementById(element.name + "_img").src = "ressources/img/tick.png";
				
			} // end if
			
			break;	
			
		case "mail":
			if(!verifMail(element.value)){
				window.document.getElementById(element.name + "_span").style.color = "#8d1322";
				window.document.getElementById(element.name + "_span").innerHTML = "Votre e-mail n'est pas correct.";
				element.style.border = "solid 1px #8d1322";
				window.document.getElementById(element.name + "_img").src = "ressources/img/croix.png";
				registerError = false;
				
			}else{
				new AjaxConnector("ressources/php/isMailExist.php", ["mail=" + element.value], getErreurFromMail, ajaxError);
				
			} // end if
			
			break;
			
		case "tick":
			var array_url = window.document.getElementById(element.name + "_img").src.split("/");
			
			if(array_url[array_url.length-1] == "croix.png" || array_url[array_url.length-1] == "transparent.png"){
				registerError = false;
				window.document.getElementById(element.name + "_img").src = "ressources/img/croix.png";
				window.document.getElementById(element.name + "_span").innerHTML = "Ce champ est obligatoire.";
				element.style.border = "solid 1px #8d1322";
				
			}else{
				window.document.getElementById(element.name + "_span").style.color = "#509f44";
				element.style.border = "solid 1px #509f44";
				window.document.getElementById(element.name + "_img").src = "ressources/img/tick.png";
				
			} // end if
			
			break;
		
	} // end switch
} // end function


// -----------------------------------------------------------------------------------------------------------------------------------
// Fonction qui récupère une valeur true/false sur l'existence du login


function getErreurFromLogin(value){
	if(value == "true"){
		window.document.getElementById("chLogin_span").style.color = "#509f44";
		window.document.getElementById("chLogin_span").innerHTML = "";
		window.document.getElementById("chLogin").style.border = "solid 1px #509f44";
		window.document.getElementById("chLogin_img").src = "ressources/img/tick.png";
		window.document.getElementById("btnValiderInscription").disabled = "";
	
	}else{
		window.document.getElementById("chLogin_span").style.color = "#8d1322";
		window.document.getElementById("chLogin_span").innerHTML = "Ce login existe déjà.";
		window.document.getElementById("chLogin").style.border = "solid 1px #8d1322";
		window.document.getElementById("chLogin_img").src = "ressources/img/croix.png";
		window.document.getElementById("btnValiderInscription").disabled = "disabled";
		registerError = false;
		
	} // end if
} // end function getErreurFromLogin


// -----------------------------------------------------------------------------------------------------------------------------------
// Fonction qui récupère une valeur true/false sur l'existence du mail


function getErreurFromMail(value){
	if(value == "true"){
		window.document.getElementById("chMail_span").style.color = "#509f44";
		window.document.getElementById("chMail_span").innerHTML = "Un e-mail de confirmation vous sera envoyé.";
		window.document.getElementById("chMail").style.border = "solid 1px #509f44";
		window.document.getElementById("chMail_img").src = "ressources/img/tick.png";
		window.document.getElementById("btnValiderInscription").disabled = "";
	
	}else{
		window.document.getElementById("chMail_span").style.color = "#8d1322";
		window.document.getElementById("chMail_span").innerHTML = "Cet adresse e-mail existe déjà.";
		window.document.getElementById("chMail").style.border = "solid 1px #8d1322";
		window.document.getElementById("chMail_img").src = "ressources/img/croix.png";
		window.document.getElementById("btnValiderInscription").disabled = "disabled";
		registerError = false;
		
	} // end if
} // end function getErreurFromMail

// -----------------------------------------------------------------------------------------------------------------------------------
// Vérification de l'e-mail


function verifMail(mail){
  syntaxeMail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
  
  if(syntaxeMail.test(mail) == true){
	  return true;
	  
  }else{
	  return false;
	  
  } // end if
} // end function


// -----------------------------------------------------------------------------------------------------------------------------------
// Un nouvel email a été envoyé pour confirmer l'inscription : on met à jour le contenu de la div maincontent de la page newUser.php
// indiquant que le mail a été envoyé


function sendConfirmRegisterMailIsSending(data){
	window.document.getElementById("mainContent").innerHTML = data;
	
} // end function sendConfirmRegisterMailIsSending

// -----------------------------------------------------------------------------------------------------------------------------------
// Un nouvel email a été envoyé pour confirmer l'inscription : on met à jour le contenu de la div maincontent de la page newUser.php


function resizeFlashObject(_height){
	window.document.getElementById("dMain").style.height = 40+_height + "px";
	window.document.getElementById("gestionCompte").height = _height + "px";
	window.document.getElementById("content").style.height = window.document.getElementById("dMain").offsetHeight+window.document.getElementById("dBlank").offsetHeight + "px";
	moveFloatElements();
	
	window.document.body.scrollTop = 0;
	
} // end function resizeFlashObject


// -----------------------------------------------------------------------------------------------------------------------------------
// redirige vers la page index.php


function returnToIndex(){
	window.location = "index.php";
	
} // end function returnToIndex

// -----------------------------------------------------------------------------------------------------------------------------------
// Vérification du formulaire de mise à jour des informations personnelles


function verifUpdateInfosForm(){
	return false;
	
} // end function verifUpdateInfosForm

// -----------------------------------------------------------------------------------------------------------------------------------
// Erreur Ajax


function ajaxError(code, message) {
	alert("Probleme de communication avec le serveur : " + code + " - " + message);

} // end function


// -----------------------------------------------------------------------------------------------------------------------------------
// Fonction appelée quand on a rempli le formulaire d'oubli de mot de passe


function mailLoginSent(optionString) {
	document.getElementById('mainContent').innerHTML = optionString;

} // end function

// -----------------------------------------------------------------------------------------------------------------------------------
// Vérification du formulaire de récupération d'identifiants


function verifFormRecupLogin(){
	if(verifMail(window.document.getElementById("chMail").value)){
		new AjaxConnector("ressources/php/recupIdentifiants.php", ["mail=" + window.document.getElementById("chMail").value], mailLoginSent, ajaxError);
	
	}else{
		window.document.getElementById("chError").style.display = "block";
		window.document.getElementById("chMail").style.border = "solid 1px #8d1322";
		
	} // end if
} // end function

// -----------------------------------------------------------------------------------------------------------------------------------
// Restriction des caractères pour les champs de login

var typesReg = ["[0-9a-zA-Z_#@$.]", "[0-9]", "[0-9]"]


function restrictLoginInput(champ,type){
	var reg = new RegExp(typesReg[type]);
	var verif;
 
    if(!reg.test(champ.value.charAt(champ.value.length-1))){
		champ.value = champ.value.substr(0, champ.value.length-1)
		
	} // end if
} // end function

// -----------------------------------------------------------------------------------------------------------------------------------
// Interdire les charactères < > dans les formulaires


function restrictHTMLChar(){
	if (event.keyCode == 60 || event.keyCode == 62){
		event.returnValue = false;
		
	} // end if
	
	if(event.which == 60 || event.which == 62){
		return false;
		
	} // end if
} // end function


// -----------------------------------------------------------------------------------------------------------------------------------
// download factures


function downloadFactures(url){
	window.open(url, "Download");
	
} // end function downloadFactures
