/**
* Funkce z templates
*/

// 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
function next(id)
{
	document.getElementById(id).focus()
}

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() {
	var trans_value = radio_value(document.order.transport)
	var paym_value = radio_value(document.order.payment)

	var price = 0
	if (trans_value) {
		price += trans_prices[trans_value];
		if (service_value[trans_value]) {
			price += service_value[trans_value]
		}
	}
	if (paym_value) {
		price += paym_prices[paym_value]
	}


	document.getElementById('price-transport').innerHTML = format_currency(price)
	document.getElementById('price-total').innerHTML = format_currency(price + base_price)

}

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) {
	//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()
}

function transport_change(id) {
	if (loyalty != 1) {
	transport_payment_change('payment', tp_enable[id])
	}
}

function payment_change(id) {
	transport_payment_change('transport', pt_enable[id])
}
var service_value = new Array();
function service_change(elem, id_tran, id_serv) {

	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();
}

