﻿function ImageData(id, caption)
{
    this.ID = id;
    this.Caption = caption;
}

function ProductOption(productoptionid, sizeid, colourid, price, stock, pricewas)
{
    this.ProductOptionID = productoptionid;
    this.SizeID = sizeid;
    this.ColourID = colourid;
    this.Price = price;
    this.Stock = stock;
    this.PriceWas = pricewas;
}

var previousImg = 0;
var firstTime = true;
function ChangeImage(id)
{   
    if(firstTime)
    {
        document.getElementById('thumb_' + id).style.display = 'none';
        firstTime = false;
    }

    document.getElementById('thumb_' + id).style.backgroundImage = 'url(/img/library/med/' + previousImg + '.jpg)';
    document.getElementById('thumb_' + id).setAttribute("href", "/imageviewer/" + previousImg + "/");
    document.getElementById('thumb_' + id).setAttribute("onclick", "ChangeImage(" + previousImg + "); return false;");
    document.getElementById('thumb_' + id).setAttribute("id", "thumb_" + previousImg);
    
    document.getElementById('imgcontainer').style.backgroundImage = 'url(/img/library/lge/' + id + '.jpg)';
    previousImg = id;
}

function DisableButton(d)
{
    var button = document.getElementById('AddToCartButton');
    button.disabled = d;
    button.className = (d) ? 'cartbuttondisabled' : 'cartbutton';
}

function CalculatePrice(f)
{	
    var size = (f.SizeID.nodeName == 'SELECT') ? parseInt(f.SizeID.options[f.SizeID.selectedIndex].value) : parseInt(f.SizeID.value);
    var colour = (f.ColourID.nodeName == 'SELECT') ? parseInt(f.ColourID.options[f.ColourID.selectedIndex].value) : parseInt(f.ColourID.value);
	
    //alert(size);
    //alert(colour);
    
    var qty = parseInt(f.Quantity.options[f.Quantity.selectedIndex].value);
	
    var validoption = false;
	
    var price = document.getElementById('price');
    
    var savings = "";
	
    if(size > 0 && colour > 0)
    {
        for(var x=0; x<productoptions.length; x++)
        {
            if(productoptions[x].SizeID == size && productoptions[x].ColourID == colour)
            {
                if(productoptions[x].PriceWas > productoptions[x].Price)
	            {
	                savings = "<br /><span id=\"savings\">RRP £" + formatPrice((productoptions[x].PriceWas) * qty) + ", Save £" + formatPrice((productoptions[x].PriceWas - productoptions[x].Price) * qty) + "</span>";
	                //alert(savings);
	            }
	            
	            if(productoptions[x].Stock)
	            {
		            price.innerHTML = "£" + formatPrice(productoptions[x].Price * qty) + savings;
		            DisableButton(false);
	            }
	            else
	            {
		            price.nodeValue = 'Out Of Stock';
		            DisableButton(true);
	            }
	            			
	            validoption = true;
				
	            break;
            }
        }
		
        if(!validoption)
        {
            price.nodeValue = 'Unavailable';
           DisableButton(true);
        }
    }
    else
    {
        price.nodeValue = range;
        DisableButton(true);
    }
}

function formatPrice (flPrice,iPlaces)
{
	iPlaces = (!iPlaces ? 2 : iPlaces);
	flPrice = Math.round(flPrice*Math.pow(10,iPlaces))/Math.pow(10,iPlaces);

	var szPrice = "" + flPrice;

	if (szPrice.indexOf(".", 0) != -1)
	{
		iNumberAfterThePoint = szPrice.length - szPrice.indexOf(".", 0);

		if  (iNumberAfterThePoint < 3)
		{
			szPrice = szPrice + "0";
		}

		if (szPrice.substring (0,1) == '0')
		{
			szPrice = szPrice.substring (1,szPrice.length);
		}
	}
	else 
	{
		szPrice = szPrice + ".00";
	}
	
	// Adds a zero to the front if < £1
	if(szPrice.indexOf(".") == 0)
	{
		szPrice = "0" + szPrice;
	}
	
	return (szPrice);
}
