var val_tmp = Array();

function dei(ido)
{
	return document.getElementById(ido);
}

function select_vide(ido, champsMess)
{
	if(dei(ido).selectedIndex == -1)
	{
		dei(champsMess).innerHTML = "Veuillez sélectionner au moins une valeur";
		set_erreur(ido);//Element.scrollTo(champsMess);
		return false;
	}
	else
	{
		dei(champsMess).innerHTML = "";
		set_ok(ido);
		return true;
	}
}

function implode_checkbox(ido,taille,sep,liste)
{
	var chaine = '';
	var i;
	if(taille == 0)
	{
		liste = liste.split("|");
		for(i = 0; i < liste.length; i++)
		{
			if(dei(ido+liste[i]).checked) chaine = chaine + dei(ido+liste[i]).value + sep;
		}
	}
	else
	{
		for(i = 1; i <= taille; i++)
		{
			if(dei(ido+i).checked) chaine = chaine + dei(ido+i).value + sep;
		}
	}
	
	return chaine.substr(0,chaine.length - sep.length);
}

function mod_checkbox(ido,taille,etat)
{
	var i;
	for(i = 1; i <= taille; i++) 
		dei(ido+i).checked=etat;
}

function compte_checkbox(ido,taille)
{
	var nb = 0;
	for(i = 1; i <= taille; i++)
	{
		if(dei(ido+i).checked) nb++;
	}
	
	return nb;
}

function vide_checkbox(ido,taille)
{
	for(i = 1; i <= taille; i++)
	{
		if(dei(ido+i).checked) return false;
	}
	
	return true;
}

function sans_espaces(val)
{
	return val.replace(/\s/g,'');
}

function compareChamps(champs1, champs2, champsMess)
{
	if(dei(champs1).value != dei(champs2).value)
	{
		set_erreur(champs1, "non");
		set_erreur(champs2);
		dei(champsMess).innerHTML = "Les deux champs doivent être identiques";
		return false;
	}
	else
	{
		set_ok(champs1);
		set_ok(champs2);
		dei(champsMess).innerHTML = "";
		return true;
	}
}

function set_erreur(ido, setFocus)
{
	dei(ido).style.border = '2px solid red';
	if(typeof setFocus == 'undefined' || setFocus != "non")
	{
		dei(ido).focus();
		Element.scrollTo(ido);
	}
}
function set_ok(ido, idoMess)
{
	dei(ido).style.border = '1px solid #000';
}

function is_vide(ido, taille, idoMess)
{
	if(typeof taille == 'undefined') taille = 1;
	
	if(sans_espaces(dei(ido).value).length < taille)
	{
		set_erreur(ido);
		if(typeof idoMess != 'undefined') dei(idoMess).innerHTML = "Nombre insuffisant de caractères";
		return false;
	}
	else
	{
		set_ok(ido);
		if(typeof idoMess != 'undefined') dei(idoMess).innerHTML = "";
		return true;
	}
	
}

function is_checked(ido, ido2)
{
	if(val_tmp["init_" + ido] != "oui")
	{
		val_tmp["init_" + ido] = "oui";
		val_tmp["etat_" + ido] = "";
		val_tmp["class_" + ido] = dei(ido2).className;
	}
	
	if(!dei(ido).checked)
	{
		val_tmp["etat_" + ido] = "pas ok";
		dei(ido2).className = "mess_erreur";
		dei(ido).focus();
		return false;
	}
	else
	{
		if(val_tmp["etat_" + ido] == "pas ok") dei(ido2).className = val_tmp["class_" + ido];
	}
	return true;
}

function isNum(ido) {
	var obj = dei(ido);
	obj.value.replace(/\s/g,'');
	var filter = /^[0-9]+$/;
  if (filter.test(obj.value))
  	return true;
  else
  	return false;
}

function in_array(tablo, elemt)
{
	var taille = tablo.length;
	var ind;
	
	for(ind = 0; ind < taille; ind++)
	{
		if(tablo[ind] == elemt)
			return true;
	}
	
	return false;	
}

function testEtat(ancien_etat)
{
	if(!dei('div_commentaire') || !dei('select_etat')) return false;
	
	var nouveau_etat = dei('select_etat').value;
	
	if(ancien_etat != "attente" && nouveau_etat == 'attente') dei('div_commentaire').style.display = '';
	else dei('div_commentaire').style.display = 'none';
}

function joue(ido, mess)
{
	affiche(ido, mess);
	setTimeout("cache('" + ido + "');", 3000);
}

function affiche(ido, mess)
{
	dei(ido).innerHTML = mess;
	Effect.Appear(ido);
}

function cache(ido)
{
	Effect.Fade(ido);
}

function sha1Hash(msg)
{
    // constants [§4.2.1]
    var K = [0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6];


    // PREPROCESSING 
 
    msg += String.fromCharCode(0x80); // add trailing '1' bit to string [§5.1.1]

    // convert string msg into 512-bit/16-integer blocks arrays of ints [§5.2.1]
    var l = Math.ceil(msg.length/4) + 2;  // long enough to contain msg plus 2-word length
    var N = Math.ceil(l/16);              // in N 16-int blocks
    var M = new Array(N);
    for (var i=0; i<N; i++) {
        M[i] = new Array(16);
        for (var j=0; j<16; j++) {  // encode 4 chars per integer, big-endian encoding
            M[i][j] = (msg.charCodeAt(i*64+j*4)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16) | 
                      (msg.charCodeAt(i*64+j*4+2)<<8) | (msg.charCodeAt(i*64+j*4+3));
        }
    }
    // add length (in bits) into final pair of 32-bit integers (big-endian) [5.1.1]
    // note: most significant word would be ((len-1)*8 >>> 32, but since JS converts
    // bitwise-op args to 32 bits, we need to simulate this by arithmetic operators
    M[N-1][14] = ((msg.length-1)*8) / Math.pow(2, 32); M[N-1][14] = Math.floor(M[N-1][14])
    M[N-1][15] = ((msg.length-1)*8) & 0xffffffff;

    // set initial hash value [§5.3.1]
    var H0 = 0x67452301;
    var H1 = 0xefcdab89;
    var H2 = 0x98badcfe;
    var H3 = 0x10325476;
    var H4 = 0xc3d2e1f0;

    // HASH COMPUTATION [§6.1.2]

    var W = new Array(80); var a, b, c, d, e;
    for (var i=0; i<N; i++) {

        // 1 - prepare message schedule 'W'
        for (var t=0;  t<16; t++) W[t] = M[i][t];
        for (var t=16; t<80; t++) W[t] = ROTL(W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16], 1);

        // 2 - initialise five working variables a, b, c, d, e with previous hash value
        a = H0; b = H1; c = H2; d = H3; e = H4;

        // 3 - main loop
        for (var t=0; t<80; t++) {
            var s = Math.floor(t/20); // seq for blocks of 'f' functions and 'K' constants
            var T = (ROTL(a,5) + f(s,b,c,d) + e + K[s] + W[t]) & 0xffffffff;
            e = d;
            d = c;
            c = ROTL(b, 30);
            b = a;
            a = T;
        }

        // 4 - compute the new intermediate hash value
        H0 = (H0+a) & 0xffffffff;  // note 'addition modulo 2^32'
        H1 = (H1+b) & 0xffffffff; 
        H2 = (H2+c) & 0xffffffff; 
        H3 = (H3+d) & 0xffffffff; 
        H4 = (H4+e) & 0xffffffff;
    }

    return H0.toHexStr() + H1.toHexStr() + H2.toHexStr() + H3.toHexStr() + H4.toHexStr();
}

//
// function 'f' [§4.1.1]
//
function f(s, x, y, z) 
{
    switch (s) {
    case 0: return (x & y) ^ (~x & z);           // Ch()
    case 1: return x ^ y ^ z;                    // Parity()
    case 2: return (x & y) ^ (x & z) ^ (y & z);  // Maj()
    case 3: return x ^ y ^ z;                    // Parity()
    }
}

//
// rotate left (circular left shift) value x by n positions [§3.2.5]
//
function ROTL(x, n)
{
    return (x<<n) | (x>>>(32-n));
}

//
// extend Number class with a tailored hex-string method 
//   (note toString(16) is implementation-dependant, and 
//   in IE returns signed numbers when used on full words)
//
Number.prototype.toHexStr = function()
{
    var s="", v;
    for (var i=7; i>=0; i--) { v = (this>>>(i*4)) & 0xf; s += v.toString(16); }
    return s;
}

  	 

