﻿//
// 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 showBasket(itemCount, subTotal)
{
    var d=objectById("basketData").innerHTML.split("|");

    if (d.length == 4)
    {
        bsktConfirm(d[0], d[1], d[2], d[3]);
        objectById("basketData").innerHTML = "";
        updateBasketSummary(itemCount, subTotal);
    }
    return;
}

function bsktConfirm(title, price, quantity, attributes)
{    
    var x, y;
    if (window.innerHeight && window.scrollMaxY) {// Firefox
	    y = "100%";
	    x = "100%";
    } else if (document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
	   y = "100%";
       x = "100%";
    } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
	    y = document.body.offsetHeight;
	    x = document.body.offsetWidth;

    }
    
    objectById("popOverlay").style.height = y;
    objectById("popBsktConf").style.height = y; 
    objectById("popOverlay").style.width = x;
    objectById("popBsktConf").style.width = x; 
    
    objectById("popOverlay").style.display = "block";
    objectById("popBsktConf").style.display = "block";
    //
    objectById("popBsktConfTitle").innerHTML = quantity + ' x ' + title + ' - ' + price;
    if (attributes != "") objectById("popBsktAttributes").innerHTML = '<br><br>-&nbsp;' + attributes.replace('#', '<br>-&nbsp;').replace('#', '<br>-&nbsp;').replace('#', '<br>-&nbsp;').replace('#', '<br>-&nbsp;').replace('#', '<br>-&nbsp;').replace('#', '<br>-&nbsp;');
}

function closeBsktConfirm()
{
    objectById("popOverlay").style.display = "none";
    objectById("popBsktConf").style.display = "none";
}

function chkStk(a,x){
    var d=objectById("prodData").innerHTML.split("|"),q=a.value;
    if (isNaN(q) || q=="")
    {
        a.value=1;
        q=1;
        //return;
    }
    var i=parseInt(d[0]),s=parseInt(d[1]),p=parseFloat(d[2]);
    
    // take one off the stock figure to give some buffer    
    //s=s-1; 
    
    if (i>0 && q>s) {
        if (s>0){
            q=s;
            a.value=q;
            calcPr(q,p);
            if (x) alert("Sorry, we do not have enough stock to fulfil your requirements. We have selected the maximum quantity currently available.");
        }
        else{
            //q=1;
            calcPr(q,p);
            if (x) alert("Please call 01467 672927 to check availability of this item.");
        }
    }
    else {
        calcPr(q,p);
    }
}

function calcPr(q,p){
    var p=q*p;
    objectById("txtTotalPrice").value="£"+asCurrency(p);
}

function updatePrice(qtySource)
{
	//alert("1");
	
	if (!qtySource) return;
	
	//alert("2");
	
	var data = objectById("prodData").innerHTML.split("|");
	
	//if (parseInt(data[0]) > 0)
	//{
	    // 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 (parseInt(data[0]) > 0 && quantity > parseInt(data[1]))
	    {
	        if (currentStock > 0) qtySource.value = currentStock;
	        alert("Sorry, we do not have enough stock to fulfil your requirements. We have selected the maximum quantity currently available.");
	    }
	    if ((isNaN(qtySource.value)) || qtySource.value.replace(/ /g, "") == "")
	    {
	        totalPrice = (bundlePrice + optGrpPrice).toFixed(2);
	        //alert("aa");
	    }
	    else
	    {
	        totalPrice = ((bundlePrice + optGrpPrice) * qtySource.value).toFixed(2);
	        //alert("bb");
	    }

	    // store the totals in the hidden fields and display on screen
	    if (objectById("txtTotalPrice")) objectById("txtTotalPrice").value = "£" + asCurrency(totalPrice);
	    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;
}

