/* Copyright 2008., Croatiaknjiga d.o.o. */


var siteHistory = {id:0, url:""}


var guiModalWindow = {
	id:null,
	fid:null,
	cid:null,
	active:false,
	onresize:null,
	open:function(oColor, oAlpha) {
		if (this.active == false) {
			if (oColor == undefined) oColor = "#000000";
			if (oAlpha == undefined || isNaN(oAlpha)) oAlpha = 50;
			var winId = document.createElement("div");
			winId.style.position = "absolute";
			winId.style.overflow = "hidden";
			winId.style.height = "0px";
			winId.style.width = "0px";
			winId.style.left = "0px";
			winId.style.top = "0px";
			winId.style.backgroundColor = oColor;
			winId.style.opacity = Number(oAlpha / 100);
			winId.style.MozOpacity = Number(oAlpha / 100);
			winId.style.filter = "alpha(opacity=" + oAlpha + ")";
			winId.style.zIndex = 9999;
			document.body.appendChild(winId);
			this.id = winId;
			if (this.ieFixNeed()) {
				var tmpId = document.createElement("iframe");
				tmpId.setAttribute("frameBorder", "0");
				tmpId.setAttribute("scrolling", "no");
				tmpId.style.filter = "mask()";
				winId.appendChild(tmpId);
				this.fid = tmpId;
			}
			var conId = document.createElement("div");
			conId.style.position = "absolute";
			conId.style.overflow = "hidden";
			conId.style.height = "100px";
			conId.style.width = "320px";
			conId.style.left = "0px";
			conId.style.top = "250px";
			conId.style.background = "url(img/img_infobox.png) 0px 0px no-repeat";
			conId.style.zIndex = 10000;
			conId.style.display = "none";
			document.body.appendChild(conId);
			this.cid = conId;
			this.active = true;
			this.onresize = window.onresize;
			this.autoresize();
			window.onresize = function () {
				if (guiModalWindow.onresize) guiModalWindow.onresize();
				guiModalWindow.autoresize();
			}
		}
	},
	close:function() {
		if (this != undefined && this.id != null && this.active == true) {
			window.onresize = this.onresize;
			var tmpId = this.id;
			while (tmpId.hasChildNodes()) {
				tmpId.removeChild(tmpId.lastChild);
			}
			document.body.removeChild(this.cid);
			document.body.removeChild(this.id);
			this.id = null;
			this.fid = null;
			this.cid = null;
			this.active = false;
			this.onresize = null;
		}
	},
	show:function() {
		if (this != undefined && this.id != null && this.cid != null && this.active == true) {
			var conId = this.cid;
			conId.style.display = "block";
		}
	},
	hide:function() {
		if (this != undefined && this.id != null && this.cid != null && this.active == true) {
			var conId = this.cid;
			conId.style.display = "none";
		}
	},
	content:function(iHTML) {
		if (this != undefined && this.id != null && this.cid != null && this.active == true) {
			var conId = this.cid;
			if (iHTML != undefined && iHTML != "") conId.innerHTML = iHTML;
		}
	},
	autoresize:function() {
		var uScreen = new Object({height:document.documentElement.clientHeight, width:document.documentElement.clientWidth});
		var uBody = new Object({height:document.body.clientHeight, width:document.body.clientWidth});
		var ieFix = this.ieFixNeed();
		if (this != undefined && this.id != null && this.active == true) {
			var winId = this.id;
			var frmId = this.fid;
			var conId = this.cid;
			if (uScreen.height > uBody.height) {
				winId.style.height = Number(uScreen.height) + "px";
				if (ieFix) frmId.style.height = Number(uScreen.height) + "px";
			} else {
				winId.style.height = Number(uBody.height) + "px";
				if (ieFix) frmId.style.height = Number(uBody.height) + "px";
			}
			if (uScreen.width > uBody.width) {
				winId.style.width = Number(uScreen.width) + "px";
				if (ieFix) frmId.style.width = Number(uScreen.width) + "px";
				if (uScreen.width > 320) conId.style.left = Math.floor((uScreen.width - 320)/2) + "px";
				else conId.style.left = "0px";
			} else {
				winId.style.width = Number(uBody.width) + "px";
				if (ieFix) frmId.style.width = Number(uBody.width) + "px";
				if (uBody.width > 320) conId.style.left = Math.floor((uBody.width - 320)/2) + "px";
				else conId.style.left = "0px";
			}
		}
	},
	ieFixNeed:function() {
		var uAgent = navigator.userAgent;
		var bVer = 0;
		var status = false;
		if ((uAgent.indexOf("MSIE") != -1) && (uAgent.indexOf("Opera") == -1)) {
			bVer = parseFloat(uAgent.substring(uAgent.indexOf("MSIE")+5));
			if (bVer < 7) status = true;
		}
		return status;
	}
}


var guiImageButtons = {
	build:function() {
		var objId = document.getElementsByTagName("div");
		for (inx = 0; inx < objId.length; inx++) {
			if (objId[inx].className == "iButton") {
				var iData = {
					action:objId[inx].getAttribute("action"),
					margin:objId[inx].getAttribute("margin"),
					align:objId[inx].getAttribute("align"),
					image:objId[inx].getAttribute("image"),
					size:objId[inx].getAttribute("size")
				};
				objId[inx].removeAttribute("action");
				objId[inx].removeAttribute("margin");
				objId[inx].removeAttribute("align");
				objId[inx].removeAttribute("image");
				objId[inx].removeAttribute("size");
				objId[inx].removeAttribute("class");
				objId[inx].removeAttribute("className");
				objId[inx].style.border = "none";
				if (this._ieBrowser()) objId[inx].style.styleFloat = "left";
				else objId[inx].style.cssFloat = "left";
				if (iData.size != null && iData.size != "") {
					var iSize = iData.size.split("|");
					if (iSize.length == 2) {
						objId[inx].style.height = parseInt(iSize[1]) + "px";
						objId[inx].style.width = parseInt(iSize[0]) + "px";
					}
				}
				if (iData.image != null && iData.image != "" && this._ieFixCheck()) {
					objId[inx].style.background = "none";
					objId[inx].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iData.image + "', sizingMethod='crop')";
				} else {
					objId[inx].style.background = "url(" + iData.image + ") 0px 0px no-repeat";
				}
				if (iData.margin != null && iData.margin != "") objId[inx].style.margin = iData.margin;
				if (iData.align == null || iData.align == "") iData.align = "left";
				switch (iData.align.toUpperCase()) {
					case "LEFT":
						if (this._ieBrowser()) objId[inx].style.styleFloat = "left";
						else objId[inx].style.cssFloat = "left";
						break;
					case "RIGHT":
						if (this._ieBrowser()) objId[inx].style.styleFloat = "right";
						else objId[inx].style.cssFloat = "right";
						break;
					case "CENTER":
						if (this._ieBrowser()) objId[inx].style.styleFloat = "none";
						else objId[inx].style.cssFloat = "none";
						objId[inx].style.marginRight = objId[inx].style.marginLeft = "auto";
				}
				if (iData.action != null && iData.action != "") {
					objId[inx].lnkAction = iData.action;
					objId[inx].style.cursor = "pointer";
					objId[inx].doClick = this._itemClickAction;
					objId[inx].onclick = function() {
						this.doClick(this);
					}
				} else {
					objId[inx].style.cursor = "default";
				}
			}
		}
	},
	_itemClickAction:function(objId) {
		var iLink = objId.lnkAction.split("|");
		if (iLink.length > 1) {
			switch (iLink[0].toUpperCase()) {
				case "FRAME":
					if (iLink[2] != undefined) {
						if (iLink[1] != "") document.frames[iLink[2]].location.href = iLink[1];
					} else {
						if (iLink[1] != "") document.frames[0].location.href = iLink[1];
					}
					break;
				case "FORM":
					if (iLink[2] != undefined) {
						if (iLink[1].toUpperCase() == "SUBMIT") {
							if ((document.forms[iLink[2]].onsubmit == null) || (document.forms[iLink[2]].onsubmit == undefined)) {
								document.forms[iLink[2]].submit();
							} else {
								if (document.forms[iLink[2]].onsubmit()) document.forms[iLink[2]].submit();
							}
						}
						if (iLink[1].toUpperCase() == "RESET") document.forms[iLink[2]].reset();
					} else {
						if (iLink[1].toUpperCase() == "SUBMIT") {
							if ((document.forms[0].onsubmit == null) || (document.forms[0].onsubmit == undefined)) {
								document.forms[0].submit();
							} else {
								if (document.forms[0].onsubmit()) document.forms[0].submit();
							}
						}
						if (iLink[1].toUpperCase() == "RESET") document.forms[0].reset();
					}
					break;
				case "URL":
					if (iLink[2] != undefined) {
						if (iLink[1] != "") iLink[2].location = iLink[1];
					} else {
						if (iLink[1] != "") window.location = iLink[1];
					}
					break;
				case "JS":
					if (iLink[1] != "") eval(iLink[1]);
					break;
			}
		}
	},
	_ieBrowser:function() {
		var uAgent = navigator.userAgent;
		var status = false;
		if ((uAgent.indexOf("MSIE") != -1) && (uAgent.indexOf("Opera") == -1)) status = true;
		return status;
	},
	_ieFixCheck:function() {
		var uAgent = navigator.userAgent;
		var bVer = 0;
		var status = false;
		if ((uAgent.indexOf("MSIE") != -1) && (uAgent.indexOf("Opera") == -1)) {
			bVer = parseFloat(uAgent.substring(uAgent.indexOf("MSIE")+5));
			if (bVer < 7) status = true;
		}
		return status;
	}
}


function setTitleLabel(idCode) {
	var titleId = document.getElementById("guiTitleBox");
	var titleText = ["CROATIAKNJIGA d.o.o.",
									 "&bull; NOVOSTI &bull;",
									 "&bull; O NAMA &bull;",
									 "&bull; DOMAĆA IZDANJA &bull;",
									 "&bull; STRANA IZDANJA &bull;",
									 "&bull; TRAŽILICA &bull;",
									 "&bull; KONTAKT &bull;",
									 "&bull; KOŠARICA &bull;",
									 "&bull; NARUDŽBENICA &bull;",
									 "&bull; BRZA PRETRAGA &bull;",
									 "&bull; INFORMACIJE O KNJIZI &bull;",
									 "&bull; PONUDA &bull;",
									 "&bull; PONUDA &bull; GRADITELJSTVO &bull;",
									 "&bull; PONUDA &bull; KONSTRUKCIJE &bull;",
									 "&bull; PONUDA &bull; GRAĐEVINSKI MATERIJALI &bull;",
									 "&bull; PONUDA &bull; NISKOGRADNJA &bull;",
									 "&bull; PONUDA &bull; HIDROGRADNJA &bull;",
									 "&bull; PONUDA &bull; KOMUNALIJE &bull;",
									 "&bull; PONUDA &bull; IZVOĐENJE RADOVA &bull;",
									 "&bull; PONUDA &bull; GEODEZIJA &bull;",
									 "&bull; PONUDA &bull; ARHITEKTURA &bull;",
									 "&bull; PONUDA &bull; STROJARSTVO &bull;",
									 "&bull; PONUDA &bull; TOPLINARSTVO &bull;",
									 "&bull; PONUDA &bull; ELEKTROENERGETIKA &bull;",
									 "&bull; PONUDA &bull; ELEKTROTEHNIKA &bull;",
									 "&bull; PONUDA &bull; TELEKOMUNIKACIJE &bull;",
									 "&bull; PONUDA &bull; FIZIKA &bull;",
									 "&bull; PONUDA &bull; TEHNOLOGIJA &bull;",
									 "&bull; PONUDA &bull; MATEMATIKA &bull;",
									 "&bull; PONUDA &bull; RJEČNICI &bull;",
									 "&bull; PONUDA &bull; IZDANJA CROATIAKNJIGE &bull;",
									 "&bull; PONUDA &bull; OSTALO &bull;"];
	if ((typeof(titleId) == "object") && (titleId != undefined) && (titleId.tagName.toLowerCase() == "div")) {
		if (idCode != undefined) {
			idCode = parseInt(idCode);
			if ((!isNaN(idCode)) && (idCode > 0) && (idCode < titleText.length)) {
				titleId.innerHTML = "<p>" + titleText[idCode] + "</p>";
			} else {
				titleId.innerHTML = "<p>" + titleText[0] + "</p>";
			}
		}
	}
}


function urlTextEncode(iText) {
	var urlText = "";
	if (iText != undefined && iText != "") {
		for (var inx = 0; inx < iText.length; inx++) {
			var tCode = encodeURI(iText.charAt(inx));
			switch (tCode.toUpperCase()) {
				case "%C5%A0":
					urlText += "%A9";
					break;
				case "%C4%90":
					urlText += "%D0";
					break;
				case "%C4%8C":
					urlText += "%C8";
					break;
				case "%C4%86":
					urlText += "%C6";
					break;
				case "%C5%BD":
					urlText += "%AE";
					break;
				case "%C5%A1":
					urlText += "%B9";
					break;
				case "%C4%91":
					urlText += "%F0";
					break;
				case "%C4%8D":
					urlText += "%E8";
					break;
				case "%C4%87":
					urlText += "%E6";
					break;
				case "%C5%BE":
					urlText += "%BE";
					break;
				default:
					urlText += tCode;
					break;
			}
		}
	}
	return urlText;
}


function urlRandEncoded(urlPath) {
	var dateObj = new Date();
	var urlId = urlPath.split("?", 2);
	if (urlId.length == 2) urlId[1] = "?id=" + dateObj.getTime() + "&" + urlId[1]
	else urlId[1] = "?id=" + dateObj.getTime();
	return urlId[0] + urlId[1];
}


function setUpdateContent() {
	var aModul = aSiteContent.ajaxModul;
	var conId = document.getElementById("guiContentBox");
	var lbrId = document.getElementById("guiLoaderBar");
	if (aModul.readyState == 4 && (aModul.status == 200 || window.location.href.indexOf("http") == -1)) {
		conId.innerHTML = aModul.responseText;
		guiFormElements.inputBox();
		guiImageButtons.build();
		conId.style.visibility = "visible";
		lbrId.style.background = "none";
		aSiteBanner.get(urlRandEncoded("infobanner.php"), setBannerContent);
		aSiteBasket.get(urlRandEncoded("infobasket.php"), setBasketContent);
	}
}


function setInfoboxContent() {
	var aModul = aSiteContent.ajaxModul;
	if (aModul.readyState == 4 && (aModul.status == 200 || window.location.href.indexOf("http") == -1)) {
		var conId = guiModalWindow.cid;
		if (conId != undefined && conId != null) {
			conId.innerHTML = aModul.responseText;
			guiImageButtons.build();
			guiModalWindow.show();
		}
	}
}


function setBannerContent() {
	var aModul = aSiteBanner.ajaxModul;
	if (aModul.readyState == 4 && (aModul.status == 200 || window.location.href.indexOf("http") == -1)) {
		var conId = document.getElementById("guiSpecialOffer");
		conId.innerHTML = aModul.responseText;
	}
}


function setBasketContent() {
	var aModul = aSiteBasket.ajaxModul;
	if (aModul.readyState == 4 && (aModul.status == 200 || window.location.href.indexOf("http") == -1)) {
		var conId = document.getElementById("guiBasketPrice");
		conId.innerHTML = aModul.responseText;
	}
}


function updateContent(urlPath) {
	if (urlPath != undefined && urlPath != "") {
		var lbrId = document.getElementById("guiLoaderBar");
		lbrId.style.background = "url(img/img_loadingbar.gif) 0px 0px no-repeat";
		siteHistory.url = urlPath;
		aSiteContent.get(urlRandEncoded(urlPath), setUpdateContent);
	}
}


function jumpNavigation(urlPath, idCode) {
	if (urlPath != undefined && urlPath != "" && idCode != undefined && !isNaN(idCode)) {
		var conId = document.getElementById("guiContentBox");
		var lbrId = document.getElementById("guiLoaderBar");
		lbrId.style.background = "url(img/img_loadingbar.gif) 0px 0px no-repeat";
		conId.style.visibility = "hidden";
		conId.style.background = "none";
		conId.style.overflow = "hidden";
		conId.innerHTML = "";
		switch (idCode) {
			case 1:
			case 2:
				conId.style.overflow = "auto";
				break;
			case 6:
				conId.style.background = "url(img/img_mapalokacije.jpg) 0px 0px no-repeat"
				break;
			case 10:
				conId.style.background = "url(img/img_bookview.png) 0px 0px no-repeat";
				break;
		}
		if (idCode == 100) {
			setTitleLabel(siteHistory.id);
			aSiteContent.get(urlRandEncoded(siteHistory.url), setUpdateContent);
		} else {
			if (idCode != 7 && idCode != 10) {
				siteHistory.id = idCode;
				siteHistory.url = urlPath;
			}
			setTitleLabel(idCode);
			aSiteContent.get(urlRandEncoded(urlPath), setUpdateContent);
		}
	}
}


function showInfoMessage(mType, mText) {
	if (mType != undefined && mText != undefined) {
		var urlPath = "infomessage.php?mid=" + parseInt(mType) + "&mtx=" + urlTextEncode(mText);
		guiModalWindow.open();
		aSiteContent.get(urlRandEncoded(urlPath), setInfoboxContent);
	}
}


function showBookContent(urlPath) {
	var wParm = null;
	var winId = null;
	var xpos = (screen.width - 920)/2;
	var ypos = (screen.height - 640)/2;
	window.blur();
	wParm = "scrollbars=no, menubar=no, resizable=yes, toolbar=no, location=no, status=no, width=920px, height=640px";
	wParm += ", top=" + ypos + "px, left=" + xpos + "px";
	winId = window.open(urlPath, "InfoBox", wParm);
	winId.onfocus = function() {
		winId.document.title = "CROATIAKNJIGA d.o.o.";
	}
	winId.focus();
	window.onunload = function() {
		winId.close();
	}
}


function doFastSearch() {
	var frmId = document.forms["frmFastSearch"];
	var sText = "";
	if (typeof(frmId) == "object") sText = frmId["ibxText"].value;
	if (sText.length > 3) {
		jumpNavigation("ponuda.php?log=1&tip=1&page=1&stx=" + urlTextEncode(sText), 9);
	} else {
		if (sText.length == 0) showInfoMessage(2, "Za korištenje brze pretrage potrebno je upisati traženi pojam!");
		else showInfoMessage(2, "Vaš traženi pojam mora sadržavati minimum 4 ili više slova!");
	}
	return false;
}


function doFullSearch() {
	var frmId = document.forms["frmFullSearch"];
	var urlPath = "";
	var sTxt = "";
	var sTip = 0;
	var sPod = 0;
	if (typeof(frmId) == "object") {
		sTxt = frmId["ibxText"].value;
		sTip = parseInt(frmId["sbxTip"].value);
		sPod = parseInt(frmId["sbxPod"].value);
	}
	if (sTxt.length > 3) {
		if (sTip > 0) {
			switch (sTip) {
				case 1:
					if (sPod > 0) {
						urlPath = "tip=5&pod=" + sPod + "&page=1&stx=" + urlTextEncode(sTxt);
					} else {
						urlPath = "tip=4&page=1&stx=" + urlTextEncode(sTxt);
					}
					break;
				case 2:
					if (sPod > 0) {
						urlPath = "tip=7&pod=" + sPod + "&page=1&stx=" + urlTextEncode(sTxt);
					} else {
						urlPath = "tip=6&page=1&stx=" + urlTextEncode(sTxt);
					}
					break;
				case 3:
					if (sPod > 0) {
						urlPath = "tip=9&pod=" + sPod + "&page=1&stx=" + urlTextEncode(sTxt);
					} else {
						urlPath = "tip=8&page=1&stx=" + urlTextEncode(sTxt);
					}
					break;
			}
		} else {
			if (sPod > 0) {
				urlPath = "tip=2&pod=" + sPod + "&page=1&stx=" + urlTextEncode(sTxt);
			} else {
				urlPath = "tip=1&page=1&stx=" + urlTextEncode(sTxt);
			}
		}
		jumpNavigation("ponuda.php?log=1&" + urlPath, 5);
	} else {
		if (sTxt.length == 0) showInfoMessage(2, "Za korištenje tražilice potrebno je upisati traženi pojam!");
		else showInfoMessage(2, "Vaš traženi pojam mora sadržavati minimum 4 ili više slova!");
	}
	return false;
}


function doBookOrder() {
	var frmId = document.forms["frmOrder"];
	var frmError = "";
	var urlParm = "";
	if (frmError.length == 0 && frmId["ibxNarucitelj"].value.length == 0) frmError = "Niste ništa upisali u polje [Naručitelj], to polje je obavezno!";
	if (frmError.length == 0 && frmId["ibxAdresa"].value.length == 0) frmError = "Niste ništa upisali u polje [Adresa], to polje je obavezno!";
	if (frmError.length == 0 && frmId["ibxGrad"].value.length == 0) frmError = "Niste ništa upisali u polje [Grad], to polje je obavezno!";
	if (frmError.length == 0 && frmId["ibxPosBroj"].value.length == 0) frmError = "Niste ništa upisali u polje [Poštanski broj], to polje je obavezno!";
	if (frmError.length == 0 && frmId["ibxOsoba"].value.length == 0) frmError = "Niste ništa upisali u polje [Kontakt osoba], to polje je obavezno!";
	if (frmError.length == 0 && frmId["ibxMatBroj"].value.length == 0) frmError = "Niste ništa upisali u polje [Matični broj], to polje je obavezno!";
	if (frmError.length == 0 && frmId["ibxTelBroj"].value.length == 0) frmError = "Niste ništa upisali u polje [Telefon], to polje je obavezno!";
	if (frmError.length == 0 && frmId["ibxEmail"].value.length == 0) frmError = "Niste ništa upisali u polje [EMAIL], to polje je obavezno!";
	if (frmError.length > 0) {
		showInfoMessage(2, frmError);
	} else {
		var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (!filter.test(frmId["ibxEmail"].value)) {
			showInfoMessage(2, "Upisana email adresa je neispravna!");
		} else {
			urlParm += "txnru=" + urlTextEncode(frmId["ibxNarucitelj"].value);
			urlParm += "&txadr=" + urlTextEncode(frmId["ibxAdresa"].value);
			urlParm += "&txgrd=" + urlTextEncode(frmId["ibxGrad"].value);
			urlParm += "&txpbr=" + urlTextEncode(frmId["ibxPosBroj"].value);
			urlParm += "&txoso=" + urlTextEncode(frmId["ibxOsoba"].value);
			urlParm += "&txmbr=" + urlTextEncode(frmId["ibxMatBroj"].value);
			urlParm += "&txtbr=" + urlTextEncode(frmId["ibxTelBroj"].value);
			urlParm += "&txfbr=" + urlTextEncode(frmId["ibxFaxBroj"].value);
			urlParm += "&txeml=" + urlTextEncode(frmId["ibxEmail"].value);
			var lbrId = document.getElementById("guiLoaderBar");
			lbrId.style.background = "url(img/img_loadingbar.gif) 0px 0px no-repeat";
			aSiteContent.post(urlRandEncoded("ordersend.php"), urlParm, setUpdateContent);
		}
	}
}
