function ControlVal( element, ealert, etype, evalue )
{
  	var etest = false;
	
	// type of num //
	if( etype == "numeric" )
	{
		if( element.value > evalue ) etest = true;
	}
	else if( etype == "text" )
	{
		if( element.value.length > evalue ) etest = true;
	}
	else if( etype == "email" )
	{
		re = new RegExp("^([a-zA-Z0-9]{1})+[a-zA-Z0-9_\.\-]+[@]{1}([^.][a-zA-Z0-9_\.\-]+[.])+[a-z]{2,7}$");
		etest = re.test( element.value );
	}
	else if( etype == "phone" )
	{
		if( element.value > 10000000 ) 
		{
			if( element.value.length > 8 ) etest = true;
		}
	}
	else if( etype == "date" )
	{
		if( ( ealert = IsDate( element ) ) == true ) etest = true;
	}
	else
	{
		etest = true;
	}
	
	
	if( etest == false )
	{
		if( ealert != "" )
		{
			alert( ealert );
		}

		element.focus();
	}
	
	return etest;
}



function IsDate( vstup ) 
{
	var text ;
	var index ;
	var tecka ;
	var den ;
	var mesic ;
	var rok ;
	var ch ;
	
	text = "" ;
	den = "" ;
	mesic = "" ;
	rok = "" ;
	tecka = 0 ;
	
	for( index = 0; index < vstup.value.length; index++ ) 
	{ 
		ch = vstup.value.charAt(index); 
		if( ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" && ch != ".") 
		{
			text="Pole Datum zadávejte ve tvaru DD.MM.RRRR (DD=den, MM=měsíc, RRRR=rok).\r"
		} 
		
		if( ( ch == "0" || ch == "1" || ch == "2" || ch == "3" || ch == "4" || ch == "5" || ch == "6" || ch == "7" || ch == "8" || ch == "9" ) && ( text == "" ) ) 
		{ 
			if (tecka == 0) 
			{ 
				den = den + ch;
			} 
			
			if( tecka == 1 ) 
			{
				mesic=mesic + ch;
			} 
			
			if( tecka == 2 )
			{
				rok=rok + ch;
			} 
		} 
		
		if( ch == "." && text == "" ) 
		{ 
			if (tecka == 1) tecka=2;
			if (tecka == 0) tecka=1;
		} 
	} 
	
	if( ( den<1 || den >31 ) && ( text == "" ) ) text = text + "Pocet dní v uvedeném měsíci nemůže být menší jak 1 a větší jak 31.\r";
	
	if( ( mesic<1 || mesic>12 ) && (text == "" ) ) text = text + "Pocet měsíců nemůže být menší než 1 a větší než 12.\r";
	
	if(rok < 1990 && tecka == 2 && text == "" && rok != "") text = text + "Rok nemůže být menší jak 1990.\r";
	
	if( ( tecka == 2 && rok == "" ) || ( tecka > 2 ) ) text = text+ "Datum zadávajte ve tvaru DD.MM.RRRR (DD=den, MM=měsíc, RRRR=rok)\r";
	
	// unor //
	if( mesic == 2 )
	{ 
		if( rok != "" ) 
		{ 
			if (rok % 4 == 0)
			{ 
				if( den > 29 ) text=text + "V únoru roku " + rok + " je maximálně 29 dní.\r"; 
			} 
			else 
			{ 
				if( den > 28 ) text=text + "V únoru roku " + rok + " je maximálně 28 dní.\r";
			} 
		} 
		else 
		{ 
			if( den > 29 ) text = text + "Vo únoru je maximálně 29 dní.\r"
		} 
	} 
	
	if( ( mesic == 4 || mesic == 6 || mesic == 9 || mesic == 11 ) && ( den > 30 ) ) 
	{
		text = text + "Počet dní v uvedeném měsíci nemůže být menší než 1 a větší než 30.\r"	} 
	
	if( text != "" ) 
	{ 
		alert(text); 
		return false;
	} 
	else 
	{ 
		return true;
	} 
}




function IsDateStr( vstup ) 
{
	var text ;
	var index ;
	var tecka ;
	var den ;
	var mesic ;
	var rok ;
	var ch ;
	
	text = "" ;
	den = "" ;
	mesic = "" ;
	rok = "" ;
	tecka = 0 ;
	
	for( index = 0; index < vstup.length; index++ ) 
	{ 
		ch = vstup.charAt(index); 
		if( ch != "0" && ch != "1" && ch != "2" && ch != "3" && ch != "4" && ch != "5" && ch != "6" && ch != "7" && ch != "8" && ch != "9" && ch != ".") 
		{
			text="Pole Datum zadávejte ve tvaru DD.MM.RRRR (DD=den, MM=měsíc, RRRR=rok).\r"
		} 
		
		if( ( ch == "0" || ch == "1" || ch == "2" || ch == "3" || ch == "4" || ch == "5" || ch == "6" || ch == "7" || ch == "8" || ch == "9" ) && ( text == "" ) ) 
		{ 
			if (tecka == 0) 
			{ 
				den = den + ch;
			} 
			
			if( tecka == 1 ) 
			{
				mesic=mesic + ch;
			} 
			
			if( tecka == 2 )
			{
				rok=rok + ch;
			} 
		} 
		
		if( ch == "." && text == "" ) 
		{ 
			if (tecka == 1) tecka=2;
			if (tecka == 0) tecka=1;
		} 
	} 
	
	if( ( den<1 || den >31 ) && ( text == "" ) ) text = text + "Pocet dní v uvedeném měsíci nemůže být menší jak 1 a větší jak 31.\r";
	
	if( ( mesic<1 || mesic>12 ) && (text == "" ) ) text = text + "Pocet měsíců nemůže být menší než 1 a větší než 12.\r";
	
	if(rok < 1990 && tecka == 2 && text == "" && rok != "") text = text + "Rok nemůže být menší jak 1990.\r";
	
	if( ( tecka == 2 && rok == "" ) || ( tecka > 2 ) ) text = text+ "Datum zadávajte ve tvaru DD.MM.RRRR (DD=den, MM=měsíc, RRRR=rok)\r";
	
	// unor //
	if( mesic == 2 )
	{ 
		if( rok != "" ) 
		{ 
			if (rok % 4 == 0)
			{ 
				if( den > 29 ) text=text + "V únoru roku " + rok + " je maximálně 29 dní.\r"; 
			} 
			else 
			{ 
				if( den > 28 ) text=text + "V únoru roku " + rok + " je maximálně 28 dní.\r";
			} 
		} 
		else 
		{ 
			if( den > 29 ) text = text + "Vo únoru je maximálně 29 dní.\r"
		} 
	} 
	
	if( ( mesic == 4 || mesic == 6 || mesic == 9 || mesic == 11 ) && ( den > 30 ) ) 
	{
		text = text + "Počet dní v uvedeném měsíci nemůže být menší než 1 a větší než 30.\r"	} 
	
	if( text != "" ) 
	{ 
		alert(text); 
		return false;
	} 
	else 
	{ 
		return true;
	} 
}


function compareDate( datFrom, datTo )
{
	var arrAA = datFrom.split( " " );
	var arrBB = datTo.split( " " );
	
	if( IsDateStr( arrAA[0] ) == true )
	{
		if( IsDateStr( arrAA[0] ) == true )
		{
						var arrA = arrAA[0].split( "." );
						var arrB = arrBB[0].split( "." );
						var arrAmin = arrAA[1].split( ":" );
						var arrBmin = arrBB[1].split( ":" );
						//alert( arrA[2] + " / " + arrB[2] );
						
						if( parseFloat( arrA[2] ) > parseFloat( arrB[2] ) )
						{
							alert( "Datum DO je starší než datum OD (rok). Zadejte, prosím, data správně. CHYBA!" );
							return false;
						}
						else
						{
							if( arrA[2] == arrB[2] )
							{
								if( parseFloat( arrA[1] ) > parseFloat( arrB[1] ) )
								{
									alert( "Datum DO je starší než datum OD (měsíc). Zadejte, prosím, data správně. CHYBA!" );
									return false;
								}
								else
								{
									if( parseFloat( arrA[1] ) == parseFloat( arrB[1] ) )
									{
										if( parseFloat( arrA[0] ) > parseFloat( arrB[0] ) )
										{
											alert( "Datum DO je starší než datum OD (den). Zadejte, prosím, data správně. CHYBA!" );
											return false;
										}
										else
										{
											if( parseFloat( arrAmin[0] ) > parseFloat( arrBmin[0] ) )
											{
												alert( "Datum DO je starší než datum OD. Zadejte, prosím, data správně. CHYBA!" );
												return false;
											}
											else
											{
												if( arrAmin[0] == arrBmin[0] )
												{
													if( parseFloat( arrAmin[1] ) > parseFloat( arrBmin[1] ) )
													{
														alert( "Datum DO je starší než datum OD. Zadejte, prosím, data správně. CHYBA!" );
														return false;
													}
												}
											}
										}
									}
								}
							}
						}
						
						return true;
		}
	}
	
	return false;
}
