﻿function formatPrice(value, digits) {
	var price = "";
	var number = formatNumber(value, digits).split(".", 2);
	var len = number[0].length;
	for (var i = 0; i <= len; i++)
		price = (i > 0 && i < len && i % 3 == 0 ? "," : "") + number[0].charAt(len - i) + price;
	return "$" + price + "." + number[1];
}

function addFavorites(elm) {
	var url = document.location.href;
	var title = document.title;
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if (window.external) {
		window.external.AddFavorite(url, title);
	} else if (window.opera && window.print) {
		elm.setAttribute("rel", "sidebar");
		elm.setAttribute("href", url);
		elm.setAttribute("title", title);
	} else {
		alert("Your browser doesn't allow auto bookmarking\nPlease bookmark this page manually");
	}
}

function addBookmark(site, keyurl, keytitle) {
	var url = "http://" + site + (site.indexOf("?") == -1 ? "?" : "&");
	url += keyurl + "=" + encodeURIComponent(document.location.href) + "&" + keytitle + "=" + encodeURIComponent(document.title);
	win = window.open();
	win.document.location.href = url;
	return false;
}

function mailingSubscribe(form, container, url) {
	form = $(form);
	$(container).attr('class', 'icon icon-loader gray').html('<small>Processing, please wait...</small>');
	$.ajax({
		type: "GET",
		url: url,
		data: form.serialize(),
		dataType: "json",
		cache: false,
		error: function(request) { this.success({ success: false, text: "Unknown error, try again" }); },
		success: function(data) {
			$(container)
				.attr('class', data.success ? "icon icon-check green" : "icon icon-error red")
				.html("<small>" + data.text + "</small>");
		}
	});
	return false;
}

// Account Windows
function openForgotPassword() { openPopupWindow(getForgotPasswordUrl(), "ForgotPassword", 400, 300); }

// Cart & Checkout Windows
function openCookiesHelp() { openPopupWindow(getCookiesHelpUrl(), "CookiesHelp", 500, 600); }
function openSecurityCode() { openPopupWindow(getSecurityCodeUrl(), "SecurityCode", 500, 600); }

// Product Windows
function openRebate(id) { openPopupWindow(getRebateUrl(id), "Rebate", 700, 750); }
function openShipCalc(id) { openPopupWindow(getShippingCalcUrl(id), "ShipCalc", 500, 600); }
function openNotifier(id) { openPopupWindow(getNotifierUrl(id), "Notifier", 450, 400); }
function openRecommend(id) { openPopupWindow(getRecommendUrl(id), "Recommend", 400, 400); }
function openImageGallery(id, image) { openPopupWindow(getImageGalleryUrl(id, image), "ImageGallery", 700, 600); }
function openProductsCompare(id) { openPopupWindow(getProductsCompareUrl(id), "ProductCompare", 700, 750); }
