/**
 * Funkce z templates
 */

function showhide_form(select, form) {
	select = document.getElementById(select);
	form = document.getElementById(form);
	if (select.value == 0) {
	
		document.getElementById('address').style.display = 'none';
		form.style.display='block';
	} else{ 
		document.getElementById('address').style.display = 'block';
		form.style.display="none";
	}
}

function showhide_delivery(select) {
	select = document.getElementById(select);
	if (select.value == 0) {
	
		document.getElementById('delivery_text').style.display = 'none';

	} else{ 
		document.getElementById('delivery_text').style.display = 'block';
     
	}
}


// mění parametry v url
// @from product_list.tpl
function changeParam(paramname, mode) {
	var location = new String(window.location)
	if (mode != -1) {
		if (location.match(paramname)) {
			var replaceexp = new RegExp ('(' + paramname + '=)[0-9]*', 'g')
			location = location.replace(replaceexp, '$1' + mode)
		} else {
			if (location.match(/\?/)) {
				location += '&'
			} else {
				location += '?'
			}
			location = location + paramname + '=' + mode;
		}

		if (paramname == 'producer' || paramname == 'sort') {
			location = firstPage(location)
		};

		window.location = location;
	}
}

// @from product_list.tpl
function firstPage(location) {
	return location.replace(/pageno=[0-9]+&?/, '')
}

// parametr funkce je id šipky (vzestupne[0] nebo sestupne[1])
// @from product_list.tpl
function sortMode(button) {
	var form = document.product_list_bar
	changeParam('sortmode', parseInt(form.sortmode.value) + parseInt(button))
}

// @from multimenu.tpl
function startMenu() {
	if (document.all && document.getElementById) {
		var multimenu = document.getElementById('multimenu');
		for (i = 0; i < multimenu.childNodes.length; i++) {
			var ul = multimenu.childNodes[i]
			if (ul.nodeName == 'UL') {
				for (j = 0; j < ul.childNodes.length; j++) {
					var node = ul.childNodes[j]
					if (node.nodeName == 'LI') {
						node.onmouseover = function() {this.className += ' over'}
						node.onmouseout = function() {this.className = this.className.replace(' over', '')}
					}
				}
			}
		}
	}
}

if (window.attachEvent) {
	// window.attachEvent('onload', startMenu)
} else {
	// window.onload = startMenu;
}

// @from categories.tpl
function depShowHide(id) {
	var dep = document.getElementById('depId' + id)
	var img = document.getElementById('depImgId' + id)
	var imgsrc = new String(img.src)

	if(dep.style.display == '') {
		dep.style.display = 'none'
		img.src = imgsrc.replace(/_open\.gif$/, '_close.gif')
	} else {
		dep.style.display = ''
		img.src = imgsrc.replace(/_close\.gif$/, '_open.gif')
	}
}

function depGet(url) {
	document.location = url
}

// @from download_categories.tpl
function depDownloadShowHide(id) {

	var dep = document.getElementById('depIdDown' + id)
	var img = document.getElementById('depImgIdDown' + id)
	var imgsrc = new String(img.src)

	if(dep.style.display == '') {
		dep.style.display = 'none'
		img.src = imgsrc.replace(/_open\.gif$/, '_close.gif')
	} else {
		dep.style.display = ''
		img.src = imgsrc.replace(/_close\.gif$/, '_open.gif')
	}
}

// @from basket.tpl
//function basket_clear_url() {
//	var location = new String(window.location)
//	var newlocation = location.replace(/[\?&]addproduct=[0-9]*/, '')
//	newlocation = newlocation.replace(/[\?&]count=[0-9]*/, '')
//	newlocation = newlocation.replace(/[\?&]jettison=[0-9]*/, '')
//	newlocation = newlocation.replace(/[0-9]+-.*/, 'kosik/')
//	newlocation = newlocation.replace(/([\?&]page=)[a-zA-Z_]*/, '$basket')

//	if (newlocation != location) 
//	{
//		window.location = newlocation;
//	}
//}

// @from order.tpl
var selected_tran = -1;
var service_value = new Array();

function radio_value(el) {
	var len = el.length
	for (i = 0; i < len; i++) {
		var radio = el[i]
		if (radio.checked) {
			return radio.value
		}
	}
	return false
}

//function payms_checking(num_id){
//	var els = document.getElementsByName('payment');
//	var i = 0;
//	for( i = 0; i < els.length; i++){
//		els[i].setAttribute('checked', '');
//		if( els['id'] == 'payment_'+num_id ){
//			els[i]['checked'] = 'checked';
//		}
//	}
//	alert('aaa');
//}

var last_paym_value = 0;
var last_trans_value = 0;

function recalc_total(curr, before, round) {
	log('<hr>ZACATEK recalc<hr>');
	var trans_value = radio_value(document.getElementsByName('transport'));
	var paym_value = radio_value(document.getElementsByName('payment'));
	var trans_change = 0;
	var paym_change = 0;
  var discount = 0;
  var mnoz = 0;
  var kum = 0;
  
	log('PAYM VALUE = '+paym_value + ' // LAST = ' + last_paym_value);
	
  mnoz = hodnota_slevy_mnozstevni;
  kum = hodnota_slevy_vernostni;

  discount = mnoz + kum;
  
	var price = 0;
	var doprava = 0;
	if (trans_value) {
		//price += trans_prices[trans_value];
		doprava = trans_prices[trans_value]
		if (service_value[trans_value]) {
			//price += service_value[trans_value]
		}
	}
	
	if(def_paym_for_trans[trans_value] && trans_value && !paym_value){
		var tmp = def_paym_for_trans[trans_value];
		var el_tmp = document.getElementById('payment_'+tmp);
		if(tmp && el_tmp.disabled == ''){
			el_tmp.checked = true;
			paym_value = tmp;
		}
	}
	
	var platba = 0;//paym_prices[last_paym_value];
	if (paym_value != false) {
		//price += paym_prices[paym_value]
		platba = paym_prices[paym_value];
	} else if (last_paym_value != false) {
		platba = paym_prices[last_paym_value];
	}
	
	if(trans_value != last_trans_value){
		trans_change = 1;
	}
	if(paym_value != last_paym_value){
		paym_change = 1
	}
	
	log('<b>recalc data</b> = ' +doprava+' / '+ platba );

	prepocti(-1 * kum, -1 * mnoz, hodnota_slevy_kupon, doprava, platba, cena_zbozi, 'recalc_total', trans_change, paym_change, 0);
	
	last_paym_value = paym_value;
	last_trans_value = trans_value;
	log('<hr>KONEC recalc<hr>');	
}

function radio_label_state(prefix, id, enabled) {
	if (enabled) {
		document.getElementById(prefix + '_' + id).disabled = ''
		removeClassName(document.getElementById(prefix + '_' + id + '_label'), 'disabled')
	} else {
		document.getElementById(prefix + '_' + id).disabled = 'disabled'
		addClassName(document.getElementById(prefix + '_' + id + '_label'), 'disabled')
		document.getElementById(prefix + '_' + id).checked = false
	}
}

function transport_payment_change(prefix, ids, curr, before, round) {
	//var form_el = document.order.elements
	//var re = new RegExp('^' + prefix + '_(\\d+)')
	if (prefix == 'payment') {
		for (i=0; i < paym_ids.length; i++) {
 			radio_label_state(prefix, paym_ids[i], false);
		}
	}
	
	for (i=0; i < ids.length; i++) {
		radio_label_state(prefix, ids[i], true)
	}

	recalc_total(curr, before, round)
}

function transport_change(id,curr, before, round) {
   
	if (loyalty != 1) {
		transport_payment_change('payment', tp_enable[id],curr, before, round)
	}

	if(selected_tran != -1) {
		elem = document.getElementById('services_'+selected_tran);
		if(elem){
			addClassName(elem, 'hidden');
		}
	}

	elem = document.getElementById('services_'+id);
	if(elem){
		removeClassName(elem, 'hidden');
	}

	selected_tran = id;
}

function payment_change(id,curr, before,round) {
	transport_payment_change('transport', pt_enable[id],curr, before, round)
}

function service_change(elem, id_tran, id_serv, curr, before, round) {
	if (!service_value[id_tran]) service_value[id_tran] = 0;

	if (elem.checked) {
		service_value[id_tran] += service_price[id_serv];
	} else {
		service_value[id_tran] -= service_price[id_serv];
	}

	recalc_total(curr, before, round);
}


// tahle funkce umi udelat z 26.32 cislo 27.68, neni na tom neco spatne???
Number.prototype.toCurrency2 = function() {
	// convert number into a currency-formatted string result
	var symb = (arguments.length>0) ? arguments[0] : "  .";

	symb = ((typeof(symb)=="string" && symb.length==3) ? symb : "$,.").split("");
	var nbr = this;

	var n = Math.round( (nbr - Math.floor(nbr))*100 ) ;
	dec = Math.floor((nbr*100+0.5)%100);
	
	nbr = Math.floor((nbr*100+0.5)/100).toString();
	if (dec < 10) dec = "0" + dec;
	for (var i = 0; i < Math.floor((nbr.length-(1+i))/3); i++) {
		nbr = nbr.substring(0,nbr.length-(4*i+3))+symb[1]+nbr.substring(nbr.length-(4*i+3));
	}
	if(n > 0) {
		return nbr.toString()+","+ (n < 10 ? 0 + n.toString() : n.toString() ) ;  
	} else {
		return nbr.toString();  
	}
	
}

function format_of_number (stat, num) {   
  stat = stat.toLowerCase();   
	nStr = num + '';   
	var x = nStr.split('.');   
	var x1 = x[0];   
	var x2;   
	if(stat=='cz')   
	x2 = x.length > 1 ? ',' + x[1] : ''; // nahradime tecku carkou   
	else if(stat=='us')   
	x2 = x.length > 1 ? '.' + x[1] : ''; // us format   

	var rgx = /(\d+)(\d{3})/;   
	while (rgx.test(x1)) {   
			if(stat=='cz'){  
				x1 = x1.replace(rgx, '$1' + ' ' + '$2'); // nahradime mezerou cesky format 1 000,00   
			} else if(stat=='us') {
				x1 = x1.replace(rgx, '$1' + ',' + '$2'); // us format 1,000.00   
			}
	}   
	return x1 + x2;   
}   

/*
 *array
  1 => string 'Na celá čísla s 2 desetinnýma místama' (length=37)
  2 => string 'Na celá čísla bez desetinných míst' (length=34)
  3 => string 'Na padesátníky' (length=14)
  4 => string 'Na dvacetníky' (length=13)
  5 => string 'Na haléře' (length=9)
 */

function custom_round(price, round) {
	
	if( round == 1 ) {
	  price =  Math.round(price * 100) / 100 ;
	} else if ( round == 2 ) {
	  price =  Math.round(price) ;
	} else if ( round == 3 ) {
	  price =   Math.round(price * 100) / 100 ;  
	  var dec = price - parseFloat(price);
	  var add;
	  if(dec < 0.25) {
	  	add = 0;
		}	else if ( dec >= 0.25 && dec < 0.75 ) {
			add	= 0.5;
		}	else {
		 	add = 1;
		}            
		price = parseFloat(price) + add;	
				
	} else if ( round == 4 ) {
	  price =  Math.round(price*10)/10 ; 
	} else {
	  price =  Math.round(price*100)/100 ; 
	}	
	
	//return price;
	return format_of_number('cz', price);
  //return price.toCurrency2();
}

// mění parametry v url
    
var pocet = 0;
var cena = 0;
var pole_pocet = new Array();
var pole_cen = new Array();
var pole_id = new Array();
var b2c = 1;
    
    
function changeParam(paramname, mode) {
	var location = new String(window.location)
		
	if (mode != -1) {
		if (location.match(paramname)) {
			var replaceexp = new RegExp ('(' + paramname + '=)[,%A-Z0-9]*', 'g')
			location = location.replace(replaceexp, '$1' + mode)
		} else {
			if (location.match(/\?/)) {
				location += '&'
			} else {
				location += '?'
			}
			location = location + paramname + '=' + mode;
		}

	}


	if (paramname == 'producer' || paramname == 'sort') {
		location = firstPage(location)
	};

	window.location = location;
}


function firstPage(location) {
	return location.replace(/pageno=[0-9]+&?/, '')
}

// parametr funkce je id šipky (vzestupne[0] nebo sestupne[1])
function sortMode(button) {
	var form = document.product_list_bar
	changeParam('sortmode', parseInt(form.sortmode.value) + parseInt(button))
}

function show_description(id_element, th){
	jQuery('#'+id_element).show();
	addClassName(th, 'over');
}

function hide_description(id_element, th){
	jQuery('#'+id_element).hide();
	removeClassName(th, 'over');
}

function load_sizes(produkt_id){
	jQuery('#size-table').load(SHOP_URL+'ajax.php',{'produkt_id': produkt_id,'page': 'ajax_product_list_sizes'},function(){
		size_table_size();
		jQuery('#size-table').show();
	});
}

function show_pending(produkt_id,type){
	jQuery('#popup').load(SHOP_URL+'ajax.php',{'produkt_id': produkt_id,
		'type': type,
		'page': 'ajax_product_list_pending'},function(){
		jQuery('#popup').hide();
		heightwindow();
	});	
}

function popup_clear() {
	jQuery('#popup').hide();
	jQuery('#popup').html('');
}

String.prototype.toNumber = function() {
	// convert number-formatted string into a numeric result
	var nbr = Number(this);
	return ((isNaN(nbr)) ? 0 : nbr);
}
String.prototype.fromCurrency = function() {
	// convert currency-formatted string into a numeric result
	var symb = (arguments.length>0) ? arguments[0] : "  .";
	symb = ((typeof(symb)=="string" && symb.length>1) ? symb : "$,.").split("");
	var re = new RegExp("[\\"+symb[0]+"\\"+symb[1]+"]","g");
	return this.replace(re,"").toNumber();
}
Number.prototype.toCurrency = function() {
	// convert number into a currency-formatted string result
	var symb = (arguments.length>0) ? arguments[0] : "  .";
	symb = ((typeof(symb)=="string" && symb.length==3) ? symb : "$,.").split("");
	var nbr = this.toString().toNumber().toString();
	dec = Math.floor((nbr*100+0.5)%100);
	nbr = Math.floor((nbr*100+0.5)/100).toString();
	if (dec < 10) dec = "0" + dec;
	for (var i = 0; i < Math.floor((nbr.length-(1+i))/3); i++) {
		nbr = nbr.substring(0,nbr.length-(4*i+3))+symb[1]+nbr.substring(nbr.length-(4*i+3));
	}
	return (nbr);
}

function is_array(input){
	return typeof(input)=='object'&&(input instanceof Array);
}


var products = new Array();

function sectiPocet(id, product_id, div_id){
      
	var pocet = 0;
	var cena = 0;
	pole_id[id] = id;
	if(!is_array(products[product_id] )) {
		products[product_id] = new Array();
	}

	//alert('cyklus');
	if(pole_pocet[id]!=""){
		if(document.getElementById(id).value != "" && parseInt(document.getElementById(id).value) > 0){
			pole_cen[id] = parseInt(document.getElementById("pricex-"+product_id).innerHTML); 
			pole_pocet[id] = parseInt(document.getElementById(id).value);
			products[product_id][id] = 1;
		} else {
			pole_cen[id] = 0; 
			pole_pocet[id] = 0;
			document.getElementById(id).value = '';
			products[product_id][id] = 0;                   
		}
	}else{
		if(document.getElementById(id).value != "" && parseInt(document.getElementById(id).value) > 0){
			pole_pocet[id] += parseInt(document.getElementById(id).value);
			pole_cen[id] += parseInt(document.getElementById("pricex-"+product_id).innerHTML);
			products[product_id][id] = 1;
		} else{
			pole_cen[id] = 0; 
			pole_pocet[id] = 0;
			document.getElementById(id).value = '';
			products[product_id][id] = 0;
		} 
	}
	var show = false;
	if(is_array(products[product_id])) {
		products[product_id].each( function(it) {
			if(it) {show = true;}
		}  
	);  
	}
	if(show) {
		$(div_id).addClassName('active');
	} else {
		$(div_id).removeClassName('active');
	}  
    
	pole_id.each(function(item){
        
		if(pole_pocet[item] == document.getElementById(id).value){
			if(document.getElementById(id).value != ""){
				pole_pocet[item] = parseInt(document.getElementById(id).value);
				pole_cen[item] = parseInt(document.getElementById("pricex-"+product_id).innerHTML);
			}else{
				pole_cen[id] = 0; 
				pole_pocet[id] = 0;
				document.getElementById(id).value = '';  
			}
		} 
		pocet += pole_pocet[item];
		cena += (pole_cen[item] * pole_pocet[item]);
	});  
                      
	if(pocet==""){
		pocet = 0;// document.getElementById(id).value;
		cena = 0;//document.getElementById("pricex-"+product_id).innerHTML;
	} else {
		fireAlert();
	}
        
   
	document.getElementById("cel-cena").innerHTML = cena.toCurrency();
	document.getElementById("cel-pocet").innerHTML = pocet;
        
	document.getElementById("cel-cena2").innerHTML = cena.toCurrency();
	document.getElementById("cel-pocet2").innerHTML = pocet;
}


function depShowHide(id) {
	var dep = document.getElementById('depId' + id)
	var img = document.getElementById('depImgId' + id)
	var imgsrc = new String(img.src)

	if(dep.style.display == '') {
		dep.style.display = 'none'
		img.src = imgsrc.replace(/_open\.gif$/, '_close.gif')
	} else {
		dep.style.display = ''
		img.src = imgsrc.replace(/_close\.gif$/, '_open.gif')
	}
}

function depGet(url) {
	document.location = url
}

function activate_tab(tab, count, prefix)
{
  if (!prefix)
  {
		prefix = '';
  }
  
	var e_tab;
	var e_box;
  
	for (i = 0; i < count; i++)
	{
		e_tab = document.getElementById(prefix + 'tab_' + i);
		e_box = document.getElementById(prefix + 'box_' + i);
    
		if (i == tab)
		{ // aktivni
			e_tab.style.background.color = 'red';
			e_tab.style.color = '#3f3f3f';
      
			e_box.style.display = 'block';
		}
		else
		{ // neaktivni
			e_tab.style.background.color = 'blue';
			e_tab.style.color = 'red';
      
			e_box.style.display = 'none';
		}
	}
}

function intriguer(search,val){
	if(search != val) {
		if(search.length >= 3) {
			document.getElementById('naseptavac-top').style.display = 'block';
			new Ajax.Updater('naseptavac-top', SHOP_URL+'ajax.php', {
				parameters: { 
					search: search,
					page: 'ajax_intriguer',
					count: '7'
				}
			});
		} else {
			setTimeout("document.getElementById('naseptavac-top').style.display = 'none'",150);
		}
	}
}

function related_products_show_hide(paramname1,paramname2, paramname3) {
	var rh = document.getElementById('related-hide')
	
	var text_show = document.getElementById('text_show_all_products')
	
	var text_hide = document.getElementById('text_hide_all_products')
	
	//var rhi = document.getElementById('related-hide-img')
	
	//var img_src = new String(rhi.src)
	
	if(rh && rh.style){
		if (rh.style.display != 'none') {
			rh.style.display = 'none'
			text_show.style.display = 'block'
			text_hide.style.display = 'none'
			//rhi.src = img_src.replace(/-[0-9](\..+)$/, '-1$1')
		} else {
			rh.style.display = ''
			text_show.style.display = 'none'
			text_hide.style.display = 'block'
			//rhi.src = img_src.replace(/-[0-9](\..+)$/, '-2$1')
		}
	}
}

function related_products_show_hide_alternative() {
	var rh = document.getElementById('related-hide-alternative')
	
  var text_show = document.getElementById('text_show_all_products_alternative')
  
	var text_hide = document.getElementById('text_hide_all_products_alternative')
	
	//var rhi = document.getElementById('related-hide-img')

	//var img_src = new String(rhi.src)
	if(rh){
		if (rh.style.display != 'none') {
			rh.style.display = 'none'
			text_show.style.display = 'block'
			text_hide.style.display = 'none'
			//rhi.src = img_src.replace(/-[0-9](\..+)$/, '-1$1')
		} else {
			rh.style.display = ''
			text_show.style.display = 'none'
			text_hide.style.display = 'block'
			//rhi.src = img_src.replace(/-[0-9](\..+)$/, '-2$1')
		}
	}
}

function show_why(element_id){
	jQuery(document).ready(function() {
		var block = document.getElementById(element_id).style.display;
		
		var max_height = document.getElementById(element_id).offsetHeight;
		
		if (block == '' || block == 'none') { 
			// hlavicka (odkaz)
			$(element_id + '-a').addClassName('active');
			// div s textem
			
			jQuery('#'+element_id).show("blind", {direction: "vertical", easing : 'easeOutCirc'}, 1000);
			$(element_id).style.display = 'block';
		} else { 
			// hlavicka (odkaz)
			$(element_id + '-a').removeClassName('active');
			// div s textem
			
			jQuery('#'+element_id).hide("blind", {direction: "vertical", easing : 'easeOutCirc'}, 1000);
			setTimeout('my_hide', 1000);
			
		}
	});
	
}

function my_hide(){$(element_id).style.display = 'none';}

	
function slideSwitch() {
	jQuery(document).ready(function($){
		
		if(i_make_click){
			i_make_click = false;
			return false;
		}
		// $("div#stripTransmitter0 a").addClass("current").parent().parent().find("a").not($("div#stripTransmitter0 a")).removeClass("current"); // wow!
		var ui 	= 	jQuery(".a_link_next");
		var container = jQuery("#slideshow");


		if(current_slide + 1 < public_size_img){
			ui.eq(current_slide).removeClass("current");
			ui.eq(current_slide+1).addClass("current");
			current_slide++;
		}else{
			ui.eq(current_slide).removeClass("current");
			ui.eq(0).addClass("current"); 
			current_slide = 0;
		}
	
	
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    var $next =  $active.next().length ? $active.next()
		: $('#slideshow DIV:first');	        
        
    $active.addClass('last-active');

    $next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 750, function() {
			$active.removeClass('active last-active');
		});
	});
    
	//load_text();
}
function close_win(id){
	jQuery('#'+id).hide();
}

function addDatatoForm(id,title){
	var forid = document.getElementById('forid');
	var titulek = document.getElementById('titulek');
  forid.setAttribute('value', id);
  titulek.setAttribute('value', title);
}

/**
 * -----------------------------------------------------------------------
 * Funkce pro pocitani slev na strance s objednavkou
 * <START>
 */

// nastaveni hodnot z JS promennych do HTML
function nastav_hodnoty_do_HTML(){
	if(el_slevy_vernostni){
		el_slevy_vernostni.innerHTML = format_currency2( custom_round(hodnota_slevy_vernostni, mena_round), coup_mena, mena_before);
	}
	if(el_slevy_mnozstevni){
		//log('mnozstevni ZAPIS = ' + hodnota_slevy_mnozstevni);
		el_slevy_mnozstevni.innerHTML = format_currency2(custom_round(hodnota_slevy_mnozstevni, mena_round), coup_mena, mena_before);
	}
	if(el_slevy_kupon){
		el_slevy_kupon.innerHTML = format_currency2( custom_round(Math.abs(hodnota_slevy_kupon), mena_round), coup_mena, mena_before);
	}
	if(el_cena_doprava_platba){
		el_cena_doprava_platba.innerHTML = format_currency2( custom_round(cena_doprava + cena_platba, mena_round), coup_mena, mena_before);
	}
	// cenu zbozi nebudu aktualizovat
	if(el_cena_celkem){
		el_cena_celkem.innerHTML = format_currency2( custom_round(celk_cena_objednavky, mena_round), coup_mena, mena_before);
	}
}

/**
 * fce je volana jen pri nacteni cele stranky s objednavku, jen poprve.
 * Snazi se nastavit rozumne hodnoty po deklaraci promennych.
 * Zinicializuje promenne pro JS z PHP, Ajaxu a z HTML
 */
function vars_init(first_call){
	if(!first_call){
		first_call = false;
	}
	
	if(first_call){
		// inicializace dopravy a platby podle zaskrtnutych RADIO
		var tmp1 = radio_value(document.getElementsByName('transport'));
		if(tmp1){
			cena_doprava = trans_prices[tmp1];
		}
		var tmp2 = radio_value(document.getElementsByName('payment'));
		if(tmp2){
			cena_platba = paym_prices[tmp2];
		}
		
		if(typeof(sleva_1) != 'undefined'){
			hodnota_slevy_mnozstevni = sleva_1;
		}
		if(typeof(sleva_2) != 'undefined'){
			hodnota_slevy_vernostni = sleva_2;
		}

		// nastaveni elementu, ktere budu pouzivat
		el_slevy_vernostni = $('hodnota_slevy_vernostni');
		el_slevy_mnozstevni = $('hodnota_slevy_mnozstevni');
		el_cena_doprava_platba = $('price-transport');
		el_cena_zbozi = $('celkova_cena2');
		el_cena_celkem = $('celcena');

		// pokud o spocitani celkove ceny objednavky
		celk_cena_objednavky = cena_zbozi + hodnota_slevy_mnozstevni + hodnota_slevy_vernostni + hodnota_slevy_kupon + cena_doprava + cena_platba;

		// nacteni souctu Vernostni a Mnozstevni slevy
		// POUZIVA SE TO NEKDE????? mam pocit, ze to uz nacitam z PHP
		if(document.getElementById('discount-price') != null){
			discount = parseFloat(document.getElementById('discount-price').innerHTML);
		}

		if(document.getElementById('discount-price-kom') != null){
			discount += parseFloat(document.getElementById('discount-price-kom').innerHTML);
		}
		
	}
	
	// incialiazica pri zavolani po zmenach - nacteni kuponu, smazani kuponu, ... (RE-INIT)
	
}

// volane z order.tpl
// funkce pro prepocitani vsech vyse uvedenych cen
function prepocti(vernostni, mnozstevni, kupon, doprava, platba, zbozi, kdo_vola, trans_changed, paym_changed, pricti_dp){
	log('KDO VOLA : "'+kdo_vola+'" -- ' + pricti_dp);
	
	if(!pricti_dp){
		pricti_dp = 0;
	}
	
	if(!trans_changed){
		trans_changed = 0;
	}
	if(!paym_changed){
		paym_changed = 0;
	}
	
	//log('---------------------<br>prepocti ('+kdo_vola+')');
	if(vernostni || vernostni == 0){
		hodnota_slevy_vernostni = -1 * Math.abs(vernostni);
		//log('vernostni: '+vernostni)
	}
	
	if(mnozstevni || mnozstevni == 0){
		hodnota_slevy_mnozstevni = -1 * Math.abs(mnozstevni);
		log('mnozstevni ('+kdo_vola+'): '+mnozstevni)
	}

	if(kupon || kupon == 0){
		hodnota_slevy_kupon = kupon;
	}
	
	if(doprava || doprava == 0){
		cena_doprava = doprava;
	}
	if(platba || platba == 0){
		cena_platba = platba;
	}
	
	if(zbozi || zbozi == 0){
		cena_zbozi = zbozi;
	}

	
//log('P <b>minula</b> = '+cena_doprava_minula+' a '+cena_platba_minula);
//
//log('<font color="red">P dop ('+cena_doprava+' - '+cena_doprava_minula+') = ' + (cena_doprava - cena_doprava_minula)+'</font>');
//log('<font color="red">P pla ('+cena_platba+' - '+cena_platba_minula+') = ' + (cena_platba - cena_platba_minula)+'</font>');

	// pokud je kupon vice nez cena zbozi - vern. a mn. sleva
	if( -1*hodnota_slevy_kupon > (cena_zbozi + hodnota_slevy_mnozstevni + hodnota_slevy_vernostni) ){
		log(' VICE ');
	}

log('PRICTI = ' + pricti_dp);
log('KUPON A = '+hodnota_slevy_kupon);
	// jak se pohnula doprava a platba od minule (pri zmene dopravy a platby)
//	if(pricti_dp && coup_slevuje_dopravu
//		// je mozno odecist dopravu a platbu zaroven a celou, jinak nic
//		&& coup_value > (cena_zbozi + cena_doprava + cena_platba)
//		&& coup_type == 'c'
//		){
//		log('<b><u>IF JEDNA</u></b>');
//		hodnota_slevy_kupon = hodnota_slevy_kupon - cena_doprava - cena_platba;
//	// pokud je pouzit kupon a zmeni se cena dopravy a platby, pak musim navysit/snizit hodnotu kuponu
//	} 
log('KUPON B = '+hodnota_slevy_kupon);
log('<b>DP = </b>' + pricti_dp);

	// je mozno slevit pomoci kuponu z dopravy a platby?
	if (coup_slevuje_dopravu){
		if(coup_type == 'c' && 
				coup_value > (cena_zbozi + cena_doprava + cena_platba)
				&& hodnota_slevy_kupon < 0
				//&& pricti_dp
		){
			// je mozno odecist dopravu a platbu zaroven a celou, jinak nic
			log('<b>IF DVA</b>');
			hodnota_slevy_kupon = hodnota_slevy_kupon - ((cena_doprava - cena_doprava_minula) + (cena_platba - cena_platba_minula));
			
		} else if (coup_type == 'p' && (trans_changed || paym_changed) ){

			var procento_ke_sleve = coup_value / 100 * (cena_doprava + cena_platba);
			hodnota_slevy_kupon = hodnota_slevy_kupon + (minula_proc_sleva_doprava_platba - procento_ke_sleve);

			if(trans_changed || paym_changed){
				minula_proc_sleva_doprava_platba = procento_ke_sleve;
			}
		}
	}
log('KUPON C = '+hodnota_slevy_kupon);
	
	// kontrola, zda neni vetsi sleva, nez je hodnota kuponu
	if( -1 * parseFloat(hodnota_slevy_kupon) > parseFloat(coup_value) ){
		// tak je potreba zjistit, kolik je mnozstevni a kum. sleva a zjistit, jak to nejlepe pouzit.

		//alert("blablabla error")
	}

	celk_cena_objednavky = cena_zbozi + hodnota_slevy_mnozstevni + hodnota_slevy_vernostni + hodnota_slevy_kupon + cena_doprava + cena_platba;
	
	if(trans_changed){
		cena_doprava_minula = cena_doprava;
	}
	if(paym_changed){
		cena_platba_minula = cena_platba;
	}

	// nastavim do HTML
	nastav_hodnoty_do_HTML();
}

function erase_used_coup(){
	if( $('qty-discount-kom') ) $('qty-discount-kom').style.display = '';
	if( $('mn_sleva') ) $('mn_sleva').style.display = '';
		
	$('slevkup').value = '';
	$('radek_pod_slevama_div').innerHTML = '';
	$('slevkup_fieldset').style.display = '';
	$('radek_pod_slevama_div').innerHTML = '';
	$('slevkup_fieldset').style.display = '';
	//$('slevkup_info_span').style.display = 'none';
	$('discount').style.display = '';
	base_price = puvodni_cena;
	
	prepocti(hodnota_slevy_vernostni, hodnota_slevy_mnozstevni, 0, cena_doprava, cena_platba, cena_zbozi, 'smazani kuponu');
	//s_kuponem = 0;
	new Ajax.Request(SHOP_URL+'ajax.php',{parameters:{page:'ajax_del_coup'}});
	
}




/**
 * Fce pro pocitani slev na objednavce
 * <KONEC>
 */
