﻿//
// ProductDetails.js
//

// Author: Colin Jaggs
// Date: 28th January 2008
// Description: Seprated JS functions from product details page

var productsLoaded = ","; 				// list of product id's that have been loaded
var productHTML = new Array(); 		// cache for product info
var floatingInfoMaxX = 460; 			// maximum x position of floating layer
var bundlePrice = 0;
var bundlePrice = 0;
var optGrpPrice = 0;

// add floating layer so mouse movements will apply to it
floatingLayers[floatingLayers.length ++] = "floatingInfo";

function updatePrice(qtySource)
{
	if (!qtySource) return;
	
	// copy over quantity from one box to the other
	if (qtySource.id.indexOf("txtQuantity2") > -1 && objectById(qtySource.id.replace("txtQuantity2", "txtQuantity"))) objectById(qtySource.id.replace("txtQuantity2", "txtQuantity")).value = qtySource.value;
	else if (objectById(qtySource.id.replace("txtQuantity", "txtQuantity2"))) objectById(qtySource.id.replace("txtQuantity", "txtQuantity2")).value = qtySource.value;

	// calculate total price
	var totalPrice = 0;
	var quantity = qtySource.value;
	//if (quantity > currentStock) qtySource.value = currentStock;
	if ((isNaN(qtySource.value)) || qtySource.value.replace(/ /g, "") == "") totalPrice = (bundlePrice + optGrpPrice).toFixed(2);
	else totalPrice = ((bundlePrice + optGrpPrice) * qtySource.value).toFixed(2);

	// store the totals in the hidden fields and display on screen
	if (objectById("layerPrice")) objectById("layerPrice").innerHTML = asCurrency(totalPrice);
	if (objectById("layerPrice2")) objectById("layerPrice2").innerHTML = asCurrency(totalPrice);
	txtBundlePrice.value = Math.round(totalPrice * 100) / 100;
}

function toggleBundleItem(productId, productType, price)
{
	// if option is already added, remove it and deduct the price
	if (txtBundleItems.value.indexOf("," + productId + "|" + productType + ",") > - 1)
	{
		txtBundleItems.value = txtBundleItems.value.replace(productId + "|" + productType + ",", "");
		bundlePrice -= price;
	}
	
	// otherwise add the option and add the price
	else
	{
		txtBundleItems.value += productId + "|" + productType + ",";
		bundlePrice += price;
	}
	
	// update the total price field
	updatePrice(txtMainQty);
}

function toggleProductInfo(productId, state)
{
	if (objectById("floatingInfo"))
	{
		if (state == 1)
		{
			xOffset = yOffset = 10;
			objectById("floatingInfo").style.display = "inline";
			if (productsLoaded.indexOf("," + productId + ",") == -1)
			{
				objectById("floatingInfo").innerHTML = "<div class=\"loading\"><img src=\"" + FolderRoot + "Images/Loading.gif\" alt=\"Loading...\" />loading ...</div>";
				xmlHttp = GetXmlHttpObject(showProductInfo);
				xmlHttp_Get(xmlHttp, FolderRoot + "XmlHttpFetch.aspx?func=ProductOptionInfo&ProductId=" + productId);
			}
			else objectById("floatingInfo").innerHTML = productHTML[productId];
		}
		else
		{
			objectById("floatingInfo").innerHTML = "";
			objectById("floatingInfo").style.display = "none";
		}
	}
}

function showProductInfo()
{
	// readyState of 4 or 'complete' represents that data has been returned
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete')
	{
		if (xmlHttp.responseText)
		{
			var productId = 0;
			var arrItems = xmlHttp.responseText.split("\|");
			productId = arrItems[0];
			productsLoaded += productId + ",";
			productHTML[productId] = arrItems[1];
			objectById("floatingInfo").innerHTML = arrItems[1];
		}
	}
}

function asCurrency(value)		// format a number as currency
{
	// check the number is a valid number or return empty string
	value = parseFloat(value);
	if (isNaN(value)) return 0;

	return value.toFixed(2);
}

function swapImage(index)
{
	objectById('mainImage').src = preLoad[index].src;
}

function altProductsToggleLoadingLayer(state)
{
	if (state == 1) objectById('ajaxLoading').innerHTML = "<div class=\"c\"><div><img src=\"" + FolderRoot + "Images/Loading.gif\" alt=\"Loading...\" />loading ...</div></div>";
	else objectById('ajaxLoading').innerHTML = "";
}

function optGrpsToggleLoadingLayer(state, groupId)
{

	if (state == 1) objectById('optGrpLoading_' + groupId).innerHTML = "<div class=\"c\"><div><img src=\"" + FolderRoot + "Images/Loading.gif\" alt=\"Loading...\" />loading ...</div></div>";
	else objectById('optGrpLoading_' + groupId).innerHTML = "";
}

function showTab(tabNo)
{
	for (var i = 0; i < tabs.length; i ++) 
	{
		if (i == tabNo)
		{
			if (tabLinks[i]) tabLinks[i].className += " active";
			if (tabs[i]) tabs[i].style.display = "block";
		}
		else
		{
			if (tabLinks[i]) tabLinks[i].className = tabLinks[i].className.replace(" active", "").replace("active", "");
			if (tabs[i]) tabs[i].style.display = "none";
		}
	}
}

function addToBasket(productId, qtySource)
{
	var qty = parseInt(objectById(qtySource).value);
	if (isNaN(qty)) qty = 1;
	location.href = FolderRoot + "shopping-basket&Add=" + productId + "&Qty=" + qty;
}

function engrvCatch(src, e, bar, lineLimit, charLimit, price, freeCharLimit, productId)
{
	// get engraving text without line breaks so we get a genuine character count
	var text = src.value.replace(/[\r\n]+/g, "");

	var d = parseInt(100 - ((charLimit - text.length) * 100) / charLimit);
	objectById("chrLimitBar" + bar).innerHTML = "<div align=center>FREE</div>";
	objectById("chrLimitBar" + bar).style.width = parseInt(d) + "px";

	if (text.length > 0)
	{
		// update the progress bar
		if (text.length > freeCharLimit)
		{
			objectById("chrLimitBar" + bar).innerHTML = "<div align=center>+&pound;" + asCurrency(price) + "</div>";
			var c = parseInt(100 - ((charLimit - freeCharLimit) * 100) / charLimit);
			objectById("chrLimitBar" + bar).style.backgroundPosition = (-100 + c) + "px top";
		}

		// make sure the option is added to the product bundle at the correct price (if it's already in there at the wrong price then remove it)
		if (text.length > freeCharLimit && txtEngraveItems.value.indexOf("," + productId + "|3|0,") > -1)
		{
			txtEngraveItems.value = txtEngraveItems.value.replace(productId + "|3|0,", "");
		}
		else if (txtEngraveItems.value.indexOf("," + productId + "|3|" + price + ",") > -1)
		{
			txtEngraveItems.value = txtEngraveItems.value.replace(productId + "|3|" + price + ",", "");
			bundlePrice -= parseFloat(price);
		}

		// now add the option to the bundle if it's not already added
		if (txtEngraveItems.value.indexOf("," + productId + "|3|") < 0)
		{
			if (text.length > freeCharLimit)
			{
				txtEngraveItems.value += productId + "|3|" + price + ",";
				bundlePrice += parseFloat(price);
			}
			else txtEngraveItems.value += productId + "|3|0,";
			
			// refresh the price
			updatePrice(txtMainQty);
		}
	}
	else
	{
		objectById("chrLimitBar" + bar).style.backgroundPosition = "left top";

		// make sure the option is removed from the product bundle (check for both with a price and without)
		if (txtEngraveItems.value.indexOf("," + productId + "|3|" + price + ",") > -1)
		{
			txtEngraveItems.value = txtEngraveItems.value.replace(productId + "|3|" + price + ",", "");
			bundlePrice -= parseFloat(price);
			updatePrice(txtMainQty);
		}
		else if (txtEngraveItems.value.indexOf("," + productId + "|3|0,") > -1)
		{
			txtEngraveItems.value = txtEngraveItems.value.replace(productId + "|3|0,", "");
			updatePrice(txtMainQty);
		}
	}

	// check which key was pressed and if it is allowed based on the current text and limits
	var keyCode;
	if (window.event) keyCode = window.event.keyCode;
	else if (e) keyCode = e.which;

	// check maximum number of lines and reject further line breaks if exceeded
	var lineCount = src.value.replace(/\r+/g, "").length - src.value.replace(/[\r\n]+/g, "").length + 1;

	// check for too many lines
	if (lineCount >= lineLimit && keyCode == 13) return false;

	// check character limit and restrict if exceeded
	if (text.length >= charLimit && keyCode >= 32) return false;

	return true;
}

