function getNodeValue(tree, el){
	if ( tree.getElementsByTagName(el)[0].firstChild != null )
		return tree.getElementsByTagName(el)[0].firstChild.nodeValue;
	else
		return '';
}

function getValue(el) {
	if ( el.firstChild != null )
		return el.firstChild.nodeValue
	else
		return '';
}

// Vyber plateb v zavislosti na doprave
function vyber_platby(doprava_id) {
	var platby_box = $('#platby');

	// skryti vsech a zobrazeni spravneho boxu
	$('#platby > div').each(function() {
		$(this).addClass('noscreen');
		$('input:radio', $(this)).each(function() { $(this).attr('disabled', true); });
	});
	$('#platby_dopr_'+doprava_id).removeClass('noscreen');
	$('input:radio', $('#platby_dopr_'+doprava_id)).each(function() {
		$(this).attr('disabled', false);
	});

	// preneseni zaskrtnuti
	var selected = $('input:radio[@checked]', platby_box);
	if ( selected.length == 1 ) {
		var new_selected = $('#'+$(selected).attr('id').replace(/(\d+)$/, doprava_id));
		if ( new_selected ) {
			$(new_selected).attr('checked', 'checked');
		}
	}
}


// AJAX - porovnani produktu
function porovnani_action(produkt_id, anchor, class_name) {
	if ( anchor.className.indexOf(class_name) < 0 ) {
		// pridani
		if ( porovnani.pridat(produkt_id) ) {
			anchor.className += ' '+class_name;
			//document.location = '#compare-box';
		}
	} else {
		// odebrani
		if ( porovnani.odebrat(produkt_id) ) {
			anchor.className = anchor.className.replace(' '+class_name, '');
		}
	}

	return false;
}

function Porovnani() {
	this.ajaxFile = '';
	this.sid = '';
	this.loader = false;
	this.loaderText = '';
	this.targetBox = false;
	this.targetCount = false;
	this.hiddenClass = 'noscreen';
	this.msgErr = '';
	this.jazyk = 0;

	this.buildLoader = function() {
	    this.loader = document.createElement('div');
	    this.loader.style.position   = 'absolute';
	    this.loader.style.top        = '50%';
	    this.loader.style.left       = '50%';
	    this.loader.style.width      = '400px';
	    this.loader.style.lineHeight = '100px';
	    this.loader.style.margin     = '-50px 0 0 -200px';
	    this.loader.style.textAlign  = 'center';
	    this.loader.style.border     = '3px double #870108';
	    this.loader.style.background = '#f6f6f6';
	    this.loader.appendChild(document.createTextNode(this.loaderText));
	    document.getElementsByTagName('body')[0].appendChild(this.loader);
	}

	this.killLoader = function() {
	    document.getElementsByTagName('body')[0].removeChild(this.loader);
	}

	this.pridat = function(produkt) {
		var url = this.ajaxFile+'?produkt=' + produkt + '&jazyk=' + this.jazyk + '&pridat=1';
		if ( this.sid != '' ) url = url + '&' + this.sid;
		this.buildLoader();

		if (window.ActiveXObject) {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			httpRequest = new XMLHttpRequest();
		}
		httpRequest.open("GET", url, true);
		targetBox = this.targetBox; targetCount = this.targetCount; hiddenClass = this.hiddenClass; msgErr = this.msgErr;
		httpRequest.onreadystatechange = function() { process = new PorovnaniProcess(targetBox, targetCount, hiddenClass, msgErr); process.process(); }
		httpRequest.send(null);

		this.killLoader();

		return true;
	}

	this.odebrat = function(produkt) {
		var url = this.ajaxFile+'?produkt=' + produkt + '&jazyk=' + this.jazyk + '&odebrat=1';
		if ( this.sid != '' ) url = url + '&' + this.sid;

		this.buildLoader();

		if (window.ActiveXObject) {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			httpRequest = new XMLHttpRequest();
		}
		httpRequest.open("GET", url, true);
		targetBox = this.targetBox; targetCount = this.targetCount; hiddenClass = this.hiddenClass; msgErr = this.msgErr;
		httpRequest.onreadystatechange = function() { process = new PorovnaniProcess(targetBox, targetCount, hiddenClass, msgErr); process.process(); }
		httpRequest.send(null);

		this.killLoader();

		return true;
	}

	this.odebratVse = function() {
		var url = this.ajaxFile+'?jazyk=' + this.jazyk + '&odebrat_vse=1';
		if ( this.sid != '' ) url = url + '&' + this.sid;

		this.buildLoader();

		if (window.ActiveXObject) {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			httpRequest = new XMLHttpRequest();
		}
		httpRequest.open("GET", url, true);
		targetBox = this.targetBox; targetCount = this.targetCount; hiddenClass = this.hiddenClass; msgErr = this.msgErr;
		httpRequest.onreadystatechange = function() { process = new PorovnaniProcess(targetBox, targetCount, hiddenClass, msgErr); process.process(); }
		httpRequest.send(null);

		this.killLoader();

		var all_anchors = document.getElementsByTagName("a");
		for(i=0; i<all_anchors.length; i++) {
			if ( all_anchors[i].className.indexOf('porovnani') >= 0 && all_anchors[i].className.indexOf('disabled') >= 0 ) {
				all_anchors[i].className = all_anchors[i].className.replace('disabled', '');
			}
		}

		return true;
	}
}

function PorovnaniProcess(targetBox, targetCount, hiddenClass, msgErr) {
	this.targetBox = targetBox;
	this.targetCount = targetCount;
	this.hiddenClass = hiddenClass;
	this.msgErr = msgErr;

	this.process = function() {
		if (httpRequest.readyState == 4) {
	    	if(httpRequest.status == 200) {
				var xml = httpRequest.responseXML;
				this.refresh(xml);
				return true;
	    	} else {
				alert(this.msgErr);
				return false;
			}
		}
	}

	this.refresh = function(xml) {
		var targetBox = document.getElementById(this.targetBox);
		var targetCount = document.getElementById(this.targetCount);
		var id = xml.getElementsByTagName('id');
		var nazev = xml.getElementsByTagName('nazev');
		var porovnavat = xml.getElementsByTagName('porovnavat');
		var pocet = xml.getElementsByTagName('pocet'); // pocet produktu v porovnani
		var produkty = xml.getElementsByTagName('produkt_id'); // produkty_v_porovnani

		targetCount.innerHTML = getValue(pocet[0]);
		if ( getValue(pocet[0]) > 0 ) {
			targetBox.className = '';
		} else {
			targetBox.className = this.hiddenClass;
		}
	}
}