function Login(login, heslo) {
	var xmlHttp;

	try {  
		// Firefox, Opera 8.0+, Safari 
		xmlHttp=new XMLHttpRequest();  
	}
	catch (e) { 
		// Internet Explorer  
		try {    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
  		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
    		catch (e) {
				alert("Váš prohlížeč nepodporuje AJAX, kontaktujte administrátora na webmaster@medikapharm.com");      
				return false;      
			}    
		} 
	}
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==2)	{
			document.getElementById("login_dialog").innerHTML='<div id="AjaxLoginLoader"></div>';
		}
		
		if(xmlHttp.readyState==4)	{
			if (xmlHttp.status == 200) {
				var xmlResponse = xmlHttp.responseXML;
				xmlRoot = xmlResponse.documentElement;
				
				// Natiahnuť s db info či je user v db alebo nie
				active = xmlRoot.getElementsByTagName("active");
				active = active.item(0).firstChild.data;

				if (active==1) {
					// Stahovať xml data len keď sa v db nejaký user nachádza
					login = xmlRoot.getElementsByTagName("login").item(0).firstChild.data;
					datum = xmlRoot.getElementsByTagName("datum").item(0).firstChild.data;
					cas = xmlRoot.getElementsByTagName("cas").item(0).firstChild.data;
					pocet = xmlRoot.getElementsByTagName("novych").item(0).firstChild.data;
					
					if(document.getElementById("question_subject")!=null) {
						document.location="gynekologicka-poradna"; // musim refreshovat kvoli odstraneniu filtra :-/
					}
						
					// Stahovať xml data len ked sme na stánke --- objednať ---
					if(document.getElementById("meno")!=null) {
						document.location="kosik"; // musim refreshovat kvoli postovnemu :-/
						if(xmlRoot.getElementsByTagName("meno").item(0).firstChild != null)
							document.getElementById("meno").value=xmlRoot.getElementsByTagName("meno").item(0).firstChild.data;
						if(xmlRoot.getElementsByTagName("adresa").item(0).firstChild != null)
							document.getElementById("ulica").value=xmlRoot.getElementsByTagName("adresa").item(0).firstChild.data;
						if(xmlRoot.getElementsByTagName("mesto").item(0).firstChild != null)
							document.getElementById("mesto").value=xmlRoot.getElementsByTagName("mesto").item(0).firstChild.data;
						if(xmlRoot.getElementsByTagName("psc").item(0).firstChild != null)
							document.getElementById("psc").value=xmlRoot.getElementsByTagName("psc").item(0).firstChild.data;
						if(xmlRoot.getElementsByTagName("krajina").item(0).firstChild != null)
							document.getElementById("krajina").value=xmlRoot.getElementsByTagName("krajina").item(0).firstChild.data;
						if(xmlRoot.getElementsByTagName("email").item(0).firstChild != null)
							document.getElementById("ekontakt").value=xmlRoot.getElementsByTagName("email").item(0).firstChild.data;

						document.getElementById("StiahnutOsobneData").innerHTML="Ste prihlásený, preto sa kontaktné údaje stiahli automaticky z Vášho konta. Pokiaľ chcete pre túto objednávku niektorý z údajov zmeniť (napr. adresu doručenia) prepíšte príslušné polia. Tovar Vám bude zaslaný na adresu uvedenú v tomto formulári.";
					}
					// Načítať okno s údajmy o prihlásenom userovi
					ShowConnectedLoginDialog(login,datum,cas,pocet);
				}
				
				// Ak sa user v db nenachádza, načítať chybové hlásenie do login okna
				if (active==0) {
					ShowErrorDialog();
				}
			}
		}
	}

	if (login==undefined) {
		var login = document.getElementById("login").value;
		var heslo = document.getElementById("password").value;
	}
	
  	var url = "xml/login.php";
	params="login="+login+"&heslo="+heslo;

	xmlHttp.open("POST", url, true);

	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
		
	xmlHttp.send(params);
}

function Logout() {
	var xmlHttp;
	
	try {  
		// Firefox, Opera 8.0+, Safari 
		xmlHttp=new XMLHttpRequest();  
	}
	catch (e) { 
		// Internet Explorer  
		try {    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
  		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
    		catch (e) {
				alert("Váš prohlížeč nepodporuje AJAX, kontaktujte administrátora na webmaster@medikapharm.com");      
				return false;      
			}    
		} 
	}
	
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==2)	{
			document.getElementById("login_dialog").innerHTML='<div id="AjaxLoginLoader"></div>';
		}
		
		if(xmlHttp.readyState==4)	{
			if (xmlHttp.status == 200) {
				document.location="http://www.indol3c.cz"; // musim refreshovat kvoli postovnemu :-/
				if(document.getElementById("meno")!=null) {
					document.getElementById("meno").value="";
					document.getElementById("ulica").value="";
					document.getElementById("mesto").value="";
					document.getElementById("psc").value="";
					document.getElementById("krajina").value="";
					document.getElementById("ekontakt").value="";
					document.getElementById("poznamka").value="";
					document.getElementById("StiahnutOsobneData").innerHTML="Keďže ste sa práve odhlásili, vyplňte prosím objednávací formulár prípadne prihlasovací formulár <strong>vpravo hore</strong> a kontaktné údaje budú automaticky doplnené z databázy. Keďže už máte svoje konto u nás, využite výhody, ktoré ponúkame registrovaným zákazníkom.";
				}
				ShowNotConnectedLoginDialog("Přihlasovací jméno:","Heslo:","");		
			}
		}
	}

  	xmlHttp.open("GET", "xml/logout.php", true);
    xmlHttp.send(null);
}

function onEnter(e){
	var eventInstance = window.event ? event : e; 
	// handle i.e. (charCode) and firefox (keyCode) 
	var unicode = eventInstance.charCode ? eventInstance.charCode : eventInstance.keyCode; 
	
	onOK(unicode);
}

function onOK(unicode) {
	if (unicode==13) {
		if (document.getElementById("login").value=='' || document.getElementById("password").value=='') {
			alert ('Nezadali jste přihlasovací jméno nebo heslo');
		} else {
			Login();
		}
	}
}

function ShowConnectedLoginDialog(UserName,LastOnlineDate,LastOnlineTime,Pocet) {
	var html='';
	
	html+='	<div id="login_object">';
	html+='		<table width="100%" border="0" cellspacing="0" cellpadding="0">';
    html+='			<tr>';
  	html+=' 			<td>Vítejte</td>';
    html+='         	<td>&nbsp;</td>';
    html+='     	</tr>';
    html+='     	<tr>';
    html+='				<td><strong>'+UserName+'</strong></td>';
    html+='				<td>&nbsp;</td>';
   	html+='			</tr>';
	html+='			<tr>';
    html+='				<td>&nbsp;</td>';
    html+='				<td>&nbsp;</td>';
    html+='			</tr>';
    html+='			<tr>';
    html+='				<td>Naposledy jste byli online:</td>';
    html+='				<td>&nbsp;</td>';
    html+='			</tr>';
    html+='			<tr>';
    html+='				<td><strong>'+LastOnlineDate+' o '+LastOnlineTime+'</strong></td>';
    html+='				<td>&nbsp;</td>';
    html+='			</tr>';
   	html+='		</table>';
	html+='	</div>';
	html+='	<div id="small_menu">';
	html+='		<div style="padding-bottom:6px">Nových příspěvků od<br />poslední návštěvy: <strong>'+Pocet+'</strong></div>';
/*	html+='		<div><a href="index.php?page=agenda" rel="nofollow">Objednávky (15/0)</a></div>';*/
	html+='		<div><a href="kosik" rel="nofollow">Nákupný košík</a></div>';
/*	html+='		<div><a href="index.php?page=registracia" rel="nofollow"><b>Vaša pošta (1/1)</b></a></div>';*/
	html+='		<div><a href="setup" rel="nofollow">Vaše osobní údaje</a></div>';
	html+='		<div><a href="javascript:Logout()" rel="nofollow">Odhlásit se</a></div>';
	html+='	</div>';
			
	document.getElementById("login_dialog").innerHTML=html;

}

function ShowNotConnectedLoginDialog() {
	var html='';
	var LoginTitle='Přihlasovací jméno:';
	var PasswordTitle='Heslo:';
	
	html+='	<div id="login_object">';
	html+='		<table width="100%" border="0" cellspacing="0" cellpadding="0">';
    html+='			<tr>';
  	html+=' 			<td>Přihlasovací jméno:</td>';
    html+='         	<td>&nbsp;</td>';
    html+='     	</tr>';
    html+='     	<tr>';
    html+='				<td><input type="text" name="login" id="login" onkeypress="javascript:onEnter(event);" /></td>';
    html+='				<td>&nbsp;</td>';
   	html+='			</tr>';
    html+='			<tr>';
    html+='				<td>Heslo:</td>';
    html+='				<td>&nbsp;</td>';
    html+='			</tr>';
    html+='			<tr>';
    html+='				<td><input type="password" name="password" id="password" onkeypress="javascript:onEnter(event);" /><a href="javascript:onOK(13)"><img src="images/arrow_login.jpg" alt="Přihlásit se" /></a></td>';
    html+='				<td>&nbsp;</td>';
    html+='			</tr>';
   	html+='		</table>';
	html+='	</div>';
	html+='	<div id="small_menu">';
	html+='		<div><a href="registracia" rel="nofollow">Registrovat se</a></div>';
	html+='		<div><a href="kosik" rel="nofollow">Nákupní košík</a></div>';
	//html+='		<div><a href="index.php?page=vyhody_registracie">Výhody registrácie</a></div>';
	html+='		<div><a href="heslo" rel="nofollow">Zapomněli jste heslo?</a></div>';
	html+='	</div>';
	
	document.getElementById("login_dialog").innerHTML=html;
}

function ShowErrorDialog() {
	var html='';

	html+='	<div id="login_object">';
	html+='		<table style="color:#9F1727; padding-top:10px" width="100%" border="0" cellspacing="0" cellpadding="0">';
    html+='			<tr>';
  	html+=' 			<td>Zadali jste nesprávné přihlašovací jméno anebo heslo. Vyberte si z následujícího menu:</td>';
    html+='     	</tr>';
   	html+='		</table>';
	html+='	</div>';
	html+='	<div id="small_menu">';
	html+='			<div><a href="registracia" rel="nofollow">Zaregistrovat se...</a></div>';
	html+='			<div><a href="heslo" rel="nofollow">Zapomněli jste heslo?</a></div>';
	html+='			<div><a href="javascript:ShowNotConnectedLoginDialog();" rel="nofollow">Zkusit znova</a></div>';
	html+='	</div>';

	document.getElementById("login_dialog").innerHTML=html;
}