window.browser = navigator.appName;
window.browserVersion = navigator.appVersion;

window.currentMenu = null;
window.currentSubmenu = null;
window.onMenu = 0;
window.onSubMenu = 0;

window.MenuName = "menuTab";
window.MainFloat = "mainFloat";
window.MenuFloat = 	"menuFloat";
window.SubmenuFloat = "submenuFloat";

window.goNext = false;

function IsNumeric(sText){
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function isSet(variable){
      return (typeof(variable) != 'undefined');
      }

function changeFlag(id,type) {
	tempArr = id.split("_");
	if (type==true) {
		pic = tempArr[1]+"_color";
	} else {
		pic = tempArr[1]+"_bw";
	}
	document.getElementById(id).src="graphics/flag/"+pic+".gif";
}


function productButton(id,text,show,lang) {
	if (show == true) {
		pic = text + "_on";
	} else {
		pic = text;
	}
	document.getElementById(id).src="graphics/menu/"+lang+"/"+pic+".gif";
}



function menuTabMouseAction(id,type) {
	if (type == 1) {
		clearAllMenu();
		window.currentMenu = id;
		window.onMenu = 1;
		if (id != window.MenuName+window.activeTab) {
			document.getElementById(id).setAttribute('class','selTabMouse');
			document.getElementById(id).className = 'selTabMouse';
		}
		showMenuForTab(id,true);
	} else {
		setTimeout("offTabMouseAction('"+id+"');",300);
		window.onMenu = 0;
	}
}

function clearAllMenu() {
	for (a=1; a<=window.totalMenu; a++) {
		if (a != window.activeTab) {
			hideMenu(window.MenuName + a);
		} else { }
	}
	document.getElementById(window.MenuFloat).innerHTML = "";
	document.getElementById(window.SubmenuFloat).innerHTML = "";
}


function offTabMouseAction(id) {
	if ((onMenu == 0)) { 
		if (id != window.MenuName+window.activeTab) { hideMenu(id); }
		window.currentMenu = null;
		document.getElementById(window.MainFloat).style.visibility= 'hidden';
		document.getElementById(window.MenuFloat).innerHTML = "";
		document.getElementById(window.SubmenuFloat).innerHTML = "";
	}
}

function hideMenu(id) {
		try {
			document.getElementById(id).setAttribute('class','othTab');
			document.getElementById(id).className = 'othTab';
			showMenuForTab(id,false);
		} catch (err) {}
}

function mouseOnMenu(id,type) {

	window.onMenu = 1;
	
	if (type=="menu") {
		deselectMenu(id);
		document.getElementById(id).className = 'menuInsideDivHover';
		document.getElementById(id).setAttribute('class','menuInsideDivHover');
		checkForSubmenu(id);
	} else {
		document.getElementById(id).setAttribute('class','submenuInsideDivHover');
		document.getElementById(id).className = 'submenuInsideDivHover';
	}
	
}

function mouseOffMenu(id,type) {

	if (type=="menu") {
	} else {
		document.getElementById(id).setAttribute('class','submenuInsideDiv');
		document.getElementById(id).className = 'submenuInsideDiv';
	}
	tempArr = id.split("_");
	window.onMenu = 0;
	setTimeout("offTabMouseAction('menuTab"+tempArr[1]+"');",300);

}

function deselectMenu(id) {
	try {
		tempArr = id.split("_")
		temp = eval("vmm_" + tempArr[1]);
		cnt = temp.length;
		
		for (z=1;z<=cnt;z++) {
			document.getElementById("mm_"+tempArr[1]+"_"+z).setAttribute('class','menuInsideDiv');
			document.getElementById("mm_"+tempArr[1]+"_"+z).className = 'menuInsideDiv';
		}
		
	} catch (err) {  }
}

function checkForSubmenu(id) {
	
	var test = false;
	submenuText="";
	try {
		temp = eval(id.replace("mm","vsm"));
		tempID = eval(id.replace("mm","vsmID"));
		tempArr = id.split("_");
		cnt = temp.length;
		test = true;
		tpHeight = document.getElementById(id).offsetTop;

		if (tpHeight > 0) {
			submenuText += "<img src=\"graphics/misc/tp.gif\" border=\"0\" width=\"1\" height=\""+tpHeight+"\">";
		}
		for (z=1;z<=cnt;z++) {
			submenuText += "<a href=\"page.php?i="+tempID[z-1]+"\" class=\"submenuInsideAnchor\">";
			submenuText += "<div id='sm_"+tempArr[1]+"_"+tempArr[2]+"_"+z+"' class='submenuInsideDiv' onmouseover='mouseOnMenu(this.id,\"submenu\");' onmouseout='mouseOffMenu(this.id,\"submenu\");'>"+temp[z-1]+"</div>";
			submenuText += "</a>";
		}	
	} catch (err) { }
	
	document.getElementById(window.SubmenuFloat).innerHTML = submenuText;
	
}

function showMenuForTab(id,show) {

	if (show == false) {

	} else {
		
		offsetYBrowser = 24;
		offsetXBrowser = 0;
		
		qot = 0;
		qoo = 0;
		
		smo = document.getElementById("siteMain").offsetLeft;
	
		switch (window.browser) {
			case "Microsoft Internet Explorer":
				offsetXBrowser = smo
				qoo = 0;
				qot = 95;
				break;
			case "Opera":
			case "Netscape":
				qoo = smo;
				qot = -1;
				break;

		}
		
		
		q = (id.substring(id.length - 1,id.length));
		menuIndex = q;
		q --;
		q = window.MenuName + q;
		
		if (q == window.MenuName + "0") {
			qo = 801 + qoo;	
		} else {
			qo = document.getElementById(q).offsetLeft;
		}
		
		
		ido = document.getElementById(id).offsetLeft;

		
		menuText="";
		//menuText +="<table class=\"dlatabeli\">";
		try {
			temp = eval("vmm_"+menuIndex);
			tempID = eval("vmmID_"+menuIndex);
			cnt = temp.length;
			for (z=1;z<=cnt;z++) {
				menuText += "<a href=\"page.php?s="+tempID[z-1]+"\" class=\"menuInsideAnchor\">";
				menuText += "<div id='mm_"+menuIndex+"_"+z+"' class='menuInsideDiv' onmouseover='mouseOnMenu(this.id,\"menu\");' onmouseout='mouseOffMenu(this.id,\"menu\");'>"+temp[z-1];
				try {
					ttt = eval("vsm_"+menuIndex+"_"+z);
			
					menuText += " &rsaquo;";
				}catch (terr) {}
				menuText += "</div></a>";
			}
		} catch (err) { }
		//menuText += "</table>";
		
		document.getElementById(window.MenuFloat).innerHTML = menuText;

		
		w = (qo - ido - 1);
		w = w.toString();
		w = w + "px";
		document.getElementById(window.MenuFloat).style.width = w;
		document.getElementById(window.SubmenuFloat).style.width = w;
		w = null;
		delete(w);
		
		l = ido + window.offsetXBrowser;
		l = l.toString();
		l = l + "px";
		document.getElementById(window.MainFloat).style.left = l;
		l = null;
		delete(l);
		
		t = document.getElementById(id).offsetTop + offsetYBrowser + qot;
		t = t.toString();
		t = t + "px";
		document.getElementById(window.MainFloat).style.top =  t;
		t = null;
		delete(t);
		
		document.getElementById(window.MainFloat).style.visibility = 'visible';	
	}
}



function ShowLevel(level,i) {
	
	if(document.getElementById(level+i).style.display == 'block'){
		document.getElementById(level+i).style.display = 'none';
		document.getElementById(level+"_node"+i).src = 'graphics/misc/tree_node_closed.gif';
		
	} else{
		document.getElementById(level+i).style.display = 'block';
		document.getElementById(level+"_node"+i).src = 'graphics/misc/tree_node_open.gif';
	}

}

function ShowBuyOptions(id){
		BuyOptions = document.getElementById(id.replace("proBuyBox","BuyOptions"));
		if(document.getElementById(id).checked){
			BuyOptions.style.display = 'inline';
		} else {
			BuyOptions.style.display = 'none';
		}
		validateProPr(id.replace("proBuyBox","proInCn"));
		
}

function proCountClick(id,dir) {
	if (dir=="up") { t=1; n="proInUp" } else { t=-1; n="proInDn" }
	try {
		proInCn = id.replace(n,'proInCn');
	} catch (exa) {  }
	
	try {
		v = document.getElementById(proInCn);
		if (v.value == "") { v.value=0; }
		v.value = parseInt(v.value) + t;
		validateProPr(proInCn);  
	} catch (exb) {  }
	
	
}

function waitgoNext(id) {
	window.goNext = true;
	validateProPr(id)
}

function validateProPr(id) {
	try {
		
		isCover = false;
		try {
			isCover = isBasketCover;
			if (window.goNext == false) {
				idCover = document.getElementById('basketCoverDivID');
				idCover.style.display = 'block';
				var t = setTimeout("waitgoNext('" + id + "');",300);
				return true;
			} else {
				isCover = true;
			}		
		} catch (exbasket) { }
		
		window.goNext = false;
		proInCn = document.getElementById(id);
		proInPr = document.getElementById( id.replace('proInCn','proInPr'));
		proInTt = document.getElementById( id.replace('proInCn','proInTt'));
		proInTtVat = document.getElementById (id.replace('proInCn','proInTtVat'));
		proInTtHid = document.getElementById( id.replace('proInCn','proInTtHid'));
		
		isForm = false;
	
		tPIP = null;
		tPIP = proInPr.innerHTML;
		if ((tPIP == null) || (tPIP == "")) { tPIP = proInPr.value; isForm = true; }


		tPIT = null;
		tPIT = proInTt.innerHTML;
		if ((tPIT == null) || (tPIT == "")) { tPIT = proInTt.value; isForm = true; }


		if (parseInt(proInCn.value) < 0) { proInCn.value=0; } 
		if (parseInt(proInCn.value) > 999) { proInCn.value=999; } 
		if (!IsNumeric(proInCn.value)){ alert("Ilość musi być liczbą!"); proInCn.value=0; }
		i = parseInt(proInCn.value);
		if (proInCn.value == "") { i=0; }

		//check prices
		arrPr = eval(id.replace('proInCn','propr'));
		
		isNr = false;
		isAm = false;

		try {
			arrNr = eval(id.replace('proInCn','pronr'));
			isNr = true;
		} catch (errNr) { isNr = false; }
	
		try {
			arrAm = eval(id.replace('proInCn','proam'));
			isAm = true;
		} catch (errAm) { isAm = false; }
		
		
		if ((! isNr) && (! isAm)) {
			proInCn.value = 0;
			alert ("Wystąpił błąd!");
			return null;
		}
		
		getVatLevel = 1;
		try {
			getVatLevel = parseFloat(eval(id.replace('proInCn','provl')));
		} catch (errVL) { }
	
		if (isNr) {
			for (b=0;b<arrPr.length;b++) {
				val = arrPr[b];
				tmpQnty = 0;
				try {
					tmpQnty = eval(id.replace('proInCn','proba'));
				} catch (exproba){ }
				if (i + tmpQnty <= arrNr[b]) {
					val = arrPr[b];
					break;
				}
			}
		}
		

		var DisAmShopPage = 0;
		if (isAm) {
			al1 = IDsArrayDisAm.length;

			for(x=0;x<al1;x++){
				arrPrAm = eval(IDsArrayDisAm[x].replace("proInTt", "propr"));	
				CnID = IDsArrayDisAm[x].replace("proInTt","proInCn");
				CnVal = document.getElementById(CnID);
				BbID = IDsArrayDisAm[x].replace("proInTt","proBuyBox");
				
				BbVal = false;				
				try {
					BbVal = document.getElementById(BbID).checked;
				} catch (eee) {
					if (parseInt(CnVal.value) > 0) {
						BbVal = true;
					}
				}
				if (BbVal) {
					DisAmShopPage = DisAmShopPage + (parseFloat(arrPrAm[0])*CnVal.value);
				}
			}

			DisAmTotal = DisAmShopPage + BasketDisAmTtPr;

			arrAmAm = eval(IDsArrayDisAm[0].replace("proInTt", "proam"));

			for (b=0;b<arrAmAm.length;b++) {

				b1=b;
				
				if (DisAmTotal <= arrAmAm[b]) {
					
				b1=b;
					break;
				}
	
			}

			for(x=0;x<al1;x++){
				
				arrPrAm = eval(IDsArrayDisAm[x].replace("proInTt", "propr"));	
				CnID = IDsArrayDisAm[x].replace("proInTt","proInCn");
				TtHID = IDsArrayDisAm[x].replace("proInTt","proInTtHid");
				TtID = IDsArrayDisAm[x];
				TtVal = document.getElementById(TtID);
				TtValH = document.getElementById(TtHID);
				TtVat = document.getElementById(IDsArrayDisAm[x].replace("proInTt","proInTtVat"));
				CnVal = document.getElementById(CnID); 
				BbID = IDsArrayDisAm[x].replace("proInTt","proBuyBox");
				PrID = IDsArrayDisAm[x].replace("proInTt","proInPr");
				PrVal = document.getElementById(PrID);
				BbVal = false;

				getVatLevel = 1;
				try {
					getVatLevel = parseFloat(eval(id.replace('proInCn','provl')));
				} catch (errVL) { }
				
				if(IsProPage==1){
					BbVal = true;
				} else {
					BbVal = document.getElementById(BbID).checked;
				}
				
				if (BbVal) {
					val = formatNumber(arrPrAm[b1]);
					yTotal = arrPrAm[b1]*CnVal.value;
					TTT = formatNumber(yTotal);
					if (isForm) {
						PrVal.value = val;
						TtVal.value = TTT;
						TtValH.value = yTotal;
						TtVat.innerHTML = formatNumber(yTotal * ((getVatLevel/999) + 1));
					} else {
						PrVal.innerHTML = val;
						TtVal.innerHTML = TTT;
						TtVat.innerHTML = formatNumber(yTotal * ((getVatLevel/999) + 1));
						try{
							TtValH.value = yTotal;
						} catch(exvui) { }
					}
				}
			}
		}
		
	
		try {
			f = tPIP;
			f = f.replace(" ","");
			f = f.replace(",",".");
			f = parseFloat(f);
		} catch (exf) {
			//alert ("exf :" + exf);
		}
	

		if (!isAm) {
			yTotal = i*val;
			val = formatNumber(val);
			TTT = formatNumber(yTotal);
			
			if (isForm) {
				proInPr.value = val;
				proInTt.value = TTT;
				proInTtHid.value=yTotal;
			} else {
				proInPr.innerHTML = val;
				proInTt.innerHTML = TTT;
				TTTVAT = (yTotal * ((getVatLevel/100) + 1));
				proInTtVat.innerHTML = formatNumber(TTTVAT);
				try{
					proInTtHid.value=yTotal;
					//proInTtHid.value=i*yTotal; //WHY IS THAT
				}
				catch(e12){
					//alert ("mam e12: " + e12);
				}
			}
		}
		

		
		try {
			al = IDsArray.length;
			total = 0;
			totalVAT = 0;
			for(x=0;x<al;x++){		
				BbID = IDsArray[x].replace("proInTt","proBuyBox");
								
				BbVal = document.getElementById(BbID).checked;
				
				if (BbVal) {
				
					TtID = IDsArray[x].replace("proInTt","proInTtHid");
					TtVal = document.getElementById(TtID).value;
					
					TtVatID = IDsArray[x].replace("proInTt","proInTtVat");
					TtVatVal = document.getElementById(TtVatID).innerHTML;
					TtVatVal = TtVatVal.replace(" ","");
					TtVatVal = TtVatVal.replace(",",".");
				
					total = total + parseFloat(TtVal);
					totalVAT = totalVAT + parseFloat(TtVatVal)
				}
			}
			if(al>0) { 
				document.getElementById("proInTOTAL").innerHTML = formatNumber(total);
				document.getElementById("proInTOTALVAT").innerHTML = formatNumber(totalVAT); 
			}


		} catch (exc) { }
		
		if (isCover == true) {
			idCover = document.getElementById('basketCoverDivID');
			idCover.style.display = 'none';
			window.goNext = false;
		}
		
	} catch (exa) { alert ("Przepraszamy ale wystąpił błąd. Ceny mogą być niepoprawnie podliczone."); }
}

function formatNumber(number) {

		var s = number.toString();
		sa = s.split(".");

		b=0;
		sm = 0;
		mt = "";
		try {
			sm = sa[0].length;
			mt = sa[0];			
		} catch (exb) { }
		tq = "";
		tm = "";
		
		
		ts = "";
		ss = 0;
		st = "00";
		try {
			ss = sa[1].length;
			st = sa[1];
		} catch (exb) { }


		if (sm > 0) {
			for (a=sm-1 ; a>=0 ; a--) {
				if (b == 3) {
					b = 0;
					tm = " " + tm;
				}
				tq = mt.substr(a,1);
				tm = tq + tm;
				b++;	
			}
		} else {
			tm = "0";
		}
									 
	
		if (ss > 0) {
			if (ss >= 3) {
				st1 = st.substring(0,2);
				st2 = "0." + st.substring(2,ss);
				
				try {
					st1i = parseInt(st1);
					st2i = parseFloat(st2);
					if (st2i >= 0.5) {
						st1i ++;
					}
					st = st1i.toString();
					
				} catch (exPP) {
					st = st1;
				}
				
			} else if (ss == 2) {
				st = st;
			} else if (ss == 1) {
				st = st + "0"
			} else {
				st = "00";
			}
		} else {
			st = "00";
		}

		
		return tm + "," +  st;
}


function mousePos(e) {


	if (IE) {
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {
		tempX = e.pageX
		tempY = e.pageY
		}  

	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  

	try {
		cov = document.getElementById('basketCoverDivID');
		cov.style.left = tempX-10;
		cov.style.top = tempY-10;
	} catch (exc) {}

	
	return true;
	
}
