/*
###########################################################################################################################
# P91AUC V.1.0 by PA-S.de
###########################################################################################################################
# P91AUC V.1.0 by PA-S.de
# Copyright 2003-2007 PA-S.de
# Script Url: www.pa-s.de
# Kontakt: kontakt [@] pa-s [.] de
# Dieser unsichtbare Verweis darf nicht entfernt werden!
###########################################################################################################################
*/
// <![CDATA[
var P91 = null;
// Initialisieren
function P91AUC_system(){
	if (window.XMLHttpRequest) {
	  P91 = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
	  try {
		P91 = new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (ex) {
		try {
		  P91 = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (ex) {
		}
	  }
	}
}
// Statusabfrage username
function P91AUC_heuri() {
  if (P91.readyState == 4) {
    var heurix = document.getElementById("una");
    heurix .innerHTML = P91.responseText;
  }
}
// Statusabfrage email
function P92AUC_heuri() {
  if (P91.readyState == 4) {
    var heurix = document.getElementById("emailanswer");
    heurix .innerHTML = P91.responseText;
  }
}

// Aufrufen
function checkUsername() {
  var pas = document.getElementById("username").value;  // Name vom Formularfeld, in diesen Fall "username"
  var heurix  = document.getElementById("una");
  heurix .innerHTML = '<img src="http://www.zooble.de/img/icons/loading.gif" alt="Lade..." />'; // Lade Grafik
  P91.open("GET", "../func/ajaxCheck.php?usernamecheck=" + pas); // Daten holen
  P91.onreadystatechange = P91AUC_heuri;
  P91.send(null);
}

function checkEmail() {
  var pas = document.getElementById("email").value;  // Name vom Formularfeld, in diesen Fall "username"
  var heurix  = document.getElementById("emailanswer");
  heurix .innerHTML = '<img src="http://www.zooble.de/img/icons/loading.gif" alt="Lade..." />'; // Lade Grafik
  P91.open("GET", "../func/ajaxCheck.php?emailcheck=" + pas); // Daten holen
  P91.onreadystatechange = P92AUC_heuri;
  P91.send(null);
}

// Script laden
var heurix = P91AUC_system();
// ]]>


// AJAX PAsswort Check
/* == CSS-Classes: Reihenfolge: 1, 2, 3, 4, 6 (rating/Bewertung) == */
var P91Styles = new Array("P91PWS_1", "P91PWS_2", "P91PWS_3", "P91PWS_4", "P91PWS_6");

/* == CSS-Class der Ausgabe: == */
var P91Output = ("P91PWS_O");

/* == Kriterien-Einschätzung == */
/* 0 = Sehr kritisch
 * 1 = Alltagsform
 */
var P91mode = 1;

/* == Erlaubte Sonderzeichen (RegExp) == */
/* null = für alle Zeichen außer 0-9A-Za-z\t\s   */
var P91valid = null;

/* Beispiel für die Sonderzeichen _-.            */
// var P91valid = /[._-]/;

/* == Ausgabe: Reihenfolge: 1, 2, 3, 4, 6 (rating/Bewertung) ==*/
/* Deutsch */
var P91Msg = new Array("Sehr sicher", "Sicher", "Stark", "Mittel", "Unsicher");

/* English */
// var P91Msg = new Array("Very Secure", "Secure", "Average", "Weak", "Very Weak");

/*
~~~~ ENDE ~~~~
*/
function checkPassword(heuri) {
	var P91b = new Array(0,0,0,0);
	var P91r = new Array(0,0,0,0);
	var P91various = 1;
	var sum = 0;
	var erg = null;
	var P91PWS = document.getElementById("P91PWS"); 
	var heuri = heuri.split('');
	for(i = 0; i <= (heuri.length - 1); i++) {
		if (heuri[i] != null) {
			// LOW
			if (heuri[i].match(/([a-z])/)) {
				P91b[0]++;
				if (i > 0) {
					if (!heuri[(i - 1)].match(/([a-z])/)) {
						P91various++;
					}
				}
			}
			// UP
			if (heuri[i].match(/([A-Z])/)) {
				P91b[1]++;
				if (i > 0) {
					if (!heuri[(i - 1)].match(/([A-Z])/)) {
						P91various++;
					}
				}
			}
			// NUM
			if (heuri[i].match(/([0-9])/)) {
				P91b[2]++;
				if (i > 0) {
					if (!heuri[(i - 1)].match(/([0-9])/)) {
						P91various++;
					}
				}
			}
			// SON
			if (P91valid == null) {
				if (heuri[i].match(/[^a-zA-Z0-9\t\s]/)) {
					P91b[3]++;
					P91various++;
				}
			} else {
				if (heuri[i].match(P91valid)) {
					P91b[3]++;
					P91various++;
				}
			}
		}
    }
	sum = (P91b[0] + P91b[1] + P91b[2] + P91b[3]);
	if(sum < 1)  {
		sum = 1;
	}
	// Bewertung
	P91r[4] = Math.round(P91various * 100 / sum);
	P91r[3] = Math.round(P91b[3] * 100 / sum);
	P91r[2] = Math.round(P91b[2] * 100 / sum);
	P91r[1] = Math.round(P91b[1] * 100 / sum);
	P91r[0] = Math.round(P91b[0] * 100 / sum);  
	// Analyse by PAS
	if(P91r[0] == 100) {
		erg = 4;
	} else if(P91r[0] == 100) {
		erg = 4;
	} else if(P91r[1] == 100) {
		erg = 4;
	}  else if(P91r[2] == 100) {
		erg = 4;
	} else if(P91r[3] == 100 && sum > 5) {
		erg = 0;
	} else 
	if (P91mode == 0) { // hart
		if (sum > 13) {
			if (P91various > 6 || P91b[3] > 4) {
				erg = 0;
			}
			else {
				erg = 1;
			}
		}
		else if (sum > 5 && sum < 14) {
				if (P91various > 5) {
					erg = 2;
				}
				else {
					erg = 3;
				}
			}
			else {
				erg = 4;
			}
	}
	else { // normal
		if (sum > 9) {
			if (P91various > 5 || P91b[3] > 4) {
				erg = 0;
			}
			else {
				erg = 1;
			}
		}
		else if (sum > 5 && sum < 10) {
				if (P91various > 3) {
					erg = 2;
				}
				else {
					erg = 3;
				}
			}
			else {
				erg = 4;
			}
	}	
	
	P91PWS.innerHTML = '<div class="' + P91Output + ' "> ' + P91Msg[erg] +'</div><div class="P91PWS_C"><div class="' + P91Styles[erg] + '"></div></div>';
	return true;
}
