var gsCharList='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '

function validateLoginForm(poDoc)
{
	if(poDoc.getElementById('txt_id').value=='')
		alert("vous devez taper votre identifiant pour vous connecter.");
	else
		if(poDoc.getElementById('txt_psw').value=='')
			alert("vous devez taper votre mot de passe pour vous connecter");
		else
			if(poDoc.getElementById('txt_psw').value.length<6)
				alert("votre mot de passe doit comporter au moins 6 caractères");
			else
				if(!validatePsw(poDoc.getElementById('txt_psw').value))
					alert("votre mot de passe doit comporter uniquement des lettres non accentuées et des chiffres");
				else
					return true;	
			
	return false;
}

function validatePsw(psPsw)
{
	for(lnK=0;lnK<psPsw.length;lnK++)
		if(gsCharList.indexOf(psPsw.charAt(lnK))<0)
			return false;
	
	return true;
}

function validatePassForm(psDoc)
{
	
}

function submitPassword(poDoc)
{
	var lsParms="submitPassword.php";
	var lsKey=getEncryptKey(16);

	lsParms+="?id="+escape(poDoc.getElementById('txt_id').value);
	lsParms+="&key="+escape(lsKey);
	lsParms+="&psw="+escape(encryptPsw(poDoc.getElementById('txt_psw').value,lsKey));
	window.open(lsParms,"_self") ;	
}

function getEncryptKey(pnLength)
{
	var lsKey='';
	var lsDigitList='0123456789';
	for(lnK=0;lnK<pnLength;lnK++)
		lsKey+=lsDigitList.charAt(Math.round(Math.random()*lsDigitList.length-1));
alert(lsKey);
	return lsKey;
}

function encryptPsw(psPsw,psKey)
{
	var lsEPsw='';
	var lnCursorPos=getNextPos(0,psKey);
	var lnCursorValue=parseInt(psKey.charAt(lnCursorPos))+1;
	var lnShift=0;

	for(lnK=0;lnK<psPsw.length;lnK++)
	{
	alert(lnCursorValue);
		for(lnU=0;lnU<=lnCursorValue;lnU++)
			lsEPsw+=gsCharList.charAt(Math.round(Math.random()*gsCharList.length));
		
		lnShift=gsCharList.indexOf(psPsw.charAt(lnK))+lnCursorValue;
	//	lsEPsw+="##"+lnShift+"#";
		if(lnShift>gsCharList.length)
			lnShift-=gsCharList.length;

	//	lsEPsw+="#"+gsCharList.charAt(lnShift);
	//	lsEPsw+="#"+psPsw.charAt(lnK)+"#";
		
	//	lsEPsw+=gsCharList.charAt(lnShift);
		lsEPsw+="@";	
			
		lnCursorPos=getNextPos(lnCursorPos,psKey);
		lnCursorValue=parseInt(psKey.charAt(lnCursorPos))+1;
	}
//alert(lsEPsw);
alert(decryptPsw(lsEPsw,psKey));
	return lsEPsw;	
}

function getNextPos(pnPos,psKey)
{
	if(pnPos>=psKey.length-1)
		return 0;
	else
		return pnPos+1;
}

function decryptPsw(psEPsw,psKey)
{
	var lsPsw='';
	var lnCursorPos=getNextPos(0,psKey);
	var lnCursorValue=parseInt(psKey.charAt(lnCursorPos))+1;
	var lnPos=lnCursorValue+1;
	
	while(lnPos<psEPsw.length)
	{
		lsPsw+=psEPsw.charAt(lnPos);
		lnCursorPos=getNextPos(lnCursorPos,psKey);
		lnCursorValue=parseInt(psKey.charAt(lnCursorPos))+1;
		lnPos+=lnCursorValue;
	}

	return lsPsw;
}
