function check_mail(s)
{
  var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

  return filter.test(s);
}

function HasNumeric(sText)
{
  var ValidChars = "0123456789";
  var HasNumber=false;
  var Char;

  for (i = 0; i < sText.length && HasNumber == false; i++) {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) > 0) {
      HasNumber = true;
    }
  }

  return HasNumber;
}

function IsNumeric(sText)
{
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;

  for (i = 0; i < sText.length && IsNumber == true; i++) {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1) {
      IsNumber = false;
    }
  }

  return IsNumber;
}

function floatval(mixed_var)
{
  mixed_var += "";
  mixed_var = mixed_var.replace(/,/g, ".");
  mixed_var = mixed_var.replace(/[^\d\.\(]*/g, "");

  return (parseFloat(mixed_var) || 0);
}

function number_format(number, decimals, dec_point, thousands_sep)
{
  var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
  var d = dec_point == undefined ? "." : dec_point;
  var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";
  var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;

  return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
}

function getVote(ID)
{
	var send = "";
	var allBoxes = document.body.getElementsByTagName("input");
	for (var i = 0; i < allBoxes.length; i++) {
		if (allBoxes.item(i).name == "vote" + ID) {
			if (allBoxes.item(i).checked == true) send += allBoxes.item(i).value + "|";
		}
  }

  if (send)
    $("#poll" + ID).load('/com_inquiry.php?voting=' + $('#voting' + ID).attr('value') + '&vote=' + send + '&sid=' + Math.random());
}

function validate(form_id)
{
  var error_message = "";

  $('#' + form_id + ' .text.required, #' + form_id + ' textarea.required, #' + form_id + ' select.required, #' + form_id + ' .file.required').each(
    function(index) {
      if ($(this).val() == "") {
        error_message += $(this).attr("title") + ".<br>";
      }
    }
  );

  $('#' + form_id + ' .checkbox.required, #' + form_id + ' .radio.required').each(
    function(index) {
      if ($(this).is(':checked') == false && error_message.indexOf($(this).attr("title")) == "-1" && $(this).parent().find(':checked').attr('name') == undefined) {
        error_message += $(this).attr("title") + ".<br>";
      }
    }
  );

  if (error_message) {
    showDialog(error_message, "Odoslať");
    return false;
  }
}

function showDialog(message, title)
{
  $("<div><p>" + message + "</p></div>").dialog({
    title: title,
    width: 400,
    modal: true,
    draggable: false,
    resizable: false,
    buttons: { "OK": function() { $(this).dialog("close") } }
  });
}

function openMenu(id)
{
  //$("ul.menu").children("li").each(function () { $(this).children("ul").css("height", $(this).height() - 22) });

  $("ul.menu *").click(function() {
                         $("ul.menu *").removeClass("ui-corner-all ui-corner-top ui-corner-bottom");
                       });

  $("ul.menu a").click(function() {
                         $("ul.menu a").removeClass("active");

                         if ($(this).parent().children("ul").length == 0) {
                           //$("ul.menu").accordion("disable");
                           window.location = $(this).attr("href");
                         }

                         $(this).blur();
                       });

  $("ul.menu").accordion({ animated: "slide", autoHeight: false, collapsible: false, active: false, icons: false });

  if (id) {
    $("#menu-category-" + id).children("a").addClass("ui-state-active");

    $("#menu-category-" + id).parents("ul").show();
    $("#menu-category-" + id).children("ul").show();

    $("ul.menu").accordion("activate", $("#menu-category-" + id).parents("li").children("a"));

    $("ul.menu a").blur();
  }

  $("ul.menu a").each(function () {
    if (window.location.href.indexOf($(this).attr("href")) != -1) {
      $(this).addClass("ui-state-active");
      $(this).parents("li").children("ul").show();
      $("ul.menu").accordion("activate", $(this).parents("li").children("a"));
    }
  });

  $("ul.menu, ul.menu *").removeClass("ui-widget ui-widget-content ui-corner-all ui-corner-top ui-corner-bottom ui-accordion");
}

$(document).ready(function() {
  $("a.lightbox").lightBox();
});
