// --- debug console --- start --- //
var _console = null;

function dbg(msg) {
	debug_console("<h3>Vlastnosti: " + msg + "</h3>");
	for (i in msg)
		try {	debug_console("<b>" + i + "</b> : " + msg[i]); }
		catch(e) { debug_console("<i>Exception: " + i + "</i>"); }
}

function debug_console(msg) {
	if ((_console == null) || (_console.closed)) {
		_console = window.open("","console","width=600,height=300,resizable,scrollbars");
		_console.document.open("text/html");
	}
	_console.document.writeln("<br />" + msg);
}
// --- debug console --- end --- //

function getVP(x) {
if (typeof x == 'undefined') x = 'vp';
return document.forms[x];
}

function r2(x, desMist) {
x = parseFloat(x);
return Math.round(x * Math.pow(10, desMist)) / Math.pow(10, desMist);
}

function nf(x) {
x = parseFloat(x.replace(/,/g, ".").replace(/ /g, ''));
return isNaN(x) ? 0 : x;
}

function showNum(x, hr) {
return (x == Infinity || x == -Infinity || isNaN(x)) ? '-' : (hr ? hr_number(x) : x);
}

function enAb(theObject) {
theObject.disabled = false;
if (theObject.nodeName == 'INPUT' && theObject.type == 'text' || theObject.nodeName == 'SELECT') theObject.style.backgroundColor = '#fff';
}

function disAb(theObject) {
theObject.disabled = true;
if (theObject.nodeName == 'INPUT' && theObject.type == 'text' || theObject.nodeName == 'SELECT') theObject.style.backgroundColor = '#ddd';
}

function itemIndex(r) {
for (var i = 0; i < r.length; i++) { if (r[i].checked == true) {return i} }
return -1;
}

function createSelect(s, p, d) {
s.length = 0;
for (var i = 0; i < p.length; i++) { s[i] = new Option(p[i]); }
s.selectedIndex = d;
}

function htmlShow(obj,displ) {
switch (displ) {
  case 'inline':
    obj.style.display='inline';
    break;
  case 'block':
  default:
    obj.style.display='block';
}
}

function htmlHide(obj) {
obj.style.display='none';
}

function hr_number(x, separator) {
var y = parseInt(x);
if (isNaN(y)) return x;
var x1 = new String(Math.abs(x));
var x2 = new String(parseInt(x1));
if (x2.length < 4) return x;
if (!separator) separator = ' ';
var a = new Array();
for (var i = x2.length - 1; i >= 0; i--) {
  a.push(x2.substr(i, 1));
  if (i > 0 && (x2.length - i) % 3 == 0) a.push(separator);
}
a.reverse();
return (y < 0 ? '-' : '') + a.join('') + new String(parseFloat(x1)).replace(x2,'');
}

function hr_number_float(x, separator, pocetMist) {
var y = r2(x, pocetMist);
if (isNaN(y)) return x;
var x1 = new String(Math.abs(y));
var x2 = new String(parseInt(x1));
if (x2.length < 4) return y;
if (!separator) separator = ' ';
var a = new Array();
for (var i = x2.length - 1; i >= 0; i--) {
  a.push(x2.substr(i, 1));
  if (i > 0 && (x2.length - i) % 3 == 0) a.push(separator);
}
a.reverse();
return (y < 0 ? '-' : '') + a.join('') + new String(parseFloat(x1)).replace(x2,'');
}

function in_array(prvek, pole) {
for (var i = 0; i < pole.length; i++) {
  if (prvek == pole[i]) return true;
}
return false;
}

var url_params=this.window.location.search.substr(1).split('&');
url_vars={}
for (i=0;i<url_params.length;i++) {
x=url_params[i].split('=');
url_vars[x[0]]=x[1];
}

function fcgi_getfirst(k, query) {
  if (query == null) {
    query = window.location.search.substring(1);
  }
  var vars = query.split("&");
  for (var i = 0; i < vars.length; i++) {
    var pair = vars[i].split("=");
    if (pair[0] == k) {
      return pair[1];
    }
  }
  return '';
}

function interpolace(x, x1, x2, y1, y2) {
return y1 + (x - x1) * (y2 - y1) / (x2 - x1);
}

// pozor - hodnoty v indexu i0 musi byt zadany vzestupne!!!
function tab_interpolace(x, p, i0, i1) {
if (i0 == null) i0 = 0;
if (i1 == null) i1 = 1;
var y = null;
if (p.length) {
  if (x <= p[0][i0]) {
    y = p[0][i1];
  }
  else if (x >= p[p.length - 1][i0]) {
    y = p[p.length - 1][i1];
  }
  else {
    for(var i = 1; i < p.length; i++) {
      var xi_1 = p[i - 1][i0];
      var xi = p[i][i0];

      if (x >= xi_1 && x <= xi) {
        var yi = p[i][i1];
        var yi_1 = p[i - 1][i1];
        y = interpolace(x, xi_1, xi, yi_1, yi)
      }
    }
  }
}
return y;
}

function jq_createSelect(s, p, d) {
  s.html('');
  $.each(p, function(key, obj) {
    var val = ('value' in obj) ? obj.value : '';
    var text = ('text' in obj) ? obj.text : '';
    var sel = ('selected' in obj) ? obj.selected : '';
    var option = '<option' + (val != null ? ' value="' + val + '"' : '') + (sel ? ' selected="selected"' : '') + '>' + text + '</option>'
    s.append(option);
  });
  if (d != null) s.attr('selectedIndex', d);
}

function jq_enAb(obj) {
  obj.removeAttr('disabled');
  if (obj.get(0).nodeName == 'INPUT' && obj.attr('type') == 'text' || obj.get(0).nodeName == 'SELECT' || obj.get(0).nodeName == 'TEXTAREA') {
    obj.css('background-color', '#fff');
  }
}

function jq_disAb(obj) {
  obj.attr('disabled', 'disabled');
  if (obj.get(0).nodeName == 'INPUT' && obj.attr('type') == 'text' || obj.get(0).nodeName == 'SELECT' || obj.get(0).nodeName == 'TEXTAREA') {
    obj.css('background-color', '#ddd');
  }
}

function arrayMin(x) {
  return Math['min'].apply(this||window, x);
}

function arrayMax(x) {
  return Math['max'].apply(this||window, x);
}

