/*
chPwdQuality.js
© Torben Wilhelmsen, 2008, www.wil.dk
Define password quality value of the character type 

The script check for password fields. If any, the function chPwdQualitySubmit() is added to the
field's form onsubmit attribute.
If the calculated quality of the typed password if below the chPwdQUALITYSUM level the form is not submitted.
*/

// I n i   v a l u e s
// chPwdQUALALERTTEXT and chPwdQUALPASSFAILTEXT can be set in the document header, before including this file.
// chPwdQUALALERTTEXT is an array with 3 strings.
// chPwdQUALPASSFAILTEXT is string.
// chPwdQUALITYSUM is default 6
// chPwdQUALITYSUMshow is default false. 

if ((typeof(chPwdQUALALERTTEXT)=='undefined')||((typeof(chPwdQUALALERTTEXT)=='object')&&(chPwdQUALALERTTEXT.lenght<3))) { 
	chPwdQUALALERTTEXT = new Array('Your password is quality checked to: ',
						'The password failed to pass current quality level.',
						'The password is approved to the current quality level.');
	}
if (typeof(chPwdQUALPASSFAILTEXT)=='undefined') chPwdQUALPASSFAILTEXT = 'Sorry, the password is too simple ... please, try again.';
if (typeof(chPwdQUALITYSUM)=='undefined') chPwdQUALITYSUM = 6;
else chPwdQUALITYSUM = parseInt(chPwdQUALITYSUM);
if (typeof(chPwdQualityDEBUG)=='undefined') chPwdQualityDEBUG=false;
if (typeof(chPwdQUALITYSUMshow)=='undefined') chPwdQUALITYSUMshow=false;

// Q u a l i t y   s e t t i n g
// Changing these surely affect the testresult.

chPwdLOWERCASEVALUE = 1;
chPwdUPPERCASEVALUE = 2;
chPwdINTEGERSVALUE = 2;
chPwdOTHERCHARSVALUE = 4;
chPwdDIFFERENTTYPEVALUE = 0.5;
chPwdDIFFERENTCHARSVALUE = 0.5;

// F u n c t i o n s

function chPwdQualityAlert() {
	cpw = chPwdQualityField[0].value
	q = chPwdQuality(cpw);
	aText = chPwdQUALALERTTEXT[0] + ' ' + q + '\n';
	if (q<chPwdQUALITYSUM) aText += chPwdQUALALERTTEXT[1];
	else aText += chPwdQUALALERTTEXT[2];
	alert(aText);
	}
function chPwdQualitySubmit(elem) {
	fo = (elem['target'])?elem['target']:(elem['srcElement'])?elem['srcElement']:false;
	validResult = true;
	if (chPwdQualityField) {
		for (i=0;i<chPwdQualityField.length;i++) {
			name = chPwdQualityField[i].name;
			if (typeof(fo[name].length)!='undefined') for (j=0;j<fo[name].length;j++) validResult = _chPwdQualitySumOnSubmit(fo[name][j].value, validResult);
			else validResult = _chPwdQualitySumOnSubmit(chPwdQualityField[i].value, validResult);
			}
		if (validResult) chPwdQualityFieldForm.submit()
		else {
			examples = findobject('chPwdQualityExamples');
			if (examples) {examples.style.display='block';}
			alert(chPwdQUALPASSFAILTEXT);
			}
		}
	return false;
	}
function _chPwdQualitySumOnSubmit(cpw, result) {
	q = chPwdQuality(cpw);
	if (q<chPwdQUALITYSUM) return false;
	return result
	}
function chPwdQualityFailed() {
}

function chPwdQualityTyping(elem) {
	pw = (elem['target'])?elem['target']:(elem['srcElement'])?elem['srcElement']:false;
	if (pw) {
		val = chPwdQuality(pw.value);
		cBar = document.getElementById(pw.name+'_cBar');
		if (cBar) {
			if (val<chPwdQUALITYSUM) {pw.style.backgroundColor='#f08888';cBar.style.backgroundColor='#f08888';}
			else if (val==chPwdQUALITYSUM) {pw.style.backgroundColor='#f0ea48'; cBar.style.backgroundColor='#f0ea48'}
			else {pw.style.backgroundColor='#b4df27'; cBar.style.backgroundColor='#b4df27';}
			val = (''+parseFloat(val)).split('.');
			if (val.length==1) val.push('00');
			else if (val[1].length==1) val[1]+='0';
			cBar.value = val.join('.');
			}
		else {
			if (val<chPwdQUALITYSUM) pw.style.backgroundColor='#f08888';
			else if (val==chPwdQUALITYSUM) pw.style.backgroundColor='#f0ea48';
			else pw.style.backgroundColor='#b4df27';
			}
		}
	}
function chPwdQuality(pw) {
	if (pw) {
		letrs = 'abcdefghijklmnopqrstuvwxyz '
		result = 0;
		chars = '';
		prevcase = false
		for (i=0;i<pw.length;i++) {
			c = pw.charAt(i);
			if (chars.indexOf(c)==-1) chars += c;
			if (parseInt(c)) {result+=chPwdINTEGERSVALUE;thiscase=0;}
			else if (letrs.indexOf(c.toLowerCase())==-1) {result+=chPwdOTHERCHARSVALUE;thiscase=1;}
			else if (c.toUpperCase()==c) {result+=chPwdUPPERCASEVALUE;thiscase=2;}
			else {result+=chPwdLOWERCASEVALUE;thiscase=3;}
			if ((prevcase)&&(thiscase!=prevcase)) result+=chPwdDIFFERENTTYPEVALUE;
			prevcase=thiscase;
			}
		result+=(chPwdDIFFERENTCHARSVALUE*chars.length);
		return result/2.0;
		return parseInt(Math.round(result/2.0));
		return parseInt(Math.floor(result/2.0));
		}
	else return 0;
	}
function addChPwdQuality(){
	inputFields = document.getElementsByTagName('input');
	for (i=0;i<inputFields.length;i++) {
		if (inputFields[i].type=='password' && inputFields[i].className!='validate.no') {
			if (!chPwdQualityFieldForm) {
				chPwdQualityFieldForm = inputFields[i].form;
				if (chPwdIeBrowser) chPwdQualityFieldForm.attachEvent('onsubmit',chPwdQualitySubmit);
				else chPwdQualityFieldForm.addEventListener('submit',chPwdQualitySubmit,true);
				}
			if (chPwdIeBrowser) inputFields[i].attachEvent('onkeyup',chPwdQualityTyping);
			else inputFields[i].addEventListener('keyup',chPwdQualityTyping,false);
			if (chPwdQualityDEBUG) {
				if (chPwdIeBrowser) inputFields[i].attachEvent('onchange',chPwdQualityAlert);
				else inputFields[i].addEventListener('change',chPwdQualityAlert,false);
				}
			if (chPwdQUALITYSUMshow) {
				chPwBar = document.createElement('input')
				chPwBar.id = inputFields[i].name+'_cBar';
				chPwBar.size='5';
				chPwBar.readOnly=true;
				chPwBar.style.backgroundColor='#f08888';
				chPwBar.style.fontweight='bold';
				chPwBar.style.textAlign='right';
				chPwBar.style.color='#000000';
				chPwBar.value='0.00';
				chPwBar.style.border='0px';
				if (inputFields[i].nextSibling) inputFields[i].parentNode.insertBefore(chPwBar,inputFields[i].nextSibling);
				else inputFields[i].parentNode.appendChild(chPwBar);
				}
			inputFields[i].style.border='0px';
			inputFields[i].style.backgroundColor='#f08888'
			if (chPwdQualityField) chPwdQualityField.push(inputFields[i]);
			else chPwdQualityField = new Array(inputFields[i]);
			}
		}
	}
chPwdIeBrowser = false;
chPwdQualityField = false;
chPwdQualityFieldForm = false;
if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	chPwdIeBrowser = true;
	window.attachEvent("onload", addChPwdQuality);
	}
else window.addEventListener('load',addChPwdQuality,false);

