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 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;
}

function isConnected()
{
	return (typeof(gsNum)=="undefined");	
}

function showHelpTooltip(psField,pbDisplay,poEvent)
{
	if(typeof gsLabels['HLP_'+psField]=="undefined")
		return;
	var loContainer=document.getElementById('helpContainer');
	var loElem;
	var lnOffsetX=0;
	var lnOffsetY=0;
	if(isIE())
	{
		loElem=poEvent.srcElement;
		loContainer.style.position="absolute";
		lnOffsetX=document.body.scrollLeft;
		lnOffsetY=document.body.scrollTop;
		loContainer.style.width="280";
	}
	else
	{
		loElem=poEvent.target;
		loContainer.style.position="fixed";
	}
	if(pbDisplay)
	{
		loContainer.innerHTML= "<div class='helpTitle'>Aide sur : "+getStr("FLD_"+psField)+"</div><p>"+getStr("HLP_"+psField)+"</p>";
		
		loContainer.style.top=(poEvent.clientY+10+lnOffsetY)+"px";
		loContainer.style.left=(poEvent.clientX+15+lnOffsetX)+"px";
		loContainer.style.display="block";
		if(loElem.href==null)
			loElem.style.cursor="help";
	}
	else
	{
		loContainer.style.display="none";
		if(loElem.href==null)
			loElem.style.cursor="";
	}
}

function closeDocument(pbEditMode)
{
	if(pbEditMode)
		if(document.getElementById("_edited").value=="1")
			if(!confirm(getStr('ADM_ConfirmeFerme')))
				return;
	location.href="?offset=0";
}

function deleteDocument()
{
	if(confirm(getStr('ADM_ConfirmeSuppr')))
	{
		document.getElementById("_action").value="delete";
		document.forms[0].submit();	
	}
}

function deleteAllDocuments()
{
	if(confirm(getStr('ADM_ConfirmeSupprTout')))
		location.href="?action=deleteall";
}

function hasRole(psValue)
{
	if((gsDroits.indexOf("ROL_Unrestricted")<0)&&(gsDroits.indexOf(psValue)<0))
		return false;
	return true;
}

function trapReturnKey(poEvent,psPrefix)
{
	if(poEvent.keyCode==13)
		submitSearch(psPrefix);
}

function submitSearch(psPrefix)
{
	if(document.getElementById(psPrefix+"searchvalue").value=="")
	{
		alert("Veuillez taper le texte à rechercher");
	}
	else
	{
		var lsHref="?"+"&searchfield="+document.getElementById(psPrefix+'searchfield').value+"&searchvalue="+document.getElementById(psPrefix+'searchvalue').value;
		if(document.getElementById('view')!=null)
			lsHref+="&view="+document.getElementById('view').value;
		if(getURLParam("fromfield") != "")
			lsHref+="&fromfield="+getURLParam("fromfield");
		location.href=lsHref;
	}
}

