function requestObject() {
	var page3d = '/?akce=3d';

	this.add3dFav = function (id,noredir) {
		var callback = {
			scope: this,
			argument: arguments,
			success: {
			    apply:function(scope, obj) {
			    try {
			      show_loading(false);
						var response = obj[0].responseText;
			      if (response.indexOf("ERRNO") >= 0
				      || response.indexOf("error:") >= 0
				      || response.length == 0)
				    	throw(response.length == 0 ? "Server nereaguje." : response);
			    
						var xmlResponse = obj[0].responseXML;
            var xmlRoot = xmlResponse.documentElement;
            var result = xmlRoot.firstChild.text;
            if (result == 1) {
              if (noredir) alert('Zboží bylo přidáno do vybraných textur.');
							else if (confirm('Byla přidána textura do 3d.' + "\n" + 'Chete-li přesměrovat na stranu se 3D programem, stiskněte OK.')) document.location = page3d;
						}
					} catch(e) {
          	displayError(e.toString());
          }
				}
			},
			failure: function(obj){alert('error: ' + obj.responseText)}
		}
    show_loading(true);
		YAHOO.util.Connect.asyncRequest(
			'GET',
			'vrmlResponse.php?action=addFav'
         +'&id='+ id,
			callback,
			null
		);
	}

	this.get3dFav = function (typ) {
		var callback = {
			scope: this,
			argument: arguments,
			success: {
			    apply:function(scope, obj) {
			    try {
			      show_loading(false);
						var response = obj[0].responseText;
			      if (response.indexOf("ERRNO") >= 0
				      || response.indexOf("error:") >= 0
				      || response.length == 0)
				    	throw(response.length == 0 ? "Server nereaguje." : response);

						var xmlResponse = obj[0].responseXML;
            var xmlRoot = xmlResponse.documentElement;
            var result = xmlRoot.firstChild.text;
            if (result == 1) {
              if (xmlRoot.childNodes(1)) {
		            dataSet = xmlRoot.childNodes(1);
	              GlobalTextureStorage = xmlZboziToArray(dataSet.getElementsByTagName("zbozi"));
							}
              generateFavourites(GlobalTextureStorage, 'Přednastavené textury');
						}
					} catch(e) {
          	displayError(e.toString());
          }
				}
			},
			failure: function(obj){alert('error: ' + obj.responseText)}
		}
    show_loading(true);
		YAHOO.util.Connect.asyncRequest(
			'GET',
			'vrmlResponse.php?action=getFav'
				+'&typ='+typ,
			callback,
			null
		);
	}

	this.del3dFav = function (id) {
		var callback = {
			scope: this,
			argument: arguments,
			success: {
			    apply:function(scope, obj) {
			    try {
						show_loading(false);
						var response = obj[0].responseText;
			      if (response.indexOf("ERRNO") >= 0
				      || response.indexOf("error:") >= 0
				      || response.length == 0)
				    	throw(response.length == 0 ? "Server nereaguje." : response);

						var xmlResponse = obj[0].responseXML;
            var xmlRoot = xmlResponse.documentElement;
            var result = xmlRoot.firstChild.text;
            if (result == 1) {
              zboziArray = new Array();
              if (xmlRoot.childNodes(1)) {
		            dataSet = xmlRoot.childNodes(1);
	              GlobalTextureStorage = xmlZboziToArray(dataSet.getElementsByTagName("zbozi"));
							}
              generateFavourites(GlobalTextureStorage, 'Přednastavené textury');
						}
					} catch(e) {
          	displayError(e.toString());
          }
				}
			},
			failure: function(obj){alert('error: ' + obj.responseText)}
		}
    show_loading(true);
		YAHOO.util.Connect.asyncRequest(
			'GET',
			'vrmlResponse.php?action=delFav'
         +'&id='+ id,
			callback,
			null
		);
	}

	this.set3dTyp = function (id,typ,sirka,vyska) {
		var callback = {
			scope: this,
			argument: arguments,
			success: {
			    apply:function(scope, obj) {
			    try {
						var response = obj[0].responseText;
			      if (response.indexOf("ERRNO") >= 0
				      || response.indexOf("error:") >= 0
				      || response.length == 0) {
                show_loading(false);
                throw(response.length == 0 ? "Server nereaguje." : response);
							}

            if (response == 1) {
              show_loading(false);
              window.status = 'Uloženo';
						}
					} catch(e) {
          	displayError(e.toString());
          }
				}
			},
			failure: function(obj){alert('error: ' + obj.responseText)}
		}
    show_loading(true);
		YAHOO.util.Connect.asyncRequest(
			'GET',
			'/sprava/ajaxResponse.php?action=setTyp'
         +'&id='+ id
         +'&typ='+ typ
         +'&s='+ sirka
         +'&v='+ vyska,
			callback,
			null
		);
	}

	this.get3dDirs = function (typ) {
		var callback = {
			scope: this,
			argument: arguments,
			success: {
			    apply:function(scope, obj) {
			    try {
            show_loading(false);
						var response = obj[0].responseText;
						GlobalDirCache[typ] = response;
						document.getElementById('zbozilistContent').innerHTML = response;
					} catch(e) {
          	displayError(e.toString());
          }
				}
			},
			failure: function(obj){alert('error: ' + obj.responseText)}
		}
		if (GlobalDirCache.length > 0 && GlobalDirCache[typ]) {
			document.getElementById('zbozilistContent').innerHTML = GlobalDirCache[typ];
			return;
		}
    show_loading(true);
		YAHOO.util.Connect.asyncRequest(
			'GET',
			'vrmlResponse.php?action=getDirTyp'
         +'&typ='+ typ,
			callback,
			null
		);
	}

	this.getZboziDir = function (dir) {
		var callback = {
			scope: this,
			argument: arguments,
			success: {
			    apply:function(scope, obj) {
			    try {
            show_loading(false);
						var response = obj[0].responseText;
						GlobalZboziCache[dir] = response;
						document.getElementById('dir3d_'+dir).innerHTML += response;
					} catch(e) {
          	displayError(e.toString());
          }
				}
			},
			failure: function(obj){alert('error: ' + obj.responseText)}
		}
		if (GlobalZboziCache.length > 0 && GlobalZboziCache[dir]) {
			document.getElementById('dir3d_'+dir).innerHTML = GlobalZboziCache[dir];
			return;
		}
    show_loading(true);
		YAHOO.util.Connect.asyncRequest(
			'GET',
			'vrmlResponse.php?action=getZboziDir'
         +'&dir='+ dir,
			callback,
			null
		);
	}
}
var rq = new requestObject();

/* function that displays an error message */
function displayError(message) {
  // display error message, with more technical details if debugMode is true
  alert("chyba při zpracování! "+ "\n" + message.replace('error:','') );
}

/* transforms all the children of an xml node into an array */
function xmlZboziToArray(resultsXml)
{
  // initiate the resultsArray
  var resultsArray= new Array();
  if (resultsXml.length<1) return resultsArray;
  // loop through all the xml nodes retrieving the content
  for(i=0;i<resultsXml.length;i++) {
		node = resultsXml.item(i);
    var id = node.getAttribute("id");
		resultsArray[id] = new Array();
		resultsArray[id]['id'] = id;
		resultsArray[id]['nazev'] = node.text;
		resultsArray[id]['typ'] = node.getAttribute("typ");
		resultsArray[id]['img'] = node.getAttribute("img");
		resultsArray[id]['param1'] = node.getAttribute("param1");
		resultsArray[id]['param2'] = node.getAttribute("param2");
	}

  return resultsArray;
}

function  show_loading(show) {
	if (show) {
    document.getElementById('loading').style.top = scrollTop()+20;
    document.getElementById('loading').style.left = scrollLeft()+20;
		document.getElementById('loading').style.visibility = 'visible';
		document.body.style.cursor = "wait";
	} else {
    document.getElementById('loading').style.top = 0;
		document.getElementById('loading').style.visibility = 'hidden';
		document.body.style.cursor = "default";
	}
}

function scrollTop() {
	return filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function scrollLeft() {
	return filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}