﻿var C_MenuAtLevel = new Array();

var delay = 300;
var t = -1;
function C(obj, level) {
	clearTimeout(t);
	if (C_MenuAtLevel[0] == null) {
		C_Menu_Show(obj, level);
	} else {
		t = setTimeout(function() {C_Menu_Show(obj, level);}, delay);
	}
}
function C2(level) {
	clearTimeout(t);
	t = setTimeout(function() {C_Menu_Hide(level);}, delay);
}

function C_Menu_Show(obj, level) {
	var objMenu = document.getElementById("C_Menu_" + obj.id.substr(2));
	
	if (C_MenuAtLevel[level] != null) C_Menu_Hide(level);
	
	var pRef = Phizz.Dom.GetAbsolutePosition(obj);
	objMenu.style.left = (pRef.x + obj.offsetWidth + 1) + "px";
	objMenu.style.top = (pRef.y - 2) + "px";
	objMenu.style.visibility = "visible";
	
	C_MenuAtLevel[level] = objMenu;
}

function C_Menu_Hide(level) {
	if (C_MenuAtLevel[level + 1] != null) C_Menu_Hide(level + 1);
	if (C_MenuAtLevel[level] != null) {
		var objMenu = C_MenuAtLevel[level];
		objMenu.style.left = "-1000px";
		objMenu.style.top = "-1000px";
		objMenu.style.visibility = "hidden";
		C_MenuAtLevel[level] = null;
	}
}

function C_Menu_HideAll() {
	C_Menu_Hide(0);
}

function Navigation_OnClick(label) {
	var url = "";
	switch (label) {
		case "Home" : url = "/"; break;
		case "Shop" : url = "/shop/"; break;
		case "About" : url = "/about-us/"; break;
		case "News" : url = "/news-and-events/"; break;
		case "Awards" : url = "/awards-and-reviews/"; break;
		case "Contact" : url = "/contact/"; break;
		case "Links" : url = "/links/"; break;
	}
	if (url.length > 0) window.location.href = url;
}

function P_Hover(obj) {
	obj.style.backgroundColor = "#e7f3f8";
}

function P_Unhover(obj) {
	obj.style.backgroundColor = "Transparent";
}

function P_Click(obj, id) {
	obj.style.backgroundColor = "Transparent";
	window.location.href = "/Product.aspx?ID=" + id;
}

function txtKeywords_OF(obj) {
	if (obj.value == "Enter Keywords") {
		obj.value = "";
		obj.style.color = "#000000";
	}
}

function txtKeywords_OB(obj) {
	if (obj.value == "") {
		obj.value = "Enter Keywords";
		obj.style.color = "#9a9b9c";
	}
}

function txtReference_OF(obj) {
	if (obj.value == "Enter Product Reference") {
		obj.value = "";
		obj.style.color = "#000000";
	}
}

function txtReference_OB(obj) {
	if (obj.value == "") {
		obj.value = "Enter Product Reference";
		obj.style.color = "#9a9b9c";
	}
}

function ProductOption_OnChange(option) {
	var basePrice = parseFloat(Phizz.Dom.ByID("BasePrice").value);
	var priceMultiplication = parseFloat(Phizz.Dom.ByID("PriceMultiplication").value);
	var adjustment = 0;
	var objsSelect = document.getElementsByTagName("SELECT");
	for (var i = 0; i < objsSelect.length; i++) {
		if (objsSelect[i].className == "P_Option") {
			var option = objsSelect[i].value;
			if (option.lastIndexOf("$") > -1) adjustment += parseFloat(option.substr(option.lastIndexOf("$") + 1));
		}
	}
	if (Phizz.Dom.ByID("OldPrice") != null) Phizz.Dom.ByID("OldPrice").innerHTML = FormatPrice((basePrice + adjustment));
	Phizz.Dom.ByID("Price").innerHTML = FormatPrice((basePrice + adjustment) * priceMultiplication);
}

function PopupImage(title, path, width, height) {
	var preferredWidth = width + 20;
	var preferredHeight = height + 20;
	Phizz.Navigation.OpenWindow("/_ViewImage.ashx?title=" + encodeURIComponent(title) + "&path=" + encodeURIComponent(path) + "&width=" + width + "&height=" + height, preferredWidth, preferredHeight);
}

function FormatPrice(nPrice) {
	nPrice = Math.round(nPrice * 100) * 0.01;
	var price = String(nPrice);
	if (price.indexOf(".") == -1) {
		price += ".00";
	} else if (price.indexOf(".") == price.length - 2) {
		price += "0";
	} else if (price.indexOf(".") < price.length - 3) {
		price = price.substring(0, price.indexOf(".") + 3);
	}
	return price;
}

function AddProduct(target) {
	var product = Phizz.Dom.ByID("ID").value;

	var options = "";
	var objOptions = Phizz.Dom.ByName("Option");
	for (var i = 0; i < objOptions.length; i++) {
		if (objOptions[i].selectedIndex == 0) {
			alert ("Please choose a " + objOptions[i].title);
			objOptions[i].focus();
			return;
		}
		if (i > 0) options += "|";
		options += objOptions[i].title + "$" + objOptions[i].value;
	}
	
	var quantity = Phizz.Dom.ByID("Quantity").value;
	if (!(/^[0-9]+$/).test(quantity)) {
		alert ("Please enter a valid quantity.");
		Phizz.Dom.ByID("Quantity").focus();
		return;
	}
	
	if (target == "Basket") {
		window.location.href = "/BasketHandler.ashx?a=" + product + "&q=" + quantity + "&o=" + encodeURIComponent(options);
		//Go("Basket.aspx?Action=Add&Product=" + product + "&Price=" + price + "&Options=" + encodeURIComponent(options) + "&Quantity=" + quantity);
	} else if (target == "WishList") {
		window.location.href = "/WishListHandler.ashx?a=" + product + "&q=" + quantity + "&o=" + encodeURIComponent(options);
		//Go("WishList.aspx?Action=Add&Product=" + product + "&Price=" + price + "&Options=" + encodeURIComponent(options) + "&Quantity=" + quantity);
	}
}

function AddListProduct(product, hasOptions, target) {
    if (hasOptions) {
        if (confirm("This product is available in one or more variations. Press OK below to visit the Product Detail page where you can choose your options before adding it to your " + (target == "Basket" ? "Basket" : "Wish List") + ".")) {
            window.location.href = "/Product.aspx?ID=" + product;
        }
    } else {
	    var quantity = 1;
    	
	    if (target == "Basket") {
	        window.location.href = "/BasketHandler.ashx?a=" + product + "&q=" + quantity + "&o=";
	    } else if (target == "WishList") {
	        window.location.href = "/WishListHandler.ashx?a=" + product + "&q=" + quantity + "&o=";
	    }
    }
}

function EmailProduct(id) {
	Phizz.Navigation.OpenWindow("/EmailProduct.aspx?ID=" + id, 460, 270, "winEmail", false);
}

function OnCheckout() {
	if (Phizz.Dom.ByID("txtGiftMessage") != null) {
		var message = Phizz.Dom.ByID("txtGiftMessage").value;
		if (message != null && message.length > 0) {
			Phizz.Dom.ByID("hidGiftMessageName").disabled = false;
			Phizz.Dom.ByID("hidGiftMessageQuantity").disabled = false;
			Phizz.Dom.ByID("hidGiftMessageAmount").disabled = false;
			Phizz.Dom.ByID("hidGiftMessageOn0").disabled = false;
			Phizz.Dom.ByID("hidGiftMessageOs0").disabled = false;
			Phizz.Dom.ByID("hidGiftMessageOs0").value = message;
		}
	}

	// send referral info via ajax
	/*var referrer = Phizz.Dom.ByID("selReferrer").value;
	if (referrer.length > 0) {
	    try {
	        var ajax = new Phizz.Ajax.Request("Referrer.ashx", DoNothing);
	        ajax.AddParam("Referrer", referrer);
	        if (referrer == "Other") ajax.AddParam("AdditionalInfo", Phizz.Dom.ByID("txtReferrerAdditionalInfo").value);
	        ajax.Send();
	    } catch (e) { }
	}*/

	return true;
}

function DoNothing() { }