// KONTROLA FORMULARE PRI POSILANI ZPRAVY
function checkKontakt() {
	var jmeno = document.getElementById("jmeno");
	var prijmeni = document.getElementById("prijmeni");
	var email = document.getElementById("email");
	var zprava = document.getElementById("zprava");
	var hlaska = document.getElementById("err");
	var hlaska2 = document.getElementById("err_e");
	var err;
	var email_format;
	var color_false = "#8396FF";
	var color_true = "#FFFFFF";
	if (jmeno.value == "") {
		err = true;
		jmeno.style.backgroundColor = color_false;
	} else {
		jmeno.style.backgroundColor = color_true;
	}
	if (prijmeni.value == "") {
		err = true;
		prijmeni.style.backgroundColor = color_false;
	} else {
		prijmeni.style.backgroundColor = color_true;
	}
	if (email.value == "") {
		err = true;
		email.style.backgroundColor = color_false;
	} else {
		if (email.value.search("^[a-zA-z0-9._-]+@[a-zA-z0-9._-]+\.[A-za-z]{2,4}$") != 0) {
			email_format = true;
			email.style.backgroundColor = color_false;
		} else { 
			email.style.backgroundColor = color_true;
		}
	}
	if (zprava.value.length < 1) {
		err = true;
		zprava.style.backgroundColor = color_false;
	} else {
		zprava.style.backgroundColor = color_true;
	}
	if (err) {
		hlaska.style.display = "block";
		return false;
	} else {
		if (email_format) {
			hlaska.style.display = "none";
			hlaska2.style.display = "block";
			return false;
		} else {
			hlaska.style.display = "none";
			hlaska2.style.display = "none";
			return true;
		}
	}
}

function inputChange(prvek) {
	
}

// KONTROLA FORMULARE PRI PRIHLASOVANI
function kontrolaLoginu() {
  var n = document.getElementById('nick');
  var h = document.getElementById('heslo');
  nick = n.value;
  nick = trim(nick);
  heslo = h.value;
  heslo = trim(heslo);
  if (nick == "" || heslo == "") {
   err = document.getElementById('err');
   err.style.display = "block";
   return false;
  } else {
    err = document.getElementById('err');
    err.style.display = "none";
    return true;
  }
}

function trim(n) {
  return n.replace(/^\s*|\s*$/g,"");
}

// KONTROLA FORMU PRI REGISTRACI
function kontrolaRegistrace() {
  div = document.getElementById('err');
  if(kontrolaJmena() && kontrolaPrijmeni() && kontrolaNicku() && kontrolaHesla1() && kontrolaHesla2()) {
    div.style.display = "none";
    if(shodnostHesel()) {
      return true;
    } else {
      return false;
    }
  } else {
    div.style.display = "block";
    return false;
  }
}

function kontrolaJmena() {
  var jmeno = document.getElementById('jmeno');
  j = jmeno.value;
  j = trim(j);
  if (j == "") {
    jmeno.style.backgroundColor = "#E20000";
    return false;
  } else {
    jmeno.style.backgroundColor = "#E6DDB9";
    return true;
  } 
}

function kontrolaPrijmeni() {
  var prijmeni = document.getElementById('prijmeni');
  p = prijmeni.value;
  p = trim(p);
  if (p == "") {
    prijmeni.style.backgroundColor = "#E20000";
    return false;
  } else {
    prijmeni.style.backgroundColor = "#E6DDB9";
    return true;
  } 
}

function kontrolaNicku() {
  var nick = document.getElementById('nick');
  n = nick.value;
  n = trim(n);
  if (n == "") {
    nick.style.backgroundColor = "#E20000";
    return false;
  } else {
    nick.style.backgroundColor = "#E6DDB9";
    return true;
  } 
}

function kontrolaHesla1() {
  var heslo = document.getElementById('heslo1');
  h = heslo.value;
  //h = trim(heslo);
  if (h == "") {
    heslo.style.backgroundColor = "#E20000";
    return false;
  } else {
    heslo.style.backgroundColor = "#E6DDB9";
    return true;
  } 
}

function kontrolaHesla2() {
  var heslo = document.getElementById('heslo2');
  h = heslo.value;
  //h = trim(heslo);
  if (h == "") {
    heslo.style.backgroundColor = "#E20000";
    return false;
  } else {
    heslo.style.backgroundColor = "#E6DDB9";
    return true;
  } 
}

function shodnostHesel() {
  var heslo1 = document.getElementById('heslo1');
  var heslo2 = document.getElementById('heslo2');
  if (heslo1.value != heslo2.value) {
    alert("Uvedená hesla se neshodují!");
    return false;
  } else {
    return true;
  }
}

// KONTROLA TEXTAREY
function kontrolaTextu() {
  var texta = document.getElementById('text');
  var div = document.getElementById('err');
  if (texta.value.length < 1) {
    texta.style.backgroundColor = "#E20000";
    div.style.display = "block";
    return false;
  } else {
    texta.style.backgroundColor = "#E6DDB9";
    div.style.display = "none";
    return true;
  }
}

function kontrolaTema() {
  var tema = document.getElementById('nazev_tema');
  t = tema.value;
  t = trim(t);
  if (t == "") {
    tema.style.backgroundColor = "#E20000";
    return false;
  } else {
    tema.style.backgroundColor = "#E6DDB9";
    return true;
  } 
}

function kontrolaFormuNaTema() {
  div = document.getElementById('err');
  if(kontrolaTema() && kontrolaTextu()) {
    div.style.display = "none";
    return true; 
  } else {
    div.style.display = "block";
    return false;
  }
}

// VYCISTENI FORMULARE VCETNE VAROVNE HLASKY
function vycistiForm() {
  div = document.getElementById('err');
  div.style.display = "none";
  document.getElementById('jmeno').style.backgroundColor = "#E6DDB9";
  document.getElementById('prijmeni').style.backgroundColor = "#E6DDB9";
  document.getElementById('nick').style.backgroundColor = "#E6DDB9";
}

// KDYZ POLE ZTRATI FOCUS A JE VYPLNENO
function poleBlur() {
  j = document.getElementById('jmeno');
  if ((j.value != "") && (j.style.backgroundColor = "#E20000")) {
   j.style.backgroundColor = "#E6DDB9";
  }
  
  p = document.getElementById('prijmeni');
  if ((p.value != "") && (p.style.backgroundColor = "#E20000")) {
   p.style.backgroundColor = "#E6DDB9";
  }

  n = document.getElementById('nick');
  if ((n.value != "") && (n.style.backgroundColor = "#E20000")) {
   n.style.backgroundColor = "#E6DDB9";
  }
  
  h1 = document.getElementById('heslo1');
  if ((h1.value != "") && (h1.style.backgroundColor = "#E20000")) {
   h1.style.backgroundColor = "#E6DDB9";
  }
  
  h2 = document.getElementById('heslo2');
  if ((h2.value != "") && (h2.style.backgroundColor = "#E20000")) {
   h2.style.backgroundColor = "#E6DDB9";
  }
}

function poleBlur2() {
  t = document.getElementById('text');
  if ((t.value.length > 0) && (t.style.backgroundColor = "#E20000")) {
   t.style.backgroundColor = "#E6DDB9";
  }
  
  tema = document.getElementById('nazev_tema');
  if ((tema.value != "") && (tema.style.backgroundColor = "#E20000")) {
   tema.style.backgroundColor = "#E6DDB9";
  }  
}

//PRESMEROVANI Z SELECTU
function presmeruj(s) {
  var adresa = s.options[s.selectedIndex].value;
  if (adresa != "") window.location.href = adresa;
}

  function zmena(data, id_u, id_img){
	$(document).ready(function() {
	  $("#loader").show();

		$.ajax({
		type: "GET",
		url: "/administrace/pristup/",
		cache: false,
		data: "data=" + data + "&id=" + id_u + "&ajax=1&id2=" + id_img,
		success: function(html){
			         $("#" + id_img).html(html);
			         $("#loader").hide();	         
		}
		});
	}); 
}
