$(document).ready(function(){

// Evenement sur l'email d'inscription (vérif format + unique)
//validmail();

validmailEvent();
validIdentEvent();
validpasswordEvent();
confvalidpasswordEvent();
EventUpdatePassword();

});

function validmailEvent()
{

	$("#email_page_enregistrement").ready(function(event)
	{
		validmail2();
	});


	$("#email_page_enregistrement").keyup(function(event)
	{
		validmail2();
	});

}


function validmail2()
{
	var rege = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,5})$/;
	var codehtml = '';
	if(rege.test($("#email_page_enregistrement").val()))
	{ 
		$('#email_valid').attr('value', '1');
		codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_ok' border='0'>"; 
	}
	else 
	{ 
		$('#email_valid').attr('value', '0');
		codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_nok' border='0'>"; 
	}
	$("#result_valid_mail").html(codehtml);

}

function validIdentEvent()
{

	$("#theident_page_enregistrement").ready(function(event)
	{
		validIdent();
	});


	$("#theident_page_enregistrement").keyup(function(event)
	{
		validIdent();
	});

}


function validIdent()
{
	var la_valeur_saisie = $("#theident_page_enregistrement").val();
	
	if (la_valeur_saisie)
	{
		var ident_ok = 1;
	
		if (la_valeur_saisie.length < 5)
		{
			var ident_ok = 0;
		}
	
	
		var rege = /([A-Za-z])+([0-9_\-])*/;
		var codehtml = '';
		if(ident_ok == 1 && rege.test($("#theident_page_enregistrement").val()))
		{ 
			$('#ident_valid').attr('value', '1');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_ok' border='0'>"; 
		}
		else 
		{ 
			$('#ident_valid').attr('value', '0');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_nok' border='0'>"; 
		}
		$("#result_valid_identifiant").html(codehtml);
	}
	else
	{
		if ($("#result_valid_identifiant"))
		{
			$('#ident_valid').attr('value', '0');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_nok' border='0'>"; 
			$("#result_valid_identifiant").html(codehtml);
		}
	
	}

}



function validpasswordEvent()
{

	$("#password_page_enregistrement").ready(function(event)
	{
		validpassword();
	});


	$("#password_page_enregistrement").keyup(function(event)
	{
		validpassword();
	});

}

function confvalidpasswordEvent()
{
		$("#conf_password_page_enregistrement").ready(function(event)
		{
			confvalidpassword();
		});


		$("#conf_password_page_enregistrement").keyup(function(event)
		{
			confvalidpassword();
		});

}



function validpassword()
{

	var la_valeur_saisie = $("#password_page_enregistrement").val();

	if (la_valeur_saisie)
	{
		var password_ok = 1;
		// On vérifie d'abord la taille de la saisie
		if (la_valeur_saisie.length < 6)
		{
			var password_ok = 0;
		}
		
		// On regarde si la chaine contient au moins 1 caractère minuscule
		/*var rege = /^(.*)([a-z])(.*)$/;
		if (password_ok == 1 && !rege.test($(this).val()))
		{
			var password_ok = 0;
		}*/

		// On regarde si la chaine contient au moins 1 caractère majuscule
		/*var rege = /^(.*)([A-Z])(.*)$/;
		if (password_ok == 1 && !rege.test($(this).val()))
		{
			var password_ok = 0;
		}*/

		// On regarde si la chaine contient au moins 1 caractère numérique
		var rege = /^(.*)([0-9])(.*)$/;
		if (password_ok == 1 && !rege.test(la_valeur_saisie))
		{
			var password_ok = 0;
		}

		var niv_securite = passwordStrength(la_valeur_saisie);
		var info_niv_securite = '';

		if (niv_securite == '1')
		{
			info_niv_securite = $('#pwd_too_short').val();
		}
		if (niv_securite == '2')
		{
			info_niv_securite = $('#pwd_bad').val();
		}
		if (niv_securite == '3')
		{
			info_niv_securite = $('#pwd_good').val();
		}
		if (niv_securite == '4')
		{
			info_niv_securite = $('#pwd_strong').val();
		}

		var codehtml = '';
		if (password_ok == 1)
		{	
			$('#password_valid').attr('value', '1');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_ok' border='0'>"; 
		}
		else
		{
			$('#password_valid').attr('value', '0');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_nok' border='0'>"; 
		}

		$("#result_valid_password").html(codehtml);
		//alert(info_niv_securite);
		$("#result_niv_securite_password").html(info_niv_securite);


		var lautre_valeur_saisie = $("#conf_password_page_enregistrement").val();

		var verif_password_ok = 1;
		// On vérifie d'abord la taille de la saisie
		if (la_valeur_saisie != lautre_valeur_saisie || lautre_valeur_saisie == '' || la_valeur_saisie == '')
		{
			var verif_password_ok = 0;
		}

		var codehtml = '';
		if (verif_password_ok == 1)
		{	
			$('#password_valid_verif').attr('value', '1');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_ok' border='0'>"; 
		}
		else
		{
			$('#password_valid_verif').attr('value', '0');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_nok' border='0'>"; 
		}

		$("#result_verif_valid_password").html(codehtml);
	}
	else
	{
		if ($("#result_valid_password"))
		{
			$('#password_valid').attr('value', '0');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_nok' border='0'>"; 
			$("#result_valid_password").html(codehtml);
		}
		if ($("#result_verif_valid_password"))
		{
			$('#password_valid_verif').attr('value', '0');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_nok' border='0'>"; 
			$("#result_verif_valid_password").html(codehtml);
		}
	}

}


function confvalidpassword()
{
	if ($("#conf_password_page_enregistrement"))
	{
		var la_valeur_saisie = $("#conf_password_page_enregistrement").val();
		var lautre_valeur_saisie = $("#password_page_enregistrement").val();

		var verif_password_ok = 1;
		// On vérifie d'abord la taille de la saisie
		if (la_valeur_saisie != lautre_valeur_saisie || lautre_valeur_saisie == '' || la_valeur_saisie == '')
		{
			var verif_password_ok = 0;
		}

		var codehtml = '';
		if (verif_password_ok == 1)
		{	
			$('#password_valid_verif').attr('value', '1');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_ok' border='0'>"; 
		}
		else
		{
			$('#password_valid_verif').attr('value', '0');
			codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_nok' border='0'>"; 
		}

		$("#result_verif_valid_password").html(codehtml);
	}

}


function getLang()
{
	return 1;
}


// Evenement sur l'email d'inscription (vérif format + unique)
function validmail()
{
	$("#email_page_enregistrement").change(function(event)
		{
$("#result_valid_mail").html('<img src="/images/loading_orange.gif" />');
			
			$.ajax({
				type: "POST",
			  url: "/includes/form_ajax/verif_mail.cfm",
				data : {lalng : getLang(), lemail: this.value},
				dataTypeString : "json",
			  success: function(data) {
						var response = eval(data);
					var codehtml;
					// Réponse OK
					if (response["0"] == 1)
					{
						codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_ok' border='0'>"; 
					}
					if (response["0"] == 0)
					{
						codehtml = "<img src='/images/point.gif' height='16' width='20' class='image_validation image_validation_nok' border='0'>"; 
					}

					 

					//var tabresult = json
					$("#result_valid_mail").html(codehtml + response["1"]);
			  },
			  error: function(data) {
				$("#result_valid_mail").html('');
			  }
			});
		});
}

/*<table id="mytable">
	<tr>
		<td><nom</td>
		<td>prix</td>
	</tr>
	<tr>
		<td>P1</td>
		<td class="price">10,00</td>
		<td><img class="add_product" id="#refproduct#" src="/images/add.png" /></td>
	</tr>
	</table>


table#mytable tr {
	
}

table#mytable1 td.price, table#mytable2 td.price {
	
}

table#mytable2 td.price {
}

function addProductCart() {
	$("#mytable span.error")
	{
		$(this).id;
	}

}
*/



//function passwordStrength(password,username)
function passwordStrength(password)
{
 
	/*var shortPass = 'Too short';
	var badPass = 'Bad';
	var goodPass = 'Good';
	var strongPass = 'Strong';*/

	var shortPass = '1';
	var badPass = '2';
	var goodPass = '3';
	var strongPass = '4';


	score = 0 
    
    //password < 4
    if (password.length < 6 ) { return shortPass }
    
    //password == username
    //if (password.toLowerCase()==username.toLowerCase()) return badPass
    
    //password length
    score += password.length * 4
    score += ( checkRepetition(1,password).length - password.length ) * 1
    score += ( checkRepetition(2,password).length - password.length ) * 1
    score += ( checkRepetition(3,password).length - password.length ) * 1
    score += ( checkRepetition(4,password).length - password.length ) * 1

    //password has 3 numbers
    if (password.match(/(.*[0-9].*[0-9].*[0-9])/))  score += 5 
    
    //password has 2 sybols
    if (password.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)) score += 5 
    
    //password has Upper and Lower chars
    if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))  score += 10 
    
    //password has number and chars
    if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))  score += 15 
    //
    //password has number and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([0-9])/))  score += 15 
    
    //password has char and symbol
    if (password.match(/([!,@,#,$,%,^,&,*,?,_,~])/) && password.match(/([a-zA-Z])/))  score += 15 
    
    //password is just a nubers or chars
    if (password.match(/^\w+$/) || password.match(/^\d+$/) )  score -= 10 

	//alert(score);
    
    //verifing 0 < score < 100
    if ( score < 0 )  score = 0 
    if ( score > 100 )  score = 100 
    
    if (score < 34 )  return badPass 
    if (score < 68 )  return goodPass
    return strongPass
}


// checkRepetition(1,'aaaaaaabcbc')   = 'abcbc'
// checkRepetition(2,'aaaaaaabcbc')   = 'aabc'
// checkRepetition(2,'aaaaaaabcdbcd') = 'aabcd'

function checkRepetition(pLen,str) {
    res = ""
    for ( i=0; i<str.length ; i++ ) {
        repeated=true
        for (j=0;j < pLen && (j+i+pLen) < str.length;j++)
            repeated=repeated && (str.charAt(j+i)==str.charAt(j+i+pLen))
        if (j<pLen) repeated=false
        if (repeated) {
            i+=pLen-1
            repeated=false
        }
        else {
            res+=str.charAt(i)
        }
    }
    return res
}


function EventUpdatePassword()
{
	
	$("#link_show_update").click(function(event)
	{
		if ($("#cadre_update_password"))
		{
			$("#link_update_password").hide();
			$('#cadre_update_password').show();
		}
	});

}


