var BasketPos,Total,Discount,BasketTimer=-1,OrderWnd=null,CurrentCookie="";

function OpenNewWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function OpenFormWindow(theURL,winName,features) { //v2.0
  return window.open(theURL,winName,features);
}

function DecodeCookie(cookie,Name)
{
	var cookie,decodedCookie="",Name,subName,pairs,pairSplit,valuePairs,valuePairSplit;

	//Separate elements in cookie into array
	pairs = cookie.split(";");

	//Loop for all pairs
	for(var n=0;n<pairs.length;n++)
	{
		//Get name and value for this element
		pairSplit = pairs[n].split("=");

		//If name matches
		if (pairSplit[0] == Name)
		{
			if (pairSplit.length>=2)
			{
				valuePairs = pairSplit[1].split(",");
				for(var i=0;i<valuePairs.length;i++)
				{
					valuePairSplit = valuePairs[i].split(":");
					decodedCookie += valuePairSplit[0] + "=" + valuePairSplit[1] + ";";
				}
			}
			else
			{
				decodedCookie = cookie;
			}
		}
	}
	return decodedCookie;
}

function GetCookieValue()//cookie,Name,subName) subName optional
{
	var cookie,Name,subName,pairs,pairSplit,valuePairs,valuePairSplit,Index=0,args=GetCookieValue.arguments;

	//Parse arguments
	if (args.length < 2) return 0;
	cookie = args[0];
	Name = args[1];
	if (args.length > 2) subName = args[2];
	else subName = "Basket";

	//Separate elements in cookie into array
	pairs = cookie.split(";");

	//Loop for all pairs
	for(var n=0;n<pairs.length;n++)
	{
		//Get name and value for this element
		pairSplit = pairs[n].split("=");
		Index += pairs[n].length + 1;

		//If name matches
		if (pairSplit[0] == Name) 
		{
			CurrentCookie = cookie.substring(Index);
			return parseInt(pairSplit[1]);
		}
		else if (pairSplit[0] == subName && pairSplit.length>=2)
		{
			valuePairs = pairSplit[1].split(",");
			for(var i=0;i<valuePairs.length;i++)
			{
				valuePairSplit = valuePairs[i].split(":");
				if (valuePairSplit[0] == Name)
				{
					return parseInt(valuePairSplit[1]);
				}
			}
		}
	}
	return 0;
}

function GetCookieValueStr()//cookie,Name,subName) subName optional
{
	var cookie,Name,subName,pairs,pairSplit,valuePairs,valuePairSplit,Index=0,args=GetCookieValueStr.arguments;

	//Parse arguments
	if (args.length < 2) return 0;
	cookie = args[0];
	Name = args[1];
	if (args.length > 2) subName = args[2];
	else subName = "History";

	//Separate elements in cookie into array
	pairs = cookie.split(";");

	//Loop for all pairs
	for(var n=0;n<pairs.length;n++)
	{
		//Get name and value for this element
		pairSplit = pairs[n].split("=");
		Index += pairs[n].length + 1;
		//alert(pairSplit[0] + "(" + pairSplit[0].length + ") = " + Name + "," + subName + "("+subName.length+")");

		//If name matches
		if (pairSplit[0] == Name) 
		{
			CurrentCookie = cookie.substring(Index);
			return pairSplit[1];
		}
		else if (pairSplit[0] == subName && pairSplit.length>=2)
		{
			valuePairs = pairSplit[1].split(",");
			for(var i=0;i<valuePairs.length;i++)
			{
				valuePairSplit = valuePairs[i].split(":");
				if (valuePairSplit[0] == Name)
				{
					return valuePairSplit[1];
				}
			}
		}
	}
	return "";
}

function FormatItems(Cookie)
{
	var html="",Items,Index,Quantity,Type,Price,p,d;

	//Get number of items
	Items = GetCookieValue(Cookie,"Items");
	CurrentCookie = DecodeCookie(Cookie,"Basket");
	Total = 0;
	Discount = 0;

	//Reset discounts sum
	for (d=0;d<categoryDiscounts.length;d++)
	{
		categoryDiscounts[d][3] = 0;
	}

	html = "<form name=BasketForm>";
	html += "<table width=686 border=1 cellspacing=0 cellpadding=0 bordercolor=#0DC7B1>";

	//Loop for all items
	for(var n=0;n<Items;n++)
	{
		//Get index in product list for this item, also get quantity value
		Index = GetCookieValue(CurrentCookie,"Item"+n);
		Quantity = GetCookieValue(CurrentCookie,"Quantity"+n);
		Type = GetCookieValue(CurrentCookie,"Type"+n);

		//Calc price of item
		if (cardData[Index][inPrice].length > 1)
		{
			for(p=0;p<cardData[Index][inPrice].length;p+=2)
			{
				if (Quantity >= cardData[Index][inPrice][p]) Price = cardData[Index][inPrice][p+1];
			}
		}
		else Price = cardData[Index][inPrice];

		//Sum Discounts
		for (d=0;d<categoryDiscounts.length;d++)
		{
			if (typeof(cardData[Index][inCat]) == "object")
			{
				for(var c=0;c<cardData[Index][inCat].length;c++)
				{
					if (cardData[Index][inCat][c] == categoryDiscounts[d][0])
					{
						categoryDiscounts[d][3] += Quantity;
					}
				}
			}
			else if (cardData[Index][inCat] == categoryDiscounts[d][0])
			{
				categoryDiscounts[d][3] += Quantity;
			}
		}

		//Write product title
		html += "<tr><td width=20%>" + cardData[Index][inTitle] + "</td>";

		//Write product desc
		Description = cardData[Index][inDesc];
		if (Type > 0 && cardData[Index].length > inType)
		{
			Description = cardData[Index][inType][Type-1];
		}
		if (Description != "")
		{
			html += "<td width=40%>" + Description + "</td>";
		}
		else
		{
			html += "<td width=40%>&nbsp;</td>";
		}

		//Write Price
		html += "<td width=12%>£" + PriceToString(Price) + "</td>";

		//Write quantity text box
		html += "<td width=10%><input type=text size=3 name=Quantity" + n + " value="+ Quantity +"></td>";

		//Write total price
		html += "<td width=12%>£" + PriceToString(Price * Quantity) + "</td>";

		//Write remove link
		html += "<td width=5% align=center><a id=Menu href='JavaScript:RemoveItem("+n+");'>X</a></td></tr>";

		//Add up total for basket
		Total += Price * Quantity;
	}

	//Calculate discount
	for (d=0;d<categoryDiscounts.length;d++)
	{
		Discount += Math.floor(categoryDiscounts[d][3] / categoryDiscounts[d][1]) * categoryDiscounts[d][2];
	}

	html += "</table></form>";
	return html;
}

function RemoveBlanks(Cookie)
{
	var temp="",c,Value=0;
	for(var n=0;n<Cookie.length;n++)
	{
		c = Cookie.charAt(n);
		if (c != " " || Value) temp += c;
		if (c == "=") Value = 1;
		if (c == ";") Value = 0;
	}
	return temp;
}

function SetupBasketPosition(StartPos,LayerID)
{
	if (BasketTimer != -1)
	{
		clearTimeout(BasketTimer);
		BasketTimer = -1;
	}

	if (obj = FindObj(LayerID))
	{
		//Calc starting position of basket
		BasketPos = (((document.body.clientHeight - parseInt(obj.style.height))/2) + document.body.scrollTop);
		if (BasketPos < 0) BasketPos = 0;

		//Set position of basket
		obj.style.top = BasketPos;
	}

	//Set position of basket items
	if (obj = FindObj(LayerID+"Items")) obj.style.top = BasketPos+53;

	//Set timer to update position of basket
	BasketTimer = setTimeout("ScrollBasketWithWindow("+StartPos+",'"+LayerID+"');",50);
}

function ScrollBasketWithWindow(StartPos,LayerID)
{
	//Calc ramped position of basket
	if (obj = FindObj(LayerID))
	{
		BasketPos = ((((document.body.clientHeight - parseInt(obj.style.height))/2) + document.body.scrollTop) - BasketPos)/5 + BasketPos;
		if (BasketPos < 0) BasketPos = 0;

		if (parseInt(obj.style.height) < document.body.clientHeight)
		{
			//Set position of basket
			obj.style.top = BasketPos;

			//Set position of basket items
			if (obj = FindObj(LayerID+"Items")) obj.style.top = BasketPos+53;
		}
	}

	//Set timer again
	BasketTimer = setTimeout("ScrollBasketWithWindow("+StartPos+",'"+LayerID+"');",50);
}

function ShowBasket()
{
	var layerHTML,html="",tag,Cookie;
	var curIndex=0,index=0,end,val;

	//Get template HTML
	if (obj = FindObj("BasketTemp")) layerHTML = obj.innerHTML;
	else return;

	//Get cookie
	Cookie = RemoveBlanks(document.cookie);

	//Show items
	if (obj = FindObj("BasketItems"))
	{
		obj.innerHTML = FormatItems(Cookie);
		obj.style.visibility = "visible";
	}

	//Loop until all HTML written
	curIndex = 0;
	while (curIndex < layerHTML.length)
	{
		//Find first [ from index
		if ((index = layerHTML.indexOf("[",curIndex)) == -1)
			break;

		//Find index of ] after [
		if ((end = layerHTML.indexOf("]",index)) == -1)
		{
			html = "<H1>Tag end not found!</H1>";
		}

		//Add html before [
		html += layerHTML.substring(curIndex,index);

		//Get text in tag
		tag = layerHTML.substring(index+1,end);

		//Check text after [
		if (tag == "Items")
		{
			tag = GetCookieValue(Cookie,"Items");
		}
		else if (tag == "Total")
		{
			tag = "£"+PriceToString(Total-Discount);
		}
		else if (tag == "Discount")
		{
			if (Discount > 0)
			{
				tag = ", with a £"+PriceToString(Discount)+" multibuy discount";
			}
			else
			{
				tag = "";
			}
		}

		//Add value of tag instead of tag
		html += tag;
		curIndex = end+1;
	}

	//Add last part of layer html
	if (curIndex < layerHTML.length)
	{
		html += layerHTML.substring(curIndex,layerHTML.length);
	}

	//Hide bigger picture (if visible) and setup basket position
	ClosePic();
	SetupBasketPosition(100,"Basket");

	//Set html on basket layer and show
	if (obj = FindObj("Basket"))
	{
		obj.innerHTML = html;
		obj.style.visibility = "visible";
	}

	return html;
}

function UpdateBasket()
{
	var Items,Cookie,Value,Quantity,Type,Index,NewCookie="Basket=";

	//Get cookie
	Cookie = RemoveBlanks(document.cookie);

	//Get number of items in basket
	Items = GetCookieValue(Cookie,"Items");
	CurrentCookie = DecodeCookie(Cookie,"Basket");

	for(var n=0;n<Items;n++)
	{
		//Get quantity value from item form element
		if (document.BasketForm)
		{
			Value = eval("document.BasketForm.Quantity"+n+".value");
			Quantity = parseInt(Value);
			if (Value != ""+Quantity) Quantity = GetCookieValue(CurrentCookie,"Quantity"+n);
		}
		else 
		{
			Quantity = GetCookieValue(CurrentCookie,"Quantity"+n);
		}

		//Get index of this product
		Index = GetCookieValue(CurrentCookie,"Item"+n);
		Type  = GetCookieValue(CurrentCookie,"Type"+n);

		//Clamp Value
		if (cardData[Index][inPrice].length > 1)
		{
			if (Quantity < cardData[Index][inPrice][0]) Quantity = cardData[Index][inPrice][0];
		}
		else if (Quantity < 1) Quantity = 1;
		if (Quantity > 1000) Quantity = 1000;
		
		//Set quantity value for item in cookie
		NewCookie += "Item"		+n+":"+Index+",";
		NewCookie += "Quantity" +n+":"+Quantity+",";
		NewCookie += "Type"		+n+":"+Type+",";
	}

	document.cookie = NewCookie;
	ShowBasket();
}

function RemoveItem(Item)
{
	var Cookie,Items,CurIndex,Index,Quantity,Type,NewCookie="Basket=";

	//Get cookie
	Cookie = RemoveBlanks(document.cookie);

	//Get number of items
	Items = GetCookieValue(Cookie,"Items");
	CurrentCookie = DecodeCookie(Cookie,"Basket");

	//If no items then return
	if (Items == 0) return;

	//If Item not last in basket
	if (Item < Items-1)
	{
		//Loop for all items after Item and shift down
		for(var n=0;n<Items-1;n++)
		{
			CurIndex = n;
			if (n >= Item)
			{
				CurIndex = n+1;	
			}
			Index    = GetCookieValue(CurrentCookie,"Item"		+CurIndex);
			Quantity = GetCookieValue(CurrentCookie,"Quantity"	+CurIndex)
			Type     = GetCookieValue(CurrentCookie,"Type"	+CurIndex)
			NewCookie += "Item"    + n + ":"+ Index    + ",";
			NewCookie += "Quantity"+ n + ":"+ Quantity + ",";
			NewCookie += "Type"    + n + ":"+ Type + ",";
		}
		document.cookie = NewCookie;
	}

	//Dec count of items
	document.cookie = "Items="+(Items-1);

	//Update basket
	ShowBasket();
}

function EmptyBasket()
{
	//Set number of items in basket to 0 and show the basket again (force update of the HTML)
	document.cookie = "Items=0";
	ShowBasket();
}

function CheckoutBasket()
{
	var Items,Cookie,Value,Quantity,Type,Index,NewCookie="Order=";

	if (OrderWnd != null)
	{
		if (!OrderWnd.closed) 
		{
			alert("You can only have a single order/sample request form open at a time!\nPlease close the other form to continue.");
			return;
		}
		OrderWnd = null;
	}

	//Make sure the basket it upto date
	UpdateBasket();

	//Get cookie
	Cookie = RemoveBlanks(document.cookie);

	//Get number of items
	Items = GetCookieValue(Cookie,"Items");
	CurrentCookie = DecodeCookie(Cookie,"Basket");

	if (Items > 0)
	{
		//Loop for all items
		for(var n=0;n<Items;n++)
		{
			//Get quantity value from item form element
			Quantity = GetCookieValue(CurrentCookie,"Quantity"+n);
			Type = GetCookieValue(CurrentCookie,"Type"+n);
			
			//Get index of this product
			Index = GetCookieValue(CurrentCookie,"Item"+n);

			//Set quantity value for item in cookie
			NewCookie += "Item"+n+":"+Index+",";
			NewCookie += "Quantity"+n+":"+Quantity+",";
			NewCookie += "Type"+n+":"+Type+",";
		}

		//Update order cookie
		document.cookie = NewCookie;
		document.cookie = "OrderItems="+Items;
		document.cookie = "OrderTimeStamp="+Date().toString();

		//Open new window with the order form
		OrderWnd = OpenFormWindow('OrderForm.htm','OrderForm','status=yes,scrollbars=yes,width=680,height=480');
		
		//If order form failed to show then change this window instead
		if (OrderWnd != null)
		{
			if (OrderWnd.closed) 
			{
				window.location = 'OrderForm.htm';
			}
		}
		else
		{
			window.location = 'OrderForm.htm';
		}
	}
}

function SampleRequest()
{
	var Cookie,Item,ValidItems,Index,NewCookie="Order=";

	if (OrderWnd != null)
	{
		if (!OrderWnd.closed) 
		{
			alert("You can only have a single order/sample request form open at a time!\nPlease close the other form to continue.");
			return;
		}
		OrderWnd = null;
	}

	//Get cookie
	Cookie = RemoveBlanks(document.cookie);

	//Get number of items
	Items = GetCookieValue(Cookie,"Items");

	//Loop for all items
	ValidItems = 0;
	for(var n=0;n<Items;n++)
	{
		//Get index in product list for this item, also get quantity value
		Index = GetCookieValue(Cookie,"Item"+n);

		//If can have sample for item
		if (isValidSampleCategory(cardData[Index][inCat]))
		{
			//Get quantity value from item form element
			Quantity = GetCookieValue(Cookie,"Quantity"+n);

			//Set quantity value for item in cookie
			NewCookie += "Item"+ValidItems+":"+Index+",";
			NewCookie += "Quantity"+ValidItems+":"+Quantity+",";

			//Count valid items
			ValidItems++;
		}
	}

	if (ValidItems > 0)
	{
		//Create order cookie
		document.cookie = NewCookie;
		document.cookie = "OrderItems="+ValidItems;

		//Open new window with the order form
		OrderWnd = OpenFormWindow('SampleReq.htm','SampleRequest','status=yes,scrollbars=yes,width=680,height=480');
	}
	else
	{
		alert("No items in your basket to request samples for!");
	}
}

function CloseBasket()
{
	if (BasketTimer != -1)
	{
		clearTimeout(BasketTimer);
		BasketTimer = -1;
	}
	if (obj = FindObj("Basket"))	  obj.style.visibility = "hidden";
	if (obj = FindObj("BasketItems")) obj.style.visibility = "hidden";
	CloseSubProduct();
}

function AddToBasket(Index)
{
	AddToBasket(Index,-1);
}

function ProductInStock(category, index)
{
	var matchcount=0;
	for(var n=0;n<cardData.length;n++)
	{
		var match=false;
		if (typeof(cardData[n][inCat]) == "object")
		{
			for(var c=0;c<cardData[n][inCat].length;c++)
			{
				if (cardData[n][inCat][c] == category)
				{
					match = true;
				}
			}
		}
		else if (cardData[n][inCat] == category)
		{
			match = true;
		}

		if (match)
		{
			if (matchcount == index)
			{
				if (cardData[n][inFlags]&1)
				{
					return false;
				}
				return true;
			}
			matchcount++;
		}
	}
	return false;
}

function AddToBasket()//Index,itemType) itemType optional
{	
	var Index,itemType=-1,Items,Cookie,NewCookie="Basket=",Item,Quantity,Type,Found=false;
	var args=AddToBasket.arguments;

	//Parse arguments
	if (args.length < 1) return;
	if (typeof(args[0]) == "string" && args.length >= 2)
	{
		//First param is Category, Second param is N
		//If first param is a string, this means add Nth product in Category
		var matchcount=0;
		for(var n=0;n<cardData.length;n++)
		{
			var match=false;
			if (typeof(cardData[n][inCat]) == "object")
			{
				for(var c=0;c<cardData[n][inCat].length;c++)
				{
					if (cardData[n][inCat][c] == args[0])
					{
						match = true;
					}
				}
			}
			else if (cardData[n][inCat] == args[0])
			{
				match = true;
			}

			if (match)
			{
				if (matchcount == args[1])
				{
					AddToBasket(n);
					break;
				}
				matchcount++;
			}
		}

		//Return, can't continue in this function
		return;
	}
	Index = args[0];
	if (args.length > 1) itemType = parseInt(args[1]);

	//If product being added has sub products ask which to add
	if (itemType==-1 && cardData[Index].length > inSubProd && cardData[Index][inSubProd] != "NotKit")
	{
		ShowSubProduct(Index);
		return;
	}

	//Get cookie
	Cookie = RemoveBlanks(document.cookie);

	//Get number of items in basket
	Items = GetCookieValue(Cookie,"Items");
	CurrentCookie = DecodeCookie(Cookie,"Basket");

	//Clamp number of items to 200
	if (Items >= 200)
	{
		alert("Cannot have more than 200 items in basket!");
		return;
	}

	//Loop through all items, updating Basket Cookie data
	for(var n=0;n<Items;n++)
	{
		//Get current index and quantity for this item
		Item = GetCookieValue(CurrentCookie,"Item"+n);
		Quantity = GetCookieValue(CurrentCookie,"Quantity"+n);
		Type = GetCookieValue(CurrentCookie,"Type"+n);

		//Add this item to New Cookie value
		NewCookie += "Item"+n+":"+Item+",";
		if (Item == Index && Type == itemType+1) //If item to be added, then add 1 to the quantity
		{
			if (Quantity < 1000) Quantity++;
			NewCookie += "Quantity"+n+":"+Quantity+",";
			Found=true;
		}
		else //Else just add current quantity
		{
			NewCookie += "Quantity"+n+":"+Quantity+",";
		}

		if (Type > 0)
		{
			NewCookie += "Type"+n+":"+Type+",";
		}
	}

	//Item not already in basket, so add
	if (!Found)
	{
		//Calc min quantity
		if (cardData[Index][inPrice].length > 1)
		{
			Quantity = cardData[Index][inPrice][0];
		}
		else Quantity = 1;

		//Add item
		NewCookie += "Item"+Items+":"+Index+",";
		NewCookie += "Quantity"+Items+":"+Quantity+",";
		if (itemType >= 0) NewCookie += "Type"+Items+":"+(itemType+1)+",";
		document.cookie = "Items="+(Items+1);
	}

	//Store new cookie and show basket
	document.cookie = NewCookie;
	ShowBasket();
};

function ShowSubProduct(Index)
{
	var layerHTML,html="",tag;
	var curIndex=0,index=0,end,val,subProd,match=false,first=true;

	//Get template HTML
	if (obj = FindObj("SubProdTemp")) layerHTML = obj.innerHTML;
	else return;

	//Loop until all HTML written
	curIndex = 0;
	while (curIndex < layerHTML.length)
	{
		//Find first [ from index
		if ((index = layerHTML.indexOf("[",curIndex)) == -1)
			break;

		//Find index of ] after [
		if ((end = layerHTML.indexOf("]",index)) == -1)
		{
			html = "<H1>Tag end not found!</H1>";
		}

		//Add html before [
		html += layerHTML.substring(curIndex,index);

		//Get text in tag
		tag = layerHTML.substring(index+1,end);

		//Check text after [
		if (tag == "Select")
		{
			//Add select list header
			tag = "<select name='SubProdSelect'>";

			//Find data for options
			if (cardData[Index].length > inType)
			{
				for(subProd=0;subProd<cardData[Index][inType].length;subProd++)
				{
					//Add option
					tag += "<option value="+subProd;

					//If first add selected to option
					if (subProd == 0)
					{
						tag += " selected";
					}
					
					//Add option name
					tag += ">" + cardData[Index][inType][subProd] + "</option>";
				}
			}
			else
			{
				for(subProd=0;subProd<cardData.length;subProd++)
				{
					//If category for this product is the sub product category then add to list
					match = false;
					if (typeof(cardData[subProd][inCat]) == "object")
					{
						if (cardData[subProd][inCat][0] == cardData[Index][inSubProd])
						{
							match = true;
						}
					}
					else if (cardData[subProd][inCat] == cardData[Index][inSubProd])
					{
						match = true;
					}

					if (match)
					{
						//Add option
						tag += "<option value="+subProd;

						//If first add selected to option
						if (first)
						{
							tag += " selected";
							first = false;
						}
						
						//Add option name
						tag += ">" + cardData[subProd][inTitle] + " - £" + PriceToString(cardData[subProd][inPrice]) + "</option>";
					}
				}
			}

			//Add select list footer
			tag += "</select>";
		}
		else if (tag == "Ok")
		{
			tag = "<input type='submit' name='Submit' value='Ok' onClick='SelectSubProduct(" + Index + ");return false;'>";
		}
		else if (tag == "Form")
		{
			tag = "<form name='SubProdForm'>";
		}
		else if (tag == "/Form")
		{
			tag = "</form>";
		}

		//Add value of tag instead of tag
		html += tag;
		curIndex = end+1;
	}

	//Add last part of layer html
	if (curIndex < layerHTML.length)
	{
		html += layerHTML.substring(curIndex,layerHTML.length);
	}

	//Hide bigger picture and basket (if visible), then setup basket position
	ClosePic();
	CloseBasket();
	SetupBasketPosition(250,"SubProd");

	//Set html on basket layer and show
	if (obj = FindObj("SubProd"))
	{
		obj.innerHTML = html;
		obj.style.visibility = "visible";
	}

	return html;
}

function SelectSubProduct(Index)
{
	var Sel;

	//Get selected index
	Sel = eval("document.SubProdForm.SubProdSelect.value");

	//Close this popup and add to basket
	CloseSubProduct();

	if (cardData[Index].length > inType)
	{
		AddToBasket(Index,Sel);
	}
	else
	{
		AddToBasket(Sel);
	}
}

function CloseSubProduct()
{
	if (BasketTimer != -1)
	{
		clearTimeout(BasketTimer);
		BasketTimer = -1;
	}
	if (obj = FindObj("SubProd"))	  obj.style.visibility = "hidden";
}

function ShowSearch()
{
	var layerHTML,html="",tag;
	var curIndex=0,index=0,end,val,subProd;

	//Get template HTML
	if (obj = FindObj("SearchTemp")) layerHTML = obj.innerHTML;
	else return;

	//Loop until all HTML written
	curIndex = 0;
	while (curIndex < layerHTML.length)
	{
		//Find first [ from index
		if ((index = layerHTML.indexOf("[",curIndex)) == -1)
			break;

		//Find index of ] after [
		if ((end = layerHTML.indexOf("]",index)) == -1)
		{
			html = "<H1>Tag end not found!</H1>";
		}

		//Add html before [
		html += layerHTML.substring(curIndex,index);

		//Get text in tag
		tag = layerHTML.substring(index+1,end);

		//Check text after [
		if (tag == "Form")
		{
			tag = "<form name='SearchForm' onsubmit='DoSearch();return false;'>";
		}
		else if (tag == "/Form")
		{
			tag = "</form>";
		}

		//Add value of tag instead of tag
		html += tag;
		curIndex = end+1;
	}

	//Add last part of layer html
	if (curIndex < layerHTML.length)
	{
		html += layerHTML.substring(curIndex,layerHTML.length);
	}

	//Hide bigger picture and basket (if visible), then setup basket position
	ClosePic();
	CloseBasket();
	SetupBasketPosition(250,"SearchDlg");

	//Set html on basket layer and show
	if (obj = FindObj("SearchDlg"))
	{
		obj.innerHTML = html;
		obj.style.visibility = "visible";
	}

	document.SearchForm.Keywords.focus();
}

function DoSearch()
{
	var KeyWords;
	KeyWords = eval("document.SearchForm.Keywords.value");
	CloseSearch();
	ShowPage("Search?"+KeyWords);
}

function CloseSearch()
{
	if (BasketTimer != -1)
	{
		clearTimeout(BasketTimer);
		BasketTimer = -1;
	}
	if (obj = FindObj("SearchDlg"))	  obj.style.visibility = "hidden";
}