//*****************************************************************************
// Common JavaScript code for www.srpnet.com pages.
//*****************************************************************************

//*****************************************************************************
// Window onload code.
//
// Note: Do not set window.onload directly, use WindowAddOnloadHandler()
// instead.
//*****************************************************************************

// Set the master onload handler.
window.onload = WindowOnload;
tm = '\u2122';
window.status = "SRP: Delivering more than power." + tm;

// Create an array for the event handlers.
WindowOnloadHandlers = new Array();

//-----------------------------------------------------------------------------
// Adds a function to the array of window.onload handlers.
//-----------------------------------------------------------------------------
function WindowAddOnloadHandler(h)
{
	WindowOnloadHandlers[WindowOnloadHandlers.length] = h
}

//-----------------------------------------------------------------------------
// The master window.onload handler, calls each function in the array.
//-----------------------------------------------------------------------------
function WindowOnload(e)
{
	for (var i = 0; i < WindowOnloadHandlers.length; i++)
		try
		{
			WindowOnloadHandlers[i](e);
		}
		catch (ex)
		{}
}

//*****************************************************************************
// SRP Banner control code.
//*****************************************************************************

// Build a list of all tab names.
var SrpBannerTabNames = new Array("None", "MyAccount", "ElectricServices", "WaterServices");

//-----------------------------------------------------------------------------
// Deactivates all tabs.
//-----------------------------------------------------------------------------
function SrpBannerClearAllTabs(prefix)
{
	try
	{
		// For each tab, show its "off" image and hide its menu bar.
		for (var i = 1; i < SrpBannerTabNames.length; i++)
		{
			document.getElementById(prefix + SrpBannerTabNames[i] + "TabOnImg").style.display  = "none";
			document.getElementById(prefix + SrpBannerTabNames[i] + "TabOffImg").style.display = "";
			document.getElementById(prefix + SrpBannerTabNames[i] + "MenuBar").style.display   = "none";
		}

		// Show the "None" menu.
		document.getElementById(prefix + SrpBannerTabNames[0] + "MenuBar").style.display = "";

		return false;
	}
	catch (ex)
	{
		return true;
	}
}

//-----------------------------------------------------------------------------
// Activates a specific tab.
//-----------------------------------------------------------------------------
function SrpBannerActivateTab(prefix, name)
{
	// Deactivate all tabs.
	SrpBannerClearAllTabs(prefix);

	try
	{
		// For the designated tab, show its "on" image and menu bar.
		document.getElementById(prefix + name + "TabOffImg").style.display = "none";
		document.getElementById(prefix + name + "TabOnImg").style.display  = "";
		document.getElementById(prefix + name + "MenuBar").style.display   = "";

		// Hide the "None" menu.
		document.getElementById(prefix + SrpBannerTabNames[0] + "MenuBar").style.display  = "none";

		// Save the selected tab name in the hidden form field.
		document.getElementById(prefix + "ClientActivatedTabHidden").value = name;

		return false;
	}
	catch (ex)
	{
		return true;
	}
}

//*****************************************************************************
// Keypress event handling code.
//
// Note: This code prevents the common browser behaviour of submitting a form
// when the ENTER key is pressed as this is usually undesireable when multiple
// submit buttons are present.
//
// You can optionally specify a default button that will be clicked instead. To
// do so, assign the button's ID to the global DefaultButton variable. You can
// use the global OldDefaultButton variable to save and restore any existing
// default button.
//*****************************************************************************

// Set the page keypress event handler.
document.onkeypress = DocumentOnkeypress;

// Define the default button for the ENTER key (set to null here for no
// default). Also define a variable for saving and restoring the current
// default button.
var DefaultButton    = null;
var OldDefaultButton = null;

//-----------------------------------------------------------------------------
// The keypress event handler, prevents form submission when the ENTER key is
// pressed. If a default button is set, it will click that button instead.
//-----------------------------------------------------------------------------
function DocumentOnkeypress(e)
{
	try
	{
		// Determine if the ENTER key was pressed.
		if ((window.event != null && window.event.keyCode == 13) || (e != null && e.keyCode == 13))
		{
			// Determine what element is in focus.
			var el;
			if (window.event != null)
				el = window.event.srcElement;
			if (e != null)
				el = e.target;

			if (el != null)
			{
				// Determine if that element is a link or button.
				var isLink   = false;
				var isButton = false;
				if (el.tagName == "A")
					isLink = true;
				if ((el.tagName == "BUTTON" || el.tagName == "INPUT") &&
				    (el.type == "button" || el.type == "image" || el.type == "reset" || el.type == "submit"))
					isButton = true;

				// If the element is not a link or button, supress the default
				// event behaviour (submitting the form).
				if (!isLink && !isButton)
				{
					if (window.event != null)
						window.event.returnValue = false;
					if (e != null)
						e.preventDefault();

					// If a default button is set, click it.
					if (DefaultButton != null)
					{
						var btn = document.getElementById(DefaultButton);
						if (btn != null && btn.click != null)
							btn.click();
					}
				}
			}
		}
	}
	catch (ex)
	{}
}

//*****************************************************************************
// Web utility code.
//*****************************************************************************

//-----------------------------------------------------------------------------
// Scrolls the page, if necessary, to place the given element into view.
//
// Note: This function should be called via a window.onload event handler.
//-----------------------------------------------------------------------------
function ScrollIntoView(id)
{
	try
	{
		// Get the element, initialize scroll values.
		var el = document.getElementById(id);
		var scrollX = 0;
		var scrollY = 0;

		// Check the horizontal positon.
		var x = GetElementPageOffsetLeft(el);
		var w = GetViewportWidth();
		if (x + el.offsetWidth > w)
			scrollX = x;

		// Check the vertical position.
		var y = GetElementPageOffsetTop(el);
		var h = GetViewportHeight();
		if (y + el.offsetHeight > h)
			scrollY = y;

		// Scroll as needed.
		if (scrollX > 0 || scrollY > 0)
			window.scrollTo(scrollX, scrollY);
	}
	catch (ex)
	{}
}

//*****************************************************************************
// IE style fixes code.
//*****************************************************************************

WindowAddOnloadHandler(IeStyleFix);

function IeStyleFix()
{
	if (document.all == null || window.opera == true)
		return false;

	try
	{
		// Simulates the following style selector:
		//
		//   h1 + p, h1 + ol, h1 + ul, h1 + ol p, h1 + ul p,
		//   h2 + p, h2 + ol, h2 + ul, h2 + ol p, h2 + ul p,
		//   h3 + p, h3 + ol, h3 + ul, h3 + ol p, h3 + ul p
		//
		// by adding a style class ("adjacentToHeader") to the latter element.
		var headerTags = new Array("H1", "H2", "H3");
		var elList, el;
		var i, j;

		var mainColumnEl = document.getElementById("maincolumn");
		for (i = 0; i < headerTags.length; i++)
		{
			elList = mainColumnEl.getElementsByTagName(headerTags[i]);
			for (j = 0; j < elList.length; j++)
			{
				el = elList[j].nextSibling;
				// If the tag that follows is a paragraph or list element, add the
				// style class.
				if (el != null &&
				    (el.tagName == "P" || el.tagName == "OL" || el.tagName == "UL"))
					el.className += " adjacentToHeader";
			}
		}
	}
	catch (ex)
	{}
}

//*****************************************************************************
// Utility functions.
//*****************************************************************************

//-----------------------------------------------------------------------------
// Opens a new browser window.
//-----------------------------------------------------------------------------
function NewWindow(url, name, features)
{
	if (name == null)
		window.open(url);
	else
	{
		if (features == null)
			window.open(url, name);
		else
			window.open(url, name, features);
	}
	return false;
}

//-----------------------------------------------------------------------------
// Clears all form input fields, for use by classic ASP forms.
//-----------------------------------------------------------------------------
function ClearForm(f)
{

	// Clear all form elements, this will also blank out fields set with a
	// default value.
	var fld;
  for (var i = 0; i < f.length; i++)
	{
		fld = f.elements[i];
		if ((fld.type == "checkbox" || fld.type == "radio") && fld.checked)
			fld.checked = false;
		if (fld.type == "select-one" || fld.type == "select-multiple")
			fld.selectedIndex = 0;
		if (fld.type == "hidden" || fld.type == "password" || fld.type == "text" || fld.type == "textarea")
			fld.value = "";
	}
	return false;
}

//-----------------------------------------------------------------------------
// Returns an element's left offset relative to the page.
//-----------------------------------------------------------------------------
function GetElementPageOffsetLeft(el)
{
	var x = el.offsetLeft;

	if (el.offsetParent != null)
		x += GetElementPageOffsetLeft(el.offsetParent);

	return x;
}

//-----------------------------------------------------------------------------
// Returns an element's top offset relative to the page.
//-----------------------------------------------------------------------------
function GetElementPageOffsetTop(el)
{
	var y = el.offsetTop;

	if (el.offsetParent != null)
		y += GetElementPageOffsetTop(el.offsetParent);

	return y;
}
		
//-----------------------------------------------------------------------------
// Returns the width of the browser viewport.
//-----------------------------------------------------------------------------
function GetViewportWidth()
{
	if (window.innerWidth != null)
		return window.innerWidth;

	if (document.documentElement.clientWidth != null)
		return (document.documentElement.clientWidth != 0 ? document.documentElement.clientWidth : document.body.clientWidth);
}
		
//-----------------------------------------------------------------------------
// Returns the height of the browser viewport.
//-----------------------------------------------------------------------------
function GetViewportHeight()
{
	if (window.innerHeight != null)
		return window.innerHeight;

	if (document.documentElement.clientHeight != null)
		return (document.documentElement.clientHeight != 0 ? document.documentElement.clientHeight : document.body.clientHeight);
}
