/**
*  Secure Hash Algorithm (SHA256)
**/

function SHA256(s){
 
	var chrsz   = 8;
	var hexcase = 0;
 
	function safe_add (x, y) {
		var lsw = (x & 0xFFFF) + (y & 0xFFFF);
		var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
		return (msw << 16) | (lsw & 0xFFFF);
	}
 
	function S (X, n) { return ( X >>> n ) | (X << (32 - n)); }
	function R (X, n) { return ( X >>> n ); }
	function Ch(x, y, z) { return ((x & y) ^ ((~x) & z)); }
	function Maj(x, y, z) { return ((x & y) ^ (x & z) ^ (y & z)); }
	function Sigma0256(x) { return (S(x, 2) ^ S(x, 13) ^ S(x, 22)); }
	function Sigma1256(x) { return (S(x, 6) ^ S(x, 11) ^ S(x, 25)); }
	function Gamma0256(x) { return (S(x, 7) ^ S(x, 18) ^ R(x, 3)); }
	function Gamma1256(x) { return (S(x, 17) ^ S(x, 19) ^ R(x, 10)); }
 
	function core_sha256 (m, l) {
		var K = new Array(0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0xFC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x6CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2);
		var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);
		var W = new Array(64);
		var a, b, c, d, e, f, g, h, i, j;
		var T1, T2;
 
		m[l >> 5] |= 0x80 << (24 - l % 32);
		m[((l + 64 >> 9) << 4) + 15] = l;
 
		for ( var i = 0; i<m.length; i+=16 ) {
			a = HASH[0];
			b = HASH[1];
			c = HASH[2];
			d = HASH[3];
			e = HASH[4];
			f = HASH[5];
			g = HASH[6];
			h = HASH[7];
 
			for ( var j = 0; j<64; j++) {
				if (j < 16) W[j] = m[j + i];
				else W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
 
				T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
				T2 = safe_add(Sigma0256(a), Maj(a, b, c));
 
				h = g;
				g = f;
				f = e;
				e = safe_add(d, T1);
				d = c;
				c = b;
				b = a;
				a = safe_add(T1, T2);
			}
 
			HASH[0] = safe_add(a, HASH[0]);
			HASH[1] = safe_add(b, HASH[1]);
			HASH[2] = safe_add(c, HASH[2]);
			HASH[3] = safe_add(d, HASH[3]);
			HASH[4] = safe_add(e, HASH[4]);
			HASH[5] = safe_add(f, HASH[5]);
			HASH[6] = safe_add(g, HASH[6]);
			HASH[7] = safe_add(h, HASH[7]);
		}
		return HASH;
	}
 
	function str2binb (str) {
		var bin = Array();
		var mask = (1 << chrsz) - 1;
		for(var i = 0; i < str.length * chrsz; i += chrsz) {
			bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (24 - i%32);
		}
		return bin;
	}
 
	function Utf8Encode(string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	}
 
	function binb2hex (binarray) {
		var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
		var str = "";
		for(var i = 0; i < binarray.length * 4; i++) {
			str += hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8+4)) & 0xF) +
			hex_tab.charAt((binarray[i>>2] >> ((3 - i%4)*8  )) & 0xF);
		}
		return str;
	}
 
	s = Utf8Encode(s);
	return binb2hex(core_sha256(str2binb(s), s.length * chrsz));
 
}




//************ouverture Fenetre Modal renseignement inscription
function showBox(){
tb_show('Veuillez remplir le formulaire ci-dessous pour vous inscrire', 'ajout_fiche.php?&height=410&width=800', false);
//$('#TB_closeWindowButton').css({'visibility':'hidden'});
$('#TB_title').css({'background-color':'#a0b64b','font':' italic bold 18px arial, sans-serif'});
$('#TB_window').css({'background-color':'#5b4e4e'});
}

//********************************************CRYPTO lOGIN****************				

function submit_login()
{
var x = this;
var result = $("#retour_login",this);
var login_user=$("#login").attr("value");
var crypto=SHA256(login_user);
$("#retour_crypto").val(crypto);
var log_saisie = $("#login").val();
$("#login").replaceWith("<input type='password' name='login' id='login' AUTOCOMPLETE='OFF' value='"+log_saisie+"'>");
$("#login").css("background-color","#808080");
$("#login").attr("disabled", "disabled");
$.ajax({
		type: "POST",
		url: "gds.php",
		data: "login_user="+crypto,
		success: function(data){
		$("#retour_login").val(data);}
	  });

}



//*************************************CRYPTO PASSWORD********************



function submit_password()
{
var x = this;
var result = $("#crypto_pass",this);
var pass_user=$("#password").attr("value");
var gds=$("#retour_login").attr("value");
var crypto=SHA256(pass_user);
var crypto_pass=crypto+gds;
$("#crypto_pass").val(crypto_pass);
$("#password").val('');
var retour_crypto=$("#retour_crypto").val();

$.post("verif_log.php", {retour_crypto:""+retour_crypto+"",crypto_pass: ""+crypto_pass+""}, function(data){
if(data.length >0)
	{
	$('#login').val('');
	$('#password').val('');
	var cookie = document.cookie;
	var cookie_pos = cookie.indexOf("USER");
	if(cookie_pos!=-1)
		{
		var x = eval(document.cookie.charAt(49) ) + 1;
		if (x==4)  {  x=1 ; }
		setCookie(x);
		}
		else
		{
		setCookie(1) ; 
		}
	}
	else
	{// log ok

	$.ajax({
		type: "POST",
		url: "modif_password.php",
		data: "pass_user="+crypto+"&log_user="+retour_crypto,
		success: function(data){// si data vide no problemo sinon pb

		if(!data)
			{
			var redir = location.href.replace("http://","https://");
			redir = redir.replace("index.php","accueil.php");
			redir = redir.replace("#t1","");
			redir = redir.replace("#t2","");
			redir = redir.replace("#t3","");
			redir = redir.replace("index.htm","accueil.php");
			redir = redir.replace("index.html","accueil.php");
			redir = redir + "accueil.php";
			redir = redir.replace("accueil.phpaccueil.php","accueil.php");
			location.href=redir;
			}
			else
			{
			tb_show('Modification du Mot de Passe', 'envoi_modif_pass.php?&height=300&width=400', false);
			$('#TB_title').css({'background-color':'#a0b64b','font':' italic bold 18px arial, sans-serif','color' :'#ffffff' });
			$('#TB_window').css({'background-color':'#5b4e4e'});
			}
		}
		});
	}
}); 

}

function submit_reset(){
	$('#login').attr("disabled",false);
	$("#login").replaceWith("<input type='text' name='login' id='login' AUTOCOMPLETE='OFF' onblur='javascript:submit_login();'>");
	$("#login").css("background-color","#FFFFFF");
}

function setCookie(x)  {
	now=new Date();
	now.setTime(now.getTime() + 1000 * 60 * 1);
	document.cookie="USER=" + x + ";expires="+now.toGMTString();
	var y=3-x;
	if(y!=0){
	alert('corrigez votre login ou mot de passe, il vous reste  '+y+'  essai')
	//tb_show('vous rest '+y+' change', 'verif_log.php?&height=0&width=200', false);
		//tb_init('corriger votre login ou mot de passe    vous rest '+y+' change');
	}
	else{
	alert("vous avez utilise vos 3 essais,bye,bye");
		//tb_show("vous n'avez pas de change,bye,bye", 'verif_log.php?&height=0&width=200', false);
	$("#authentification").hide();
	}
	}



//*********VERIF***************AJOUT IDENTIFIANT***********************


function verif_user(username) {//alert(username);
	$.post("verif_username.php", {username: ""+username+""}, function(data){//alert(data);
			if(data.length >0)
				{
				$("#username_notice").css("visibility","visible");
				$('#username_notice').html(data);
				$("#message_identifiant").css("visibility","visible");
				$("#ajout_login").css("border","1px solid #ff0033");
				$("#ajout_login").val("");
				$("#ajout_login").focus();
				$("#crypto_login").val('');
				}
		}); 
}

function clean_id(champ1) {//chiffre lettre manjuscule minuscule c'est tout

var champ = champ1;
var ok = 0;
var rego1 = "[a-z]-|-[0-9]-|-[A-Z]";//-|-[A-Z]-|-[0-9]
var rego = rego1.split('-|-');
var taille = rego.length;
var taillechamp = champ.length;
var okcible = taillechamp;
	
for (i = 0; i < taillechamp; i++) {
     for (j = 0; j < taille; j++) {
	testouille = rego[j];
	 Resultat = champ.charAt(i); 
     if (Resultat.match(testouille)) {
	ok++;
	}
      }
    }

if (okcible == ok) {return champ;}
else {alert("caractÃ¨res non valides dans identifiant !");return "";}

}


function ajout_identifiant()
{

var login_user= clean_id($("#ajout_login").attr("value"));
$("#ajout_login").val(login_user);

if(login_user == "")
	{
	$("#message_identifiant").css("visibility","visible");
	$("#ajout_login").css("border","1px solid #ff0033");
	$("#ajout_login").focus();
	$("#crypto_login").val('');
	$("#ajout_password").val('');
	$("#crypto_password").val('');
	return false;
	}
	else
	{
	$("#message_identifiant").css("visibility","hidden");
	$("#username_notice").css("visibility","hidden");
	$("#ajout_login").css("border","");
	verif_user(login_user);
	return true;
	}
}


//********VERIF********************AJOUT PASSWORD************************


function verif_ajout_password()
{

var x = this;
var login_user=$("#ajout_login").attr("value");
var password_user=$("#ajout_password").attr("value");
if(password_user == "")
	{
	$("#message_password").css("visibility","visible");
	$("#ajout_password").css("border","1px solid #ff0033");
	$("#ajout_password").focus();
	$("#crypto_password").val('');
	return false;
	}
	else
	{
	$("#message_password").css("visibility","hidden");
	$("#ajout_password").css("border","");
	return true;
	}
}


//*******************CRYPTO AJOUT IDENTIFIANT & PASSWORD**********



function verif_inscription_log_pass()
{

if(ajout_identifiant()  && verif_ajout_password())
	{
	var ajout_login=$("#ajout_login").val();
	var pass_user=$("#ajout_password").val();
	var crypto_login=SHA256(ajout_login);
	var crypto_password=SHA256(pass_user);
	$("#ajout_password").css("border","");
	$("#crypto_login").val(crypto_login);
	$("#crypto_password").val(crypto_password);
	$("#ajout_password").val('');
	$("#ajout_password").attr("disabled", "disabled");
	
	if (crypto_password != "" && ajout_login != "" && pass_user != "" && crypto_login != "")
	{
	$.ajax({
		type: "POST",
		url: "/log_pass_tmp.php", // enleve le s http://www.compta247.fr/rek
		data: "login_user="+ajout_login+"/"+pass_user +"/" +crypto_login+ "/" +crypto_password,
		success: function(data){if (data != "") {showBox();}else {alert("problème identifiant / mot de passe, recommencez");}}
	});
//	showBox();
	}else {alert("problème identifiant / mot de passe, recommencez");}
}
}




//****************************TEST SECURITE PASSWORD**************************************



 function test_securite_pass(){
	 password = $("#ajout_password").attr("value");
	 passwordlow = password.toLowerCase();
	 majuscule = false;
	 	
	 //*********Verif. Majuscules
	 if(password != passwordlow)
	 {
	 majuscule = true;
	 }

	 taille = password.length;
	 numerique = false;
	 
	 //*********Verif. Chiffres
	 for(i=0;i<taille-1;i++)
	{
	caractere = password.substring(i,i+1);
	if(!isNaN(caractere))
	{
	numerique = true;
	}
	}

	if((majuscule==false && numerique==false))
	{
	$("#test_password").val("SECURITE FAIBLE");
	$("#test_password").css("background-color","#ff0033");
	$("#test_password").css("display","block");

	}
	if((majuscule || numerique) && taille<='6')
	 {
	 $("#test_password").val("SECURITE MOYENNE");
	$("#test_password").css("background-color","#f48900");
	$("#test_password").css("display","block");
	 }
	if(majuscule && numerique && taille>'6')
	 {
	$("#test_password").val("SECURITE ELEVEE");
	$("#test_password").css("background-color","#00ff00");
	$("#test_password").css("display","block");
	}
	 
 }
 
 function test_securite_pass_modif(){
	$("#message_password_modif").css("visibility","hidden");
	password = $("#modif_password").attr("value");
	 passwordlow = password.toLowerCase();
	 majuscule = false;
	  //*********Verif. Majuscules
	 if(password != passwordlow)
	 {
	 majuscule = true;
	 }
	 taille = password.length;
	 numerique = false;
	 //*********Verif. Chiffres
	 for(i=0;i<taille-1;i++)
	{
	caractere = password.substring(i,i+1);
	if(!isNaN(caractere))
	{
	numerique = true;
	}
	}
	if((majuscule==false && numerique==false))
	{
	$("#test_password_modif").val("FAIBLE");
	$("#test_password_modif").css("background-color","#ff0033");
	$("#test_password_modif").css("visibility","visible");
	}
	if((majuscule || numerique) && taille<='6')
	 {
	 $("#test_password_modif").val("MOYEN");
	$("#test_password_modif").css("background-color","#00ff00");
	$("#test_password_modif").css("visibility","visible");
	 }
	if(majuscule && numerique && taille>'6')
	 {
	$("#test_password_modif").val("ELEVEE");
	$("#test_password_modif").css("background-color","#33ffff");
	$("#test_password_modif").css("visibility","visible");
	}
 }


//***********************************************************OUBLI PASSWORD*********************************************

function OpenBox(){
tb_show('Mail Password oubliÃ©..', 'envoi_mail_pass.php?&height=300&width=400', false);
//$('#TB_closeWindowButton').css({'visibility':'hidden'});
$('#TB_title').css({'background-color':'#a0b64b','font':' italic bold 18px arial, sans-serif','color' :'#ffffff' });
$('#TB_window').css({'background-color':'#5b4e4e'});
}

function affich_bouton_valider_id(){
if($("#bouton_login:hidden"))
	{
	$("#bouton_login").css("visibility","visible");
	}
if($("#ligne_mail:visible"))
	{
	$("#ligne_mail").css("visibility","hidden");
	}
}

function verif_identifiant_mail(){
var identif = $("#saisie_identif").attr("value");
if(identif !=="" && identif !=="essai")
	{
	identif = SHA256(identif);
	$.ajax({
			type: "POST",
			url: "identif_mail.php",
			data: "login_user="+identif,
			success: function(data){
			if(data != "")
				{
				$("#bouton_login").css("visibility","hidden");
				$("#ligne_mail2").css("visibility","visible");
				$("#ligne_mail3").css("visibility","visible");
				}
				else
				{
				$("#ligne_mail").css("visibility","visible");
				$("#bouton_login").css("visibility","hidden");
				}
			}
		  });
	}
}

function save_modif_password(){
var password = $("#modif_password").attr("value");
if(password !=="")
	{
	var password_hash = SHA256(password);
	$.ajax({
			type: "POST",
			url: "/rek/REQ_save_modif_password.php",
			data: "crypto_pass="+password_hash,
			success: function(data){
			location.href='accueil.php';
			}
		});
	}
	else
	{
	$("#message_password_modif").css("visibility","visible");	
	}

}
//********************ENVOI MAIL*****************


function envoi_mail()
{
var login = $("#saisie_identif").attr("value");
login = login+"/"+SHA256(login);
	$.ajax({
			type: "POST",
			url: "envoi_identifiant.php",
			data: "variable="+login,
			success: function(data){
			
			}
		  });
tb_remove();	
}
