///////////////////////////////////////////////////////////////////////////////
/* Soubor:  cena_slilder.js
 * Datum:   1.sprna 2011
 * Autor:   Tomáš Konečný,
 * Projekt: 
 * Popis:   
 * Popis s.:Soubor cena_slider.js umožnuje dynamicky nacitat data
*//////////////////////////////////////////////////////////////////////////////

//global variables
var xmlHttp = createXmlHttpRequestObject();//stores XMLHttpRequestObject  

var xmlHttp2 = createXmlHttpRequestObject2();//stores XMLHttpRequestObject   
//functions
function createXmlHttpRequestObject(){
  //creates XMLHttpRequest object if it is possible
  var xmlhttp
  try {
  //should work on all browsers except IE6 or older
    xmlHttp = new XMLHttpRequest();
  }
  catch (e){
    //browser is IE6 or older
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch (e){
      //ignore error
    }
  }
  if (!xmlHttp)
    alert ("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}   

function createXmlHttpRequestObject2(){
  //creates XMLHttpRequest object if it is possible
  var xmlhttp2
  try {
  //should work on all browsers except IE6 or older
    xmlHttp2 = new XMLHttpRequest();
  }
  catch (e){
    //browser is IE6 or older
    try {
      xmlHttp2 = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch (e){
      //ignore error
    }
  }
  if (!xmlHttp2)
    alert ("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp2;
}   

function cena_slider(sekce){
  //sends HTTP request
  if (xmlHttp){
    try{
    //try to connect to server
      var OD_CENY = document.getElementById('od_cena').value;
	    var DO_CENY = document.getElementById('do_cena').value;
	    var NOSNOST = document.getElementById('nosnost').value;
	    var TYP = document.getElementById('typ').value;
	    
	   // var VAKCI = document.getElementById('vakci').value;
	    if(document.getElementById('vakci').checked == true){
          VAKCI = "1";
      }else{VAKCI = "";}
	    
	    var values ="";
	    if(TYP == 1 || TYP == 4 || TYP == 2){
        if(TYP == 1 || TYP == 4){ 
	      var ZONY = document.getElementById('zony').value;
        var VYSKAM = document.getElementById('vyskam').value; 
	      if(TYP == 1){
          var nodes = document.getElementsByName('tuhost[]');   
          var n;
          values = new Array();  var t=0;          
          for (var i = 0; i < nodes.length; i++) {
            n = nodes[i];
            if (n.checked == true) {
                values[t]=n.value;
                t++;
             }
          } // for
        } // if
      } // if
          var DELKAM = document.getElementById('delkam').value;
	        var SIRKAM = document.getElementById('sirkam').value;                       
      } // if typ matrace , rosty
      else{var ZONY = ""; var VYSKAM = ""; var DELKAM = 'undefined'; var SIRKAM ='undefined';}

	   /* if(typeof OD_CENY == 'undefined'){OD_CENY = od_ceny;}
	    if(typeof DO_CENY == 'undefined'){DO_CENY = do_ceny;}
	    if(typeof NOSNOST == 'undefined'){NOSNOST = nosnost;}
	    if(typeof TYP == 'undefined'){TYP = typ;}     */
	    var SEKCE = sekce;
	    if(TYP != '1' && TYP != '4'){ 
							NOSNOST = 0;
						}
	  //create string of parameters
	           
      xmlHttp.open("POST", "../inc/compute.php", true);
      xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
      xmlHttp.onreadystatechange = handleRequestStateChange;

	    xmlHttp.send('od_ceny=' + OD_CENY + '&do_ceny=' + DO_CENY + '&sekce=' + SEKCE + '&nosnost=' + NOSNOST + '&typ=' + TYP + '&tuhost=' + values + '&zony=' + ZONY + '&vyskam=' + VYSKAM + '&vakci=' + VAKCI + '&delkamm=' + DELKAM + '&sirkamm=' + SIRKAM);
	    document.getElementById('pocet').innerHTML = '<img src="/images/ajax-loader-min.gif" />';
	    document.getElementById('stav').innerHTML = '<div style="z-index:50; background: #FEBB1D; border: 1px solid #7C7C7C; color: black; font-size: 18px; margin-top: 20px; padding: 10px 20px 20px 20px; width: 250px; height:30px;"><img style="float:left; margin-left:5px; margin-right:15px;" src="/images/ajax-loader.gif" /><div style="float:left; margin-top:8px;">Nahrávám produkty...</div></div>';
      document.getElementById('backdiv').style.display='block';
      document.getElementById('backdiv').style.opacity='0.5';
      
      return true;
    }
    catch (e){
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function handleRequestStateChange(){  
  //request state has changed
  if (xmlHttp.readyState==4){//process is completed
    if (xmlHttp.status==200){
    //http status is OK
      try {
        var xmlResponse = xmlHttp.responseXML;
        //process response

	   var Pocet = xmlResponse.getElementsByTagName('pocet')[0].firstChild.nodeValue
	   var Pocet_rozmeru = xmlResponse.getElementsByTagName('pocet_rozmer')[0].firstChild.nodeValue
	   
     var Dotaz = xmlResponse.getElementsByTagName('data')[0].firstChild.nodeValue
     
    respDiv3 = document.getElementById('pocet');
        respDiv3.innerHTML = "";
        respDiv3.innerHTML += Pocet;

        if(Pocet > 200){
             document.getElementById('stav').innerHTML = '<div style="z-index:50; background: #FEBB1D; border: 1px solid #7C7C7C; color: black; font-size: 18px; margin-top: 20px; padding: 10px 10px 10px 10px; width: 250px; height:60px;"><div style="float:left;">Zadaným parametrům odpovídá příliš mnoho položek. Upřesněte výběr.</div></div>';
        }
        else if(Pocet == 0 || Pocet_rozmeru == 0){
            if(Pocet_rozmeru == 0){
                   document.getElementById('stav').innerHTML = '<div style="z-index:50; background: #FEBB1D; border: 1px solid #7C7C7C; color: black; font-size: 18px; margin-top: 20px; padding: 10px 10px 10px 10px; width: 250px; height:130px;"><div style="float:left;">Zadaným rozměrům neodpovídá žádný standardní rozměr. Upravte výběr nebo nám <a href="http://www.i-matrace.cz/napiste-nam/" title="Napište nám, o jaký rozměr máte zájem. Zboží Vám vyrobíme na míru." target="_blank">napište</a>, o jaký rozměr máte zájem. Zboží Vám vyrobíme na míru.</div></div>';
            }else if(Pocet == 0 && Pocet_rozmeru > 0){
                 document.getElementById('stav').innerHTML = '<div style="z-index:50; background: #FEBB1D; border: 1px solid #7C7C7C; color: black; font-size: 18px; margin-top: 20px; padding: 10px 10px 10px 10px; width: 250px; height:100px;"><div style="float:left;">Zadaným rozměrům odpovídá ' + Pocet_rozmeru + ' položek. Tyto položky však neodpovídají ostatním parametrům. Upravte výběr.</div></div>';
            }else if(Pocet == 0){
                document.getElementById('stav').innerHTML = '<div style="z-index:50; background: #FEBB1D; border: 1px solid #7C7C7C; color: black; font-size: 18px; margin-top: 20px; padding: 10px 10px 10px 10px; width: 250px; height:60px;"><div style="float:left;">Zadaným parametrům neodpovídá žáná položka. Upravte výběr.</div></div>';
            }
        }
        else{
          nacti_data(Dotaz);  
         }    
      }
      catch (e){
        alert("Error reading the response: " + e.toString());
      }
    }
  }
  return Pocet;
}



function nacti_data(Dotaz){
  //sends HTTP request
  if (xmlHttp2){
    try{
    //try to connect to server
      var STAV = document.getElementById('stav').value;
	  //create string of parameters

	    var DOTAZY = Dotaz; 
	      
      xmlHttp2.open("POST", "../inc/nacti.php", true);
      xmlHttp2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
      xmlHttp2.onreadystatechange = DatahandleRequestStateChange;

	  xmlHttp2.send('dotazy=' + DOTAZY);
	     document.getElementById('stav').innerHTML = '<div style="z-index:50; background: #FEBB1D; border: 1px solid #7C7C7C; color: black; font-size: 18px; margin-top: 20px; padding: 10px 20px 20px 20px; width: 250px; height:30px;"><img style="float:left; margin-left:5px; margin-right:15px;" src="/images/ajax-loader.gif" /><div style="float:left; margin-top:8px;">Nahrávám produkty...</div></div>';
   
   document.getElementById('backdiv').style.display='block';
   document.getElementById('backdiv').style.opacity='0.5'; 
      return true;
    }
    catch (e){
      alert("Can't connect to server:\n" + e.toString());
    }
  }
}

function DatahandleRequestStateChange(){  
  //request state has changed
  if (xmlHttp2.readyState==4){//process is completed
    if (xmlHttp2.status==200){
    //http status is OK
      try {
        var xmlResponse = xmlHttp2.responseXML;
        //process response
 
        document.getElementById('stav').innerHTML = '';
        
        var textOdpovedi = xmlHttp2.responseText;

        document.getElementById('dynamicka_data').innerHTML = textOdpovedi;
        document.getElementById('backdiv').style.display='block';
        document.getElementById('backdiv').style.opacity='1.0';       
      }
      catch (e){
        alert("Error reading the response: " + e.toString());
      }
    }
  }
  return true;
}



var xmlHttp3 = createXmlHttpRequestObject3();//stores XMLHttpRequestObject 

function createXmlHttpRequestObject3(){
  //creates XMLHttpRequest object if it is possible
  var xmlhttp3
  try {
  //should work on all browsers except IE6 or older
    xmlHttp3 = new XMLHttpRequest();
  }
  catch (e){
    //browser is IE6 or older
    try {
      xmlHttp3 = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch (e){
      //ignore error
    }
  }
  if (!xmlHttp3)
    alert ("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp3;
}

function cena(){
  //sends HTTP request
  if (xmlHttp3){
  var NOSNOST = document.getElementById('nosnost').value;
  var TYP = document.getElementById('typ').value; 
  
  if(TYP != '1' && TYP != '4'){ 
							NOSNOST = 0;
						}
			
	xmlHttp3.open("POST", "../inc/cena.php", true);
	xmlHttp3.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlHttp3.onreadystatechange = handleRequestStateChange3;
	xmlHttp3.send('nosnost=' + NOSNOST + '&typ=' + TYP);
	/*     // vhodne odkomentovat, pokud se zbozi nacita automaticky a ne po stisku tlacitka hledat
	document.getElementById('pocet').innerHTML = '<img src="/images/ajax-loader-min.gif" />';
	
	document.getElementById('stav').innerHTML = '<div style="z-index:50; background: #FEBB1D; border: 1px solid #7C7C7C; color: #888888; font-size: 18px; margin-top: 20px; padding: 10px 20px 20px 20px; width: 250px; height:30px;"><img style="float:left; margin-left:5px; margin-right:15px;" src="/images/ajax-loader.gif" /><div style="float:left; margin-top:8px;">Nahrávám produkty...</div></div>';
	
	document.getElementById('backdiv').style.display='block';
   document.getElementById('backdiv').style.opacity='0.5'; 
   */
   
   document.getElementById('stav').innerHTML = '<div style="z-index:50; background: #FEBB1D; border: 1px solid #7C7C7C; color: black; font-size: 18px; margin-top: 20px; padding: 10px 20px 20px 20px; width: 250px; height:30px;"><img style="float:left; margin-left:5px; margin-right:15px;" src="/images/ajax-loader.gif" /><div style="float:left; margin-top:8px;">Nahrávám parametry</div></div>';
   document.getElementById('backdiv').style.display='block';
   document.getElementById('backdiv').style.opacity='0.5'; 
  }
}

function handleRequestStateChange3(){  
  //request state has changed
  if (xmlHttp3.readyState==4){//process is completed
    if (xmlHttp3.status==200){
    //http status is OK
      try {
        var xmlResponse3 = xmlHttp3.responseXML;
        //process response
        var Od_cen = xmlResponse3.getElementsByTagName('odceny')[0].firstChild.nodeValue
        var Do_cen = xmlResponse3.getElementsByTagName('doceny')[0].firstChild.nodeValue
       // var DELKA = xmlResponse3.getElementsByTagName('delka')[0].firstChild.nodeValue
	        
	 		  document.getElementById('od_cena').value=Od_cen;
			  document.getElementById('do_cena').value=Do_cen;
           
        slide();
        
        
    
        
        var TYP = document.getElementById('typ').value;
        if(TYP == '1' || TYP == '4' || TYP == '2'){
        
              //////////////////////    
      var txt = "<select size=\"1\" id=\"delkam\" name=\"delkam\" >";
      var x = xmlResponse3.getElementsByTagName("delka");
    for (i=0;i<x.length;i++)
      {
      xx=xmlResponse3.getElementsByTagName("delka");
        {
        try
          {
          txt=txt + "<option value=" + xx[i].firstChild.nodeValue + " >" + xx[i].firstChild.nodeValue + "</option>";
          }
        catch (er)
          {
          txt=txt + "<option value=\"NULL\" >NULL</option>";
          }
        } 
       } // for
       txt_delka= txt + "  </select>"; 
       
       var txt_sirka = "<select size=\"1\" id=\"sirkam\" name=\"sirkam\" >";
      var x = xmlResponse3.getElementsByTagName("sirka");
    for (i=0;i<x.length;i++)
      {
      xx=xmlResponse3.getElementsByTagName("sirka");
        {
        try
          {
          txt_sirka=txt_sirka + "<option value=" + xx[i].firstChild.nodeValue + " >" + xx[i].firstChild.nodeValue + "</option>";
          }
        catch (er)
          {
          txt_sirka=txt_sirka + "<option value=\"NULL\" >NULL</option>";
          }
        } 
       } // for
       txt_sirka= txt_sirka + "  </select>"; 
     
     if(TYP == '1' || TYP == '4'){  
       var txt_vyska = "<select size=\"1\" id=\"vyskam\" name=\"vyskam\" >";
      var x = xmlResponse3.getElementsByTagName("vyska");
    for (i=0;i<x.length;i++)
      {
      xx=xmlResponse3.getElementsByTagName("vyska");
        {
        try
          {
          txt_vyska=txt_vyska + "<option value=" + xx[i].firstChild.nodeValue + " >" + xx[i].firstChild.nodeValue + "</option>";
          }
        catch (er)
          {
          txt_vyska=txt_vyska + "<option value=\"NULL\" >NULL</option>";
          }
        } 
       } // for
       txt_vyska= txt_vyska + "  </select>";
       
       
       var txt_zony = "<select size=\"1\" id=\"zony\" name=\"zony\" >";
       var sekce = new Array("Vše","Bez zón","3 zóny","5 zón","7 zón");
       var sekcex = new Array("vse","1","2","3","4");
  
    for (i=0;i<sekce.length;i++)
      {
      xx=xmlResponse3.getElementsByTagName("vyska");

          txt_zony=txt_zony + "<option value=" + sekcex[i] + " >" + sekce[i] + "</option>";

       } // for
       txt_zony= txt_zony + "  </select>";
      } // if typ 1 a 4  
        //////////////////////
          if(TYP == '1'){
                document.getElementById('matrace-tuhost').style.display='block';     // zobrazim matrace
          }else{
                document.getElementById('matrace-tuhost').style.display='none';     // nezobrazim matrace
          }
          if(TYP == '4'){
                document.getElementById('rosty-tuhost').style.display='block';     // zobrazim matrace
          }else{
                document.getElementById('rosty-tuhost').style.display='none';     // nezobrazim matrace
          }
          if(TYP == '2'){
                document.getElementById('postel-tuhost').style.display='block';     // zobrazim matrace
          }else{
                document.getElementById('postel-tuhost').style.display='none';     // nezobrazim matrace
          }
          
                    document.getElementById('dyn_delka'+TYP).innerHTML = txt_delka;     // zobrazim matrace
                    document.getElementById('dyn_sirka'+TYP).innerHTML = txt_sirka;     // zobrazim matrace 
                  if(TYP == '1' || TYP == '4'){  
                    document.getElementById('dyn_vyska'+TYP).innerHTML = txt_vyska;     // zobrazim matrace
                    document.getElementById('dyn_zony'+TYP).innerHTML = txt_zony;     // zobrazim matrace
                    selectTrans4.transform4("zonyz"+TYP);
                    selectTrans5.transform5("vyskaz"+TYP);
                    document.getElementById('zony').value='undefined'; 
                  } // if
                    selectTrans6.transform6("delkaz"+TYP);
                    selectTrans7.transform7("sirkaz"+TYP);
                    
                    
        } // if
        else{
          document.getElementById('matrace-tuhost').style.display='none';
          document.getElementById('rosty-tuhost').style.display='none';
          document.getElementById('postel-tuhost').style.display='none';
        } // else
        
        document.getElementById('dynamicka_data').innerHTML = "";
        document.getElementById('pocet').innerHTML = "";
        
        document.getElementById('nosnost').value=0;
        
        //document.getElementById('vyskam').value=-1;
        document.getElementById('stav').innerHTML = "";
        document.getElementById('backdiv').style.display='block';
        document.getElementById('backdiv').style.opacity='1.0';
      }
      catch (e){
        alert("Error reading the response: " + e.toString());
      }
    }
  }
  return true;
}

    /**
     * returns an array with the values of the checked checkboxes with name
     * @param name name of the checkboxes
     * @return new_arr array with checked-values
     */
    function get_array_for_checkboxes_with_name(name) {
        var new_arr = [];
        var nodes = document.getElementsByName(name);
     
        for (var i = 0, n; n = nodes[i]; i++) {
            if (n.checked == true) {
                new_arr.push(n.value);
            }
        }
     
        return new_arr;
    }


