﻿//*****************************************************************************
// Common JavaScript code for www.srpnet.com pages.
//*****************************************************************************
//*****************************************************************************
// Window onload code.
//
// Note: Do not set window.onload directly, use WindowAddOnloadHandler()
// instead.
//*****************************************************************************
window.onload = WindowOnload;

// 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", "electric", "water");
//-----------------------------------------------------------------------------
// 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] + "TabOn").style.display  = "none";
			document.getElementById(prefix + SrpBannerTabNames[i] + "TabOff").style.display = "";
			document.getElementById(prefix + SrpBannerTabNames[i] + "Menu").style.display   = "none";
		}

		// Show the "None" menu.
		document.getElementById(prefix + SrpBannerTabNames[0] + "Menu").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 + "TabOff").style.display = "none";
		document.getElementById(prefix + name + "TabOn").style.display  = "";
		document.getElementById(prefix + name + "Menu").style.display   = "";

		// Show the "None" menu. No clue why SrpBannerTabNames[0] doesn't work in place of
		// "none" like up above.
		document.getElementById(prefix + SrpBannerTabNames[0] + "Menu").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;
	}
}
//*****************************************************************************
// IE style fixes code.
//*****************************************************************************

WindowAddOnloadHandler(IeStyleFix);

function IeStyleFix()
{
	if (document.all == null || window.opera == true)
		return false;

	// Check the IE version.
	try
	{
		var ver = "";
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			ver = parseFloat(RegExp.$1);
		if (ver >= 7.0)
			return false;
	}
	catch (ex)
	{}

	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("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)
	{}
}
//*****************************************************************************
// 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)
	{}
}