var jelzo1 = 0;
var jelzo2 = 0;
var jelzo3 = 0;
var jelzo4 = 0;
var ajaxRequest;
var pipa = "&nbsp;&nbsp;<img src='images/stb/pipa.png'>";
var iksz = "&nbsp;&nbsp;<img src='images/stb/iksz.png'>";

function ajax() {
	try{ ajaxRequest = new XMLHttpRequest(); }
	catch (e){ 	try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
				catch (e) {	try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
							catch (e){	alert("Kérlek, kapcsold be a javascriptet!"); return false;	}}}	
}

function gomb() {
	if(jelzo1 == 1 && jelzo2 == 1 && jelzo3 == 1 && jelzo4 == 1) {
		document.regisztracio.gomb.disabled = "";
		document.regisztracio.gomb.style.cssText = "margin-left:110px; color:#333333";
	}
	else {
		document.regisztracio.gomb.disabled = "disabled";
		document.regisztracio.gomb.style.cssText = "background-color: #fff; margin-left:110px; color:#999999";
	}
}

function nick_ellenorzo(felhasznalonev){
	ajax();						
	ajaxRequest.open("POST", "sys/ajax_ellenorzo.php", true);
	ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
	ajaxRequest.send('felhasznalonev=' + felhasznalonev); 
	
	ajaxRequest.onreadystatechange = function(){if(ajaxRequest.readyState == 4){
		var nick = document.getElementById('nick_ellenorzes');
		var szoveg_nick = "";
		var speckarakter = 0;
		
		// spec karakterek nem kellenek
		var iChars = "*|,\":<>[]{}`\';()@&$#%";
		for (var i = 0; i < felhasznalonev.length; i++) {
			if (iChars.indexOf(felhasznalonev.charAt(i)) != -1){
			speckarakter = 1;
			break;
			}
		}
		
		if (ajaxRequest.responseText == "0" || felhasznalonev.length < 3 || felhasznalonev.length > 15 || speckarakter == 1)
			{	szoveg_nick = iksz; jelzo1=0;	gomb(); }
		else {	szoveg_nick = pipa; jelzo1=1; gomb(); }
			
		nick.innerHTML = szoveg_nick;
	}}
}

function email_ellenorzo(email_) {
	   ajax();
	   var email = document.getElementById('email_ellenorzes');
	   var szoveg_email = "";
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   var address = document.regisztracio.email.value;
	   if(reg.test(email_) == false) {
	   		szoveg_email = iksz;
			email.innerHTML = szoveg_email;
			jelzo2=0;
			gomb();
	   }
	   else {
	   		ajaxRequest.open("POST", "sys/ajax_ellenorzo.php", true);
			ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
			ajaxRequest.send('email=' + email_); 
			ajaxRequest.onreadystatechange = function(){if(ajaxRequest.readyState == 4){
			email.innerHTML = ajaxRequest.responseText;
			if (ajaxRequest.responseText == "1")
				{
				szoveg_email = pipa;
				jelzo2=1;
				gomb();
				}
			else
				{
				szoveg_email = iksz;
				jelzo2=0;
				gomb();
				}
				email.innerHTML = szoveg_email;
				}}
			}   
}


function jelszothasonlit(jelszo2) {
	var jelszo = document.getElementById('jelszo_ellenorzes');
	if(jelszo2 == document.regisztracio.jelszo1.value && jelszo2.length > 4) {
		jelszo.innerHTML = pipa; jelzo3=1;
	}
	else {
		jelszo.innerHTML = iksz; jelzo3=0;
	}
	
	gomb();
}

function biztonsagi_kod_ellenorzo(biztonsagi_kod){
	ajax();						
	ajaxRequest.open("POST", "sys/ajax_ellenorzo.php", true);
	ajaxRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
	ajaxRequest.send('biztonsagi_kod=' + biztonsagi_kod); 
	
	ajaxRequest.onreadystatechange = function(){if(ajaxRequest.readyState == 4){
		var kodpipahely = document.getElementById('biztonsagi_kod_ellenorzes');
		
		if (ajaxRequest.responseText == "0" || biztonsagi_kod.length < 5)
			{	kodpipahely.innerHTML = iksz; jelzo4=0; gomb(); }
		else {	kodpipahely.innerHTML = pipa; jelzo4=1; gomb(); }
	}}
}

var commonPasswords = new Array('password', 'pass', '1234', '1246'); 
 
var numbers = "0123456789"; 
var lowercase = "abcdefghijklmnopqrstuvwxyz"; 
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
var punctuation = "!.@$L#*()%~<>{}[]"; 
 
function checkPassword(password) { 
 
    var combinations = 0; 
 
    if (contains(password, numbers) > 0) { 
        combinations += 10; 
    } 
 
    if (contains(password, lowercase) > 0) { 
        combinations += 26; 
    } 
 
    if (contains(password, uppercase) > 0) { 
        combinations += 26; 
    } 
 
    if (contains(password, punctuation) > 0) { 
        combinations += punctuation.length; 
    } 
 
    // work out the total combinations 
    var totalCombinations = Math.pow(combinations, password.length); 
 
    // if the password is a common password, then everthing changes... 
    if (isCommonPassword(password)) { 
        totalCombinations = 75000 // about the size of the dictionary 
    } 
 
    // work out how long it would take to crack this (@ 200 attempts per second) 
    var timeInSeconds = (totalCombinations / 200) / 2; 
 
    // this is how many days? (there are 86,400 seconds in a day. 
    var timeInDays = timeInSeconds / 86400 
 
    // how long we want it to last 
    var lifetime = 365; 
 
    // how close is the time to the projected time? 
    var percentage = timeInDays / lifetime; 
 
    var friendlyPercentage = cap(Math.round(percentage * 100), 100); 
    if (totalCombinations != 75000 && friendlyPercentage < (password.length * 5)) { 
        friendlyPercentage += password.length * 5; 
    } 
 
    var progressBar = document.getElementById("progressBar"); 
    progressBar.style.width = friendlyPercentage + "%"; 
 
    if (percentage > 1) { 
        // strong password 
        progressBar.style.backgroundColor = "#3bce08"; 
        return; 
    } 
 
    if (percentage > 0.5) { 
        // reasonable password 
        progressBar.style.backgroundColor = "#ffd801"; 
        return; 
    } 
 
    if (percentage > 0.10) { 
        // weak password 
        progressBar.style.backgroundColor = "orange"; 
        return; 
    } 
 
    // useless password! 
    if (percentage <= 0.10) { 
        // weak password 
        progressBar.style.backgroundColor = "red"; 
        return; 
    } 
 
 
} 
 
function cap(number, max) { 
    if (number > max) { 
        return max; 
    } else { 
        return number; 
    } 
} 
 
function isCommonPassword(password) { 
 
    for (i = 0; i < commonPasswords.length; i++) { 
        var commonPassword = commonPasswords[i]; 
        if (password == commonPassword) { 
            return true; 
        } 
    } 
 
    return false; 
 
} 
 
function contains(password, validChars) { 
 
    count = 0; 
 
    for (i = 0; i < password.length; i++) { 
        var char = password.charAt(i); 
        if (validChars.indexOf(char) > -1) { 
            count++; 
        } 
    } 
 
    return count; 
} 
