/**
 * MySmartChannels JavaScript
 *
 * Commonly used services
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * P R O P R I E T A R Y   I N F O R M A T I O N   N O T I C E
 *
 * This file contains CONFIDENTIAL INFORMATION ("Information") from the
 * MySmartTags codebase that belongs to F. Andy Seidl and Bill French
 * ("Owners").  This Information is considered a TRADE SECRET of the
 * Owners. This file may not be divulged to or used by any party without
 * prior written authorization of the Owners.
 *
 * Should the Owners elect to publish this file in such a way as to lose
 * its trade secret status, this file shall remain a copyrighted work
 * bearing the following copyright notice:
 *
 * Copyright (C) 2002 MyST Technology Partners, All rights reserved.
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 * $Header: services.js, 15, 2/2/09 11:08:05 PM, F. Andy Seidl$
 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 **/

//---------------------------------------------------------------
// LinkPage()
//---------------------------------------------------------------
//
// If a global link page has been specified, this function loads that
// page into the main frame.
//
// Returns true if a page was linked.
//
function LinkPage()
{
	if (parent.g_LinkPage == "")
		return false;

	var href = parent.g_LinkPage;
	parent.g_LinkPage = "";

	parent.content.href = href;
	return true;
}

//---------------------------------------------------------------
// OnEnterPage()
//---------------------------------------------------------------
//
// Called upon entry into child web pages.
//
// The function returns true upon success or false upon failure.  In the
// current release, the return value is not tested by the generated pages.
// However, for future compatibility, functions should be written to return
// true.
//
function OnEnterPage(frame_, page_)
{
	var FrameLoaded = parent.g_FrameLoaded;
	if (FrameLoaded == null)
	{
		var Href = frame_ + "?page=" + page_;
		window.location =  Href;
		return true;
	}

	LinkPage();
	return true;
}

//---------------------------------------------------------------
// ParseSearch()
//---------------------------------------------------------------

function ParseSearch(s_)
{
	var iPage = s_.indexOf("page=");
	if (iPage == -1)
		return;

	var page = s_.substr(iPage + 5);
	var iAnd = page.indexOf("&");
	if (iAnd != -1)
	{
		page = page.substring(0, iAnd);
	}

	g_LinkPage = page;
}

//---------------------------------------------------------------------------------------------
// Intra-page jumps
function jump(fragment)
{
	var href = window.location.href;
	var i    = href.indexOf("#");
	if (i != -1)
	{
		href = href.substring(0, i);
	}
	window.location.href = href + '#' + fragment;
}

function jumpTop()
{
	jump("_top_");
}
//---------------------------------------------------------------------------------------------
function fixQuotes(s)
{
	var amp     = new RegExp("&",  "g");
	var quote1  = new RegExp("'",  "g");
	var quote2  = new RegExp("\"", "g");
//	var percent = new RegExp("%",  "g");

	var buf = "" + s;
	
	buf = buf.replace(amp,     "&amp;");
	buf = buf.replace(quote1,  "&#39;"); 
	buf = buf.replace(quote2,  "&#34;"); 
//	buf = buf.replace(percent, "&#37;"); 
	
	return buf;
}
//---------------------------------------------------------------------------------------------
// Encodes a string as HTML by replacing '<' and '&' with the corresponding HTML entities.
function htmlEncode(text_)
{
	var buf = "";
	var i=0;
	while (i < text_.length)
	{
		var c = text_.charAt(i++);

		if (c == '<')
		{
			buf += "&lt;";
			continue;
		}

		if (c == '&')
		{
			buf += "&amp;";
			continue;
		}

		buf += c;
	}

	return buf;
}
//---------------------------------------------------------------------------------------------
function createHiddenField(name_, value_)
{
	return "<input name='" + name_ + "' type='hidden' value='" + fixQuotes(value_) + "' />\n";
}
//---------------------------------------------------------------------------------------------

// +----------+
// | Base URL |
// +----------+

var g_BaseURL = "/";

function setBaseURL(url_)
{
	g_BaseURL = url_;
}

function getBaseURL()
{
	return g_BaseURL;
}

function addBaseURL(relativeURL_)
{
	return g_BaseURL + fixNull(relativeURL_);
}

// +---------+
// | Strings |
// +---------+

//---------------------------------------------------------------------------------------------
// Convert null to an empty string.
function fixNull(s_)
{
	if (s_ == null)
		return "";

	return s_;
}
//---------------------------------------------------------------------------------------------
// Returns a copy of a string without leading spaces.
function LTrim(str)
{
	var whitespace = new String(" \t\n\r");

	var s = new String(str);

	if (whitespace.indexOf(s.charAt(0)) != -1)
	{
		// We have a string with leading blank(s)...

		var j = 0;
		var i = s.length;

		// Iterate from the far left of string until we
		// don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
		{
			j++;
		}

		// Get the substring from the first non-whitespace
		// character to the end of the string...
		s = s.substring(j, i);
	}
	return s;
}

//---------------------------------------------------------------------------------------------
//Returns a copy of a string without trailing spaces.
function RTrim(str_)
{
	// We don't want to trip JUST spaces, but also tabs,
	// line feeds, etc.  Add anything else you want to
	// "trim" here in Whitespace
	var whitespace = new String(" \t\n\r");

	var s = new String(str_);
	var i = s.length - 1;

	if (whitespace.indexOf(s.charAt(i)) != -1)
	{
		// We have a string with trailing blank(s)...

		// Iterate from the far right of string until we
		// don't have any more whitespace...
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
		{
			i--;
		}

		// Get the substring from the front of the string to
		// where the last non-whitespace character is...
		s = s.substring(0, i+1);
	}

	return s;
}

//---------------------------------------------------------------------------------------------
// Returns a copy of a string without leading or trailing spaces
function Trim(str)
{
   return RTrim(LTrim(str));
}

// +--------+
// | Gizmos |
// +--------+

g_Gizmos = new Array();

function Gizmo(table_)
{
	this.m_Table         = table_;
	this.m_TopRow        = null;  
	this.m_ButtonCell    = null;  
	this.m_MiddleRow     = null;  
	this.m_BottomRow     = null;  
	this.m_BottomRow2    = null;  
	this.m_bOpen         = false;  

	var trElements = table_.getElementsByTagName("TR");
	var i;
	for (i=0; i < trElements.length; ++i)
	{
		var tr = trElements[i];

		if (tr.className == "GizmoTopRow")
		{
			this.m_TopRow = tr;
			continue;
		}

		if (tr.className == "GizmoMiddleRow")
		{
			this.m_MiddleRow = tr;
			continue;
		}

		if (tr.className == "GizmoBottomRow")
		{
			this.m_BottomRow = tr;
			continue;
		}

		if (tr.className == "GizmoBottomRow2")
		{
			this.m_BottomRow2 = tr;
			continue;
		}
	}

	if (this.m_TopRow != null)
	{
		var tdElements = this.m_TopRow.getElementsByTagName("TD");
		for (i=0; i < tdElements.length; ++i)
		{
			var td = tdElements[i];
			if (td.className == "GizmoButtonCell")
			{
				this.m_ButtonCell = td;
				break;
			}
		}
	}
}

function addGizmo(gizmo_)
{
	g_Gizmos = g_Gizmos.concat([gizmo_]);
}

function findGizmo(iGizmo_)
{
	return g_Gizmos[iGizmo_];
}

function initGizmos()
{
	var tableElements = document.getElementsByTagName("TABLE");
	var i;

	for (i=0; i < tableElements.length; i++)
	{
		var table = tableElements[i];

		if (table.className == "Gizmo")
		{
			var gizmo = new Gizmo(table);
			if (gizmo.m_ButtonCell != null)
			{
				addGizmo(gizmo);
			}
		}
	}

	for (i=0; i < g_Gizmos.length; ++i)
	{
		updateGizmo(i, null);
	}
}

function onClickGizmo(event_, iGizmo_)
{
	var bOpen = null;		// toggle state
	var bAll  = false;

	var e = event_
	if (!e)
	{
		e = window.event;
	}

	if (e)
	{
		var bAll  = e.ctrlKey;

//		var bCtrl  = e.ctrlKey;
		var bAlt   = e.altKey;
		var bShift = e.shiftKey;

		if (bShift)
		{
			bOpen = true;
		}
		else if (bAlt)
		{
			bOpen = false;
		}
	}
	
	if (bAll)
	{
		updateAllGizmos(bOpen);
	}
	else
	{
		updateGizmo(iGizmo_, bOpen);
	}
}

function setDisplay(object_, display_)
{
	if (object_ != null)
	{
		object_.style.display = display_;
	}
}

function updateAllGizmos(bOpen_)
{
	for (var i=0; i < g_Gizmos.length; ++i)
	{
		updateGizmo(i, bOpen_);
	}
}

function updateGizmo(iGizmo_, bOpen_)
{
	var gizmo = g_Gizmos[iGizmo_];
	if (bOpen_ == null)
	{
		gizmo.m_bOpen = !gizmo.m_bOpen;
	}
	else
	{
		gizmo.m_bOpen = bOpen_
	}

	// Parameters for "open".
	var image     = "_gizmo-btn-roll-up.gif";
	var display   = "";                      
	var display2  = "none";                      

	// Change parameters if not "open".
	if (!gizmo.m_bOpen)
	{
		image    = "_gizmo-btn-roll-down.gif";
		display  = "none";
		display2 = "";
	}

	// Assert parameters.
	setDisplay(gizmo.m_MiddleRow,  display);
	setDisplay(gizmo.m_BottomRow,  display);
	setDisplay(gizmo.m_BottomRow2, display2);

	var button =
			"<img src='images/{image}' height='10' width='11' border='0' onclick='onClickGizmo(event, {index})' />";

	button = button.replace("{image}", image);
	button = button.replace("{index}", iGizmo_);

	gizmo.m_ButtonCell.innerHTML = button;
}

// +---------------------+
// | Password Validation |
// +---------------------+

//---------------------------------------------------------------------------------------------
// This is actually a little stricter than the actual validation peformed by MyST because this
// function does not recognize Unicode punctuation characters.  However, if this function
// classifies a password as strong, so will MyST.
function isStrongPassword(password_)
{
	var bDigit       = false;
	var bLowerCase   = false;
	var bUpperCase   = false;
	var bWhitespace  = false;
	var bPunctuation = false;

	var nLen = password_.length;
	var i;
	for (i=0; i < nLen; ++i)
	{
		var c = password_.charAt(i);
		if ('0' <= c && c <= '9')
		{
			bDigit = true;
		}
		else if ('a' <= c && c <= 'z')
		{
			bLowerCase = true;
		}
		else if ('A' <= c && c <= 'Z')
		{
			bUpperCase = true;
		}
		else if (c==' ' || c=='\t' || c=='\r' || c=='\n')
		{
			bWhitespace = true;
		}
		else if (c < '\x80')
		{
			bPunctuation = true;
		}
	}

	var nTypes = 0;
	if (bDigit)
	{
		++nTypes;
	}
	if (bLowerCase)
	{
		++nTypes;
	}
	if (bUpperCase)
	{
		++nTypes;
	}
	if (bWhitespace)
	{
		++nTypes;
	}
	if (bPunctuation)
	{
		++nTypes;
	}

	return (nTypes >= 2)
}
//---------------------------------------------------------------------------------------------

// +-------+
// | Misc. |
// +-------+

function ss(w){window.status=w;return true;}
function cs(){window.status='';}

// +-------------------+
// | DOM Compatibility |
// +-------------------+

// Browser independent DOM access
// See: http://www.quirksmode.org/js/dhtmloptions.html
function ObjRef(id_)
{
	if (document.getElementById)
	{
		this.obj   = document.getElementById(id_);
		if (this.obj)
		{
			this.style = this.obj.style;
		}
	}
	else if (document.all)
	{
		this.obj   = document.all[id_];
		if (this.obj)
		{
			this.style = this.obj.style;
		}
	}
	else if (document.layers)
	{
		this.obj   = document.layers[id_];
		this.style = document.layers[id_];
	}
}

function getObjRef(id_)
{
	return new ObjRef(id_);;
}

function getObj(id_)
{
	return getObjRef(id_).obj;
}

function getObjStyle(id_)
{
	return getObjRef(id_).style;
}

