//Nastaveni Reguired(povinnost vyplneni textboxu) true, false
function SetTextBoxRequired(ID, stav){
	if(stav=="True" || stav=="true" || stav==true){
		document.all.item(ID).Required="True";
		document.all.item(ID).style.backgroundColor="lemonchiffon";
	}	
	if(stav=="False" || stav=="false" || stav==false){
		document.all.item(ID).Required="False";
		document.all.item(ID).style.backgroundColor="#FFFFFF";
	}	
}

//otestuje zda se jedna o existujici datum
function _OtestujDatum(den, mesic, rok)
{
	mesic=mesic-1;
	datum = new Date(rok,mesic,den);						
	var testDen = datum.getDate();
	var testMesic = datum.getMonth();						
	var testRok = datum.getFullYear();
	if ((testDen != den) || (testMesic != mesic) || (testRok != rok)){
		return false;	
	}
	else {
		return true;		
	}	
}

//otestuje zda se jedna pouze o ciselne znaky
function _TestIntZnaku(strTest){
	for(x=0; x<strTest.length; x++){
		if(isNaN(strTest.substring(x,x+1)) == true) return false;
	}
	return true;
}

//otestuje zda se jedna pouze o neciselne znaky
function _TestCharZnaku(strTest){
	for(x=0; x<strTest.length; x++){
		if(isNaN(strTest.substring(x,x+1)) == false) return false;
	}
	return true;
}

//nastavi textbox dle nastavenych parametru, vymaze obsah nastavi TestOK ...
function _NastavTbox(ID, stav){
	document.all.item(ID).TestOK=stav;
	if(stav == false || stav == "false" || stav == "False"){
		var mazat = document.all.item(ID).MazatPriChybe;
		if(mazat == true || mazat == "true" || mazat == "True"){
			document.all.item(ID).value="";
			document.all.item(ID).TestOK=true;
		}
		else document.all.item(ID).select();
	}
	var hlidat = document.all.item(ID).HlidatZmenu;
	if(hlidat == "True" || hlidat == "true" || hlidat == true)	
		document.all.item("_zmena_formulare").value = "1";
}

// pomocna funkce
//function _OnChangeTbox(ID,funkce){
//	var hlidat = document.all.item(ID).HlidatZmenu;
//	if(hlidat == "True" || hlidat == "true" || hlidat == true)	
//		document.all.item("_zmena_formulare").value = "1";
//	if(funkce.length > 0) return eval(funkce);
//	return true;
//}

function _OnChangeTbox(ID){
	var _hlidat = document.all.item(ID).HlidatZmenu;
	var _ValueType = document.all.item(ID).ValueType;
	var _OnChangeFunction = document.all.item(ID).OnChangeFunction;
	var _OddelovatTisice = document.all.item(ID).OddelovatTisice;
	var _PamatovatVyslProToolbar = false;
	
	if(document.all.item("_toolbar_focus_test_obj") != null){
		_PamatovatVyslProToolbar = true;
		document.all.item("_toolbar_focus_test_obj").value = "0";
	}
	
	if(_OnChangeFunction == null || _OnChangeFunction == "undefined") _OnChangeFunction = "";
	if(_OddelovatTisice == null || _OddelovatTisice == "undefined") _OddelovatTisice = "";
		
	if(_hlidat == "True" || _hlidat == "true" || _hlidat == true)	
		document.all.item("_zmena_formulare").value = "1";
		
	if(_ValueType == "RC_ICO" || _ValueType == "RC" || _ValueType == "ICO"){
		if(!_KontrolaRC_ICO(ID,_ValueType,"")) return false;
	}
	
	if(_ValueType == "Decimal" || _ValueType == "Int"){
		var _MinCis = document.all.item(ID).MinCis;
		var _MaxCis = document.all.item(ID).MaxCis;
		if(_MinCis == null || _MinCis == "undefined") _MinCis = 0;	
		if(_MaxCis == null || _MaxCis == "undefined") _MaxCis = 0;	
		if(_ValueType == "Decimal")
			if(!_KontrolaDecimal(ID,_MinCis,_MaxCis,"")) return false;
		if(_ValueType == "Int")
			if(!_KontrolaInt(ID,_MinCis,_MaxCis,"")) return false;
	}
	
	if(_ValueType == "Date" || _ValueType == "DateTime"){
		var _MinDatum = document.all.item(ID).MinDatum;
		var _MaxDatum = document.all.item(ID).MaxDatum;
		if(_MinDatum == null || _MinDatum == "undefined") _MinDatum = "";
		if(_MaxDatum == null || _MaxDatum == "undefined") _MaxDatum = "";	
		if(_ValueType == "Date")
			if(!_KontrolaDate(ID,_MinDatum,_MaxDatum,"")) return false;
		if(_ValueType == "DateTime")	
			if(!_KontrolaDateTime(ID,_MinDatum,_MaxDatum,"")) return false;
	}
	
	if(_ValueType == "DIC"){
		if(!_KontrolaDIC(ID, "")) return false;
	}
	
	if(_ValueType == "Time"){
		if(!_KontrolaTime(ID, "")) return false;
	}
	
	if(_ValueType == "E_Mail"){
		if(!_KontrolaEmail(ID)) return false;
	}
		
	if(_OddelovatTisice.toLowerCase() == "true" || _OddelovatTisice == true)
		_ProvedOddeleniTis(ID);
	
	var _returnVal = true;
	if(_OnChangeFunction.length > 0) _returnVal = eval(_OnChangeFunction);
	if(_PamatovatVyslProToolbar == true && _returnVal == true)
		document.all.item("_toolbar_focus_test_obj").value = "1";
	return _returnVal;	
}


//otestuje zda jde o Int a zda splnuje urcenz rozsah min-max
function _KontrolaInt(ID, min, max, funkce)
{
	var hodnota = document.all.item(ID).value;
	hodnota = hodnota.replace(/ /g, "");
	if(hodnota != ""){
		testHodnota = hodnota;
		if(testHodnota.substring(0,1) == "-" && testHodnota.length > 1) testHodnota=testHodnota.substring(1,testHodnota.lenght);
		if(_TestIntZnaku(testHodnota) == false){
			window.alert(unescape("Mus%ED%20b%FDt%20vypln%u011Bn%E9%20cel%E9%20%u010D%EDslo.%20%28p%u0159%EDklad%3A%20128%29"));
			_NastavTbox(ID, false);
			return false;
		}
		strChyba="";
		if(min != 0 || max != 0){
			if(parseFloat(hodnota) < min) strChyba="Hodnota%20mus%ED%20b%FDt%20minim%E1ln%u011B%3A "+min+".";
			if(parseFloat(hodnota) > max) strChyba="Hodnota%20mus%ED%20b%FDt%20maxim%E1ln%u011B%3A "+max+".";
		}
		if(strChyba != ""){
			window.alert(unescape(strChyba));
			_NastavTbox(ID, false);
			return false;
		}
	}
	_NastavTbox(ID, true)
	if(funkce.length > 0) return eval(funkce);
	return true;	
}

//otestuje zda se jedna o Decimal cislo a zda splnuje urceny rozsah min-max
function _KontrolaDecimal(ID, min, max, funkce){
	var hodnota = document.all.item(ID).value;
	hodnota = hodnota.replace(/ /g, "");
	if(hodnota != "")
	{		
		ndecimal = hodnota.replace(",",".");
		if(isNaN(ndecimal) == true)
		{
			window.alert(unescape("Mus%ED%20b%FDt%20vypln%u011Bn%E9%20%u010D%EDslo.%20%28p%u0159%EDklad%3A%20128%20nebo%20128.5%29"));
			_NastavTbox(ID, false);
			return false;
		}
		strChyba="";
		if(min != 0 || max != 0){
			if(parseFloat(ndecimal) < min) strChyba="Hodnota%20mus%ED%20b%FDt%20minim%E1ln%u011B%3A "+min+".";
			if(parseFloat(ndecimal) > max) strChyba="Hodnota%20mus%ED%20b%FDt%20maxim%E1ln%u011B%3A "+max+".";
		}
		if(strChyba != ""){
			window.alert(unescape(strChyba));
			_NastavTbox(ID, false);
			return false;
		}
	}
	_NastavTbox(ID, true)
	if(funkce.length > 0) return eval(funkce);
	return true;
}

//kontrola spravneho formatu datumu a rozmezi min,max
function _KontrolaDate(ID, min, max, funkce){
	var hodnota = document.all.item(ID).value;
	if(hodnota != "")
	{		
		hodnota = hodnota.replace("/",".");
		hodnota = hodnota.replace("/",".");
		hodnota = hodnota.replace(",",".");
		hodnota = hodnota.replace(",",".");
		rok = hodnota.substring(hodnota.lastIndexOf(".")+1,hodnota.length);
		hodnota=hodnota.substring(0, hodnota.lastIndexOf("."));
		mesic = hodnota.substring(hodnota.lastIndexOf(".")+1,hodnota.length);
		hodnota=hodnota.substring(0, hodnota.lastIndexOf("."));
		den = hodnota;
		if(_OtestujDatum(den, mesic, rok) == false || rok > 9999 || rok < 1754){
			window.alert(unescape("Mus%ED%20b%FDt%20vypln%u011Bn%20platn%FD%20datum.%20%28p%u0159%EDklad%3A%2031.12.2001%29"));
			_NastavTbox(ID, false);
			return false;
		}
		mesic = "0"+mesic; den = "0"+den; chyba = 0; strChyba="";
		datum = rok+mesic.substring(mesic.length-2, mesic.length)+den.substring(den.length-2, den.length);
		if(min != 10101 && min != ""){
			if(min > datum){
				strChyba = "Datum%20mus%ED%20b%FDt%20minim%E1ln%u011B%20od%3A "+min.substring(6,8)+"."+min.substring(4,6)+"."+min.substring(0,4);
				chyba = 1;
			}
		}
		if(max != 10101 && min != ""){
			if(max < datum){
				strChyba = "Datum%20mus%ED%20b%FDt%20maxim%E1ln%u011B%20do%3A "+max.substring(6,8)+"."+max.substring(4,6)+"."+max.substring(0,4);
				chyba = 1;
			}
		}
		if(strChyba != ""){
			window.alert(unescape(strChyba));
			_NastavTbox(ID, false);
			return false;
		}
	}
	_NastavTbox(ID, true);
	if(funkce.length > 0) return eval(funkce);
	return true;
}

//funkce otestuje platnost rodneho cisla nebo ICO
function _KontrolaRC_ICO(ID, Typ, funkce){
	var hodnota = document.all.item(ID).value;
	if(hodnota != "")
	{		
		if(hodnota.length > 8 && hodnota.substring(6,7)=="/"){
			hodnota=hodnota.substring(0,6)+hodnota.substring(7,hodnota.length);
		}
		if(_TestIntZnaku(hodnota) == false){
			if(Typ == "ICO")
				window.alert(unescape("Mus%ED%20b%FDt%20vypln%u011Bny%20pouze%20%u010D%EDslice."));
			else
				window.alert(unescape("Mus%ED%20b%FDt%20vypln%u011Bny%20pouze%20%u010D%EDslice.%20R%u010C%20m%E1%20povolen%FD%20znak%20/.%20%28p%u0159%EDklad%20R%u010C%3A%20000000/0000%29"));	
			_NastavTbox(ID, false);
			return false;
		}
		if((hodnota.length < 8 || hodnota.length > 10) && Typ == "RC_ICO" ){
			window.alert(unescape("Mus%ED%20b%FDt%20vypln%u011Bno%208-10%20%u010D%EDslic.%20%28I%u010CO-8%20%u010D%EDslic%2C%20R%u010C-9%2C10%20%u010D%EDslic%29"));
			_NastavTbox(ID, false);
			return false;
		}
		if((hodnota.length < 9 || hodnota.length > 10) && Typ == "RC" ){
			window.alert(unescape("R%u010C%20mus%ED%20obsahovat%209%2C10%20%u010D%EDslic.%20R%u010C%20m%E1%20povolen%FD%20znak%20/.%20%28p%u0159%EDklad%20R%u010C%3A%20000000/0000%29."));
			_NastavTbox(ID, false);
			return false;
		}
		if(hodnota.length != 8 && Typ == "ICO" ){
			window.alert(unescape("I%u010CO%20mus%ED%20obsahovat%208%20%u010D%EDslic."));
			_NastavTbox(ID, false);
			return false;
		}
		if(hodnota.length == 8){
			sum = 0;
			if(hodnota == 99999999){_NastavTbox(ID, true); if(funkce.length > 0) return eval(funkce); return true;}
			for(i=0; i<7; i++){
				sum=sum+(9-(i+1))*parseInt(hodnota.substring(i,i+1));
			}
			sum = 11-(sum%11);
			if(sum >= 10) sum=sum-10;
			if(sum != parseInt(hodnota.substring(7,8))){
				window.alert(unescape("I%u010CO%20-%20neodpov%EDd%E1%20kontroln%ED%20%u010D%EDslice."));
				_NastavTbox(ID, false);
				return false;
			}
		}
		else{
			if(hodnota == 9999999999){_NastavTbox(ID, true); if(funkce.length > 0) return eval(funkce); return true;}	
			rok = (hodnota.substring(0,2));
			mesic = (hodnota.substring(2,4));
			den = (hodnota.substring(4,6));
			sDatum=0;
			if(mesic > 50) mesic=mesic-50;
			if(hodnota.length == 9){
				if(rok >= 54){
					window.alert(unescape("R%u010C%20s%209%20%u010D%EDslicemi%2C%20byla%20p%u0159id%u011Blov%E1na%20do%20roku%201953."));
					_NastavTbox(ID, false);
					return false;
				}
				if(_OtestujDatum(den, mesic, "19"+rok) == true) sDatum=1;
			}
			if(hodnota.length == 10){
				if(rok <= 53) year = "20"+rok.toString();
				else year = "19"+rok;
				if(_OtestujDatum(den, mesic, year) == true) sDatum=1;
				sumrc=hodnota.substring(0,9)%11;
				if(sumrc == 10) sumrc = 0;
				if(sumrc != parseInt(hodnota.substring(9,10))){
					window.alert(unescape("R%u010C%20-%20neodpov%EDd%E1%20kontroln%ED%20%u010D%EDslice."));
					_NastavTbox(ID, false);
					return false;
				}
			}
			if(sDatum == 0){
				window.alert(unescape("R%u010C%20-%20Prvn%EDch%206%20%u010D%EDslic%20nen%ED%20platn%FDm%20datumem."));
				_NastavTbox(ID, false);
				return false;
			}
		}
	}
	_NastavTbox(ID, true);
	if(funkce.length > 0) return eval(funkce);	
	return true;
}

//otestuje DIC
function _KontrolaDIC(ID, funkce)
{
	var hodnota = document.all.item(ID).value.toLowerCase();
	if(hodnota != ""){	
		if(hodnota.length < 8){
			alert(unescape("DI%u010C%20mus%ED%20obsahovat%20minim%E1ln%u011B%208%20znak%u016F."));
			_NastavTbox(ID, false);
			return false;
		}
		var subjekt = hodnota.substring(3,hodnota.length);
		if(hodnota.substring(0,2) == "cz"){
			hodnota = "01"+hodnota.substring(2,hodnota.length)
			subjekt = hodnota.substring(2,hodnota.length);
		}
		else if(hodnota.substring(3,4) == "-"){
			subjekt = hodnota.substring(4,hodnota.length);
		}		
		var kod = hodnota.substring(0,3);
		if(_TestIntZnaku(kod) == false || kod < 1 || _TestIntZnaku(subjekt) == false || subjekt < 1 || subjekt.length > 10 || subjekt.length < 8){
			window.alert(unescape("Neplatn%E9%20DI%u010C."));
			_NastavTbox(ID, false);
			return false;
		}
	}
	_NastavTbox(ID, true);
	if(funkce.length > 0) return eval(funkce);
	return true;	
}

//otestuje datum a cas
function _KontrolaDateTime(ID, min, max, funkce){
	var hodnota = document.all.item(ID).value;
	if(hodnota != ""){
		var hodiny=hodnota.substring(hodnota.lastIndexOf(" ")+1,hodnota.length);
		var datum=hodnota.substring(0, hodnota.length-(hodiny.length+1));
		if(datum.length<1 || hodiny.length<1){
			alert(unescape("Chybn%u011B%20vypln%u011Bn%20datum%20a%20%u010Das%20%28p%u0159%EDklad%3A%2031.12.2001%2014%3A30%29."));
			_NastavTbox(ID, false);
			return false;
		}
		document.all.item(ID).value = datum;
		if(_KontrolaDate(ID, min, max, '') == true){
			document.all.item(ID).value = hodiny;
		}
		else{
			document.all.item(ID).value = hodnota;
			_NastavTbox(ID, false);
			return false;
		}	
		if(_KontrolaTime(ID , '') == true){
			document.all.item(ID).value = hodnota;
		}
		else{
			document.all.item(ID).value = hodnota;
			_NastavTbox(ID, false);
			return false;
		}	
	}
	_NastavTbox(ID, true);
	if(funkce.length > 0) return eval(funkce);
	return true;
}
		
//otestuje vyplneni casu		
function _KontrolaTime(ID, funkce){
	var hodnota = document.all.item(ID).value;
	if(hodnota != ""){
		var pocet = 0;
		for(x=0; x<hodnota.length; x++){
			if(hodnota.substring(x,x+1) == ":") pocet++;
		}
		if(pocet < 1 || pocet > 2){
			alert(unescape("%DAdaj%20o%20%u010Dasu%20je%20%u0161patn%u011B%20vypln%u011Bn%20%28p%u0159%EDklad%3A%2014%3A30%29."));
			_NastavTbox(ID, false);
			return false;
		}
		var vterina = "0";
		if(pocet==2){
			vterina = hodnota.substring(hodnota.lastIndexOf(":")+1,hodnota.length);
			hodnota=hodnota.substring(0, hodnota.lastIndexOf(":"));
		}
		minuta = hodnota.substring(hodnota.lastIndexOf(":")+1,hodnota.length);
		hodnota=hodnota.substring(0, hodnota.lastIndexOf(":"));				
		hodina = hodnota;
		if(_TestIntZnaku(hodina+"0"+minuta+"0"+vterina) == false || hodina>23 || minuta>59 || vterina>59 || hodina=="" || minuta=="" || vterina=="" || hodina.length>2 || minuta.length>2 || vterina.length>2){
			alert(unescape("%DAdaj%20o%20%u010Dasu%20je%20chybn%u011B%20vypln%u011Bn%20%28p%u0159%EDklad%3A%2014%3A30%29."));
			_NastavTbox(ID, false);
			return false;
		}
	}
	_NastavTbox(ID, true);
	if(funkce.length > 0) return eval(funkce);
	return true;
}

// otestuje e-mail
function _KontrolaEmail(ID) 
{
	var StrE = document.all.item(ID).value;
	var strError = "";
	var x = 0;

	if (StrE != "")
	{
		//zda ma minimalni delku
		if (StrE.length < 6)
		{
			window.alert(unescape("D%E9lka%20E-mailov%E9%20adresy%20nen%ED%20platn%E1"));
			_NastavTbox(ID, false);
			return false;
		}
		
		//test zda prvni nebo poslední znak neni @ nebo . a tecka je az treti znak od konce
		if (StrE.charAt(0)== "@") strError += unescape("chyb%ED%20jm%E9no%20p%u0159ed%20@\n");
		if (StrE.charAt(StrE.length-1)== "@") strError += unescape("chyb%ED%20dom%E9na%20za%20@\n");					
		if (StrE.charAt(StrE.length-1)== ".") strError += unescape("chyb%ED%20dom%E9na%20za%20.\n");
		if (StrE.charAt(StrE.length-2)== ".") strError += unescape(".%20na%20nespr%E1vn%E9m%20m%EDst%u011B\n");
		if (StrE.charAt(0)== ".") strError += unescape("chyb%ED%20text%20p%u0159ed%20.%20\n");

		x=0;
		var JeTamZavinac = 0;
		var JeTamMezera = 0;
		var JeTamTecka = 0;
		var JeTamLomitko = 0;
		var JeTamHvezdicka = 0;
		var JeTamPlus = 0;
		
		// Zacatek kontroly jednotlivych znaku a jejich poctu
		for(x=0; x<StrE.length; x++)
		{
			if (StrE.charAt(x)== "@") JeTamZavinac++; 
			if (StrE.charAt(x)== " ") JeTamMezera++;
			if (StrE.charAt(x)== ".") JeTamTecka++;
			if (StrE.charAt(x)== "\/") JeTamLomitko++;
			if (StrE.charAt(x)== "*") JeTamHvezdicka++;
			if (StrE.charAt(x)== "+") JeTamPlus++;
		}
		
		//Vyhodnoceni kontroly
		if (JeTamZavinac == 0) strError += unescape("chyb%ED%20znak%20@%20v%20adrese%20\n");
		if (JeTamZavinac >1) strError += unescape("mnoho%20znak%u016F%20@%20%28" + JeTamZavinac + "%29%20v%20adrese\n");
		if (JeTamMezera > 0) strError += unescape("mezery%20%28" + JeTamMezera + "%29%20v%20adrese\n");
		if (JeTamTecka == 0) strError += unescape("chyb%ED%20.\n");
		if (JeTamLomitko > 0) strError += unescape("lom%EDtko%28" + JeTamLomitko + "%29%20v%20adrese\n");
		if (JeTamHvezdicka >0) strError += unescape("hv%u011Bzdi%u010Dka%28" + JeTamHvezdicka + "%29%20v%20adrese\n");
		if (JeTamPlus > 0) strError += unescape("plus%28" + JeTamPlus + "%29%20v%20adrese\n");
			
		//Kontrola dvojice znaku	
		for(x=0; x<StrE.length; x++)
		{
			if (StrE.substring(x,x+2) == ".@") strError += unescape("chybn%u011B%20zadan%E9%20znaky%20.@\n"); 
			if (StrE.substring(x,x+2) == "@.") strError += unescape("chybn%u011B%20zadan%E9%20znaky%20@.\n"); 
			if (StrE.substring(x,x+2) == "..") strError += unescape("chybn%u011B%20zadan%E9%20znaky%20..\n");
		}
			
		var strErrZnaky = "";
		// kontrola relevantnich znaku
		// vypise vsechny vadne znaky
		for(x=0; x<StrE.length; x++) 
		{
			// preskoci mezeru ktera je uz testovana drive
			if (StrE.charAt(x) != " ")
				if (StrE.charAt(x) != escape(StrE.charAt(x))) strErrZnaky += escape(StrE.charAt(x)) + " ";
		}		
			
		// sestaveni zpravy o chybnych znacich
		if (strErrZnaky != "") strError = strError + "chybn%E9%20znaky%20-%20" + strErrZnaky ;
			
		// zobrazeni chybove zpravy s vyhodnocenim  
		if (strError.length != 0)
		{
			window.alert(unescape("Chyby%20v%20E-mailov%E9%20adrese%20%3A%20\n\n" + strError));
			_NastavTbox(ID, false);		
			return false;	
		}
	}
	_NastavTbox(ID, true);
	return true;
}


function OddelTisice(as_string)
{
	var	ls_des = "";
	var ls_vystup = "";
	var ls_tecka = ".";
	var	ls_string;
	var li_tecka = 0;
	as_string = as_string.replace(/ /g, "");
	if (as_string.indexOf(".") != -1)
	{
		li_tecka = 1;
		ls_string = as_string.split('.');
	}
	else
	{
		if (as_string.indexOf(",") != -1) li_tecka = 1;
		ls_string = as_string.split(',');
		ls_tecka = ",";
	}
	as_string = ls_string[0];
	if (li_tecka == 1) ls_des = ls_tecka + ls_string[1];
	var	li_delka = as_string.length;
	var i = 0;
	for(i = 0; i<li_delka; i++)
	{
		ls_vystup = as_string.substring(li_delka - i - 1, li_delka - i) + ls_vystup;
		if ((i+1)%3 == 0)
		{
			if (i<as_string.length -1) ls_vystup = " " + ls_vystup;
		}
	}
	ls_vystup = ls_vystup + ls_des;
	return ls_vystup;

}

	function _ProvedOddeleniTis(ID)
	{	//alert(window.event.keyCode);
		var proved = document.all.item(ID).OddelovatTisice;
		if(proved == "True" || proved == "true" || proved == true)	
		{
			//if (((window.event.keyCode >= 48)&&(window.event.keyCode <= 57)) || ((window.event.keyCode >= 96)&&(window.event.keyCode <= 105)) || (window.event.keyCode == 110) || (window.event.keyCode == 188) || (window.event.keyCode == 190) || (window.event.keyCode == 8) || (window.event.keyCode == 46) || (window.event.keyCode == 109) || (window.event.keyCode == 187) || (window.event.keyCode == 189)) document.all.item(ID).value = OddelTisice(document.all.item(ID).value);
			document.all.item(ID).value = OddelTisice(document.all.item(ID).value);
		}
		return true;
	}
