function get_obj (obj) {
  try { 
    return new ActiveXObject(obj); 
  }
  catch(e) { 
    return null; 
  }
}

var page = {
  sel_mt_id : null,
  sel_vmt_id : null,
  xml  : null,

  init : function() {
    page.sel_mt_id  = document.getElementById('mt_id'); 
    page.sel_vmt_id = document.getElementById('vmt_id'); 

    // hide "non-js" buttons 
    document.getElementById( 'mt_submit').style.display = 'none';
    document.getElementById('vmt_submit').style.display = 'none';
    // hang event handlers
    page.sel_vmt_id.onchange = page.vmt_change;
    page.sel_mt_id.onchange = page.mt_change;
    // init AJAX
    page.xml = get_obj("Msxml2.XMLHTTP");
    if (!page.xml) page.xml = get_obj("Microsoft.XMLHTTP");
    if (!page.xml) {
      if (typeof XMLHttpRequest!="undefined") page.xml = new XMLHttpRequest();
    }
    // vlastnosti
    var table = document.getElementById('vlastnosti');
    if (table) {
      var tr = table.getElementsByTagName('tr');
      for(var i=0; i<tr.length; i++) {
        tr[i].onmouseover = page.tr_over;
        tr[i].onmouseout = page.tr_out;
      }	 
    }
  },

  vmt_change : function() {
    page.sel_mt_id.disabled = true;
    page.sel_mt_id.options.length = 0;
    page.sel_mt_id.options[0] = new Option('Nahrávám...','0');
    vmt_id = this.value.split('|');
    if (!page.xml) return;
    if (page.xml.readyState != 0) page.xml.abort();
    page.xml.onreadystatechange = page.vmt_change_func;
    page.xml.open ("GET", "http://" + window.location.host + "/index.php?get_mt_list&vmt_id="+vmt_id[0]);
    page.xml.send(null);
  },

  vmt_change_func : function() {
    if (page.xml.readyState == 4 && page.xml.responseText) {
      var res = page.xml.responseText;
      res = res.split(';');
      page.sel_mt_id.options.length = 0;
      for (var i=0; i<res.length-1; i++) {
        var item = res[i]; 
	var item = item.split(',');
	page.sel_mt_id.options[i] = new Option(item[1],item[0]);
      }
      page.sel_mt_id.disabled = false;
    }
  },

  mt_change : function() {
    if (this.value=='') return;
    vmt_kratky = page.sel_vmt_id.value.split('|'); vmt_kratky = vmt_kratky[1]; 
    mt_kratky  = this.value.split('|'); mt_kratky = mt_kratky[1];   
    window.location = "http://"+vmt_kratky+"."+server_base + "/"  + vmt_kratky + "-" + mt_kratky + "/";
  },
  
  tr_over : function() {
    if (this.className!='l0') this.className += ' hi'; 
  },
  
  tr_out : function() {
    if (this.className!='l0') {
      var cl = this.className.split(' ');
      this.className = cl[0];
    }   
  }

}

window.onload = page.init;
