function AdjustHeaderFrame()
{
	var headerFrame = document.all.HeaderFrame;
	if (headerFrame)
	{
		var innerBody = headerFrame.contentWindow.document.body;
		innerBody.leftMargin = 0;
		innerBody.topMargin = 0;
		innerBody.bottomMargin = 0;
		innerBody.rightMargin = 0;
		innerBody.marginRight = 0;
		headerFrame.style.width = document.body.clientWidth + 16 + 'px';
	}
}

var originalClientHeight = 0;
var originalScrollTop = 0;

function SaveOriginals()
{
	originalScrollTop = document.body.scrollTop;

	var allAdjustables = document.all("adjustable");
	
	if (allAdjustables)
	{
		var colAdjustables;
		if (allAdjustables.length)
			colAdjustables = allAdjustables;
		else
		{
			colAdjustables = new Array();
			colAdjustables[0] = allAdjustables;
		}
		originalClientHeight = colAdjustables[0].clientHeight + 1;
	}
}

function SelfAdjust()
{
	AdjustHeaderFrame();

	var allAdjustables = document.all("adjustable");
	
	if (allAdjustables)
	{
		var colAdjustables;
		if (allAdjustables.length)
			colAdjustables = allAdjustables;
		else
		{
			colAdjustables = new Array();
			colAdjustables[0] = allAdjustables;
		}

		var bottomOffset;
		switch (colAdjustables.length)
		{
			case 1:
				bottomOffset = 1;
				break;
			case 2:
				bottomOffset = 59;
				break;
		}

		var height = (document.body.clientHeight - colAdjustables[0].offsetTop - bottomOffset) / colAdjustables.length;
		var diff = 0;

		for (var i = 0; i < colAdjustables.length; i++)
		{
			try
			{
				if (i > 0 || (colAdjustables[i].clientHeight + diff) >= height || (colAdjustables.length == 1))
					colAdjustables[i].style.height = height + diff;
				else
					diff += height - colAdjustables[i].clientHeight;
			}
			catch(e)
			{}

			var allChildren = colAdjustables[i].children;
			
			if (allChildren)
			{
				var colChildren;
				if (allChildren.length)
					colChildren = allChildren;
				else
				{
					colChildren = new Array();
					colChildren[0] = allChildren;
				}

				for (var j = 0; j < colChildren.length; j++)
				{
					try
					{
						// Do nothing for now with colChildren[j]. If an <OBJECT>,
						// try to set its height to that of the contained page--later on.
					}
					catch(e)
					{}
				}
			}
		}

		try
		{
			colAdjustables[0].scrollTop = originalScrollTop;
			document.body.style.visibility = "visible";
			colAdjustables[0].focus();
		}
		catch(e)
		{}
	}
}

function BackToHome()
{
	window.navigate("/");
}

function CheckAndDisplayItemInfo()
{
	if (window.top.IsConn && !window.top.IsConn()) return;

	var src = event.srcElement;

	var selection = new String(src.innerText);
	if (selection.match(/^[0-9]{6}$/))
	{
		var quantity = new String();
		try
		{
			var selection2 = new String(src.previousSibling.innerText);
			if (selection2.match(/^[0-9]{1,5}$/))
			{
				quantity = selection2;
			}
		}
		catch(e)
		{
			quantity = "";
		}

		var contextUrl = window.top.location.href;
		var source = new String(encodeURIComponent(window.top.location.href));

		event.returnValue = false;
		OpenWindow(GetLiveRequestAuthority() + "/Prices/PriceDetail.aspx?itemNo=" + selection + "&quantity=" + quantity + "&source=" + source, "s", 388, 300, false, false);
	}
}

function CheckAndDisplayItemOptions()
{
	if (window.top.IsConn && !window.top.IsConn()) return;

	var src = event.srcElement;

	var selection = new String(src.innerText);
	if (!selection.match(/^[0-9]{6}$/))
	{
		event.returnValue = false;
	}
}

function CheckAndEnterItem()
{
	var src = event.srcElement;

	var selection = new String(src.innerText);
	if (selection.match(/^[0-9]{6}$/))
	{
		event.returnValue = false;
		if (window.top.IsConn && window.top.IsConn())
		{
			src.style.cursor = "hand";
			src.style.textDecoration = "underline";
			src.style.color = "blue";
			src.title = "Click for details"
		}
		else
		{
			src.style.cursor = "default";
			src.title = "The network is not available (access to item details will be reinstated when the network is available)";
		}
	}
}

function CheckAndLeaveItem()
{
	if (window.top.IsConn && !window.top.IsConn()) return;

	var src = event.srcElement;

	var selection = new String(src.innerText);
	if (selection.match(/^[0-9]{6}$/))
	{
		event.returnValue = false;
		src.style.cursor = "default";
		src.style.textDecoration = "";
		src.style.color = "black";
	}
}

function OpenWindow(url, name, width, height, scroll, resize)
{   	   
   var winl = (screen.width - width) / 2;
   var wint = (screen.height - height) / 2;
   var winprops = 'height=' + height + ',width=' + width + ',top=' + wint + ',left=' + winl + ',scrollbars=' + scroll + ', resizable=' + resize;
   var winObj = window.open(url, name, winprops);   
   if (parseInt(navigator.appVersion) >= 4)
   {
      try
      {
         winObj.focus();
      }
      catch(e)
      {}
   }
}

function AddToCart()
{
	alert("Adding item to order... (under construction)");
}

function SeeItemPictures()
{
	alert("Retrieving item photos and diagrams... (under construction)");
}

function InspectItemDetails()
{
	alert("Retrieving item details and specifications... (under construction)");
}


function DoSetContextMenu()
{
	var a = new Array(8);

	a[0] = "Add to &order"
	a[1] = "AddToCart";
	a[2] = "";
	a[3] = "";
	a[4] = "See &photos and diagrams"
	a[5] = "SeeItemPictures"
	a[6] = "See details and specifications"
	a[7] = "InspectItemDetails"

	window.external.setContextMenu(a);
}

function StopError()
{
	return true;
}

function DeployLogin()
{
	var winObj = OpenWindow(GetLiveRequestAuthority() + '/LoginBox.aspx', 'LoginWindow', 500, 400, 0, 0);
}

function GetLiveRequestAuthority()
{
	var defaultRequestAuthority = "http://www.powermaxparts.com";
	try
	{
		var targetHostname = null;
		var targetPort;

		var currentLocation = window.location;
		var protocol = currentLocation.protocol;
		var hostname = currentLocation.hostname;
		var port = currentLocation.port;

		if (hostname.toLowerCase() == "localhost")
		{
			targetHostname = "www.powermaxparts.com";
			targetPort = 80;
		}
		else
		{
			targetHostname = hostname;
			targetPort = port;
		}

		return protocol
			+ "//"
			+ targetHostname
			+ (targetPort == 80 ? "" : ":" + targetPort)
	}
	catch(e)
	{
		return defaultRequestAuthority;
	}
}

function CommitLogin(obj)
{
	alert("within COmmitLogin");
	return;
	obj.document.cookie = "O=0";
	obj.location.reload();
	window.close();
}

function SaveScrollPosition()
{
	var srcElement = window.event.srcElement;

	var i = 0;
	var found = false;
	while (!found && i++ < 6)
	{
		if (srcElement == null)
		    break;
		else if (srcElement.tagName == "A")
			found = true;
		else
			srcElement = srcElement.parentElement;
	}

	if (found && srcElement.href.substring(0, 11) != "javascript:")
	{
		var allAdjustables = document.all("adjustable");
		
		if (allAdjustables)
		{
			var colAdjustables;
			if (allAdjustables.length)
				colAdjustables = allAdjustables;
			else
			{
				colAdjustables = new Array();
				colAdjustables[0] = allAdjustables;
			}
			var currentScrollTop = colAdjustables[0].scrollTop;
			colAdjustables[0].style.height = originalClientHeight;
			document.body.style.visibility = "hidden";
			window.scrollTo(0, currentScrollTop);
		}
	}
}

try
{
	window.onerror = StopError;
	window.attachEvent("onload", SelfAdjust);
	window.attachEvent("onload", SaveOriginals);
    window.onresize = SelfAdjust;
	document.attachEvent("onclick", SaveScrollPosition);
	document.attachEvent("onclick", CheckAndDisplayItemInfo);
	document.attachEvent("oncontextmenu", CheckAndDisplayItemOptions);
	document.attachEvent("onmouseover", CheckAndEnterItem);
	document.attachEvent("onmouseout", CheckAndLeaveItem);
}
catch(e)
{}

//	if (document.body.clientWidth < 796)
//		window.status = "(!) Catalog best viewed at a minimum of 800x600 pixels with a maximized window."
//	else
//		window.status = "Done";

