/**
 * @author julien
 */
var xmlHttp = false;
init();

function switchheaderlogin(divlogin) {
	document.getElementById('headerloginmessage').style.display = 'none';
	document.getElementById('headerloginform').style.display = 'none';
	document.getElementById('headerloginerror').style.display = 'none';
	document.getElementById(divlogin).style.display = 'block';
	return false;
}

function init() {
  if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
    xmlHttp = new XMLHttpRequest();
  }
}

function login()
 {
  if (xmlHttp.readyState!=0) {
    xmlHttp.onreadystatechange = function() {};
    xmlHttp.abort();
    xmlHttp = false;
    init();
  }
  
  xmlHttp.onreadystatechange = updatePage;
  
  xmlHttp.open("POST", "/mon-compte/login-xml.php",true);
  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  xmlHttp.send("user_login="+document.getElementById('user_login').value+"&user_passwd="+document.getElementById('user_passwd').value);
  
  return true;
}

function updatePage() {
  if(xmlHttp.readyState == 4) {
    response = xmlHttp.responseText;
    response = response+'';
    if (response == 'bad_passwd') {
		document.getElementById("headerloginerror").innerHTML = 'Mauvais mot de passe';
		switchheaderlogin("headerloginerror");
		document.getElementById('headerloginmessage').style.display = 'block';
	} else if (response == 'bad_login') {
		document.getElementById("headerloginerror").innerHTML = 'Mauvais Login';
		switchheaderlogin("headerloginerror");
		document.getElementById('headerloginmessage').style.display = 'block';
    } else {
		document.getElementById('headerloginmessage').innerHTML = response;
		switchheaderlogin("headerloginmessage");
    }
  }
}