var cook_adresa= new Array(); //slouzi pro pamatovani adresy v kosiku

function adresa(nazev,hodnota)
{
	nazev=nazev.replace(/[\t]/g,""); 
	nazev=nazev.replace(/[#]{2}/g," "); 
	if (nazev!="zobrfirud")
	{
		hodnota=hodnota.replace(/[\t]/g,""); 
		hodnota=hodnota.replace(/[#]{2}/g," "); 
	}
	
	if (nazev)
	{
		cook_adresa[nazev]=hodnota;
		var vystup="";
		for (var klic in cook_adresa)
		{
			if (klic!="removeDuplicates" && klic!="empty")
			{
				//alert('Index='+ klic+ ' - value='+ cook_adresa[klic]);		
				vystup=vystup+klic+"##"+encodeURIComponent(cook_adresa[klic])+"\t";		
			}
		}
		setCookie("cook_adresa",vystup,7);
	}
}

function number_format (number, decimals, dec_point, thousands_sep) {

// Formats a number with grouped thousands
//
// version: 906.1806
// discuss at: http://phpjs.org/functions/number_format
// +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// +     bugfix by: Michael White (http://getsprink.com)
// +     bugfix by: Benjamin Lupton
// +     bugfix by: Allan Jensen (http://www.winternet.no)
// +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
// +     bugfix by: Howard Yeend
// +    revised by: Luke Smith (http://lucassmith.name)
// +     bugfix by: Diogo Resende
// +     bugfix by: Rival
// +     input by: Kheang Hok Chin (http://www.distantia.ca/)
// +     improved by: davook
// +     improved by: Brett Zamir (http://brett-zamir.me)
// +     input by: Jay Klehr
// +     improved by: Brett Zamir (http://brett-zamir.me)
// +     input by: Amir Habibi (http://www.residence-mixte.com/)
// +     bugfix by: Brett Zamir (http://brett-zamir.me)
// *     example 1: number_format(1234.56);
// *     returns 1: '1,235'
// *     example 2: number_format(1234.56, 2, ',', ' ');
// *     returns 2: '1 234,56'
// *     example 3: number_format(1234.5678, 2, '.', '');
// *     returns 3: '1234.57'
// *     example 4: number_format(67, 2, ',', '.');
// *     returns 4: '67,00'
// *     example 5: number_format(1000);
// *     returns 5: '1,000'
// *     example 6: number_format(67.311, 2);
// *     returns 6: '67.31'
// *     example 7: number_format(1000.55, 1);
// *     returns 7: '1,000.6'
// *     example 8: number_format(67000, 5, ',', '.');
// *     returns 8: '67.000,00000'
// *     example 9: number_format(0.9, 0);
// *     returns 9: '1'
// *     example 10: number_format('1.20', 2);
// *     returns 10: '1.20'
// *     example 11: number_format('1.20', 4);
// *     returns 11: '1.2000'
// *     example 12: number_format('1.2000', 3);
// *     returns 12: '1.200'
var n = number, prec = decimals;

var toFixedFix = function (n,prec) {
    var k = Math.pow(10,prec);
    return (Math.round(n*k)/k).toString();
};

n = !isFinite(+n) ? 0 : +n;
prec = !isFinite(+prec) ? 0 : Math.abs(prec);
var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

var abs = toFixedFix(Math.abs(n), prec);
var _, i;

if (abs >= 1000) {
    _ = abs.split(/\D/);
    i = _[0].length % 3 || 3;

    _[0] = s.slice(0,i + (n < 0)) +
          _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
    s = _.join(dec);
} else {
    s = s.replace('.', dec);
}

var decPos = s.indexOf(dec);
if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
    s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
}
else if (prec >= 1 && decPos === -1) {
    s += dec+new Array(prec).join(0)+'0';
}
return s;}


function zobrfirud(kde)
{
	if (kde=="text")
	{
		if (document.getElementById("zobrfirud").checked)
		{
			document.getElementById("zobrfirud").checked=false;
		}
		else
		{
			document.getElementById("zobrfirud").checked=true;
		}
	}
	if (document.getElementById("zobrfirud").checked)
	{
		document.getElementById("firemniudaje").style.display="block";
		adresa("zobrfirud",1);
	}
	else
	{
		document.getElementById("firemniudaje").style.display="none";
		adresa("zobrfirud",0);
	}
}

function spocitejzbozi()
{
	if (zbozivlastni)
	{		
		celkem=0;
		for (i=0; i<zbozivar.length; i++)
		{
			if (zbozivar[i])
			{
				celkem=celkem+zbozivar[i];
			}
		}
		document.form_celkem.spoc_celkem.value=celkem;
	}
	else	
	{
		document.form_celkem.spoc_celkem.value=zbozisklad+zbozivar[0];
	}
	document.getElementById('vcetnepriplatku').innerHTML = number_format (document.form_celkem.spoc_celkem.value, 0, ",", " ")+",- Kč";
	
}


function vybervar(id)
{
	document.getElementById(id).checked="checked";
}

function doprava(radio)
{
	document.getElementById('kdoruceni').value=radio;
	setCookie("kdoruceni",radio,7);
	//window.location.reload();
	window.location="../kosik/";
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getStyleClass (className) {
	for (var s = 0; s < document.styleSheets.length; s++)
	{
		if(document.styleSheets[s].rules)
		{
			for (var r = 0; r < document.styleSheets[s].rules.length; r++)
			{
				if (document.styleSheets[s].rules[r].selectorText == '.' + className)
				{
					return document.styleSheets[s].rules[r];
				}
			}
		}
		else if(document.styleSheets[s].cssRules)
		{
			for (var r = 0; r < document.styleSheets[s].cssRules.length; r++)
			{
				if (document.styleSheets[s].cssRules[r].selectorText == '.' + className)
					return document.styleSheets[s].cssRules[r];
			}
		}
	}
	
	return null;
}


// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject();
// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject()
{
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// if running Internet Explorer
	if(window.ActiveXObject)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	// if running Mozilla or other browsers
	else
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp = false;
		}
	}
	// return the created object or display an error message
	if (!xmlHttp)
	{
		alert("Nepodařilo se vytvořit XMLHttpRequest object.");
	}
	else
	{
		return xmlHttp;
	}	
}

function upravkos(koren,id,akce) //akce ma hodnotu uprav nebo smaz
{
	getStyleClass('vyber').style.visibility = 'hidden';
	getStyleClass('vstup').style.visibility = 'hidden';
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		// retrieve the name typed by the user on the form
		//name = encodeURIComponent(document.getElementById("myName").value);
		jsid = parseInt(id); //global
		id = parseInt(id); //local
		pocet=parseInt(document.getElementById("pocet"+id).value,10);
		
		if (document.getElementById("barva"+id)==null)
		{
			barva=0;			
		}
		else
		{
			barva=parseInt(document.getElementById("barva"+id).value,10);
		}
		
		if ((isNaN(pocet) || pocet<1) && akce=='uprav')
		{
			document.getElementById("kosik").innerHTML ="<p>Do košíku lze vložit pouze kladný počet zboží.<p><p><a href='javascript: zavri();' onclick='javascript: zavri();'>Zavřít</a></p>";
		}
		else if (document.getElementById("barva"+id) && barva==0 && akce=='uprav')
		{
			document.getElementById("kosik").innerHTML ="<p>Vyberte barvu produktu.<p><p><a href='javascript: zavri();' onclick='javascript: zavri();'>Zavřít</a></p>";
		}
		else
		{
			// execute the php page from the server
			if (akce=='uprav')
			{
				xmlHttp.open("GET", koren+"ajax/kosik.php?id=" + id + "&pocet=" + pocet + "&barva=" + barva + "&akce=uprav", true);
			}
			else //akce je smaz
			{
				xmlHttp.open("GET", koren+"ajax/kosik.php?id=" + id + "&akce=smaz", true);
			}
			
			// define the method to handle server responses
			xmlHttp.onreadystatechange = handleServerResponse;
			// make the server request
			xmlHttp.send(null);
		}
		
		// velikost okna
		var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	 // window.alert( 'Width = ' + myWidth );
	  // window.alert( 'Height = ' + myHeight );
	  
	  //moje
	  var obj = document.getElementById('kosik');
	  obj.style.visibility = 'hidden';
	  obj.style.display = 'block';
	  	if (document.documentElement && document.documentElement.scrollTop)
	  	{
	  		theTop = document.documentElement.scrollTop;
	  	}
		else if (document.body)
		{
			theTop = document.body.scrollTop;
		}
	  obj.style.top = (myHeight-obj.clientHeight-40)/2+theTop+"px";
 	 	obj.style.left = (document.documentElement.scrollWidth-400)/2+"px";
 	 	//obj.style.visibility = 'visible';
 	 	
 	 	obj = document.getElementById('overlay');
  		obj.style.height = document.documentElement.scrollHeight +"px";
		obj.style.display = 'block';
		
		document.getElementById('kosik').style.visibility = 'visible'; //zobrazi okno potvrzeni
	}
	else
	{
		// if the connection is busy, try again after one second
		setTimeout('process()', 1000);
	}
}

// make asynchronous HTTP request using the XMLHttpRequest object
function process(koren,id)
{
	getStyleClass('vyber').style.visibility = 'hidden';
	getStyleClass('vstup').style.visibility = 'hidden';
	// proceed only if the xmlHttp object isn't busy
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
		// retrieve the name typed by the user on the form
		//name = encodeURIComponent(document.getElementById("myName").value);
		jsid = parseInt(id); //global
		id = parseInt(id); //local
		pocet=parseInt(document.getElementById("pocet"+id).value,10);
		
		if (document.getElementById("barva"+id)==null)
		{
			barva=0;			
		}
		else
		{
			barva=parseInt(document.getElementById("barva"+id).value,10);
		}
		if (isNaN(pocet) || pocet<1)
		{
			document.getElementById("kosik").innerHTML ="<p>Do košíku lze vložit pouze kladný počet zboží.<p><p><a href='javascript: zavri();' onclick='javascript: zavri();'>Zavřít</a></p>";
		}
		else if (document.getElementById("barva"+id) && barva==0)
		{
			document.getElementById("kosik").innerHTML ="<p>Vyberte barvu produktu.<p><p><a href='javascript: zavri();' onclick='javascript: zavri();'>Zavřít</a></p>";
		}
		else
		{
			// execute the quickstart.php page from the server
			xmlHttp.open("GET", koren+"ajax/kosik.php?id=" + id + "&pocet=" + pocet + "&barva=" + barva, true);
			// define the method to handle server responses
			xmlHttp.onreadystatechange = handleServerResponse;
			// make the server request
			xmlHttp.send(null);
		}
		
		// velikost okna
		var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	 // window.alert( 'Width = ' + myWidth );
	  // window.alert( 'Height = ' + myHeight );
	  
	  //moje
	  var obj = document.getElementById('kosik');
	  obj.style.visibility = 'hidden';
	  obj.style.display = 'block';
	  	if (document.documentElement && document.documentElement.scrollTop)
	  	{
	  		theTop = document.documentElement.scrollTop;
	  	}
		else if (document.body)
		{
			theTop = document.body.scrollTop;
		}
	  obj.style.top = (myHeight-obj.clientHeight-40)/2+theTop+"px";
 	 	obj.style.left = (document.documentElement.scrollWidth-400)/2+"px";
 	 	//obj.style.visibility = 'visible';
 	 	
 	 	obj = document.getElementById('overlay');
  		obj.style.height = document.documentElement.scrollHeight +"px";
		obj.style.display = 'block';
		
		document.getElementById('kosik').style.visibility = 'visible'; //zobrazi okno potvrzeni

	}
	else
	{
		// if the connection is busy, try again after one second
		setTimeout('process()', 1000);
	}
}
// executed automatically when a message is received from the server
function handleServerResponse()
{
// move forward only if the transaction has completed
	if (xmlHttp.readyState == 4)
	{
	// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200)
		{
			// extract the XML retrieved from the server
			xmlResponse = xmlHttp.responseXML;
			// obtain the document element (the root element) of the XML structure
			xmlDocumentElement = xmlResponse.documentElement;
			// get the text message, which is in the first child of
			// the the document element
			helloMessage = xmlDocumentElement.firstChild.data;
			// update the client display using the data received from the server
			document.getElementById("kosik").innerHTML =helloMessage;
			// restart sequence
			//setTimeout('process()', 1000);
		}
		// a HTTP status different than 200 signals an error
		else
		{
			alert("Nepodařilo se připojit k serveru: " +
			xmlHttp.statusText);
		}
	}
}

function ukaz(foto,alt,sirka,vyska)
  {
  	getStyleClass('vyber').style.visibility = 'hidden';
	getStyleClass('vstup').style.visibility = 'hidden';
  	var obj = document.getElementById('foto');
  	obj.style.display = 'block';
  	obj.style.visibility = 'hidden';
  	
  	obj.innerHTML="<p>"+alt+"</p><img src='"+foto+"_.jpg' height='"+vyska+"' width='"+sirka+"' alt='"+alt+"' />";
  	
  	  if (document.documentElement && document.documentElement.scrollTop)
	theTop = document.documentElement.scrollTop;
	else if (document.body)
	theTop = document.body.scrollTop;
	
	// velikost okna
		var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	 // window.alert( 'Width = ' + myWidth );
	  // window.alert( 'Height = ' + myHeight );
	  
 //obj.style.top = theTop+10+(document.documentElement.clientHeight-vyska)/2+"px";
 //obj.style.top = theTop+15+"px";
 obj.style.top = (myHeight-obj.clientHeight)/2+theTop+"px";
 obj.style.left = (document.documentElement.scrollWidth-sirka)/2+"px";
 //obj.style.left = (880-sirka)/2+"px";
  	obj.style.visibility = 'visible';
  	
	document.getElementById('body').style.height = document.documentElement.scrollHeight +"px";
	
  	obj = document.getElementById('overlay');
  	//obj.style.height = document.body.clientHeight+"px";
  	//obj.style.height = document.body.scrollHeight +"px";
  	obj.style.height = document.documentElement.scrollHeight +"px";
	obj.style.display = 'block';
  	}
  	
function zprava(zprava)
{
	if (document.getElementById('form_celkem'))
		{
			document.getElementById('form_celkem').style.display = 'none';
		}
	
	obj = document.getElementById('overlay');
  	obj.style.height = document.documentElement.scrollHeight +"px";
	obj.style.display = 'block';
	getStyleClass('vyber').style.visibility = 'hidden';
	getStyleClass('vstup').style.visibility = 'hidden';
	var obj = document.getElementById('kosik');
  	obj.style.visibility = 'hidden';
	obj.style.display = 'block';
	//obj.innerHTML="<p>"+zprava+"</p><p><a href='javascript: zavri();' onclick='javascript: zavri();'>Zavřít</a></p>";
	obj.innerHTML=zprava;
	
	if (document.documentElement && document.documentElement.scrollTop)
	theTop = document.documentElement.scrollTop;
	else if (document.body)
	theTop = document.body.scrollTop;
	
	// velikost okna
		var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	  
	   obj.style.top = (myHeight-obj.clientHeight)/2+theTop+"px";
	   obj.style.width = "400px";
 obj.style.left = (document.documentElement.scrollWidth-400)/2+"px";
 //obj.style.left = (880-sirka)/2+"px";
  	obj.style.visibility = 'visible';
}
  
  function zavri()
  {
	if (document.getElementById('form_celkem'))
		{
			document.getElementById('form_celkem').style.display = 'block';
		}
  	getStyleClass('vyber').style.visibility = 'hidden';
	getStyleClass('vstup').style.visibility = 'hidden';
  	//obj = document.getElementById('foto');
	//obj.style.display = 'none';
	//obj.innerHTML="";
	document.getElementById('overlay').style.display = 'none';
	document.getElementById('kosik').style.display = 'none';
	document.getElementById('body').style.height = "auto";
	getStyleClass('vyber').style.visibility = 'visible';
	getStyleClass('vstup').style.visibility = 'visible';
  }

function spocti_kuchyn(vstup,kolik)
{
	if (kolik>0)
	{
		document.form_celkem.spoc_celkem.value=parseInt(document.form_celkem.spoc_celkem.value)+kolik;
		vstup.value++;
	}
	if (kolik<0)
	{
		if (vstup.value>0)
		{
			vstup.value--;
			document.form_celkem.spoc_celkem.value=parseInt(document.form_celkem.spoc_celkem.value)+kolik;
		}
		else
		{
			vstup.value=0;
		}
	} 
}

function pobocka($hodnota)
{
	if ($hodnota==1)
	  	{document.getElementById('div_kpobocka').style.display='block';}
	  	else {document.getElementById('div_kpobocka').style.display = 'none';}
}

