﻿function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function zobraz_podmenu_pre(id_menu) {
	var obj = document.getElementById('submenu'+id_menu);
	obj.style.visibility = 'visible';
}

function zmaz(id_menu) {
	var obj = document.getElementById('submenu'+id_menu);
	obj.style.visibility = 'hidden';
}

function zmaz_podmenu_pre(id_menu, status) {
	if (status == '1') {
		window.clearTimeout(t);
	}
	if (status == '0')
		t=window.setTimeout("zmaz("+id_menu+")",200);
}

function linka(url) {
	window.location=url;
}

function formValidator(){
	var email = document.getElementById('ekontakt');
	var heslo = document.getElementById('heslo');
	var heslo2 = document.getElementById('heslo_rep');
	var meno = document.getElementById('meno');
	
	
	// Check each input in the order that it appears in the form!
	if(emailValidator(email, "Prosím zadajte správnu emailovú adresu")){
		if(heslo_lengthRestriction(heslo, 5, 12)){
			if(heslo.value != heslo2.value)
				alert("Nesprávne potvrdené heslo!")
			else if (nick_lengthRestriction(meno, 2, 100)) {
				document.app_form.submit();
			}
		}
	}
	return false;
}

function forgotValidator(){
	var email = document.getElementById('ekontakt');
	
	// Check each input in the order that it appears in the form!
	if(emailValidator(email, "Prosím zadajte správnu emailovú adresu")) {
		document.app_form.submit();
	}
	return false;
}

function formValidatorUpdate(){
	var email = document.getElementById('ekontakt');
	var heslo = document.getElementById('heslo');
	var heslo2 = document.getElementById('heslo_rep');
	var meno = document.getElementById('meno');
	if (heslo.value=='' && heslo2.value=='') {
		i=0;
	}
	else
		i=5;
	
	// Check each input in the order that it appears in the form!
	if(emailValidator(email, "Prosím zadajte správnu emailovú adresu")){
		if(heslo_lengthRestriction(heslo, i, 12)){
			if(heslo.value != heslo2.value)
				alert("Nesprávne potvrdené heslo!")
			else if (nick_lengthRestriction(meno, 2, 100)) {
				document.app_form.submit();
			}
		}
	}
	return false;
}

function orderValidator(){
	var ekontakt = document.getElementById('ekontakt');
	var meno = document.getElementById('meno');
	var adresa = document.getElementById('adresa');
	var psc = document.getElementById('psc');
	var obec = document.getElementById('obec');
	
	// Check each input in the order that it appears in the form!

	if(emailValidator(ekontakt, "Prosím zadajte správnu emailovú adresu")){
		if(meno_lengthRestriction(meno, 1, 200)){
			if(adresa_lengthRestriction(adresa, 1, 200)){
				if(psc_lengthRestriction(psc, 1, 10)){
					if(obec_lengthRestriction(obec, 1, 200)){
						document.app_form.submit();
					}
				}
			}
		}
	}
	return false;

}

function loginValidator(){
	var email = document.getElementById('ekontakt');
	var heslo = document.getElementById('heslo');
	
	// Check each input in the order that it appears in the form!
	if(emailValidator(email, "Prosím zadajte správnu emailovú adresu")){
		if(heslo_lengthRestriction(heslo, 5, 12))
				document.app_form.submit();
	}
	return false;

}

function nick_lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Zadajte prosím cele meno");
		elem.focus();
		return false;
	}
}

function adresa_lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Zadajte prosím adresu a číslo");
		elem.focus();
		return false;
	}
}

function meno_lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Zadajte prosím svoje meno");
		elem.focus();
		return false;
	}
}

function psc_lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Zadajte prosím PSČ");
		elem.focus();
		return false;
	}
}

function obec_lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Zadajte prosím obec");
		elem.focus();
		return false;
	}
}

function heslo_lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("HESLO musí mat minimálne " +min+ " a maximálne " +max+ " znakov");
		elem.focus();
		return false;
	}
}

function emailValidator(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

	if(elem.value.match(emailExp)){
		return true;
	}
	else{
	
		alert(helperMsg);
		elem.focus();
		return false;
	}
}
