/**
 * Funkce ověřuje textová formulářová pole zda nejsou prázdná
 */ 
	function check_count(form){
	var values= new Array();
		for (i = 0; i < form.length; i++){
			if(form.elements[i].type=="text" && form.elements[i].value!=""){
		  	 return true;
		  }
		}
		alert('Zadejte prosím počet kusů');
		return false;
	}

/**
* Funkce z templates
*/

function showhide_form(select, form) {
    select = document.getElementById(select);
    form = document.getElementById(form);
    if (select.value == 0) {
	form.style.display='block';
    } else form.style.display="none";
}


// 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 recalc_total(dodavatel,curr,id) {
	var trans_value = trans_prices[dodavatel][trans_selected[dodavatel]];
	var paym_value = 	trans_prices[dodavatel][id];


		document.getElementById('price-transport').innerHTML = format_currency2(price,curr, before)
		document.getElementById('price-total').innerHTML = format_currency2(trans_value+ paym_value + base_price,curr, before)
}

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) {
	//var form_el = document.order.elements
	//var re = new RegExp('^' + prefix + '_(\\d+)')
	if (prefix == 'payment') {
		for (i in paym_ids) {
 		   radio_label_state(prefix, paym_ids[i], false);
		}
	}
	
	for (i in ids) {
		radio_label_state(prefix, ids[i], true)
  	}

	recalc_total(curr, before)
}
function new_payment_change(dodavatel,id,curr){

	var decimal_places=2;
	var total_trans = 0;
	var price_transport = document.getElementById('price-transport');
	var total_order = document.getElementById('price-total');
	var total_dodavatel = document.getElementById('total_'+dodavatel);
	var total_trans_dodavatel = document.getElementById('trans_pay_'+dodavatel);
	var trans_price =     trans_prices[dodavatel][trans_selected[dodavatel]];
	var paym_price = 			paym_prices[dodavatel][id];
	dodavatel_trans_total[dodavatel] = (parseInt(trans_price)+parseInt(paym_price));
	for(var i in dodavatel_trans_total){
		total_trans += dodavatel_trans_total[i];
	} 
	total_trans_dodavatel.innerHTML = (parseInt(trans_price)+parseInt(paym_price)).toFixed(decimal_places).replace(".",",")+' '+curr;
	total_dodavatel.innerHTML = (dodavatel_total[dodavatel]+trans_price+paym_price)+' '+curr;
  
	
	price_transport.innerHTML = total_trans +' '+ curr;
	total_order.innerHTML = (base_price+total_trans) +' '+curr; 
	
	 
	
	
	
}
  var sleva_nobillis = 0;
	var sleva_dobirka = 0;
function new_transport_change(id,curr, round, dodavatel,paym){
	var slevacelkem = 0;

	trans_selected[dodavatel] = id;
	var decimal_places=0;
	var total_trans = 0;

	if(round==2){
		decimal_places = 0;
	}else{
		decimal_places = 2;	
	}
	
	document.getElementById('trans_pay_'+dodavatel).innerHTML=trans_prices[dodavatel][id]+paym_prices[dodavatel][paym]+' '+curr;
	var trans_price =     trans_prices[dodavatel][trans_selected[dodavatel]]+paym_prices[dodavatel][paym];
 	var total_dodavatel = document.getElementById('total_'+dodavatel);
 	var price_transport = document.getElementById('price-transport');
 	var price_discount = document.getElementById('price-discount');
 	var total_order = document.getElementById('price-total');
 	
 	
	dodavatel_trans_total[dodavatel] = (parseInt(trans_price));
	for(var i in dodavatel_trans_total){
		total_trans += dodavatel_trans_total[i];
	}  
	 
	if(dodavatel==1){
		sleva_nobillis = (((dodavatel_total[dodavatel])/100)*4);
		document.getElementById('sleva_nobillis_'+dodavatel).style.display='block';
// 		if(paym==1){
// 			sleva_dobirka = (((dodavatel_total[dodavatel])/100)*2);
// 			document.getElementById('sleva_dobirka_'+dodavatel).style.display='block';
// 		}else{
// 			document.getElementById('sleva_dobirka_'+dodavatel).style.display='none';	
// 			sleva_dobirka = 0;	
// 		}
 		total_dodavatel.innerHTML = (dodavatel_total[dodavatel]+trans_price-sleva_nobillis-sleva[dodavatel]).toFixed(decimal_places).replace(".",",")+' '+curr;		
 	}else{
 		document.getElementById('sleva_nobillis_'+dodavatel).style.display='none';
// 		document.getElementById('sleva_dobirka_'+dodavatel).style.display='none';
 		total_dodavatel.innerHTML = (dodavatel_total[dodavatel]+trans_price-sleva[dodavatel]).toFixed(decimal_places).replace(".",",")+' '+curr;	
 	} 	
	
//	total_dodavatel.innerHTML = (dodavatel_total[dodavatel]+trans_price-sleva[dodavatel]).toFixed(decimal_places).replace(".",",")+' '+curr;  
	price_transport.innerHTML = total_trans +' '+ curr;
	if(total_discount>0){
		price_discount.innerHTML = (total_discount + sleva_nobillis + sleva_dobirka).toFixed(decimal_places).replace(".",",")+' '+curr;
	}
	
	total_order.innerHTML = (base_price+total_trans-sleva_nobillis-sleva_dobirka).toFixed(decimal_places).replace(".",",")+' '+curr; 
	
}

function transport_change(id,curr, before) {
	if (loyalty != 1) {
	transport_payment_change('payment', tp_enable[id],curr, before)
	}

	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) {
	transport_payment_change('transport', pt_enable[id],curr, before)
}

function service_change(elem, id_tran, id_serv, curr, before) {
	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);
}

