var dynamicPath = "/webapp/wcs/stores/servlet/";

function getXmlHttpObject() {
	try {
		return new XMLHttpRequest();
	} catch (e) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			return new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return null;
}

function setCartCount(storeId, catalogId, languageId) {
	var xmlHttp = getXmlHttpObject();

	if (xmlHttp) {
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				var cartCountElement = document.getElementById("cartCount");

				if (cartCountElement) {
					var cartCount = xmlHttp.responseText;

					if (cartCount != null && cartCount.indexOf("ajaxCartCount") > -1)
						cartCountElement.innerHTML = cartCount;
				}
			}
		}

		xmlHttp.open("GET", dynamicPath + "SiteLogicView?storeId=" + storeId + "&catalogId=" + catalogId + "&langId=" + languageId + "&action=getCartCount", true);
		xmlHttp.send(null);
	}
}

function newWindow(url, menubar, scrollbars, resizable, toolbar, locationEntryField, width, height) {
	var top = 0;
	var left = 0;

	if (screen.height >= height)
		top = (screen.height - height) / 2;
	if (screen.width >= width)
		left = (screen.width - width) / 2;

	if (url.indexOf("/") != 0 && url.indexOf("http://") != 0 && url.indexOf("https://") != 0)
		url = (defaultHost ? "http://" + defaultHost : "") + dynamicPath + url;

	newPopUp=window.open(url, "_blank", 'directories=no,status=no,copyhistory=yes,menubar=' + menubar + ',scrollbars=' + scrollbars + ',resizable=' + resizable + ',toolbar=' + toolbar + ',location=' + locationEntryField + ',width=' + width + ',height=' + height + ',top=' + top + ',left=' + left);
	newPopUp.focus();
}

function getValidatedBCT(uri, bct) {
	if (uri != null && uri != "" && bct != null && bct != "") {
		var newBCT = (uri.indexOf("?") == -1 ? "?" + (bct.charAt(0) == '&' ? bct.substring(1, bct.length) : bct) : bct);

		if (uri.lastIndexOf((newBCT.charAt(0) == '?' || newBCT.charAt(0) == '&' ? newBCT.substring(1, newBCT.length) : newBCT)) == -1)
			return newBCT;
	}

	return "";
}

function anchorJump(newAnchor) {
	var urlForAnchor = location.href;
	var indexOfPound = urlForAnchor.indexOf("#");

	if (indexOfPound > -1)
		urlForAnchor = urlForAnchor.substring(0, indexOfPound);

	location.href = urlForAnchor + "#" + newAnchor;
}
