function zmien(klasa1, klasa2, check){
	
	var forms = document.getElementById('srodek').getElementsByTagName('table');
	var pom1 = klasa1;
	var pom2 = klasa2;
	
	if(check.checked){
		pom1 = klasa2;
		pom2 = klasa1;
	}else{
		ajaxKoszty('cart2', 'id_typu_dostawy','id_typu_platnosci'); 
	}
	//alert(forms.length);
	for(i=0; i<forms.length; i++){
		if(forms[i].className==pom1){
			forms[i].className = pom2;
		}else if(forms[i].className==pom2){
			forms[i].className = pom1;
		}
	}
	return false;
}

function addbookmark(pole) { 
	var tytul = document.title; 
	var adres = document.location; 
	if (window.sidebar){ 
		window.sidebar.addPanel(tytul, adres, ""); 
	}else if (window.external) { 
		window.external.AddFavorite(adres, tytul); 
	}else if (window.opera && window.print) {
		var a = document.getElementById(pole); 
		a.setAttribute('href', adres); 
		a.setAttribute('title', tytul); 
		a.setAttribute('rel','sidebar'); 
		a.click(); 
	} 
} 
function zaznacz_radio(form,id){
	var elts = document.getElementById(form).elements;
	for(i=0;i<elts.length;i++){
	 if(elts[i].type=='radio'){
		if(elts[i]['id']!=id){
			elts[i].checked = false;
		}else{
			elts[i].checked = true;
		}
	 }
	}
	return true;
}

function dodaj_etui(pole1){
	var elts = document.form_etui.elements;
	//alert(elts.length);
	for(i=0;i<elts.length;i++){
	 if((elts[i].type=='radio') && elts[i].checked){
		document.getElementById(pole1).value = elts[i].value;
		break;
	 }
	}
	return true;
}

function kopiujWartosc(objIdBaza, objIdCel){
	$(objIdCel).value = $(objIdBaza).value;
	return true;
}

function kopiujAdres(){
	kopiujWartosc('firma', 'firma_koresp');
	kopiujWartosc('telefon', 'telefon_koresp');
	kopiujWartosc('imie', 'imie_koresp');
	kopiujWartosc('nazwisko', 'nazwisko_koresp');
	kopiujWartosc('ulica_glowny', 'ulica_koresp');
	kopiujWartosc('nr_domu_glowny', 'nr_domu_koresp');
	kopiujWartosc('nr_mieszkania_glowny', 'nr_mieszkania_koresp');
	kopiujWartosc('kod_pocztowy_glowny', 'kod_pocztowy_koresp');
	kopiujWartosc('miejscowosc_glowny', 'miejscowosc_koresp');
	return true;
}

var IE = (document.all && document.getElementById && !window.opera );
var FF = (!document.all && document.getElementById && !window.opera);
var OP = (document.all && document.getElementById && window.opera);

function mMoveXY(event){
	if(mBoxBgImageIsLoaded){
		mouseX = 0;
		mouseY = 0;
		if(!event){
			var event = window.event;
		}
		if(IE){
			mouseX = event.clientX + document.documentElement.scrollLeft;
			mouseY = event.clientY + document.documentElement.scrollTop;
		}else{ 
			mouseX = event.pageX;
			mouseY = event.pageY;
		}
		mouseX -= $(mContainerId).offsetLeft;
		mouseY -= $(mContainerId).offsetTop;
	
		mFrameOffsetX = parseFloat(mFrameWidth / 2);
		mFrameOffsetY = parseFloat(mFrameHeight / 2);
	
		mBoxBgX = parseInt((-imagesWidthRatio * mouseX) + parseFloat(mBoxWidth / 2));
		mBoxBgY = parseInt((-imagesHeightRatio * mouseY) + parseFloat(mBoxHeight / 2));
	
		if(mBoxBgX > 0){
			mBoxBgX = 0;
		}else if(mBoxBgX < -(mBoxBgImageWidth - mBoxWidth)){
			mBoxBgX = -(mBoxBgImageWidth - mBoxWidth); 
		}
		if(mBoxBgY > 0){
			mBoxBgY = 0;
		}else if(mBoxBgY < -(mBoxBgImageHeight - mBoxHeight)){
			mBoxBgY = -(mBoxBgImageHeight - mBoxHeight); 
		}
		
		setCssBackgroundPosition(mBoxId, mBoxBgX, mBoxBgY);
		
		mFrameY = mouseY - mFrameOffsetY;
		mFrameX = mouseX - mFrameOffsetX;
	
		if(mouseX + mFrameOffsetX > mBaseWidth){
			mFrameX = mBaseWidth - (2 * mFrameOffsetX);
		}else if(mFrameX < 0){
			mFrameX = 0;
		}
			
		if(mouseY + mFrameOffsetY > mBaseHeight){
			mFrameY = mBaseHeight - (2 * mFrameOffsetY);
		}else if(mFrameY < 0){
			mFrameY = 0;
		}
	
		setCssPosition(mFrameId, mFrameY, mFrameX);
		//setCssBackgroundImage(mFrameId, mBoxBgImage, mFrameY, mFrameX);
		//setCssBackgroundImage(mFrameId, '/img/loading.gif', '50%', '50%');
	}
	return true;
}

function calculatemFrameSize(objId, width1, height1, width2, height2){
	width = Math.floor(parseInt(width2) / parseInt(width1));
	height = Math.floor(parseInt(height2) / parseInt(height1));
	setCssSize(objId, height * mFrameRatio, width * mFrameRatio);
	return true;
}

function setCssBackgroundPosition(objId, x, y){
	$(objId).style.backgroundPosition = ''+x+'px '+y+'px';
}

function setCssPosition(objId, top, left){
	$(objId).style.top = parseInt(top)+'px';
	$(objId).style.left = parseInt(left)+'px';
}

function setCssSize(objId, width, height){
	$(objId).style.width = parseInt(width)+'px';
	$(objId).style.height = parseInt(height)+'px';
}

function setCssBackgroundImage(objId, img, left, top){
	$(objId).style.backgroundImage = 'url('+img+')';
	$(objId).style.backgroundPosition = left+' '+top;
}

function mBoxHide(){
	$(mBaseId).className ='';
	$(mFrameId).style.display = 'none';
	$(mBoxId).style.display = 'none';
	var pole = document.getElementById('powiekszenie-kom');
	pole.style.display='block';
	return true;
}
function mBoxShow(){
	var pole = document.getElementById('powiekszenie-kom');
	pole.style.display='none';
	$(mFrameId).style.display = 'block';
	//calculatemFrameSize(mFrameId, mBaseWidth, mBaseHeight, mBoxBgImageWidth, mBoxBgImageHeight);
	mFrameWidth = parseInt(getCssStyleProperties($(mFrameId), 'width'));
	mFrameHeight = parseInt(getCssStyleProperties($(mFrameId), 'height'));
	$(mBaseId).className = 'trans';	
	$(mBoxId).style.display = 'block';
	setCssBackgroundImage(mBoxId, '/img/loading.gif', '50%', '50%');
	if(mBoxBgImageIsLoaded){
		setCssBackgroundImage(mBoxId, mBoxBgImage, '0', '0');
	}else{
		var pomImg = new Image();
		pomImg.src = mBoxBgImage;
		pomImg.onload = function(){
			mBoxBgImageIsLoaded = true;
			setCssBackgroundImage(mBoxId, mBoxBgImage, '0', '0');
		}
	}
	return true;
}

function getCssStyleProperties(objId,styleProperties){
	if ($(objId).currentStyle){
		return $(objId).currentStyle[styleProperties];
	}else if (window.getComputedStyle){
		return document.defaultView.getComputedStyle($(objId),null).getPropertyValue(styleProperties);
	}
}

function przeladuj(loc){
	document.location.href = loc;
}


function loadKoszty(request){
	var xmlResponse = request.responseXML.documentElement;
	//if(xmlResponse.childNodes.length > 0){
		$('koszty-info').innerHTML = xmlResponse.childNodes.length ? xmlResponse.childNodes.item(0).firstChild.data : 'error: 036';
	//}
	return true;
}

function ajaxKoszty(form, poleDostawa, polePlatnosci){
	idDostawy = 0;
	idPlatnosci = 0;
	for(i=0; i < $(form).elements.length; i++){
		if($(form).elements[i].type == 'radio'){
			if($(form).elements[i].name == poleDostawa && $(form).elements[i].checked){
				idDostawy = $(form).elements[i].value;
			}else if($(form).elements[i].name == polePlatnosci && $(form).elements[i].checked){
				idPlatnosci = $(form).elements[i].value;
			}
		}
	}
	
	if(idDostawy && idPlatnosci){
		var parametry = "id_dostawy="+escape(idDostawy)+"&id_platnosci="+escape(idPlatnosci);
		var url = '/dori/ajax_koszty.php';
		new ajax(''+url+'', {postBody: ''+parametry+'', onComplete: loadKoszty});
	}
	return true;
}


function loadZone1(request){
	var xmlResponse = request.responseXML.documentElement;
	//if(xmlResponse.childNodes.length > 0){
		$('zone1').innerHTML = xmlResponse.childNodes.length ? '<span class="error">'+xmlResponse.childNodes.item(0).firstChild.data+'</span>' : 'error: 034';
		ifReload = xmlResponse.childNodes.length ? xmlResponse.childNodes.item(1).firstChild.data : 0;
		if(ifReload){
			document.location.href = document.location.href;
		}
	//}
	return true;
}

function ajaxLogin(login, pass){
	var parametry = "login="+escape(login)+"&pass="+escape(pass);
	var url = 'login.php';
	new ajax(''+url+'', {postBody: ''+parametry+'', onComplete: loadZone1});
	return true;
}

function loadLoginInfo(objId){
	$(objId).innerHTML = 'Nie masz jeszcze konta? &nbsp; <a href="/new_account.php"><img src="/img/bt_zaloz_konto.gif" alt="załóż konto" width="125" height="30" border="0" /></a> <a href="javascript:void(0);" onclick="loadLoginForm(\'zone1\');"><img src="/img/bt_zaloguj.gif" alt="zaloguj się" width="125" height="30" border="0" /></a>';	
	return true;
}

function loadLoginForm(objId){
	$(objId).innerHTML = '<form name="logform" method="post" action="login.php" onsubmit="return false;">'
+ 'login: <input type="text" name="log_login" id="log_login" class="inp" size="14" maxlength="32" /> &nbsp; '
+ 'hasło: <input type="password" name="log_pass" id="log_pass" class="inp" size="14" maxlength="32" /> '
+ '<input name="submit_log" type="image" src="img/bt_zaloguj.gif" align="middle" onclick="ajaxLogin($(log_login).value,$(log_pass).value)" /> '	
+ '<a href="/password.php"><img src="img/bt_przypomnij_haslo.gif" width="125" height"30" alt="" /></a> '
+ '<a href="javascript:void(0);" onclick="loadLoginInfo(\'zone1\');"><img src="img/bt_back.gif" width="42" height"30" alt="" /></a></form>';	
	return true;
}

function checkClientZone(){
	bazTop = 113;
	bazLeft = 0;
	pozTop = 0;
	pozLeft = 0;
	obj = document.getElementById('client-zone');

	if(navigator.appName == "Microsoft Internet Explorer"){
		pozTop = document.documentElement.scrollTop;
		pozLeft = document.documentElement.scrollLeft;
	}else{
		pozTop = window.pageYOffset;
		pozLeft = window.pageXOffset;
	}

	if(pozTop < bazTop){
		pozTop = bazTop;
	}
	
	if(pozLeft < bazLeft){
		pozLeft = bazLeft;
	}
	
	obj.style.top = pozTop+'px';
	//obj.style.left = pozLeft+'px';
	
	bazTop = null;
	bazLeft = null;
	pozTop = null;
	pozLeft = null;
	obj	= null;
	return true;
}

function zliczPlik(id){
	var parametry = "id="+id;
	var url = '/dori/ajax_plik.php';
	new ajax(''+url+'', {postBody: ''+parametry+'', onComplete: function(){ return true; }});
	return true;
}

function ustawWartosc(nazwaPola, wartosc){
	document.getElementById(nazwaPola).value = wartosc;
}

function ustaw_checkboxy(formularz, opcja){
	d = eval("document." + formularz + "");
	if(opcja == "select"){
		for (i = 0; i<d.elements.length; i++){
			if(d.elements[i].type == "checkbox"){
				d.elements[i].checked = true;
			}
		}
	}else if(opcja == "deselect"){
		for (i = 0; i<d.elements.length; i++){
			if(d.elements[i].type == "checkbox"){
				d.elements[i].checked = false;
			}
		}
	}else if(opcja == "invert"){
		for (i = 0; i<d.elements.length; i++){
			if(d.elements[i].type == "checkbox"){
				if(d.elements[i].checked == true){
					d.elements[i].checked = false;
				}else{
					d.elements[i].checked = true;
				}
			}
		}
	}
}

function formatuj_liczbe(n, dokladnosc){
	/*
	n = Math.round(n * 100) / 100;
	//n = (n + 0.001) + '';
	n = (n + 0.001) + '';
	n = (n - 0.001) + '';
	//alert("n: " + n);
	return n.substring(0, n.indexOf('.') + 3);
	*/
	//num = 930.9805;
	//result = n.toFixed(2);
	return n.toFixed(dokladnosc);
}

function limit_dla_pola(limit, pole, span_pole){
	if (document.getElementById(pole).value.length > limit){
		document.getElementById(pole).value = document.getElementById(pole).value.substring(0, limit);
	}
	if(span_pole.length > 0){
		txt = "";
		ile_pozostalo = limit - document.getElementById(pole).value.length;
		txt = "Pozostało " + ile_pozostalo + " z " + limit + " znaków.";
		document.getElementById(span_pole).innerHTML = txt;
	}
}

function migajacy_text(id, k1, t1, k2, t2){
  if (document.getElementById){
    document.getElementById(id).style.color = k1;
	setTimeout('migajacy_text("' +  id + '","' + k2 + '",' + t2 + ',"' + k1 + '",' + t1 + ')', t1);
  }else if (document.all){
    document.all[id].style.color = k1;
	setTimeout('migajacy_text("' +  id + '","' + k2 + '",' + t2 + ',"' + k1 + '",' + t1 + ')', t1);
  }
}

function popraw_telefon(nazwa_pola, max_znakow) {
    document.getElementById(nazwa_pola).value = document.getElementById(nazwa_pola).value.replace(/[^0-9\(\)\ ]/g,"");
	if(max_znakow.length > 0){
		document.getElementById(nazwa_pola).value = przytnij_text(document.getElementById(nazwa_pola).value, max_znakow);
	}
}

function sprawdz_mail(email_pole) {
	if (document.getElementById(email_pole).value.length>0){
		Wzor = /^[0-9a-zA-Z.-_\-]+\@[0-9a-zA-Z.-_\-]+\.[0-9a-zA-Z.]{2,4}$/;
	   	if (Wzor.test(document.getElementById(email_pole).value) == false){
    		alert("Niewłasciwy format adresu e-mail!");
	    	return false;
	    }  
   		return true; 
    }
  	return true;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function MM_swapImgRestore(){ //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages(){ //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d){ //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage(){ //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function pop_new_window(nazwa_pliku, zmienna, id, qs){
	var plik = nazwa_pliku + "?" + zmienna + "=" + id + qs;
	var Win = window.open(plik, "okno","menubar=0,location=0,status=0,toolbar=0,directories=0,alwaysRaised=1,scrollbars=1,titlebar=1,resizable=1");
}

function pop_zdjecie(nazwa_zdjecia_big, nazwa_podkatalogu){
	var plik = "pop_zdjecie.php?foto=" + nazwa_zdjecia_big + "&podkatalog=" + nazwa_podkatalogu;
	var Win = window.open(plik, "big_foto","menubar=0,location=0,status=0,toolbar=0,directories=0,alwaysRaised=1,scrollbars=1,titlebar=1,resizable=1");
}

function przytnij_text(text, max_znakow){
	if (text.length > max_znakow){
		text = text.substring(0, max_znakow);
	}
	return text;
}

function tylko_cyfry(nazwa_pola, max_znakow) {
    document.getElementById(nazwa_pola).value = document.getElementById(nazwa_pola).value.replace(/[^0-9]/g,"");
	if(max_znakow.length > 0){
		document.getElementById(nazwa_pola).value = przytnij_text(document.getElementById(nazwa_pola).value, max_znakow);
	}
}

function tylko_litery(nazwa_pola, max_znakow) {
    document.getElementById(nazwa_pola).value = document.getElementById(nazwa_pola).value.replace(/[^a-z]/g,"");
	if(max_znakow.length > 0){
		document.getElementById(nazwa_pola).value = przytnij_text(document.getElementById(nazwa_pola).value, max_znakow);
	}
}

function usun_biale_znaki(ciag){
	ciag = ciag.replace(/\r/g, " ");
  	ciag = ciag.replace(/[^ A-Za-z0-9`~!@#\$%\^&\*\(\)-_=\+\\\|\]\[\}\{'";:\?\/\.>,<]/g, "");
	ciag = ciag.replace(/'/g, "");
	ciag = ciag.replace(/ +/g, " ");  
	ciag = ciag.replace(/^\s/g, "");
	ciag = ciag.replace(/\s$/g, "");	
	return ciag;
}

function uwzglednij_rabat(pole_brutto, pole_oryg_netto,  pole_netto, pole_rabat, pole_vat){
		document.getElementById(pole_netto).value = formatuj_liczbe((document.getElementById(pole_oryg_netto).value * (1 - usun_biale_znaki(document.getElementById(pole_rabat).value) / 100)), 3);		
		wylicz_wartosc(pole_netto, pole_brutto, '0', pole_vat, 'brutto');
}

function wylicz_wartosc(pole_baza, pole_zmiana, rabat, pole_vat, opcja){
	if(opcja == 'brutto'){
		document.getElementById(pole_zmiana).value = formatuj_liczbe(document.getElementById(pole_baza).value * (1 + usun_biale_znaki(document.getElementById(pole_vat).value) / 100), 3);
	}else if(opcja == 'netto'){
		document.getElementById(pole_zmiana).value = formatuj_liczbe(document.getElementById(pole_baza).value / (1 + usun_biale_znaki(document.getElementById(pole_vat).value) / 100), 3);
	}
}

