function cenaRound(cena){
	cena=Math.round(cena*100)/100;
	cena=cena.toFixed(2);
	
	return cena;
}
function spravCena(value){
	value = value.replace(/ /gi, "");
  	value = value.replace(/,/, '.');
  	
  	return value;
}

function prepocitatDPH(dph){
	var cenabez = document.getElementById("bez_dph");
	var cenas   = document.getElementById("s_dph");
	
	cenas.value = cenaRound((1*cenabez.value) + (((1*cenabez.value) / 100) * (dph*1))); 	
}

/*=======================================================*/

/*function divShowHide( id ){
	object = document.getElementById( id );
	if(object.style.display == 'none'){
		object.style.display = 'block';
	}
	else {
		object.style.display = 'none';
	}
}*/

function divShowHide( id ){
	var stav = $("#"+id).css("display");
	$("#"+id).slideToggle(500);
}

function show_hide(obj){
	obj = document.getElementById(obj);
	obj.style.display = 'block';
}

/*=======================================================*/

jQuery.fn.inputHints = function() {
    // hides the input display text stored in the title on focus
    // and sets it on blur if the user hasn't changed it.

    // show the display text
    $(this).each(function(i) {
        $(this).val($(this).attr('title'));
    });

    // hook up the blur & focus
    $(this).focus(function() {
        if ($(this).val() == $(this).attr('title'))
            $(this).val('');
    }).blur(function() {
        if ($(this).val() == '')
            $(this).val($(this).attr('title'));
    });
};

function tree_zmena(objLI,objIMG){
	objLI = document.getElementById(objLI);
	objIMG = document.getElementById(objIMG);
	if (objIMG.className == 'plus') {
		objIMG.className = 'minus';
		$(objLI).slideToggle(500);
	} else {
		objIMG.className = 'plus'
		$(objLI).slideToggle(500);
	}
}
/*function tree_zmena(objLI,objIMG){
	objLI = document.getElementById(objLI);
	objIMG = document.getElementById(objIMG);
	if (objIMG.className == 'plus') {
		objIMG.className = 'minus';
		objLI.style.display = 'block';
	} else {
		objIMG.className = 'plus'
		objLI.style.display = 'none';
	}
}*/

function navigateWithReferrer(url){
	var fakeLink = document.createElement ("a");
	if (typeof(fakeLink.click) == 'undefined')
	location.href = url;  // sends referrer in FF, not in IE
	else
	{
		fakeLink.href = url;
		document.body.appendChild(fakeLink);
		fakeLink.click();   // click() method defined in IE only
	}
}

var cislo = 0;
function insertInput(){
	var div = document.getElementById("variace");
	
	var nazev = document.createElement("input");  
	nazev.setAttribute("type", "text");  
	nazev.setAttribute("name", "varNazev["+cislo+"]");  
	
	var cena = document.createElement("input");  
	cena.setAttribute("type", "text");  
	cena.setAttribute("name", "varCena["+cislo+"]");  
	cena.onkeyup = function(){cenas.value = cenaRound((1*this.value) + (((1*this.value) / 100) * (this.form.dph.value*1)))};
	
	var cenas = document.createElement("input");  
	cenas.setAttribute("type", "text");  
	cenas.setAttribute("name", "varCenaS["+cislo+"]"); 
	cenas.onkeyup = function(){cena.value = cenaRound(100*this.value / (100 + this.form.dph.value*1))};
	 
	var span1 = document.createElement("span"); 
	var span2 = document.createElement("span");
	var span3 = document.createElement("span");
	
	var new_div = document.createElement("p"); 
	//new_div.setAttribute("id", "variace-"+cislo);  
	
	div.appendChild(new_div);
	new_div.appendChild(span1);
	new_div.appendChild(nazev);    
	span1.appendChild(document.createTextNode("Variace: ")); 
	
	new_div.appendChild(span2);
	new_div.appendChild(cena);    
	span2.appendChild(document.createTextNode(" Cena: ")); 
	
	new_div.appendChild(span3);
	new_div.appendChild(cenas);    
	span3.appendChild(document.createTextNode(" Cena s DPH: ")); 
	
	cislo++;
}

//==================================================================================
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  for(i=0; i < selLength; i++){
  	if(theSel.options[i].value == theValue){ var jeTam = "ano"; }
  }
  if(jeTam != "ano"){
  	theSel.options[selLength] = newOpt;
  	theSel.options[selLength].setAttribute("title", theText);
  }
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function addOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].title;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  
  if(NS4) history.go(0);
}
function deleteOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      deleteOption(theSelFrom, i);
    }
  }
 
  
  if(NS4) history.go(0);
}
function loop_select() {
	var select = document.getElementById("kategorieSelected");
	for(i=0; i <= select.length-1;i++) {
		select.options[i].selected = true;
	}
}
function markAllRows(container_id,value) {
	    var rows = document.getElementById(container_id).getElementsByTagName('input');
	    var unique_id;
	    var checkbox;
	    
	    if(value == 1){
	    	value = true;
	    }
	    else {
	    	value = false;
	    }

	    for ( var i = 0; i < rows.length; i++ ) {
	
	        checkbox = rows[i];
	
	        if ( checkbox && checkbox.type == 'checkbox' ) {
	                checkbox.checked = value;
	        }
	    }
	
	    return true;
	}
//==================================================================================

function dotaz(input_ID,span_ID,hodnota)
{
	var input 	= document.getElementById(input_ID);
	var div 	= document.getElementById(span_ID);
	var hodnota = hodnota;
	
	if (hodnota.length < 2)
	{
		div.innerHTML = "";
		div.style.visibility = "hidden";
		return;
	}
	
	var myRandom= parseInt(Math.random()*99999999);  // cache buster
	var xmlhttp;
	
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		alert("Your browser does not support XMLHTTP!");
	}
	
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
		{
			div.style.visibility = "visible";
			div.innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET", "java.dotazy.php?nazev=" + escape(input_ID) + "&hodnota=" + hodnota + "&rand=" + myRandom, true);
	xmlhttp.send(null);
}
