// JavaScript Document

// I do not recommend modifying between the ########### lines
// ######################################################################
// SYSTEM SETUP
// ######################################################################

// Define Strengths and Weaknesses
function buildWeak() {
var $weak='';
$weak=$weak+'a';
$weak=$weak+'b';
$weak=$weak+'c';
$weak=$weak+'d';
$weak=$weak+'e';
$weak=$weak+'f';
$weak=$weak+'g';
$weak=$weak+'h';
$weak=$weak+'i';
$weak=$weak+'j';
$weak=$weak+'k';
$weak=$weak+'l';
$weak=$weak+'m';
$weak=$weak+'n';
$weak=$weak+'o';
$weak=$weak+'p';
$weak=$weak+'q';
$weak=$weak+'r';
$weak=$weak+'s';
$weak=$weak+'t';
$weak=$weak+'u';
$weak=$weak+'v';
$weak=$weak+'w';
$weak=$weak+'x';
$weak=$weak+'y';
$weak=$weak+'z';
return $weak;
}

function buildMedium() {
var $medium='';
$medium=$medium+'0';
$medium=$medium+'1';
$medium=$medium+'2';
$medium=$medium+'3';
$medium=$medium+'4';
$medium=$medium+'5';
$medium=$medium+'6';
$medium=$medium+'7';
$medium=$medium+'8';
$medium=$medium+'9';
$medium=$medium+'A';
$medium=$medium+'B';
$medium=$medium+'C';
$medium=$medium+'D';
$medium=$medium+'E';
$medium=$medium+'F';
$medium=$medium+'G';
$medium=$medium+'H';
$medium=$medium+'I';
$medium=$medium+'J';
$medium=$medium+'K';
$medium=$medium+'L';
$medium=$medium+'M';
$medium=$medium+'N';
$medium=$medium+'O';
$medium=$medium+'P';
$medium=$medium+'Q';
$medium=$medium+'R';
$medium=$medium+'S';
$medium=$medium+'T';
$medium=$medium+'U';
$medium=$medium+'V';
$medium=$medium+'W';
$medium=$medium+'X';
$medium=$medium+'Y';
$medium=$medium+'Z';
return $medium;
}

function buildStrong() {
var $strong='';
$strong=$strong+'!';
$strong=$strong+'!';
$strong=$strong+'#';
$strong=$strong+'$';
$strong=$strong+'%';
$strong=$strong+'&';
$strong=$strong+',';
$strong=$strong+'.';
$strong=$strong+':';
$strong=$strong+'-';
$strong=$strong+'­';
$strong=$strong+'­';
$strong=$strong+'–';
$strong=$strong+'—';
$strong=$strong+';';
$strong=$strong+'?';
$strong=$strong+'@';
$strong=$strong+'[';
$strong=$strong+']';
$strong=$strong+'^';
$strong=$strong+'ˆ';
$strong=$strong+'_';
$strong=$strong+'{';
$strong=$strong+'|';
$strong=$strong+'}';
$strong=$strong+'~';
$strong=$strong+'¡';
$strong=$strong+'¡';
$strong=$strong+'¦';
$strong=$strong+'¦';
$strong=$strong+'¨';
$strong=$strong+'¨';
$strong=$strong+'¯';
$strong=$strong+'¯';
$strong=$strong+'¸';
$strong=$strong+'¸';
$strong=$strong+'¿';
$strong=$strong+'¿';
$strong=$strong+'˜';
$strong=$strong+'‚';
$strong=$strong+'„';
$strong=$strong+'‹';
$strong=$strong+'›';
$strong=$strong+'›';
$strong=$strong+'+';
$strong=$strong+'<';
$strong=$strong+'=';
$strong=$strong+'>';
$strong=$strong+'±';
$strong=$strong+'±';
$strong=$strong+'«';
$strong=$strong+'«';
$strong=$strong+'»';
$strong=$strong+'»';
$strong=$strong+'×';
$strong=$strong+'×';
$strong=$strong+'÷';
$strong=$strong+'÷';
$strong=$strong+'¢';
$strong=$strong+'¢';
$strong=$strong+'£';
$strong=$strong+'£';
$strong=$strong+'¤';
$strong=$strong+'¤';
$strong=$strong+'¥';
$strong=$strong+'¥';
$strong=$strong+'§';
$strong=$strong+'§';
$strong=$strong+'©';
$strong=$strong+'©';
$strong=$strong+'¬';
$strong=$strong+'¬';
$strong=$strong+'®';
$strong=$strong+'®';
$strong=$strong+'°';
$strong=$strong+'°';
$strong=$strong+'µ';
$strong=$strong+'µ';
$strong=$strong+'†';
$strong=$strong+'‡';
$strong=$strong+'•';
$strong=$strong+'…';
$strong=$strong+'‰';
$strong=$strong+'€';
$strong=$strong+'¼';
$strong=$strong+'¼';
$strong=$strong+'½';
$strong=$strong+'½';
$strong=$strong+'¾';
$strong=$strong+'¾';
$strong=$strong+'¹';
$strong=$strong+'¹';
$strong=$strong+'²';
$strong=$strong+'²';
$strong=$strong+'³';
$strong=$strong+'³';
$strong=$strong+'ª';
$strong=$strong+'ª';
$strong=$strong+'Á';
$strong=$strong+'á';
$strong=$strong+'À';
$strong=$strong+'à';
$strong=$strong+'Â';
$strong=$strong+'â';
$strong=$strong+'Ä';
$strong=$strong+'ä';
$strong=$strong+'Ã';
$strong=$strong+'ã';

$strong=$strong+'Å';
$strong=$strong+'å';
$strong=$strong+'Æ';
$strong=$strong+'æ';
$strong=$strong+'Ç';
$strong=$strong+'ç';
$strong=$strong+'Ð';
$strong=$strong+'ð';
$strong=$strong+'É';
$strong=$strong+'é';
$strong=$strong+'È';
$strong=$strong+'è';
$strong=$strong+'Ê';
$strong=$strong+'ê';
$strong=$strong+'Ë';
$strong=$strong+'ë';
$strong=$strong+'ƒ';
$strong=$strong+'Í';
$strong=$strong+'í';
$strong=$strong+'Ì';
$strong=$strong+'ì';
$strong=$strong+'Î';
$strong=$strong+'î';
$strong=$strong+'Ï';
$strong=$strong+'ï';
$strong=$strong+'Ñ';
$strong=$strong+'ñ';
$strong=$strong+'º';
$strong=$strong+'º';
$strong=$strong+'Ó';
$strong=$strong+'ó';
$strong=$strong+'Ò';
$strong=$strong+'ò';
$strong=$strong+'Ô';
$strong=$strong+'ô';
$strong=$strong+'Ö';
$strong=$strong+'ö';
$strong=$strong+'Õ';
$strong=$strong+'õ';
$strong=$strong+'Ø';
$strong=$strong+'ø';
$strong=$strong+'Œ';
$strong=$strong+'œ';
$strong=$strong+'Š';
$strong=$strong+'š';
$strong=$strong+'ß';
$strong=$strong+'Þ';
$strong=$strong+'þ';
$strong=$strong+'™';
$strong=$strong+'Ú';
$strong=$strong+'ú';
$strong=$strong+'Ù';
$strong=$strong+'ù';
$strong=$strong+'Û';
$strong=$strong+'û';
$strong=$strong+'Ü';
$strong=$strong+'ü';
$strong=$strong+'Ý';
$strong=$strong+'ý';
$strong=$strong+'Ÿ';
$strong=$strong+'ÿ';
$strong=$strong+'Ž';
$strong=$strong+'ž';
return $strong;
}
function buildIllegal() {
var $illegal='';
$illegal=$illegal+'(';
$illegal=$illegal+')';
$illegal=$illegal+'*';
$illegal=$illegal+"'";
$illegal=$illegal+'`';
$illegal=$illegal+'´';
$illegal=$illegal+'‘';
$illegal=$illegal+'’';
$illegal=$illegal+'“';
$illegal=$illegal+'"';
$illegal=$illegal+'”';
$illegal=$illegal+'¶';
$illegal=$illegal+'·';
$illegal=$illegal+'/';
$illegal=$illegal+'\\';
return $illegal;
}



function isWhat($ch,$what) {
// Searches for something, if it has it, it returns true...

var $isat=-1;

	if ($what=="Interdit") {
	// Find if this is a strong char
	var $illegal=buildIllegal();
	$isat=$illegal.indexOf($ch);
	}
	
	if ($what=="Fort") {
	// Find if this is a strong char
	var $strong=buildStrong();
	$isat=$strong.indexOf($ch);
	}
	
	if ($what=="Moyen") {
	// Find if this is a strong char
	var $medium=buildMedium();
	$isat=$medium.indexOf($ch);
	}
	
	if ($what=="Bas") {
	// Find if this is a strong char
	var $weak=buildWeak();
	$isat=$weak.indexOf($ch);
	}

if ($isat>-1) {
return true;
}
if ($isat==-1) {
return false;
}

}

// ######################################################################
// END SYSTEM SETUP
// ######################################################################

/////////////////////////////////////////////////////////////////////////
// MAIN FUNCTION!!!! (You shouldn't modify the above part...
// MODIFYING THIS SCRIPT, AND IT STOPS WORKING, MEANS FIX YOUR ERRORS....
/////////////////////////////////////////////////////////////////////////

function updateStrength() {
// Different factors will contribute to password strength
// A weak password contains only characters or only numbers
// A strong password contains numbers, characters, upper case + lower case
// A medium password contains characters, lower case + upper case
// A 3/4 password contains characters, numbers, upper + lower

$score='Bas';

// Pull back password field
$pw=register.reg_pw.value; // nameoftheform.nameofthefield

// Takes current PW, and loads it into the slicer
var $ch='';
var $isweak='';
var $isstrong='';
var $ismedium='';
var $isillegal='';

var $highest='';

////////////////////////////////////////////////////////////////
// Before dissecting the password, see if it is a number only!
// Numbers only are the WEAKEST... If you had 5000 as your password,
// It could be hacked in 5000 tries! Bad Idea!
////////////////////////////////////////////////////////////////
var $divnum=0;

$divnum=$pw * 1;

	if ($divnum==$pw) {
	// This is a number, because it multiplied times 1 and returned itself.
	// I find it's the easiest way to check numbers
	$highest='Bas';

	$score='Bas';
	} else {
		//document.print('<hr><br>slicing: ' + $pw + ' with ' + $pw.length + ' slices.<br><br>');
		for ($x=0; $x < $pw.length; $x++) {
		
		// This will keep cutting the pw, checking for weak/strong.
		// For the password to be strong, it must be 4 characters long, and be isstrong
		// For a password to be medium, it must be 4 characters long, and be ismedium
		// For a password to be weak, it must be 4 characters long, and be isweak
		// Passwords that are numbers only or less than 4 characters are weak.
		
		$ch=$pw.charAt($x);
		$isillegal=isWhat($ch,'Interdit');
		$isstrong=isWhat($ch,'Fort');
		$ismedium=isWhat($ch,'Moyen');
		$isweak=isWhat($ch,'Bas');
		
		// Based on this character, we can determine the nature of the strength
			if ($isillegal==true) {
			// contains an illegal character, we don't want these in our systems!
			// prompt the user, letting them know they entered an illegal character
			$isresult='Interdit';
			$highest='Interdit';
			}
			if ($isstrong==true) {
			// contains a weak character
			$isresult='Fort';
				if ($highest=='Interdit') {
				} else {
				$highest='Fort';
				}
			}
			if ($ismedium==true) {
			// contains a weak character
			$isresult='Moyen';
				if ($highest=='Interdit') {
				} else {
					if ($highest=='Fort') {
					} else {
					$highest='Moyen';
					}
				}
			}
			if ($isweak==true) {
			// contains a weak character
			$isresult='Bas';
				if ($highest=='Interdit') {
				} else {
					if ($highest=='Fort') {
					} else {
						if ($highest=='Moyen') {
						} else {
						$highest='Bas';
						}
					}
				}
			} 
		} // for x
	} // check for div * 1


// From here we should measure the strength on a scale of some sorts...
// Preferably images...

if ($highest=='Interdit') {
alert('Ce caratère ne peut être utilisé dans un mot de passe : '+$ch+'.');
$score='Interdit';
// ChangeMDP.NewPwd1.value=''; // This will reset the field, because illegal characters aren't wanted.
document.images["strength"].src="images/pwdstrenght/illegal.gif";
} else {
$score=$highest;
	if ($highest=="Interdit") {
	document.images["strength"].src="images/pwdstrenght/10.gif";
	}
	if ($highest=="Fort") {
	document.images["strength"].src="images/pwdstrenght/100.gif";
	}
	if ($highest=="Moyen") {
	document.images["strength"].src="images/pwdstrenght/55.gif";
	}
	if ($highest=="weak") {
	document.images["strength"].src="images/pwdstrenght/20.gif";
	}
}


// If it contains 4 or more characters, allow over weak
$len=$pw.length;

if ($len<6) {
$score='Trop Court';
document.images["strength"].src="images/pwdstrenght/10.gif";
}

// You should do more stuff here... Like what I did:
if ($len>8) {
	if ($score=='Bas') {
	// Well... it's weak, but it has 8 characters.. Seems pretty tough to me?
	document.images["strength"].src="images/pwdstrenght/55.gif";
	$score='Moyen';
	}
}
if ($len>10) {
	if ($score=='Moyen') {
	// Well... it's medium, but it has 30 characters.. Seems pretty tough to me?
	document.images["strength"].src="images/pwdstrenght/100.gif";
	$score='Fort';
	}
}
if ($len>20) {
	if ($score=='Fort') {
	// Too long of a password man!!!!
	document.images["strength"].src="images/pwdstrenght/illegal.gif";
	alert('Pensez-vous vraiment pouvoir vous souvenir d\'un mot de passe aussi long ?.');
	$score='Trop Long';
	// ChangeMDP.NewPwd1.value='';
	}
}
document.getElementById('strengthName').innerHTML=$score;
// ChangeMDP.strengthName.innerHTML=$score;
}
