function $(id) {
  return document.getElementById(id);
}
function css_display(id, a, b) {
  if ($(id).style.display == a) {
    $(id).style.display = b;
  } else {
    $(id).style.display = a;
  }
}
function ajax_create() {
    var ro, br;
    br = navigator.appName;
    if(br == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}
var http = ajax_create();

function ajax(args) {

    if (!args.progress_id) {
      args.progress_id = 'ajax';
    }
    if (!args.progress_content) {
      args.progress_content = '<img align="baseline" src="img/ajax.gif" />';
    }
    if (!args.url) {
      args.url = 'ajax.php';
    }

    $(args.progress_id).innerHTML = args.progress_content;

    var q = '', arg;
    for (arg in args) {
      
      // remove setup args
      if (arg != 'progress_id' && arg != 'progress_content' && arg != 'method' && arg != 'url') { 
        q = q + '&' + arg + '=' + args[arg];
      }
    }
    q = q.replace("&", "", "i");
    
    if (args.method == 'post') {
      // make post request
      http.open('post', args.url, true);
      http.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=utf-8');
      http.onreadystatechange = ajax_handle;
      http.send(q);
    } else {
      http.open('get', args.url + '?' + q);
      http.onreadystatechange = ajax_handle;
      http.send(null);
    }
}

function ajax_handle() {

  if(http.readyState == 4) {
    
    if (http.status == 200) {
      
      var r, upd;
      r = http.responseText;
      upd = new Array();

      if(r.indexOf('|' != -1)) {

        upd = r.split('|');
        $(upd[0]).innerHTML = upd[1];
        // ak id do ktoreho ide odpoved je ine nez 'ajax' a 'ajax' ID vymaz jeho obsah
        if (upd[0] != 'ajax') {
          if ($('ajax')) {
            $('ajax').innerHTML = '';
          }
        }
      }
    } else {
      alert ('Chyba:' + http.status);
    }
  }
}
function max_pos(max, check_id) {

  var cur = parseInt($('pos-counter').innerHTML);

  if ($('pos-checkbox-id-'+check_id).checked) { // ak sa zaskrtava
    if (cur < max) {
      $('pos-counter').innerHTML = (cur + 1).toString();
    } else {
      $('pos-checkbox-id-'+check_id).checked = false;
      cur = max + 1;
      alert ('Označiť môžete maximálne ' + max + ' pozícií');
    }
  }
  if ($('pos-checkbox-id-'+check_id).checked == false && (cur <= max)) { // ak sa odzaskrtava
    $('pos-counter').innerHTML = (cur - 1).toString();
  }
}

