﻿function isString(x) { return (typeof x == 'string'); }

function getById(id)
{
	if (!id)
		return null;
	var element = document.getElementById(id);
	if (!element && (typeof(top.g_bodyFrameCode) != "undefined") && g_bodyFrameCode)
		element = g_bodyFrameCode.document.getElementById(id);
	if(!element)
		element = getByIdAspx(id);
	return element;
}

var g_ElementCache = new Array();
function getByIdAspx(id)
{
	var element = null;
	try
	{
		element = g_ElementCache[id];
	}
	catch (e)
	{
		// continue;
	}

	if (element == null)
	{
		try
		{
			var pattern = "<[^>]+?\\s(?:id|ID|Id|iD)=([\"']?)([\\w\\$\\:]*?[_\\:\\$]{1}" + id + "\\b)\\1[^<>]*?>";
			var re = new RegExp(pattern);
			re.multiline = true;
			var firstMatch = re.exec(document.body.innerHTML);
			if (firstMatch)
			{
				element = document.getElementById(firstMatch[2]);
				if (element)
					g_ElementCache[id] = element;
			}
		}
		catch (e)
		{
			// continue;
		}
	}
	return element;
}

function isString(x) { return (typeof x == 'string'); }

function trim(s, ch)
{
	if (!s)
		return s;
	else if (ch)
		return s.replace(new RegExp("^" + ch + "*|" + ch + "*$", "g"), '');
	else
		return s.replace(/^\s*|\s*$/g, '');
}

var g_ctrlSelect = null;
function selectCtrl(ctrl)
{
	try
	{
		if (ctrl)
			g_ctrlSelect = ctrl;
		if (g_ctrlSelect)
			if ((g_ctrlSelect.type == "text") || (g_ctrlSelect.type == "textarea") || (g_ctrlSelect.type == "file"))
				g_ctrlSelect.select();
		g_ctrlSelect = null;
	}
	catch (e)
	{
		return;
	}
}

function setFocusByID(sID) { return setFocus(getById(sID)); }

function setFocus(ctrl)
{
	try
	{
		if (ctrl && !ctrl.disabled)
		{
			ctrl.focus();
			g_ctrlSelect = ctrl;

			// allow the browser to catch up with window refreshing tasks before selecting the contents
			//
			window.setTimeout("selectCtrl();", 50);

			return true;
		}
	}
	catch (e)
	{
		// eat it
	}
	return false;
}

function validateRequiredField(id, label, defaultValue)
{
	var input = getById(id);
	input.value = trim(input.value);
	if (input.value)
		return true;
	if (defaultValue)
	{
		input.value = defaultValue;
		return true;
	}
	alert('Please enter a value for "' + label + '".');
	selectCtrl(input);
	return false;
}

// NOTE: Returns { left:0, top:0, right:0, bottom:0, width:0, height:0 } if sID is bad or not found.
//
function getElementPosition(ctrl)
{
	var offsetLeft = 0, offsetTop = 0;
	var cpxWidth = 0, cpxHeight = 0;

	if (ctrl)
	{
		cpxWidth  = ctrl.offsetWidth;
		cpxHeight = ctrl.offsetHeight;
	}

	while (ctrl)
	{
		offsetLeft+= ctrl.offsetLeft;
		offsetTop += ctrl.offsetTop;
		ctrl = ctrl.offsetParent;
	}

	return { left:offsetLeft, top:offsetTop, 
			 right:(offsetLeft + cpxWidth), bottom:(offsetTop + cpxHeight),
			 width:cpxWidth, height:cpxHeight };
}

function windowSize ()
{
	var width = 0;
	var height = 0;

	if (window.innerWidth)	// W3C
	{
		width = window.innerWidth;
		height = window.innerHeight;
	}
	else	// IE
	{
		if (!(document.documentElement.clientWidth == 0))	// strict mode
		{
			width = document.documentElement.clientWidth;
			height = document.documentElement.clientHeight;
		}
		else	//quirks mode
		{
			width = document.body.clientWidth;
			height = document.body.clientHeight;
		}
	}
	return {width:width, height:height};
}

function getEvent(evt)
{
	return (evt ? evt : (window.event ? window.event : null));
}

function keyCode(evt)
{
	return (evt.charCode ? evt.charCode : (evt.which ? evt.which : evt.keyCode));
}

function isEnter(key)
{
	return (key == 13);
}

function onEnterClick(evt, id)
{
	evt = getEvent(evt);
	if (evt && isEnter(keyCode(evt)))
	{
		cancelEvent(evt);
		if (id)
			click(id);
		return true;
	}
	return false;
}

function cancelEvent(evt)	// cancels an event & prevents further event bubbling
{
	if (evt)
	{
		if (evt.preventDefault)
		{
			evt.preventDefault();
			evt.stopPropagation();
		}
		else	// IE
		{
			evt.returnValue = false;
			evt.cancelBubble = true;
		}
	}
	return false;
}

function click(ctrl)
{
	if (!ctrl)
		return;

	if (isString(ctrl))
	{
		ctrl = getById(ctrl);
		if (!ctrl)
			return;
	}

	if (ctrl.click)
		ctrl.click();
	else
	{
		var evt = document.createEvent('MouseEvents');
		evt.initEvent('click', true, true);
		ctrl.dispatchEvent(evt);
	}
}

function getExceptionInfo(e, msg)
{
	return (msg + e.name + ' (' + e.number + '): ' + (e.description ? e.description : e.message));
}

function getExceptionInfoWithoutNumber(e, msg)
{
	return (msg + e.name + ': ' + (e.description ? e.description : e.message));
}

function call(fn)
{
	try
	{
		try
		{
			eval(_getFunctionName(fn));
		}
		catch (e)
		{
			return;		// the function doesn't exist
		}
		
		eval(fn);
	}
	catch (e)
	{
		alert(getExceptionInfoWithoutNumber(e, 'Calling ' + fn + ' failed: '));
	}

	function _getFunctionName(fn)
	{
		if (!fn)
			return fn;
		var i = fn.indexOf('(');
		if (i == -1)
			return fn;
		return fn.substring(0, i);
	}
}

var g_rgctrlBlink	= null;	// array of controls to blink
var g_idBlinkTimer	= null;	// blink timer ID
var g_cBlinkTimer	= 400;	// time between blinks (milli-seconds)
var g_cBlinks		= -1;	// number of times to blink; default is -1 for endless blinking

function blink
(
	rgidCtrl,	// array of controls/IDs to blink or null to stop blinking; true if called by timer
	cBlinks		// number of times to blink; blinking continues endlessly if parameter is missing or it's non-positive
)
{
	if (rgidCtrl && (rgidCtrl.length == null))	// if called by timer?
	{
		_blink();
		return;
	}

	if (!rgidCtrl)
	{
		_stopBlink();
		return;
	}

	_stopBlink();		// stop blinking before starting anew!
	
	g_cBlinks = (cBlinks ? (2 * cBlinks) : -1);

	// create array of controls to blink
	//
	var fHasControl = false;
	g_rgctrlBlink = new Array(rgidCtrl.length);
	for (var i = 0; i < g_rgctrlBlink.length; i++)
	{
		g_rgctrlBlink[i] = _getBlinkCtrl(rgidCtrl[i]);
		if (g_rgctrlBlink[i])
			fHasControl = true;
	}	
	if (!fHasControl)
		return;			// there's nothing to blink

	// setup interval for continous blink
	//
	_blink();
	return (g_idBlinkTimer = window.setInterval("blink(true);", g_cBlinkTimer));

	//
	// nested helper functions
	//
	
	function _blink()
	{
		if (g_rgctrlBlink)
			for (var i = 0; i < g_rgctrlBlink.length; i++)
				if (g_rgctrlBlink[i])
					g_rgctrlBlink[i].style.visibility = ((g_rgctrlBlink[i].style.visibility == "hidden") ? "visible" : "hidden");

		if (--g_cBlinks == 0)
			_stopBlink();		
	}
	
	function _getBlinkCtrl(_ctrl)	// _ctrl is either the ID of the control or the control itself
	{
		var ctrl = getById(_ctrl);
		if (!ctrl && _ctrl && (typeof(_ctrl) != "string"))
			ctrl = _ctrl;
		return (isVisible(ctrl) ? ctrl : null);	// we only blink visible controls
	}

	function _stopBlink()
	{
		window.clearInterval(g_idBlinkTimer);

		// ensure currently blinking controls (if any) inherit their visibility
		//
		if (g_rgctrlBlink)
			for (var i = 0; i < g_rgctrlBlink.length; i++)
				if (g_rgctrlBlink[i])
					g_rgctrlBlink[i].style.visibility = "inherit";
		
		g_rgctrlBlink = g_idBlinkTimer = null;
	}	
}

// The visibility style must be defined directly on the object since this
// function does not go through document.styleSheets (very complex).
//
function isVisible(ctrl)
{
	while (ctrl && ctrl.style)
	{
		switch (ctrl.style.visibility)
		{
			case "hidden":
				return false;
			case "visible":
				return true;
			default:
				ctrl = ctrl.parentNode;
				break;
		}
	}

	return true;
}

function enableButton(id, enable)
{
	var button = getById(id);
	button.disabled = !enable;
	button.className = (enable ? "button" : "buttonDisabled");
}

function toMilliSeconds(min)
{
	return (min * 60 * 1000);
}
