﻿// JavaScript Document
/** odeslání XMLHttp požadavku
@param function obsluha funkce zajišťující obsluhu při změně stavu požadavku, dostane parametr s XMLHttp objektem
@param string method GET|POST|...
@param string url URL požadavku
@param string [content] tělo zprávy
@param array [headers] pole předaných hlaviček ve tvaru { 'hlavička': 'obsah' }
@return bool true v případě úspěchu, false jinak
*/
function send_xmlhttprequest(obsluha, method, url, content, headers)
{
    var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest : (window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : false));
	

	if (!xmlhttp) {
        alert(" nemuzu vytvorit xmlhttp object");
		return false;
    }
    xmlhttp.open(method, url);
    xmlhttp.onreadystatechange = function() {
        obsluha(xmlhttp);
    };
    if (headers) {
        for (var key in headers) {
            xmlhttp.setRequestHeader(key, headers[key]);
        }
    }
    xmlhttp.send(content);
    return true;
}


function change_region(id60,lang)
{
	document.getElementById('pregion').className = 'hide';
	document.getElementById('pcity').className = 'hide';
// odeslání požadavku na aktualizaci dat
    
	if (id60!=0) { 
	if (!send_xmlhttprequest(region_obsluha, 'GET', 'region.php?id60=' + id60 + '&lang=' + lang )) { 
        return false;
    }
    document.getElementById('stav-search').innerHTML = 'loading...';
    document.getElementById('stav-search').className = 'full';
	return true;
	} else document.getElementById('stav-search').innerHTML = '&nbsp;';
}


function region_obsluha(xmlhttp)
{
    if (xmlhttp.readyState == 4) {
        // aktualizace odpovědí na základě aktuálního stavu
        removeValue(document.getElementById('pregion'),'Region');
		var regiony = xmlhttp.responseXML.getElementsByTagName('region');
        for (var i=0; i < regiony.length; i++) {
		insertValue(document.getElementById('pregion'), regiony[i].firstChild.data, regiony[i].getAttribute('id'));
		} 
	   document.getElementById('pregion').className = 'full'; 
	   document.getElementById('stav-search').style.display = 'none';
    }
}

function change_city(id61,lang)
{
	document.getElementById('pcity').className = 'hide';
// odeslání požadavku na aktualizaci dat
    
	if (id61!=0) { 
	if (!send_xmlhttprequest(city_obsluha, 'GET', 'city.php?id61=' + id61 + '&lang=' + lang )) { 
        return false;
    }
    document.getElementById('stav-search').innerHTML = 'loading...';
    document.getElementById('stav-search').className = 'full';
	return true;
	} else document.getElementById('stav-search').innerHTML = '&nbsp;';
}

function change_city_reg(id60,lang)
{
	document.getElementById('pcity').className = 'hide';
// odeslání požadavku na aktualizaci dat
    
	if (id60!=0) { 
	if (!send_xmlhttprequest(city_obsluha, 'GET', 'city_reg.php?id60=' + id60 + '&lang=' + lang )) { 
        return false;
    }
    document.getElementById('stav-search').innerHTML = 'loading...';
    document.getElementById('stav-search').className = 'full';
	return true;
	} else document.getElementById('stav-search').innerHTML = '&nbsp;';
}


function city_obsluha(xmlhttp)
{
    if (xmlhttp.readyState == 4) {
        // aktualizace odpovědí na základě aktuálního stavu
        removeValue(document.getElementById('pcity'),'City');
		var regiony = xmlhttp.responseXML.getElementsByTagName('region');
        for (var i=0; i < regiony.length; i++) {
		insertValue(document.getElementById('pcity'), regiony[i].firstChild.data, regiony[i].getAttribute('id'));
		} 
	   document.getElementById('pcity').className = 'full'; 
	   document.getElementById('stav-search').style.display = 'none';
    }
}

function change_detailB(id15,lang)
{
	document.getElementById('building_detail').className = 'hide';
// odeslání požadavku na aktualizaci dat
    //alert (id15);
	if (id15!=0) { 
	if (!send_xmlhttprequest(detailB_obsluha, 'GET', 'detail_building.php?id15=' + id15 + '&lang=' + lang )) { 
        return false;
    }
//    document.getElementById('stav-search').innerHTML = 'loading...';
//    document.getElementById('stav-search').className = 'full';
	return true;
	} else { //document.getElementById('stav-search').innerHTML = '&nbsp;';
		}
}


function detailB_obsluha(xmlhttp)
{
    if (xmlhttp.readyState == 4) {
//alert('We are currently working on this website, therefore you may encounter some errors.');
        // aktualizace odpovědí na základě aktuálního stavu
        //removeValue(document.getElementById('pcity'),'City');
	  
		var popisky = xmlhttp.responseXML.getElementsByTagName('pom');
 		for (var i=0; i < popisky.length-1; i++) {
		//document.getElementById('li1').style.display='none';
		//document.getElementById('pom_1').innerHTML=i;
		if (popisky[i].firstChild) {
		 if (popisky[i].firstChild.data!='') {
		  document.getElementById('li'+popisky[i].getAttribute('id')).style.display = '';
		  p=popisky[i].firstChild.data;
		 } else {
		  p=' '; 
		  document.getElementById('li'+popisky[i].getAttribute('id')).style.display='none';
		 }
		} else {
		 p=' '; 
		 document.getElementById('li'+popisky[i].getAttribute('id')).style["display"]='none';
		 //alert(popisky[i].getAttribute('id')); 
		}
		//alert(document.getElementById('li'+popisky[i].getAttribute('id')).style.display);
		document.getElementById('pom_'+popisky[i].getAttribute('id')).innerHTML=' '+p;

		}
		// the last node of the XML is not processed in the for(;;) because it creates an error
		photosTotal = popisky[i].firstChild.data; buildingID = popisky[i].getAttribute('id15'); parkID = popisky[i].getAttribute('id10'); photoIndex =  popisky[i].getAttribute('index'); 
		//insertValue(document.getElementById('pcity'), regiony[i].firstChild.data, regiony[i].getAttribute('id'));
		if (t = document.getElementById('r0')) t.parentNode.removeChild(t);
		if (t = document.getElementById('r0_img')) t.parentNode.removeChild(t);
		if (t = document.getElementById('r0_printLink')) t.parentNode.removeChild(t);

		// pozice a rozmery budovy pro tisk
		myBuildingCSSRules = getCSSRule('#b'+buildingID);
		//alert(myBuildingCSSRules.style.top);
		linkAppend = "&left="+myBuildingCSSRules.style.left+"&top="+myBuildingCSSRules.style.top+"&width="+myBuildingCSSRules.style.width+"&height="+myBuildingCSSRules.style.height+"&lang=en";
		//alert(linkAppend);
		printLink = document.createElement('a');
		printLink.href = "print.php?id15="+buildingID+"&id10="+parkID+linkAppend;
		printLink.id = "r0_printLink";
		printLink.innerHTML = "&raquo; <strong>Print the detail</strong><br>&nbsp;";
		parentDiv = document.getElementById('building_detail');
		parentDiv.appendChild(printLink);

	   document.getElementById('benefits_data').className = 'hide';
	   document.getElementById('building_detail').className = ''; 
	   //document.getElementById('stav-search').style.display = 'none';

		if (photosTotal > 0) {
			indexKey = photoIndex.split('-');
			thumbs = document.createElement('div');
			thumbs.id 			 = "r0";
			thumbs.className = "thumbs";
			for (var i=1; i <= photosTotal; i++) {
			thumblink = document.createElement('a');
			thumblink.href = "javascript:setImage(0,'f"+buildingID+"',"+indexKey[i-1]+");";
			thumb = document.createElement('img');
			thumblink.id  = "r0_"+i;
			if (i==1) thumblink.className = "thumbnail selected"; else thumblink.className  = "thumbnail";
			thumb.src = "img_upload/f"+buildingID+"_"+indexKey[i-1]+"t.jpg"+killCache;
			thumblink.appendChild(thumb);
			thumbs.appendChild(thumblink);
			} // end for i < photosTotal;
			parentDiv = document.getElementById('building_detail');
			photo = document.createElement('img');
			photo.id = "r0_img";
			photo.src = "img_upload/f"+buildingID+"_"+indexKey[0]+".jpg";
			parentDiv.appendChild(thumbs);
			parentDiv.appendChild(photo);
		} // end if (photosTotal > 0)

}
}

function removeValue(theSel,set)
{
	

for(i=theSel.length-1; i>=0; i--)
    {
        theSel.options[i] = null;
    }
var newOpt1 = new Option(set, '0');
    theSel.options[0] = newOpt1;
/*    if (theSel.length > 0) {
      theSel.selectedIndex = selIndex == 0 ? 0 : selIndex - 1;
    } */
  
}

function insertValue(theSel, newText, newValue)
{
  if (theSel.length == 0) {
    var newOpt1 = new Option(newText, newValue);
    theSel.options[0] = newOpt1;
    //theSel.selectedIndex = 0;
  } else {
    
	var newCount=theSel.length;
	var newOpt1 = new Option(newText, newValue);
    theSel.options[newCount] = newOpt1;
  } 
}

function poslat(pre,firm,dom) {
	//document.write('<a href="mailto:' + pre + '@' + firm + '.' + dom '">' + pre + '@' + firm + '.' + dom + '</a>');
	document.write('<a href="mailto:' + pre + '@' + firm + '.' + dom + '">' +pre + '@' + firm + '.' + dom + '</a>');
}


function change_souradnice(id15,top,left,width,height) {

if (id15!=0) { 

	if (!send_xmlhttprequest('', 'GET', 'souradnice.php?id15=' + id15 + '&top=' + top + '&left=' + left + '&width=' + width + '&height=' + height)) { 
        
		return false;
    }
	return true;
	} else { //document.getElementById('stav-search').innerHTML = '&nbsp;';
	}	
}
