var popupTimeout = 3000;

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) {
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

function getQueryValues(url, exclude) {
	var args = new Object();
	if (exclude==undefined) exclude = "";
	if ((url==undefined) || (url=="")) {
		var query = location.search.substring(1);
		var rurl = "";
	} else {
		var q = url.split("?");
		var rurl = q[0];
		(q.length>1) ? query = q[q.length-1] : query = "";
	}
	var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		if (exclude!=argname) {
			var value = pairs[i].substring(pos+1);
			args[argname] = unescape(value);
		}
	}
	if (rurl!="") {
		var qs = "";
		for (var i in args)	{
			if (qs.length>0) qs = qs + '&';
			qs = qs + i+"="+args[i];
		}
		if (qs.length>0) qs = "?"+qs;
		return rurl+qs;
	} else {
		return args;
	}
}

function getElementLeft(elem) {
	xPos = elem.offsetLeft;
	tempEl = elem.offsetParent;
	while (tempEl != null) {
		xPos += tempEl.offsetLeft;
  		tempEl = tempEl.offsetParent;
	}
	return xPos;
}

function showCartPopup() {
	document.getElementById("popup_cart").style.left = (getElementLeft(document.getElementById("cartimage"))+8)+"px";
	document.getElementById("popup_cart").style.visibility = 'visible';
	document.getElementById("popup_cart").style.display = 'block';
	document.getElementById("popup_cart").style.zindex = 6;
	setTimeout("hideCartPopup()", popupTimeout);
}

function hideCartPopup() {
	document.getElementById("popup_cart").style.visibility = 'hidden';
	document.getElementById("popup_cart").style.display = 'none';
}

function showWishlistPopup() {
	document.getElementById("popup_wishlist").style.left = (getElementLeft(document.getElementById("wishlistimage"))+8)+"px";
	document.getElementById("popup_wishlist").style.visibility = 'visible';
	document.getElementById("popup_wishlist").style.display = 'block';
	setTimeout("hideWishlistPopup()", popupTimeout);
}

function hideWishlistPopup() {
	document.getElementById("popup_wishlist").style.visibility = 'hidden';
	document.getElementById("popup_wishlist").style.display = 'none';
}

function addToCart(productid, amount, variations) {
	if (variations>1) {
		loadXMLDoc(baseurl+'/product.php?requestedcontentid=main&product='+productid);
	} else {	
		loadXMLDoc(currentpageurl+'?requestedcontentid=top2.popup_cart&mode=add&productid='+productid+'&amount='+amount, true, showCartPopup);
	} 
}

function addToCart2(productid, amount) {
	var e = document.getElementById("sizeselector");
	if ((e != 'undefined') && (e != null)) {
		loadXMLDoc(currentpageurl+'?requestedcontentid=top2.popup_cart&mode=add&productid='+e.value+'&amount='+amount, true, showCartPopup);
	} else {
		addToCart(productid, amount);
	}
}

function removeFromCart(productid, amount) {
	loadXMLDoc(currentpageurl+'?requestedcontentid=top2.main&mode=remove&productid='+productid+'&amount='+amount);
}

function increaseCartAmount(productid) {
	loadXMLDoc(currentpageurl+'?requestedcontentid=top2.main&mode=increase&productid='+productid);
}

function decreaseCartAmount(productid) {
	loadXMLDoc(currentpageurl+'?requestedcontentid=top2.main&mode=decrease&productid='+productid);
}

function addToWishList(productid) {
	loadXMLDoc(currentpageurl+'?requestedcontentid=top1.popup_wishlist&mode=addtowishlist&productid='+productid, true, showWishlistPopup);
}

function addToWishList2(productid) {
	var e = document.getElementById("sizeselector");
	if ((e != 'undefined') && (e != null)) {
		loadXMLDoc(currentpageurl+'?requestedcontentid=top1.popup_wishlist&mode=addtowishlist&productid='+e.value, true, showWishlistPopup);
	} else {
		addToWishList(productid);
	}
}

function removeFromWishList(productid) {
	loadXMLDoc(currentpageurl+'?requestedcontentid=top1.top2.main&mode=removefromwishlist&productid='+productid);
}

function removeAllFromWishList() {
	loadXMLDoc(currentpageurl+'?requestedcontentid=top1.top2.main&mode=removeallfromwishlist');
}

function toggleWishListNotify(productid) {
	loadXMLDoc(currentpageurl+'?requestedcontentid=top1.main&mode=togglenotify&productid='+productid);
}

function moveAllFromWishListToCart() {
	loadXMLDoc(currentpageurl+'?requestedcontentid=top2.popup_cart&mode=addallfromwishlist', false);
	removeAllFromWishList();
}

function moveFromWishListToCart(productid) {
	loadXMLDoc(currentpageurl+'?requestedcontentid=top2.popup_cart&mode=add&productid='+productid+'&amount=1', false, showCartPopup);
	removeFromWishList(productid);
}

function handleSPE(event) {
	var e = document.elementFromPoint(event.clientX, event.clientY);
	enableproductlink = (e.name!='cartbutton');
}