function AInit ()
{
  var ajax = (window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
  if (!ajax)
    alert ("Je nám líto, ale na Vašem počítačni není možné tuto funkci používat!");
  return ajax;
}


function ACheckLogin (login)
{
	if (login)
	{
		CheckLogin = AInit ();
  	CheckLogin.onreadystatechange = function ()
		{
			ACheckLoginProcess (CheckLogin);
	  }
		CheckLogin.open ("POST", "wasp/user/user_ajax.php", true);
		CheckLogin.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
		CheckLogin.send ("action=CheckLogin&login=" + login);
	}
	else
	{
		document.getElementById('fregistration_CheckLogin').innerHTML = "<span class=\"message_warrning\">Musíte zadat uživatelské jméno!</span>";
		CheckRegistration['login'] = 0;
	}
}
										
function ACheckLoginProcess (CheckLogin)
{
	var responseText;
  if (CheckLogin.readyState == 4)
  {
    if(CheckLogin.status == 200 || CheckLogin.status==0)
    {
    	user = new Array ();
      txtResponse = CheckLogin.responseText;
      eval (txtResponse);
      if (user['id'])
      {
	      document.getElementById('fregistration_CheckLogin').innerHTML = "<span class=\"message_warrning\">Toto uživatelské jméno je již registrováno!</span>";
				CheckRegistration['login'] = 0;
			}
			else
			{
	      document.getElementById('fregistration_CheckLogin').innerHTML = "<span class=\"message_succesful\">Toto uživatelské jméno lze použít.</span>";
				CheckRegistration['login'] = 1;
			}
    }
    else 
      alert("Chyba: "+ CheckLogin.status +"\n"+ CheckLogin.statusText);
  }
}

function CheckPassword ()
{
	if (document.getElementById('registration_password').value != document.getElementById('registration_check_password').value)
	{
	  document.getElementById('fregistration_CheckPassword').innerHTML = "<span class=\"message_warrning\">Zadaná hesla nejsou stejná!</span>";
		return false;
	}
	else
	{
	  document.getElementById('fregistration_CheckPassword').innerHTML = "";
		return true;
	}
}
										
function CheckEmail (email)
{
	var result = true;
    var pozice_zavinace = email.indexOf("@");
    if (pozice_zavinace < 0)
        result = false;
    var cast_pred_zavinacem = email.substring(0,pozice_zavinace);
    var cast_po_zavinaci = email.substring(pozice_zavinace+1,email.length);
    if (cast_po_zavinaci.indexOf("@") >= 0)
        result = false;
    if (cast_pred_zavinacem.length <= 0)
        result = false;
    if (cast_po_zavinaci.length <= 0)
        result = false;
    var pozice_posledni_tecky = cast_po_zavinaci.lastIndexOf(".");
    if (pozice_posledni_tecky < 0)
        result = false;
    var pocet_znaku_za_posledni_teckou = cast_po_zavinaci.length - pozice_posledni_tecky - 1;
    if (pocet_znaku_za_posledni_teckou < 2  ||  pocet_znaku_za_posledni_teckou > 3)
        result = false;
    var pozice_dvou_tecek_vedle_sebe = email.indexOf("..");
    if (pozice_dvou_tecek_vedle_sebe >= 0)
        result = false;
    if (cast_pred_zavinacem.charAt(0) == "."  ||  cast_pred_zavinacem.charAt(cast_pred_zavinacem.length-1) == ".")
        result = false;
    if (cast_po_zavinaci.charAt(0) == "."  ||  cast_po_zavinaci.charAt(cast_po_zavinaci.length-1) == ".")
        result = false;
	if (!result && email)
	{
	  document.getElementById('fregistration_CheckEmail').innerHTML = "<span class=\"message_warrning\">E-mailá adresa, kterou jste zadali není správná!</span>";
		CheckRegistration['email'] = 0;
	}
	else
	{
		document.getElementById('fregistration_CheckEmail').innerHTML = "<br />"
		CheckRegistration['email'] = 1;
	}
}

function CheckFormRegistration ()
{
	var result = 1;
	if (!document.getElementById('registration_login').value) 
	{
    document.getElementById('fregistration_CheckLogin').innerHTML = "<span class=\"message_warrning\">Musíte zadat uživatelské jméno!</span>";
    result = 0;
	}
	else if (!CheckRegistration['login'])
	{
    result = 0;
	}
	
	if (!document.getElementById('registration_password').value) 
	{
	  document.getElementById('fregistration_CheckPassword').innerHTML = "<span class=\"message_warrning\">Musíte zadat heslo!</span>";
    result = 0;
	}
	else if(!CheckPassword())
	{
    result = 0;
	}
	

 	if (!CheckRegistration['email'] && document.getElementById('registration_email').value)
	{
		result += "Ve zadání vašeho e-mailu se objevila chyba. Zkontrolujte prosím vámi zadaný email.\n";
    result = 0;
	}
	
	if (result)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function CheckKeyPress (keyCode, strict, message)
{
//	alert (keyCode);
	switch (strict)
	{
	  case "tn":
			if
			(
				(keyCode > 64 && keyCode < 91) || // A..Z
				(keyCode > 96 && keyCode < 123) || // a..z
				(keyCode > 47 && keyCode < 58)  || // 0..9
				(keyCode != 8) // enter
			)
			{
			  document.getElementById(message).innerHTML = "";
				return true;
			}
			else
			{
			  document.getElementById(message).innerHTML = "<span class=\"message_warrning\">Povolené jsou pouze písmena nebo číslice.</span>";
				return false;
			}
	}
}


var CheckRegistration = new Array ('login', 'password', 'email');

