//$=function(x){return document.getElementById(x)}

mkPopup=function(mypage,myname,w,h,scroll){
 var winl=(screen.width-w)/2;
 var wint=(screen.height-h)/2;
 var settings='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=no';
 win=window.open(mypage,myname,settings);
}
resBody=function(o){
	return true;
	var w=o.width;
	var h=o.height;
	if (w>0&&h>0) {
		var resWidth = w + 0;
		var resHeight = h + 40;
		var ua = navigator.userAgent.toLowerCase();
		var isOp=(ua.indexOf("opera")!=-1?true:false);
		var isIe=(ua.indexOf("msie")!=-1&&!isOp?true:false);
		if (isIe) {
			resWidth = w + 35;
			resHeight = h + 65;
		}
	
		if (window.screen.width < resWidth)resWidth=window.screen.width-20;
		if (window.screen.height < resHeight)resHeight=window.screen.height-20;
		
		window.resizeTo(resWidth, resHeight);
		
		var wx=(screen.width-w)/2, wy=(screen.height-h)/2;
 		window.moveTo(wx,wy);
		//self.focus();
	}
}
function preloadImg(src) {
	new Image().src=src;
}

function tabs(tabsconfig)
{
	this.tabsconfig = tabsconfig;
	
	this.ch = function(tab_active)
	{
		for(i=0; i<this.tabsconfig.length; i++)
		{
			if (this.tabsconfig[i] == tab_active)
			{
				$('t'+this.tabsconfig[i]).className = this.tabsconfig[i]+'_a';
				$('c'+this.tabsconfig[i]).className = 'txt'+this.tabsconfig[i]+'_a';
			}
			else
			{
				$('t'+this.tabsconfig[i]).className = this.tabsconfig[i];
				$('c'+this.tabsconfig[i]).className = 'txt'+this.tabsconfig[i];
			}
		}
	}
}

function gourl(u)
{
	document.location.replace(u);
	return false;
}

function chmimg(imgid) {
	if (imgs[imgid]) {
		$('phone_mimg').src = imgs[imgid].S;
		$('phone_mimg').parentNode.href=imgs[imgid].D;
		new Image().src=imgs[imgid].D;
	}
}



/* newsletter */
function newsletter_change(obj){input_change(obj,'Wpisz swój adres e-mail')}
function input_change(obj,txt){if(obj.value==txt)obj.value=''};
function newsletter_send(o){
	if (typeof o == 'undefined') {
		o = '';
	}
	var err=false;
	var eObj=$('newsletter_email'+o);
	if (eObj.value==null||eObj.value==''||eObj.value=='Wpisz swój adres e-mail'||!isEmail(eObj.value))
	{
		err=true;
		alert('Wpisz poprawny adres e-mail.');
		eObj.focus();
		return false;
	}
	
	advAJAX.get({
		url: "/index.php?m=newsletter&a=addemail&email="+eObj.value,
		onLoading : function(obj){ /* Wy?wietlenie obrazka wskazuj?cego na ?adowanie */ },
		onComplete : function(obj) { /* Ukrycie obrazka... */ },
		onSuccess : function(obj) {
			eval('ret = obj.responseText');
			if (ret == '-1') {
				alert('Podany adres email już został dopisany do naszej bazy.');
				eObj.value='';
				eObj.focus();
			} else if (ret == '1') {
				alert('Dziękujemy. Twój email został dopisany do naszego newslettera.');
				eObj.value='';
			} else {
				alert('Wpisz poprawny adres e-mail.');
			}
		}
	})
	return false;
}

function search_change(obj){input_change(obj,'szukaj...')}

/** email validation stuff. Based on: SmartWebby.com (http://www.smartwebby.com/dhtml/) */
isEmail=function(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
	
	if (str.indexOf(" ")!=-1){
		return false
	}

	return true					
}

function checkHeight(d) {
	if (!d) return false;
	var divHeight=0;
	//d=$(arguments[0]);
	if(d.offsetHeight)
		divHeight=d.offsetHeight;
	else if(d.style.pixelHeight)
		divHeight=d.style.pixelHeight;
	return divHeight;
}
function max(a,b) {return (a>b)?a:b}
function bim(){try{window.event.returnValue=false}catch(e){};return false}

function addToFavorite()
{
	try {
		window.external.AddFavorite(document.location, document.title);
	} catch(e) {
		alert('Jeżeli chcesz dodać tę stronę do ulubionych, naciśnij OK a następnie CTRL+D');
	}
}
var cart = {
	prodPrices: {},
	prodCnt: {},
	sumPrice: 0.0,
	deliveryPrice: 0.0,
	totalPrice: 0.0,
	addProduct: function(prodId, sizeObj) {
		//alert('Obsługa dodania do koszyka produktu o ident. '+prodId)
		var _size = '';
		if (sizeObj) {
			if (sizeObj.selectedIndex==0) {
				alert('Proszę wybrać rozmiar.');
				return false;
			} else {
				_size = sizeObj[sizeObj.selectedIndex].value;
			}
		}
		document.location.replace('/koszyk/?a=prod_add&p='+prodId+'&s='+_size);
	},
	recntAll: function() {
		//totalPrice i uaktualnienie formularza:
		cart.totalPrice=cart.sumPrice+cart.deliveryPrice;
//		$('sum_price').innerHTML=cart.sumPrice;
//		$('delivery_price').innerHTML=cart.deliveryPrice;
//		$('total_price').innerHTML=cart.totalPrice;
		
		if ($H(cart.prodCnt).values().length) {
			new Ajax.Request('koszyk.html?a=update', {
				method: 'post',
				parameters: $('fkoszyk').serialize(),
				onLoading: function() {
					$('fkoszyk').disable();
				},
				onLoaded: function() {
					$('fkoszyk').enable();
				},
				onError: function() {
					$('fkoszyk').enable();
				},
				onSuccess: function(t) {
					$('fkoszyk').enable();
					var _f = t.responseText.split(':');
					if (-1==parseFloat(_f[0]) || -1==parseFloat(_f[1]) || parseFloat(_f[0])-parseFloat(_f[1]) != cart.sumPrice) {
						//document.location.reload();
						if (console&&console.log) console.log(t.responseText);
					} else {
						cart.deliveryPrice = parseFloat(_f[1]);
						cart.totalPrice=cart.sumPrice+cart.deliveryPrice;
						$('sum_price').innerHTML=cart.sumPrice;
						$('delivery_price').innerHTML=cart.deliveryPrice;
						$('total_price').innerHTML=cart.totalPrice;
//						$$('input#paytype_online_kurier').first().nextSibling.select('span').first().innerHTML=parseFloat(_f[4]);
						$('paytype_online_kurier_span').innerHTML=parseFloat(_f[4]);
//						$$('input#paytype_przelew_kurier').first().nextSibling.select('span').first().innerHTML=parseFloat(_f[5]);
						$('paytype_przelew_kurier_span').innerHTML=parseFloat(_f[5]);
//						$$('input#paytype_pobranie_kurier').first().nextSibling.select('span').first().innerHTML=parseFloat(_f[6]);
						$('paytype_pobranie_kurier_span').innerHTML=parseFloat(_f[6]);
						
						//free_delivery_txt
						if (parseFloat(_f[2])>0 && (parseFloat(_f[2])-cart.sumPrice)>0) {
							$$('#free_delivery_txt').first().show();
							$$('#free_delivery_txt td').first().innerHTML='Uwaga! Zamów dodatkowo za <strong>' + (Math.round(100*(parseFloat(_f[2])-cart.sumPrice))/100.0) + ' zł</strong> a otrzymasz <strong>' + (_f[3]=='1' ? 'DARMOWĄ' : 'tańszą') + '</strong> przesyłkę!';
						} else {
							$$('#free_delivery_txt').first().hide();
							$$('#free_delivery_txt td').first().innerHTML='';
						}
					}
				}
			});
		}
	},
	checkOrderFrm : function(fobj) {
		var err = true;
		$$('#platnosci-wybor input[type="radio"]').each(function(e){
			if (e.checked)	{
				err = false;
			}
		})
		if (err) {alert('Wybierz sposób płatności.');}
		return !err;
	},
	load: function() {
		if ($$('#platnosci-wybor input').length) {
			var _ch=false,_els=$$('#platnosci-wybor input');
			_els.each(function(e,i){if(e.checked)_ch=true});
			if (!_ch){
				_els.first().checked=true;
			}
			_els.each(function(e,i){if(e.checked)e.nextSiblings()[0].setStyle({'fontWeight' : 'bold'})});
			_els.each(function(e,i){if(e.checked){$('delivery_price').innerHTML=cart.deliveryPrice=cart.payMap[e.id.substring(8)+'_price'];$('total_price').innerHTML=cart.sumPrice+cart.deliveryPrice;throw $break}});
		}
		
		//ustalenie sumPrice po zmianie liczby zamawianych produktów
		$$('input.prod-quantity').each(function(el){
			Event.observe(el, 'change', function(){
				cart.sumPrice=0;
				cart.prodCnt={};
				$$('input.prod-quantity').each(function(e){
					e.value=parseInt(e.value);
					if (isNaN(e.value)||e.value<=0) {
						e.value=1;
					}
					cart.sumPrice+=cart.prodPrices[e.getAttribute('rel')]*e.value;
					cart.prodCnt[e.getAttribute('rel')]=e.value;
					el.parentNode.nextSiblings().first().innerHTML=(Math.round(100*cart.prodPrices[e.getAttribute('rel')]*e.value)/100.0)+' zł';
				})
				cart.recntAll();
			})
		});
		
		//ustalenie deliveryPrice po kliknięciu na dany typ płatności na liście
		$$('#platnosci-wybor input[type=radio]').each(function(el){
			Event.observe(el, 'click', function(e){
				$$('#platnosci-wybor label').invoke('setStyle', {fontWeight:'normal'});
				if(this.checked){
					this.nextSiblings()[0].setStyle({fontWeight : 'bold'});
					cart.deliveryPrice = cart.payMap[this.id.substring(8)+'_price'];
				}
				cart.recntAll();
			})
		});
		
		//przeliczenie koszyka
		cart.recntAll();
	}
};