/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}


/**
 * isDefined v1.0
 * 
 * Check if a javascript variable has been defined.
 * 
 * Author : Jehiah Czebotar
 * Website: http://www.jehiah.com
 * Usage  : alert(isdefined('myvar'));
 */
function isDefined( variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}



function checkUncheckAll(theElement) {
	var checkBoxes = getCheckboxes();
	for(z = 0; z < checkBoxes.length; z++){
		checkBoxes[z].checked = !(checkBoxes[z].checked);
	}
}

function getCheckboxes() {
	var node_list = document.getElementsByTagName('input');
	var checkboxes = [];

	for (var i =0; i < node_list.length; i++) {
		var node = node_list[i];

		if (node.getAttribute('type') == 'checkbox') {
			checkboxes.push(node);
		}
	} 

	return checkboxes;
}


function jumpFromSelect(theSelect){

    window.location.href = theSelect.options[theSelect.selectedIndex].value
}

function displayBlock(id) {
	
	var block = document.getElementById(id);
	if(block.style.display == "block")
		block.style.display = "none";
	else
		block.style.display = "block";
}

function showBlock(id) {
	var block = document.getElementById(id);
	block.style.display = "block";
}

function hideBlock(id) {
	var block = document.getElementById(id);
	block.style.display = "none";
}

function showMessage( message, color) {
	
	element = document.getElementById('messenger');
	element.style.display = "block";
	element.style.backgroundColor = color; 
	element.innerHTML = message+" <br />";
}

//Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

// po ztraceni focusu nastavi text prvku formulare na defaultni hodnotu
setText = function(form, element, msg) {
	
	/*
	var el = document.getElementById(theElement);
	if (el.value == '') {	// nastavi defaultni, jen pokud uzivatel nezadal svoji hodnotu
		el.value = msg;
	}
	*/
	
	el = $("#"+form).find("#"+element);
	val = el.val();
	if (val == '') {
		el.val(msg);
	}
}

// po ziskani focusu vymaze text prvku
removeText = function(form, element, msg) {

	el = $("#"+form).find("#"+element);
	val = el.val();
	if (val == msg) {
		el.val('');
	}
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param container DOM element
 */
function setAllRadios( container_id, value) {
	
	var rows = document.getElementById(container_id).getElementsByTagName('tr');
	
	var radio;
	for ( var i = 0; i < rows.length; i++ ) {
	
		inputElements = rows[i].getElementsByTagName( 'input' );
		for ( var j = 0; j < inputElements.length; j++) {
		
			element = inputElements[j];
			if ( element && element.type == 'radio' ) {
				element.checked = false;
				if(element.value == value.toString()) {
					element.checked = true;
				}
			}
		}
	}
	return true;
}

/**
*
* @param id id prvku, ktery obsahuje checkboxy
* @param name jmeno checkboxu, ktere maji byt oznaceny
* @param flag 0-odznacit, 1-oznacit
* @return
*/
function jqCheckAll( id, name, flag )
{
  if (flag == 0)
  {
     $("#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', false);
  }
  else
  {
     $("#" + id + " INPUT[@name=" + name + "][type='checkbox']").attr('checked', true);
  }
}

/*
 * Market - pridani polozky do porovnavaciho kosiku
 */
function addBasketElement(label, url, id, pathToAction, count){
	
		// pokud je kosik prazdny, odstrani se veta s touto informaci
		itemsCount = $("#div-compare-products > p").size();
		if (itemsCount == 0) {
			$("#div-compare-products > div.empty").remove();
		}
		
		// ID hledaneho elementu
		elementID = 'p-in-basket-'+id;
		// nalezne element. Pokud neexistuje, vytvori ho
		element = $('#div-compare-products > #' + elementID);
		if( element.size() == 0) {
			
			// vytvoreni elementu
			element = $('<p>');
			element.attr( 'class', 'product-in-basket');
			element.attr( 'id', elementID);
			$( '#div-compare-products').append( element);
		}
		// kontrola na count
		countLabel = ( count > 0) ? ' (' + count + ') ' : "";
		// vyplneni obsahu elementu
		element.html( '<a href="'+ url +'" title="'+ label +'">'+ countLabel + label + '</a> <span style="cursor:pointer;color:#ff7800;" onClick="$.get( location.protocol + \'//\' + location.hostname + JSbaseUrl+ \'' + pathToAction + id+'\'  , function(data){eval(data)});" id="in-basket-'+id+'" title="odebrat '+ label+' z porovnání">[X]</span>');	

}

/*
 * Market - odebrani polozky z porovnavaciho kose
 */
function deleteFromBasket(id, module) {
	var parentDiv = document.getElementById('div-compare-products');
	var myP = document.getElementById('p-in-basket-'+id);
	parentDiv.removeChild(myP);
	
	if (module == "eshop") {
		msg = "Žádné položky v nákupním košíku";
	}
	else if (module == "market") {
		msg = "Žádné položky k&nbsp;porovnání";
	}
	
	// pokud se odebira posledni polozka z kosiku, prida se o tom veta
	itemsCount = $("#div-compare-products > p").size();
	if (itemsCount == 0) {
		$("#div-compare-products").append("<div class='empty'>"+msg+"</div>");
	}
}

/**
 * funkce, aby fungovalo :hover v IE6
 */
startList = function() {
	if (document.all&&document.getElementById) {
	navRoot = document.getElementById("menu_list");
	for (i=0; i<navRoot.childNodes.length; i++) {
	node = navRoot.childNodes[i];
	if (node.nodeName=="LI") {
	node.onmouseover=function() {
	this.className+=" over";
	  }
	  node.onmouseout=function() {
	  this.className=this.className.replace
	        (" over", "");
	   }
	   }
	  }
	 }
	}
window.onload=startList;

/**
 * funkce pro odeslani odpovedi v diskuzi - vyplni "komu se odpovida"
 * @param date
 * @param author
 * @return
 */
function post_reply(date, author) {
	var msg = document.getElementById('message');
	msg.value += "*>> **"+author+"**, "+date+"*\n\n";
	msg.focus();
}
