var sth, kOK, uOK, eOK, lOK, iOK, pOK;

function ProvjeraKljuca( th ) {
	if( th === "undefined" ) {
		th = sth;
	} else {
		sth = th;
	}
	if( http && !uPogonuSam ) {
		http.open( "GET", "korisnici.php?action=ProvjeriKljuc&kljuc="+th.value, true );
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				if( http.responseText != "1" ) {
					alert( http.responseText );
					th.select();
					th.focus();
					kOK = 0;
				} else {
					kOK = 1;
				}
			}
		}
		uPogonuSam = true;
		http.send( null );		
	} else {
		setTimeout( "ProvjeraKljuca()", 10 );
	}
}

function ProvjeraUlimena( th ) {
	if( th === "undefined" ) {
		th = sth;
	} else {
		sth = th;
	}
	if( th.value.length < 2 ) {
		alert( "Ulazno ime je prekratko! ");
//		th.select();
		th.focus();
	} else if( http && !uPogonuSam ) {
		http.open( "GET", "korisnici.php?action=ProvjeriUlime&ulime="+th.value, true );
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				if( http.responseText != "1" ) {
					alert( http.responseText );
					th.select();
					th.focus();
					uOK = 0;
				} else {
					uOK = 1;
				}				
			}
		}
		uPogonuSam = true;
		http.send( null );		
	} else {
		setTimeout( "ProvjeraKljuca()", 10 );
	}
}

function ProvjeraEmaila( th ) {
	if( th === "undefined" ) {
		th = sth;
	} else {
		sth = th;
	}
	if( jeMail( th.value ) ) {
		if( http && !uPogonuSam ) {
			http.open( "GET", "korisnici.php?action=ProvjeriEmail&email="+th.value, true );
			http.onreadystatechange = function() {
				if( http.readyState == 4 ) {
					uPogonuSam = false;
					if( http.responseText != "1" ) {
						if( confirm("Ova email adresa je već registrirana!\nŽelite li da Vam na nju pošaljemo postojeće podatke?") ) {
							location = "korisnici.php?action=PosaljiPodatke&email="+th.value;
						}
						th.select();
						th.focus();
						eOK = 0;
					} else {
						eOK = 1;
					}
				}
			}
			uPogonuSam = true;
			http.send( null );		
		} else {
			setTimeout( "ProvjeraKljuca()", 10 );
		}
	} else {
		alert("Email adresa nije ispravno upisana!");
//		th.select();
		th.focus();
	}
}

function ProvjeraLozinke( th ) {
	if( th === "undefined" ) {
		th = sth;
	} else {
		sth = th;
	}
	if( http && !uPogonuSam ) {
		http.open( "POST", "korisnici.php", true );
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				if( http.responseText != "1" ) {
					alert( "UPISITE LOZINKU!\n\nNe mozete mijenjati podatke\nako u odgovarajuce polje\nne upisete svoju trenutnu lozinku!" );
					th.select();
					th.focus();
					lOK = 0;
				} else {
					lOK = 1;
				}
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		http.send( "action=ProvjeriLozinku&ulime="+th.form.ulime.value+"&lozinka="+th.value );		
	} else {
		setTimeout( "ProvjeraLozinke()", 10 );
	}
}

function ProvjeraImena( th ) {
	if( th.value.length < 2 ) {
		alert( "Ime je prekratko!" );
//		th.select();
		th.focus();
		iOK = 0;
	} else {
		iOK = 1;
	}
}

function ProvjeraPrezimena( th ) {
	if( th.value.length < 2 ) {
		alert( "Prezime je prekratko!" );
//		th.select();
		th.focus();
		pOK = 0;
	} else {
		pOK = 1;
	}

}

function ProvjeraNC( f ) {
	ProvjeraKljuca( f.kljuc );
	ProvjeraUlimena( f.ulime );
	ProvjeraEmaila( f.email );
	ProvjeraImena( f.ime );
	ProvjeraPrezimena( f.prezime );
	if( kOK + uOK + eOK + iOK + pOK == 5 ) {
		f.submit();
	}
}

lOK = 0;

function ProvjeraSC( f ) {
	ProvjeraLozinke( f.ulkod );
	if( f.ime.value.length < 2 ) {
		alert( "Ime je prekratko!" );
		f.ime.focus();
	} else if ( f.prezime.value.length < 2 ) {
		alert( "Prezime je prekratko!" );
		f.prezime.focus();
	} else if( ! lOK ) {
		alert( "UPISITE LOZINKU!\n\nNe mozete mijenjati podatke\nako u odgovarajuce polje\nne upisete svoju trenutnu lozinku!" );
		f.ulkod.focus();
	} else if( ! jeMail( f.email.value ) ) {
		alert( "Mail možda nije ispravno upisan!" );
		f.email.focus();
	} else if( ( puno( f.novikod.value ) || puno( f.novikod2.value ) ) && ( f.novikod.value != f.novikod2.value ) ) {
		alert( "Lozinke se razlikuju!" );
		f.novikod2.focus();
	} else {
		f.submit();
	}
}

function UsporedbaNovihLozinki(a, b) {
	if(a.value != b.value) {
		alert( "Novu lozinku morate dva puta jednako upisati!\nPonovite!" );
		a.select();
		a.focus();
	}
}

function PokaziKorisnika( ulime ) {
	var jk = document.getElementById( "JedanKorisnik" );
	jk.innerHTML = "<h3>Trazim podatke o korisniku ["+ulime+"]<\/h3>";
	if( http && !uPogonuSam ) {
		http.open( "GET", "korisnici.php?action=Vidi2&ulime="+ulime, true );
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				jk.innerHTML = http.responseText;
			}
		}
		uPogonuSam = true;
		http.send( null );		
	}
}
