﻿// JScript File
var items = new Array();

function Item(id, name, code, outOfStock)
{
	var item = new Object();
	item.Id = id;
	item.ItemName = name;
	item.ItemCode = code;
	item.OutOfStock = outOfStock;
	
	return item;
}

function AddItem(id, name, code, outOfStock)
{
	items.push(new Item(id, name, code, outOfStock));
}

function StockAvailable(id)
{
	var item = FindItem(id);
	
	if(item == null)
		return false;
		
	return !item.OutOfStock;
}

function FindItem(id)
{
	var i;
	for(i = 0; i < items.length; i++)
	{
		if(items[i].Id == id)
			return items[i];
	}
	
	return null;
}

function AddToCart()
{
	var dropdown = document.getElementById('lstItems');
	var qty = document.getElementById('txtQuantity').value;
	var iId = dropdown.options[dropdown.selectedIndex].value;
	
	// Check stock
	if(StockAvailable(iId))			
	{
		window.location.href = 'addItem.aspx?itemId=' + iId + '&qty=' + qty;
	}
	else
	{
		AlertOutOfStock();
		return false;
	}
}

function AlertOutOfStock()
{
	alert('Sorry, this item is currently out of stock');
}