var sCookieName = "cart";				// Cookie name
var sRefer		= sGetReferString();	// Refer string for current page


// Name:		sGetReferString
//
// Abstract:	Return "refer" string for the current page: "srchlib",
//				"index", "replace", "replnet", "thumb", or "editor"
// --------------------------------------------------------------------

function sGetReferString()
{
	var sPage = document.URL;
	var sRef  = "";

	if (sPage.search( /srchlib.htm/i ) != -1)
		sRef = "srchlib";
	else if (sPage.search( /indexsys.htm/i ) != -1)
		sRef = "index";
	else if (sPage.search( /replace.htm/i ) != -1)
		sRef = "replace";
	else if (sPage.search( /replnet.htm/i ) != -1)
		sRef = "replnet";
	else if (sPage.search( /thumbvw.htm/i ) != -1)
		sRef = "thumb";
	else if (sPage.search( /witzed.htm/i ) != -1)
		sRef = "editor";

	return sRef;
}



// Name:		OnOrder
//
// Abstract:	Called when the page's Order button is clicked
// --------------------------------------------------------------------

function OnOrder( idBtn )
{
	var sURL = "";

	switch (idBtn)
	{
		case 1:
			sURL = "order.htm?refer=" + sRefer;
			break;
		case 2:
			sURL = "order.htm";
			break;
		case 3:
			sURL = "https://www.regnow.com/softsell/nph-softsell.cgi?items=" + sGetShoppingCartList();
			break;
	}

	navigate( sURL );
}



// Name:		iGetCookie
//
// Abstract:	Read cookie, which holds current cart contents
// --------------------------------------------------------------------

function iGetCookie()
{
	var sCookie	= "";
	var iRet	= -1;				// Return -1 if cookies disabled

	// If cookie doesn't exist, create it with a value of 0
	if (document.cookie.length == 0)
		document.cookie = sCookieName + "=0; path=/";

	if (document.cookie.length > 0)
	{
		var i = document.cookie.indexOf( ";", sCookieName.length+1 );

		if (i == -1)
			i = document.cookie.length;
		sCookie = unescape( document.cookie.substring( sCookieName.length+1, i ) );
	}

	if (sCookie.length)
		iRet = parseInt( sCookie, 10 );

	return iRet;
}



// Name:		sGetShoppingCartList
//
// Abstract:	Return contents of shopping cart based on our cookie
//
// Note:		List is in RegNow format: "17486-1,17486-2,...". If
//				only one item, it is repeated so that list always
//				contains at least two items. This solves RegNow bug
//				in which a single item causes RegNow's Order page to
//				open instead ofShopping Cart page if no RegNow cookie
//				exists.
// --------------------------------------------------------------------

function sGetShoppingCartList()
{
	var iCookie = iGetCookie();				// Get shopping cart contents
	var	sList	= "";

	do
	{
		if ((iCookie & 1)  ||  sRefer == "srchlib")
		{
			if (sList.length)
				sList += ",";
			sList += "17486-4";
		}
		if ((iCookie & 2)  ||  sRefer == "thumb")
		{
			if (sList.length)
				sList += ",";
			sList += "17486-3";
		}
		if ((iCookie & 4)  ||  sRefer == "replace")
		{
			if (sList.length)
				sList += ",";
			sList += "17486-1";
		}
		if ((iCookie & 8)  ||  sRefer == "replnet")
		{
			if (sList.length)
				sList += ",";
			sList += "17486-5";
		}
		if ((iCookie & 16)  ||  sRefer == "index")
		{
			if (sList.length)
				sList += ",";
			sList += "17486-5";
		}
		if ((iCookie & 32)  ||  sRefer == "editor")
		{
			if (sList.length)
				sList += ",";
			sList += "17486-2";
		}
	} while (sList.length == 7);	// If only one item, repeat

	return sList;
}




// Name:		ShowPopUp
//
// Abstract:	Show list of supported document types
// --------------------------------------------------------------------

function ShowPopUp( sURL )
{
	window.open( sURL, null, "directories=no,resizable=yes,scrollbars=no,status=no,toolbar=no,menubar=no,location=no,width=540,height=300,left=25,top=25" );
}

