//
// Title: shopping_basket.js
//
// This document is the copyright and property of Tim Mathias.
// It must not be copied (in whole or in part) used for manufacture or
// otherwise disclosed without prior written consent. Any copies of this
// document made by any method must also include a copy of this Legend,
// © Tim Mathias 2005. All rights reserved.
//
// **************************
// *	 Change History	 *
// **************************
//
// Version  Author		  Date	  Description
// -------  --------------  --------  -----------------------------------------
//	   1  Tim Mathias	 14-01-05  Original (in shopping_basket.asp).
//
//	   2  Tim Mathias	 31-01-05  Added check for whole number in
//									ValidateQuantity ().
//
//	   3  Tim Mathias	 08-06-05  Placed original script in this file and
//									prepended functions names with SB_.
//
// ****************************************************************************
//
// 1.0	 Introduction
// --------------------
//
//   Display contents of user's shopping basket.
//
// 2.0	 Program Listing
// -----------------------

window.onload = SB_DoLoad;
window.onunload = SB_DoUnload;
window.onscroll = SB_DoScroll;

function SB_DoLoad ()
{
	window.focus ();

	scrollposx = GetCookie ("wnd_jaja_shopping_basket_scrollposx");
	scrollposy = GetCookie ("wnd_jaja_shopping_basket_scrollposy");
	if (scrollposx != null && scrollposy != null)
	{
		window.scrollTo (scrollposx, scrollposy);
		//alert ("Shopping Basket onload scrolled to " + scrollposx + "," + scrollposy);
	}
	
	//window.status = "1: " + GetCookie ("b_wnd_jaja_shopping_basket_loaded");
	SetCookie ("b_wnd_jaja_shopping_basket_loaded", 1);
}

function SB_DoUnload ()
{
	SetCookie ("wnd_jaja_shopping_basket_scrollposx", document.documentElement.scrollLeft);
	SetCookie ("wnd_jaja_shopping_basket_scrollposy", document.documentElement.scrollTop);
	//alert ("Shopping Basket unload scrollposx,scrollposy " + scrollposx + "," + scrollposy);
}

function SB_DoScroll ()
{
	SetCookie ("wnd_jaja_shopping_basket_scrollposx", document.documentElement.scrollLeft);
	SetCookie ("wnd_jaja_shopping_basket_scrollposy", document.documentElement.scrollTop);
	//alert ("Shopping Basket onscroll scrollposx,scrollposy " + scrollposx + "," + scrollposy);
}

function SB_ShelveItOver (id)
{
	id.className = "btn_orange_over";
	id.style.cursor = "pointer";
	window.status = "Click to place this item back on the shop shelf.";
}
function SB_ShelveItOut (id)
{
	id.className = "btn_orange_normal";
	window.status = "";
}
function SB_ShelveItClick (id)
{
	var query = "shopping_basket.asp?action=SHELVE&item=" + id;
	//alert (query);
	location.replace (query);
}

function SB_EmptyOver (id)
{
	id.className = "btn_red_over";
	id.style.cursor = "pointer";
	window.status = "Click to empty your Shopping Basket.";
}
function SB_EmptyOut (id)
{
	id.className = "btn_red_normal";
	id.style.cursor = "";
	window.status = "";
}
function SB_EmptyClick ()
{
	var query = "shopping_basket.asp?action=EMPTY";
	//alert (query);
	location.replace (query);
}

function SB_SaveOver (id)
{
	id.className = "btn_green_over";
	id.style.cursor = "pointer";
	window.status = "Click to save quantities.";
}
function SB_SaveOut (id)
{
	id.className = "btn_green_normal";
	id.style.cursor = "";
	window.status = "";
}
function SB_SaveClick (str_sc)
{
	if (SB_ValidateQuantities ())
	{
		query = SB_GetQuantitiesQuery ();
		if (str_sc == "SAVE")
			location.replace ("shopping_basket.asp?action=SAVE" + query);
		else if (str_sc == "SAVE_AND_CLOSE")
		{
			location.replace ("shopping_basket.asp?action=SAVE_AND_CLOSE" + query);
			window.close ();
		}
	}
}

function SB_CheckoutOver (id)
{
	id.className = "btn_orange_over";
	id.style.cursor = "pointer";
	window.status = "Click to take your Shopping Basket to the Checkout.";
}
function SB_CheckoutOut (id)
{
	id.className = "btn_orange_normal";
	id.style.cursor = "";
	window.status = "";
}
function SB_CheckoutClick ()
{
	if (SB_ValidateQuantities ())
	{
		query = SB_GetQuantitiesQuery ();
		location.replace ("checkout.asp?action=INIT" + query);
	}
}

function SB_CloseOver(id)
{
	id.className = "btn_green_over";
	id.style.cursor = "pointer";
	window.status = "Click to save quantities.";
}
function SB_CloseOut(id)
{
	id.className = "btn_green_normal";
	id.style.cursor = "";
	window.status = "";
}
function SB_CloseClick(str_sc)
{
	window.close();
}

function SB_GetQuantitiesQuery()
{	
	var query = "&QUANTS=";
	var i = 0;
	var obj = document.getElementById ("quantity" + i++);
	while (obj)
	{
		query += "" + parseInt (obj.value) + ";";
		obj = document.getElementById ("quantity" + i++);
	}
	return query;
}

function SB_ValidateQuantities ()
{
	var i = 0;
	var obj = document.getElementById ("quantity" + i++);
	var obj_err = null;
	while (obj)
	{
		if (!SB_ValidateQuantity (obj))
			if (!obj_err) obj_err = obj;
		obj = document.getElementById ("quantity" + i++);
	}
	if (obj_err)
	{
		obj_err.select ();
		obj_err.focus ();
		obj_err.scrollIntoView ();
		alert ("Enter a whole number greater than zero for quantities highlighted in red.");
		return false;
	}
	return true;
}

function SB_ValidateQuantity (obj)
{
	var err_colour = "#ffcccc";
	if (obj)
	{
		val = obj.value;
		if (isNaN (val))
		{
			obj.style.backgroundColor = err_colour;
			return false;
		}
		else if (isFinite (val))
		{
			val = parseFloat (val);
			val = "" + val;
			if (val < 1 || (val.indexOf (".") != -1))
			{
				obj.style.backgroundColor = err_colour;
				return false;
			}
			obj.style.backgroundColor = "";
			return true;
		}
	}
}

