﻿/* Vérifie les informations de connexion et redirige l'utilisateur vers le dashboard */
function connection() {
    var email=document.getElementById('email').value;
    var password=MD5(document.getElementById('password').value);
    document.getElementById('form').innerHTML='Vérification en cours...\n<br>\n<br>\n<img src="./ressources/img/login/login_loader_001.gif">\n';
    var xhr = getXMLHttpRequest();
    if(xhr && xhr.readyState != 0) {
        xhr.abort(); 
    }
    xhr.onreadystatechange=function () {
        if(xhr.readyState == 4 && (xhr.status == 200 || xhr.status == 0)) {
            if(xhr.responseText!='<form id=\"login_form\" action=\"javascript:connection();\">\n<input id=\"email\" name=\"email\" type=\"text\" onfocus=\"emailIn()\" onBlur=\"emailOut()\" maxlength=\"150\" size=\"30\" value=\"Adresse e-mail\"/>\n<br>\n<input id=\"password\" name=\"password\" type=\"password\" onfocus=\"passwordIn()\" onBlur=\"passwordOut()\" maxlength=\"30\" size=\"30\" value=\"Password\"/>\n<br>\n<input id=\"button\" name=\"connection\" type=\"submit\" value=\"Connexion\"/>\n<br>\n</form>\n<br>\n<b>E-mail ou mot de passe erroné</b>\n') {
            	document.getElementById('form').innerHTML=xhr.responseText;
                setTimeout("window.location.href='index.php'", 1000);
            } else {
                document.getElementById('form').innerHTML=xhr.responseText;
            }
        }
    }
    xhr.open("POST", "./ressources/scripts/login/login_check_user.php", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send("email="+email+"&password="+password);
}
/* Vide le champ e-mail au focus */
function emailIn() {
	document.getElementById('email').value='';
}
/* Remplie champ e-mail au blur et si champ est vide */
function emailOut() {
	if(document.getElementById('email').value=='') {
		document.getElementById('email').value='Adresse e-mail';
	}
}
/* Vide le champ password au focus */
function passwordIn() {
	document.getElementById('password').value='';
}
/* Remplie champ password au blur et si champ est vide */
function passwordOut() {
	if(document.getElementById('password').value=='') {
		document.getElementById('password').value='password';
	}
}
