// vyhledavani v katalogu


// prumysl

function filter(id_section)
{
  section_id=id_section;
  if (document.getElementById('filter-form'))
  {
    getSections();
    return true;
  }
  else
  {
    return false;
  }
}






























// sekce

function getSections(s, m, p)
{

  html='<h3 style="margin: 20px 0 0 5px; padding: 5px 0 0 0; border-top: 4px solid #ccc;">Rychlé výhledání výrobku</h3>';
  section_id=s;
  manufacturer_id=m;
  price_id=p;

  if (!manufacturer_id) 
{
  manufacturer_id=0;
}

  if (!price_id) 
{
  price_id=0;
}


xmlhttpSection=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Mozilla, etc.
  xmlhttpSection=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE5, IE6
  xmlhttpSection=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttpSection!=null)
  {
  	
    xmlhttpSection.onreadystatechange=onResponseSections;
    xmlhttpSection.open("GET",'/toppocitace/ajax/sections.php',true);
    xmlhttpSection.send(null);
  }
else
  {
    return false;
  }

}


function onResponseSections()
{


  if(xmlhttpSection.readyState!=4) return;
  if(xmlhttpSection.status!=200)
  {
    return false;
  }

  var value, text, selected, sel, section;

  if ( document.getElementById('section-select') )
  {
    section=document.getElementById('section-select').value;
    if (section_id!=section)
    {
      var selects=document.forms['filter-form'].getElementsByTagName('select');

      for (i=2; i<selects.length; i++)
      {
        selects[i].value='';
      }    
    }
    section_id=section;
  }
  else 
  {
    section=section_id;
  }
  
  

  x=xmlhttpSection.responseXML.documentElement.getElementsByTagName("SECTION");
  

//  document.getElementById('filter-form').innerHTML='<div>';
  html+='<div class="input"><label>Kategorie</label> <select class="select" type="select" name="section" id="section-select" onchange="getSections(this.value, '+manufacturer_id+', '+price_id+');"> ';

      if (!section && !section_id)
      {
        selected=' selected="selected"';
      }
      else
      {
        selected='';
      }
  html+='<option value=""'+selected+'>- Zvolte kategorii - </option>';

  
  for (i=0; i<x.length; i++)
  {
    xx=x[i].getElementsByTagName("ID");
    var id=xx[0].firstChild.nodeValue;
    xx=x[i].getElementsByTagName("NAME");
    var name=xx[0].firstChild.nodeValue;
      
      if (id==section || id==section_id)
      { 
        selected=' selected="selected"';
      }
      else
      {
        selected='';
      }
      
      
      html+='<option value="'+id+'"'+selected+'>'+name+'</option>';
    }
    
      
    html+='</select><div class="clear_both"><!-- --></div></div>';



  

  if (section)
  {
    section_id=section;
    getManufacturers();
  }
  else
  {
    html+='<div class="input"><label>Výrobce</label> <select class="select" disabled="disabled"></select></div>';
    html+='<div class="input"><label>Cena</label> <select class="select" disabled="disabled"></select></div>';
    document.getElementById('filter-form').innerHTML=html;
  }


}













// sekce

function getManufacturers()
{




xmlhttpManufacturer=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Mozilla, etc.
  xmlhttpManufacturer=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE5, IE6
  xmlhttpManufacturer=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttpManufacturer!=null)
  {
    xmlhttpManufacturer.onreadystatechange=onResponseManufacturers;
    xmlhttpManufacturer.open("GET",'/toppocitace/ajax/manufacturers.php?section='+section_id+'&price='+price_id,true);
    xmlhttpManufacturer.send(null);
  }
else
  {
    return false;
  }

}


function onResponseManufacturers()
{


  if(xmlhttpManufacturer.readyState!=4) return;
  if(xmlhttpManufacturer.status!=200)
  {
    return false;
  }

  var value, text, selected, sel, manufacturer;

  if ( document.getElementById('manufacturer-select') )
  {
    manufacturer=document.getElementById('manufacturer-select').value;
    if (manufacturer_id!=manufacturer)
    {
      var selects=document.forms['filter-form'].getElementsByTagName('select');

      for (i=2; i<selects.length; i++)
      {
        selects[i].value='';
      }    
    }
    manufacturer_id=manufacturer;
  }
  else 
  {
    manufacturer=manufacturer_id;
  }
  
  
  

  x=xmlhttpManufacturer.responseXML.documentElement.getElementsByTagName("SECTION");
  

//  document.getElementById('filter-form').innerHTML='<div>';
  html+='<div class="input"><label>Výrobce</label> <select class="select" type="select" name="manufacturer" id="manufacturer-select" onchange="getSections('+section_id+', this.value, '+price_id+');"> ';

      if (!manufacturer && !manufacturer_id)
      {
        selected=' selected="selected"';
      }
      else
      {
        selected='';
      }
  html+='<option value=""'+selected+'>- Všichni výrobci - </option>';

  
  for (i=0; i<x.length; i++)
  {
    xx=x[i].getElementsByTagName("ID");
    var id=xx[0].firstChild.nodeValue;
    xx=x[i].getElementsByTagName("NAME");
    var name=xx[0].firstChild.nodeValue;
      
      if (id==manufacturer || id==manufacturer_id)
      { 
        selected=' selected="selected"';
      }
      else
      {
        selected='';
      }
      
      
      html+='<option value="'+id+'"'+selected+'>'+name+'</option>';
    }
    
      
    html+='</select><div class="clear_both"><!-- --></div></div>';



  


  getPrices(section_id, manufacturer_id);

}






function getPrices()
{

if (!manufacturer_id) 
{
  manufacturer_id=0;
}


xmlhttpPrice=null;
if (window.XMLHttpRequest)
  {// code for IE7, Firefox, Mozilla, etc.
  xmlhttpPrice=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE5, IE6
  xmlhttpPrice=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttpPrice!=null)
  {
//    alert('/toppocitace/ajax/prices.php?section='+section_id+'&m='+manufacturer_id);
    xmlhttpPrice.onreadystatechange=onResponsePrices;
    xmlhttpPrice.open("GET",'/toppocitace/ajax/prices.php?section='+section_id+'&m='+manufacturer_id,true);
    xmlhttpPrice.send(null);
  }
else
  {
    return false;
  }

}


function onResponsePrices()
{


  if ( document.getElementById('price-select') )
  {
    price=document.getElementById('price-select').value;
    if (price_id!=price)
    {
      var selects=document.forms['filter-form'].getElementsByTagName('select');

      for (i=2; i<selects.length; i++)
      {
        selects[i].value='';
      }    
    }
    price_id=price;
  }
  else 
  {
    price=price_id;
  }


  if(xmlhttpPrice.readyState!=4) return;
  if(xmlhttpPrice.status!=200)
  {
    return false;
  }

  var priceV, text, selected, sel, price;


  

  x=xmlhttpPrice.responseXML.documentElement.getElementsByTagName("SECTION");
  
  

      if (!price && !price_id)
      {
        selected=' selected="selected"';
      }
      else
      {
        selected='';
      }

//  document.getElementById('filter-form').innerHTML='<div>';
  html+='<div class="input"><label>Cena</label> <select class="select" type="select" name="price" id="price-select" onchange="getSections('+section_id+', '+manufacturer_id+', this.value);"> ';

  html+='<option value=""'+selected+'>- Všechny cenové intervaly - </option>';

  
  for (i=0; i<x.length; i++)
  {
    xx=x[i].getElementsByTagName("PRICE");
    var id=xx[0].firstChild.nodeValue;
    priceV=((id-1)*5000)+' Kč &ndash; '+((id-1)*5000+5000)+' Kč';

      if (id==price || id==price_id)
      { 
        selected=' selected="selected"';
      }
      else
      {
        selected='';
      }
      
      html+='<option value="'+id+'"'+selected+'>'+priceV+'</option>';
    }
    
      
    html+='</select><div class="clear_both"><!-- --></div><br /><input type="submit" name="search" value="Vyhledat" class="button" /></div>';


    
  


    document.getElementById('filter-form').innerHTML=html;


}






var section;
var section_id;
var manufacturer_id;
var price_id;
var html;



