﻿//
// 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;

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;
	
	var totalPrice = 0;
	var quantity = qtySource.value;
	//if (quantity > currentStock) qtySource.value = currentStock;
	if ((isNaN(qtySource.value)) || qtySource.value.replace(/ /g, "") == "") totalPrice = Math.round(bundlePrice * 100) / 100;
	else totalPrice = Math.round((bundlePrice * qtySource.value) * 100) / 100;
	if (objectById("layerPrice")) objectById("layerPrice").innerHTML = asCurrency(totalPrice);
	if (objectById("layerPrice2")) objectById("layerPrice2").innerHTML = asCurrency(totalPrice);
	if (objectById("layerPrice3")) objectById("layerPrice3").innerHTML = asCurrency(totalPrice);
	txtBundlePrice.value = bundlePrice;
}

function toggleBundleItem(productId, productType, price)
{
	// if option is already added, remove it and deduct the price
	if (txtBundleItems.value.indexOf("," + productId + "|" + productType + ",") > - 1)
	{
		objectById("plus" + productId).src = "images/plus.png";
		txtBundleItems.value = txtBundleItems.value.replace(productId + "|" + productType + ",", "");
		bundlePrice -= parseFloat(price);
		
	}
	
	// otherwise add the option and add the price
	else
	{
		objectById("plus" + productId).src = "images/minus.png";
		txtBundleItems.value += productId + "|" + productType + ",";
		bundlePrice += parseFloat(price);
		
	}
	
	// update the total price field
	updatePrice(txtMainQty);
}

function toggleProductInfo(productId, state)
{
	if (objectById("floatingInfo"))
	{
		if (state == 1)
		{
			activeFloat = "floatingInfo";
			xOffset = yOffset = 10;
			objectById("floatingInfo").style.display = "inline";
			if (productsLoaded.indexOf("," + productId + ",") == -1)
			{
				objectById("floatingInfo").innerHTML = objectById("loadingTemplate").innerHTML;
				xmlHttp = GetXmlHttpObject(showProductInfo);
				xmlHttp_Get(xmlHttp, FolderRoot + "XmlHttpFetch.aspx?func=ProductOptionInfo&ProductId=" + productId);
			}
			else objectById("floatingInfo").innerHTML = productHTML[productId];
		}
		else 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 response = objectById("infoTemplate").innerHTML.replace(/[\r\n]/g, "");
			var productId = 0;
			var arrItems = xmlHttp.responseText.split("\n");
			for (var i = 0; i < arrItems.length; i ++)
			{
				var elements = arrItems[i].split("::");
				elements[1] = elements[1].replace(/False/g, "no").replace(/True/g, "yes");
				eval("response = response.replace(/#" + elements[0] + "#/g, \"" + elements[1] + "\");");
				if (elements[0] == "ProductId") productId = elements[1];
			}
			productsLoaded += productId + ",";
			productHTML[productId] = response;
			objectById("floatingInfo").innerHTML = response;
		}
	}
}

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;

	// convert to string and check for a decimal place - if there isn't one then append .00 to the value
	if (value.toString().indexOf(".") < 1) value = value.toString() + ".00";

	// split in to xxx and xx, and format the xx part
	var poundsPence = value.toString().split(".");
	while (poundsPence[1].length < 2) poundsPence[1] += "0";

	return poundsPence[0] + "." + poundsPence[1];
}

function swapImage(index)
{
	objectById('mainImage').src = preLoad[index].src;
}

function altProductsToggleLoadingLayer(state)
{
	if (state == 1)
	{
		objectById('ajaxLoading').style.display = "";
	}
	else
	{
		objectById('ajaxLoading').style.display = "none";
	}
}

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;
}
