function LoginBox(idElement, collapsed){
    this.conteneur = new Box(idElement, collapsed);
    this.cadreBoutons = Element.getChildElements(this.conteneur.body, 'DIV');
    boutons = Element.getChildElements(this.cadreBoutons[1], 'SPAN');
    this.champs = Element.getChildElements(this.cadreBoutons[0], 'INPUT');
    this.boutonValider = boutons[0];
    this.boutonAnnuler = boutons[1];
    this.tampon = this.boutonValider.innerHTML;
    this.setComportementValider(true);
    this.setComportementAnnuler(false);
}

LoginBox.prototype = {
    iconLoadURL: "medias/standard/images/load.gif",

    setComportementAnnuler: function(etat){
	var courant = this;
	if(etat){
	    this.boutonAnnuler.style.cursor = "pointer";
	    this.boutonAnnuler.onclick = function (){
		courant.annulerLogin();
	    };
	    this.boutonValider.onclick = function (){};
	} else {
	    this.boutonAnnuler.style.cursor = "";
	    this.boutonAnnuler.onclick = function (){};
	};
    },

    setComportementValider: function(etat){
	var courant = this;
	if(etat){
	    this.boutonValider.style.cursor = "pointer";
	    this.boutonValider.onclick = function (){
		if (courant.validerLogin()){
		    courant.connection();
		};
	    };
	} else {
	    this.requete = null;
	    this.boutonValider.style.cursor = "";
	    this.boutonValider.onclick = function (){};
	};
    },

    validerLogin: function(){
	if(document.getElementById("messErrLogin")) this.cadreBoutons[1].removeChild(this.message);
	if(this.champs[0].value && this.champs[1].value){
	    this.login = this.champs[0].value;
	    this.pass = this.champs[1].value;
	    return true
	} else {
	    alert("info manquante !");
	    return false;
	};
    },

    annulerLogin: function(){
	if(this.requete != null) this.requete.abort();
	this.requete = null;
	this.boutonValider.innerHTML = this.tampon;
	this.setComportementValider(true);
	this.setComportementAnnuler(false);
    },

    connection: function(){
	this.setComportementAnnuler(true);
	this.url = "idElement=login&type=Widgets&login="+this.login+"&pass="+this.pass;
	this.requete = this.recupRequete();
	this.reponse = '';
	if(this.requete != null){
	    try {
		this.requete.open("POST", "engine.php", true);
	        this.requete.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	        var courant = this;
	        this.requete.onreadystatechange = function(){
	    	    if(courant.requete.readyState == 4 && courant.requete.status == 200){
			courant.extradeResult();
		    };
		};
		this.requete.send(this.url);
	        this.enAttente();
	    } catch (exc) {
	        alert('pas bon!');
	    };
	};
    },

    recupRequete: function(){
	var result = this.requete;
	if(result == null){
	    if(window.XMLHttpRequest) {
		result = new XMLHttpRequest();
	    } else if (window.ActiveXObject) {
		result = new ActiveXObject("Microsoft.XMLHTTP");
	    };
	}
	return result;
    },
    
    enAttente: function(){
	this.cadre = document.getElementById('login');
	this.iconWait = document.createElement('img');
	this.iconWait.src = this.iconLoadURL;
	this.iconWait.width = 29;
	this.iconWait.height = 10;
	
	this.tampon = this.boutonValider.innerHTML;
	this.boutonValider.innerHTML = "";
	this.boutonValider.appendChild(this.iconWait);
    },
    
    extradeResult: function(){
	this.reponse = this.requete.responseText;
	if(/ERR_(.*)/.test(this.reponse)){
	    this.boutonValider.innerHTML = this.tampon;
	    this.setComportementValider(true);
	    this.setComportementAnnuler(false);

	    this.message = document.createElement('div');
	    this.message.id = "messErrLogin";
	    this.message.innerHTML = this.reponse.slice(4);
	    this.cadreBoutons[1].appendChild(this.message);
	} else {
	    this.conteneur.body.innerHTML = this.reponse;
	    document.location = ".";
	};
    }
}

function DeconnectBox(idElement, collapsed){
    this.conteneur = new Box(idElement, collapsed);
    this.cadreBoutons = Element.getChildElements(this.conteneur.body, 'DIV');
    boutons = Element.getChildElements(this.cadreBoutons[0], 'SPAN');
    this.boutonDeconnecter = boutons[0];
    this.boutonDeconnecter.style.cursor = "pointer";
    this.setComportementDeconnecter(true);
}

DeconnectBox.prototype = {
    iconLoadURL: "medias/standard/images/load.gif",

    setComportementDeconnecter: function(){
	var courant = this;
	this.boutonDeconnecter.onclick = function (){
	    courant.deconnecte();
	};
    },
    
    deconnecte: function(){
	if(confirm('sur ?')){
	    this.url = "idElement=login&type=Widgets&deconnection=true";
	    this.requete = this.recupRequete();
	    this.reponse = '';
	    if(this.requete != null){
		try {
		    this.requete.open("POST", "engine.php", true);
	    	    this.requete.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    	    var courant = this;
	    	    this.requete.onreadystatechange = function(){
	    		if(courant.requete.readyState == 4 && courant.requete.status == 200){
			    courant.reponse = courant.requete.responseText;
			    document.location = ".";
			};
		    };
		    this.requete.send(this.url);
	    	this.enAttente();
		} catch (exc) {
	    	    alert('pas bon!');
		};
	    };
	    
	};
    },

    recupRequete: function(){
	var result = this.requete;
	if(result == null){
	    if(window.XMLHttpRequest) {
		result = new XMLHttpRequest();
	    } else if (window.ActiveXObject) {
		result = new ActiveXObject("Microsoft.XMLHTTP");
	    };
	}
	return result;
    },
    
    enAttente: function(){
	this.cadre = document.getElementById('login');
	this.iconWait = document.createElement('img');
	this.iconWait.src = this.iconLoadURL;
	this.iconWait.width = 29;
	this.iconWait.height = 10;
	
	this.boutonDeconnecter.innerHTML = "";
	this.boutonDeconnecter.appendChild(this.iconWait);
    }
}

