/**
* 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_cert() {
	var trans_value = radio_value(document.coupone.transport)
	var paym_value = radio_value(document.coupone.payment)

	var price = 0
	if (trans_value) {
		price += trans_prices[trans_value]
	}
	if (paym_value) {
		price += paym_prices[paym_value]
	}

    base_price = eval(document.coupone.cert_value.value);
    if (base_price) {
      if (base_price >= 1000) price = 0;
    } else {
      base_price = 0;
    }
    document.coupone.cenaDopPla.value = price + ' ' + 'Kč';
    document.coupone.cenaCelkem.value = (price + base_price) + ' ' + 'Kč';
    celkem = price + base_price;
}

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 (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, isOrder) {
	//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)
  	}

	if (isOrder) recalc_total();
	else recalc_cert();
}

function transport_change(id, isOrder) {
	if (loyalty != 1) {
	transport_payment_change('payment', tp_enable[id], isOrder)
	}
}

function payment_change(id, isOrder) {
	transport_payment_change('transport', pt_enable[id], isOrder)
}

function priceToStr(intCislo){	

  	 strCislo = intCislo.toString().replace(".",",")
  	 carka = strCislo.length-1 - strCislo.toString().search(",")
  	 
		 if(carka==0)
  	    strCislo = strCislo+',00';					
  	 else if(carka == 1 && intCislo!="0")
		    strCislo = strCislo+'0';
					
  	 strLength = strCislo.length;
  	 if(strLength > 6){ // cislo vetsi nez tisic
  	    tisice = strCislo.substr(0, strLength-6);
  			ostatni = strCislo.substr(strLength-6);
  			strCislo = tisice+' '+ostatni;
  	 } 	
  	 
  	 return strCislo;		

}

function orderPrice() {
     certprice = eval(document.coupone.cert_value.value);
		 if (certprice < 1000) {	
			paymprice = document.coupone.paymprice.value;
			tranprice = document.coupone.tranprice.value;
		 } else {
		 	paymprice = 0;
			tranprice = 0;
		 }

  	 celkem = eval(certprice)+eval(tranprice)+eval(paymprice);
  	 base_price = celkem;
  	 document.coupone.cenaCelkem.value = priceToStr(celkem)+' Kč';
  	 document.coupone.cenaDopPla.value = priceToStr(eval(tranprice)+eval(paymprice))+' Kč';
}

// @from product_detail.tpl
var old_text
if (document.getElementById('related-hide-text') != null)
	var old_text = document.getElementById('related-hide-text').innerHTML
var new_text = 'Skrýt většinu souvisejících produktů'

function related_products_show_hide() {
	var rh = document.getElementById('related-hide')
	var rht = document.getElementById('related-hide-text')
	var rhi = document.getElementById('related-hide-img')

	var img_src = new String(rhi.src)

	if (rh.style.display != 'none') {
		rh.style.display = 'none'
		rht.innerHTML = old_text
		rhi.src = img_src.replace(/-[0-9](\..+)$/, '-1$1')
	} else {
		rh.style.display = ''
		rht.innerHTML = new_text
		rhi.src = img_src.replace(/-[0-9](\..+)$/, '-2$1')
	}
}
// zobrazovani skladovych zasob na detailu produktu
	function topScroll() {
		if (window.innerHeight) {
			  return ( window.pageYOffset )
		} else if (document.documentElement && document.documentElement.scrollTop) {
			return ( document.documentElement.scrollTop )
		} else if (document.body) {
			return ( document.body.scrollTop )
		}
	}
	function show_sklad() {
		if(topScroll() > 28) {
			document.getElementById('light').style.top = topScroll() -28 +'px'; 
		} else {
			document.getElementById('light').style.top = topScroll() +'px'; 
		}
		
		document.getElementById('light').style.display='block';
		document.getElementById('fade').style.display='block'
	}
	function hide_sklad() {
		document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'
	}