var explanationPresent = false;

function fill_login() {
	var grupa = window.document.forma.login_radio;
	if (grupa.length == undefined) {
		window.document.forma.login.value = grupa.value;
		} else {
			for(var i = 0; i < grupa.length; i++) {
			    if(grupa[i].checked) {
					window.document.forma.login.value = grupa[i].value;
			    }
			}
		}
}
    
function clear_radio() {
	var grupa = window.document.forma.login_radio;
	for(var i = 0; i < grupa.length; i++) {
	    grupa[i].checked = false;
	}
}

function pokazPytanie() {
	$('pytanieError', 'odpowiedzError').compact().invoke('show') ; 
	$('pytanieItem').show();
	$('odpowiedzItem').show();
}

function schowajPytanie() {
	$('pytanieError', 'odpowiedzError').compact().invoke('hide') ; 
	$('pytanieItem').hide();
	$('odpowiedzItem').hide();
}

function toggle_question() {
    var czypyt = $('czyPytanie');
    if (!czypyt) { return false; }
	if (czypyt.checked) { pokazPytanie(); } 
	else { schowajPytanie(); }
}

function questionCheckToggle(todo) {
	if (todo == 0) {
		$('czyPytanie').checked = false;
		$('czyPytanie').disabled = true;
		$('pytanieCheck').style.color = "#555";
		schowajPytanie();
	} else if ( todo == 1) {
		$('czyPytanie').checked = false;
		$('czyPytanie').disabled = false;
		$('pytanieCheck').style.color = "#222";
	}
}

function checkPhone() {
	if( $('telefon') && $('telefon').length > 0) { return true; }
	else { return false; }
}


function checkAge(forceCheck){
                if( !document.getElementById('rok') ) return;
                var userYear = document.getElementById('rok').value;
                var alcotest = document.getElementById('alcotest');
                var alcotest_container = document.getElementById('alcotest_container');

                var d = new Date();
                if( userYear.length < 4 ){
                    alcotest.disabled = true;
                    alcotest.checked = false;
                    return false;
                }
                
                if( d.getYear() < 1000 ){
                    var year = d.getYear() + 1900;
                } else {
                    var year = d.getYear();                  
                }

                if( userYear >= year - 19 ){
                    alcotest.disabled = true;
                    alcotest.checked = false;
                    alcotest_container.style.display = 'none';
		    document.getElementById('alcotest_surname').style.display = 'none';
                    return;
                } else {
                    alcotest.disabled = false;
                    alcotest_container.style.display = 'block';
		if( alcotest.checked ){
		    document.getElementById('alcotest_surname').style.display = 'block';
		}else{
		    document.getElementById('alcotest_surname').style.display = 'none';
		}
      
                    alcotest.onchange = function(){
                        if( alcotest.checked ){
                            document.getElementById('alcotest_surname').style.display = 'block';
                        }else{
                            document.getElementById('alcotest_surname').style.display = 'none';
                        }
                    }
                    return;
                }            }

function explanation(displayed) {
	if (!displayed) {
		var msg = document.createElement('li');
		msg.setAttribute('id','explanation');
		$('pytanieCheck').parentNode.insertBefore(msg,$('pytanieCheck'));
		$('explanation').update('<p>Wybrałeś bezpieczną metodę odzyskiwania hasła przez SMS-a. Nie możesz skorzystać z metody "pytanie i odpowiedź". Aby móc skorzystać z metody "pytanie i odpowiedź", pozostaw pole "Numer telefonu komórkowego" puste.');
	} else { 
		$('explanation').remove();
	}
}

function startup() {
	toggle_question();
	if(checkPhone()) {
		questionCheckToggle(0);
		explanation(explanationPresent);
		explanationPresent = true;
	}
    if( $('telefon') ){
        $('telefon').observe('keyup', function(e) { 
                    if(checkPhone() && !explanationPresent) {
                        questionCheckToggle(0);
                        explanation(explanationPresent);
                        explanationPresent = true;
                    } else if (!checkPhone()&& explanationPresent) {
                        questionCheckToggle(1);
                        explanation(explanationPresent);
                        explanationPresent = false;
                    }
                }
            );
    }
    checkAge(true);  
}
function sprawdz_sile(src_id,dst_id,zabronione_id){
	
	var src = document.getElementById(src_id);
	var points = 0;
	var haslo = src.value;//pass.value;


	//Ocena wykorzystanych znaków 
		if(haslo.match(/[a-z]/)!=null) 			points += 1;
		if(haslo.match(/[A-Z]/)!=null) 			points += 1;
		if(haslo.match(/[0-9]/)!=null) 			points += 1;	
		if(haslo.match(/[^a-zA-Z0-9\s]/)!=null) 	points += 2;
	//Ocena grup znaków
		if(haslo.match(/[a-z]/)!=null && haslo.match(/[A-Z]/)!=null) 			points += 4;
		if(haslo.match(/[a-zA-Z]/)!=null && haslo.match(/[0-9]/)!=null) 			points += 4;
		if(haslo.match(/[a-zA-Z]/)!=null && haslo.match(/[^a-zA-Z0-9\s]/)!=null)	points += 4;	
		if(haslo.match(/[a-zA-Z]/)!=null && haslo.match(/[^a-zA-Z0-9\s]/)!=null && haslo.match(/[0-9]/)!=null) points += 4;	
	//Ocena długości hasła
		if(haslo.length >= 10) 		points += 8;
		else if(haslo.length >= 7) 	points += 4;
		else if(haslo.length >= 5) 	points += 1;
		else points = -1;
	//Max 22pkt

	if(zabronione_id!=null && points>0){
		for(var i in zabronione_id){
			var element = document.getElementById(zabronione_id[i]);
			if(element && element.value == haslo) points = -2;
		}
	}


//Wyświetla jeżeli podane jest id elementu docelowego
	var border;
	var opis;
	if(dst_id==null) return points;
	var ret = document.getElementById(dst_id);
	if(points >= 20){
		border = "3px solid #0c0";
		opis = "[?? Bardzo mocne hasło ??]";
	}
	else if(points >= 11){
		border = "3px solid #5f5";
		opis = "[?? Mocne haslo ??]";
	}
	else if(points >= 7){
		border = "3px solid #ff0";
		opis = "[?? Średnie hasło ??]";
	}
	else if(points >= 4){
		border = "3px solid #f00";
		opis = "[?? Słabe hasło ??]";
	}
	else if(points >= 0){
		border = "3px solid #d00";
		opis = "[?? Bardzo słabe hasło ??]";
	}
	else if(points == -1){
		border = "3px solid #ddd";
		opis = "[?? za krótkie ??]";
	}
	else if(points == -2){
		border = "3px solid #d00";
		opis = "[?? haslo takie samo jak login ??]";
	}		
		//ret.style.borderBottom = border;
		//document.all.dst_id.style.borderBottom = border;
		ret.setAttribute("style","border-bottom:"+border+";");
		ret.innerHTML = opis;	
}
function porownaj(src1_id, src2_id)
{
	var src1 = document.getElementById(src1_id);
	var src2 = document.getElementById(src2_id);
	if(src1.value == src2.value)
	{
		src2.style.background = "#fff";
		return true;	
	}
	else
	{
		src2.style.background = "#f00";
		return false;	
	}
}

