// vraci dany text pro klic z pole AlertMessages
function getText(wKey)
{
	if (AlertMessages[wKey] != null)
	{
		return AlertMessages[wKey];
	}
	return "";
}

function CheckEmail(email)
{
	var regMail = "^[_a-zA-Z0-9\.\-]+@[_a-zA-Z0-9\.\-]+[\.]{1}[a-zA-Z]{2,4}$";

	if (email.match(regMail))
	{
		return true;
	}
	return false;
}

function CheckInput(idErrorBox, idErrorInput, errorText)
{
	var readOnly = false;
	var text = "";

	//console.log("IDE " >> idErrorBox);

	if ($("input[name='" + idErrorInput + "']").val() != null)
	{
		if( $("input[name='" + idErrorInput + "']").attr("readonly") == true)
		{
			readOnly = true;
		}
		text = $("input[name='" + idErrorInput + "']").val();
	}
	else if ($("#" + idErrorInput).val() != null)
	{
		if( $("#" + idErrorInput).attr("readonly") == true)
		{
			readOnly = true;
		}
		text = $("#" + idErrorInput).val();
	}

	//alert( $("#" + idErrorInput).attr("name") + " | " + $("#" + idErrorInput).val() + " | " + $("#" + idErrorInput).attr("id") );

	// readonly
	if (readOnly || (!$("#" + idErrorInput).attr("name") && $("#" + idErrorInput).attr("id") == true ))
	{
		alert("ro");
		return true;
	}

	//alert("|" + text + "|" + errorText + "|" + idErrorInput + " | " + $("input[name='" + idErrorInput + "']").attr("name")); return false;

	// is not defined
	if ( !$("#" + idErrorInput).attr("name") && !$("#" + idErrorInput).attr("id") && !$("input[name='" + idErrorInput + "']").attr("name") )
	{
		return true;
	}

	//alert("|" + text + "|" + errorText + "|" + idErrorInput);

	if (text == "")
	{
		SetError(idErrorBox,idErrorInput,true);
		SetErrorText(idErrorBox, true, errorText);

		return false;
	}
	else
	{
		SetError(idErrorBox,idErrorInput,false);
		SetErrorText(idErrorBox, false, errorText);

		return true;
	}
}

function SetError(idErrorBox, idErrorInput, error)
{
	if (error == true)
	{
		if ($("#" + idErrorBox).html() != null)
		{
			$("#" + idErrorBox).addClass('error');
		}
		if ($("input[name='" + idErrorInput + "']").attr("name") != null)
		{
			$("input[name='" + idErrorInput + "']").addClass('error');
		}
		else if ($("#" + idErrorInput).attr("name") != null)
		{
			$("#" + idErrorInput).addClass('error');
		}
	}
	else
	{
		if ($("#" + idErrorBox).html() != null)
		{
			$("#" + idErrorBox).removeClass('error');
		}
		if ($("input[name='" + idErrorInput + "']").attr("name") != null)
		{
			$("input[name='" + idErrorInput + "']").removeClass('error');
		}
		else if ($("#" + idErrorInput).attr("name") != null)
		{
			$("#" + idErrorInput).removeClass('error');
		}
	}
}

function SetErrorText(idErrorBox, error, errorText)
{
	if (error == true)
	{
		if ($("#" + idErrorBox).html() != "" && inputOldValues[ idErrorBox ] == null)
		{
			inputOldValues[ idErrorBox ] = $("#" + idErrorBox).html();
		}

		$("#" + idErrorBox).html( errorText );
	}
	else
	{
		if (errorText != null && errorText != "")
		{
			$("#" + idErrorBox).html(errorText);
		}
		else
		{
			if (inputOldValues[ idErrorBox ] != null)
			{
				$("#" + idErrorBox).html( inputOldValues[ idErrorBox ] );
			}
			else
			{
				$("#" + idErrorBox).html("");
			}
		}
	}
}



function isNumberKey(element, evt)
{
	/*
    var key="";
    if(window.event){
        key = event.keyCode;
    } else {
        key = evt.which;
    }

    var value = element.value;
    
    if ((key==null) || (key == 0) || (key == 8) || (key == 13) || (key == 27))
    {
        return true;
    }
    else
    {
        if((key < 48 || key > 57) && (key != 46) )
        {            
            return false;
        }
        else
        {
            return true;
        }
    }
     */

	var charCode = (evt.which) ? evt.which : event.keyCode

	if ($(element).hasClass("error") )
	{
		$(element).removeClass("error");
	}
    
	//console.log("|" + charCode + "|");

	if ((charCode==null) || (charCode == 0) || (charCode == 8) || (charCode == 9) || (charCode == 13) || (charCode == 27) || (charCode == 18) || charCode == 46 || charCode == 39 || charCode == 37 || charCode == 38 || charCode == 39 || charCode == 40 || charCode == 46)
	{
		$(element).removeClass("error");
		return true;
	}
    
	element.onkeyup = function()
	{
		if ((charCode==null) || (charCode == 0) || (charCode == 8) || (charCode == 13) || (charCode == 27) || (charCode == 18) || charCode == 46 || charCode == 39 || charCode == 37 || charCode == 38 || charCode == 39 || charCode == 40 || charCode == 46)
		{
			$(element).removeClass("error");
			return true;
		}
        
		if ( (charCode >=0 && charCode < 48) || (charCode > 57 && charCode < 96) || charCode > 105)
		{
			$(element).addClass("error");
		//alert(getText("jspouzecislice"));
		}
	}

	//if ((charCode < 48 || charCode > 57) && (charCode < 96 || charCode > 105))
	if ( (charCode >=0 && charCode < 48) || (charCode > 57 && charCode < 96) || charCode > 105)
	{
		$(element).addClass("error");
		return false;
	}

	$(element).removeClass("error");
	return true;
}

function SearchSetText()
{
	if ($("#searchString").val() == "")
	{
		$("#searchThisString").val("");
	}
}

/**
 *   Print page
 **/
function printPage()
{
	print();
	return false;
}

/*
 **  Add to favorites
 */
function addToFavorites(linkObj,addUrl,addTitle)
{
	return BookmarkApp.addBookmark(linkObj , addUrl , addTitle , getText("jspridatdoobl") );
}

/**
 * sklonovani podle poctu
 * @return string text
 */
function decline(count, textKus, textKusy, textKusu)
{
	count = parseInt(count);
	
	if (count == 1)
	{
		return textKus;
	}
	if (count >= 2 && count <= 4)
	{
		return textKusy;
	}
	
	return textKusu;
}

/**
 * směrování do nového okna
 */
function targetBlank()
{

	// vybereme všechny odkazy
	$("a.targetBlank:not(a.targetBlankSetted)").click(function(event)
	{
		if(!$(this).hasClass('disabled'))
		{
			window.open($(this).attr("href"), "");
		}

		event.preventDefault();
	});

	$('a.targetBlank:not(a.targetBlankSetted)').addClass('targetBlankSetted');

}
