﻿function init() {
	// on recupere chaque champ a verifier
	var username = document.getElementById('username');
	var password = document.getElementById('password');
	var email = document.getElementById('email');
	var codepostal = document.getElementById('codepostal');
	// initialise l'appel aux fonctions pour chaque champ
	// il y a des différences de traitement DOM entre IE et Firefox, notamment pour l'affectation d'évenement
	// pour IE
	if (window.attachEvent) {
		username.onkeyup = function() { verifUsername(username); };
		password.onkeyup = function() { verifPassword(password); };
		email.onkeyup = function() { verifEmail(email); };
		//email.onmouseout=function() { verifEmail(email); };
		codepostal.onkeyup = function() { validCodePostal(codepostal); };
	}
	// pour Firefox
	else {
		username.setAttribute('onKeyUp', 'verifUsername(username)');
		password.setAttribute('onKeyUp', 'verifPassword(password)');
		email.setAttribute('onKeyUp', 'verifEmail(email)');
		//email.setAttribute('onmouseout','verifEmail(email)');
		codepostal.setAttribute('onKeyUp', 'validCodePostal(codepostal)');
	}
}
 
function verifUsername(username) {
	var XHR = new XHRConnection();
	XHR.appendData("username", username.value);
	XHR.sendAndLoad("verifUsername.php", "POST", afficheDispo);	
}
 
function verifPassword(password) {
	motDePasse = password.value;
	password_alert = document.getElementById('password_alert');
	// suppression du texte existant
	while(password_alert.firstChild != null) {
		password_alert.removeChild(password_alert.firstChild);
	}
	// creation du message suivant le cas
	if(motDePasse.length < 6) {
		var texte = document.createTextNode("Niveau faible");
	}
	if(motDePasse.length >= 6 && motDePasse.length < 8) {
		var texte = document.createTextNode("Niveau correct");
	}
	if(motDePasse.length >= 8) {
		var texte = document.createTextNode("Niveau élevé");
	}
	password_alert.appendChild(texte);
}
 
function verifEmail(email) {
	adresse = email.value;
	email_alert = document.getElementById('email_alert');
	// suppression du texte existant
	while(email_alert.firstChild != null) {
		email_alert.removeChild(email_alert.firstChild);
	}
	// creation du message suivant le cas
	if (!checkEmail(adresse)) {
		var texte = document.createTextNode("Adresse incorrecte");
		email_alert.appendChild(texte);
	} else {
		var texte = document.createTextNode("Adresse correcte");
		email_alert.appendChild(texte);
	}
}
 
function checkEmail(email) {
	var arobase = email.indexOf("@");
	var point = email.lastIndexOf(".");
	if((arobase < 3) || (point + 3 > email.length) || (point < arobase+3)) {
		return false;
	}
	return true;
}

 function validCodePostal(codepostal) {
	code = codepostal.value;
	codepostal_alert = document.getElementById('codepostal_alert');
	// suppression du texte existant
	while(codepostal_alert.firstChild != null) {
		codepostal_alert.removeChild(codepostal_alert.firstChild);
	}
	// creation du message suivant le cas
	if(code >10000 && code<99999){
		var texte = document.createTextNode("Code postal correcte");
		codepostal_alert.appendChild(texte);
	}
	else  {
		var texte = document.createTextNode("Code postal incorrecte");
		codepostal_alert.appendChild(texte);
	}	
} 
function afficheDispo(obj) {
	username_alert = document.getElementById('username_alert');
	// suppression du texte existant
	while(username_alert.firstChild != null) {
		username_alert.removeChild(username_alert.firstChild);
	}	
	// Construction des noeuds
	var tabResult = obj.responseXML.getElementsByTagName('resultat');
	var resultat = tabResult.item(0);
	var dispo = resultat.getAttribute('dispo');	
	// creation du message suivant le cas
	if (dispo == "true") {
		var texte = document.createTextNode("Le nom d'utilisateur est disponible");
		username_alert.appendChild(texte);
	} else {
		if (dispo == "false") {
			var texte = document.createTextNode("Le nom d'utilisateur n'est pas disponible");
			username_alert.appendChild(texte);
		}
	}
}
	function popup(url, name, x, y, top, left){
window.open(url,name,'location=no,toolbar=no,directories=no,menubar=no,resizable=no,scrollbars=no,status=no,width='+x+',height='+y+'screenY='+top+',screenX='+left);
}

function popup_v(url, name, x, y, top, left){
var text="'location=no,toolbar=no,directories=no,menubar=no,resizable=no,scrollbars=no,status=no,width="+x+",height="+y+"screenY=0"+",screenX="+left+"'";
window.open(url,name,text);
}
function popupstatus(url, name, x, y, top, left){
window.open(url,name,'location=no,toolbar=no,directories=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,width='+x+',height='+y+'screenY='+top+',screenX='+left);
}
function openNewWindow(){
window.open('../liveboard/index.php','Liveprice','fullscreen=yes, toolbar=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=no,minimizable=no, dialog=no'); 
}
function openNewWindowVideo(src,width,height){
window.open('../liveboard/index.php','Liveprice','fullscreen=yes, toolbar=no,directories=no,status=no,menubar=no, scrollbars=yes,resizable=no,minimizable=no, dialog=no'); 
}
