﻿/*
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.06
Description:	Javascript resource file for the application.
				This script is compiled into the assembly
Revisions:
	2007.05.11 - aleonard:	Added file header block
							Added swapOptions(), moveSelectedOptionDown(), 
							moveSelectedOptionUp() and optionOrder()
	2007.06.01 - aleonard:	Added AppendClassName() and RemoveClassName()
							Changed SelectedNode()
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
*/

/*
-------------------------------------------------------------------------------
Author:			jbuist
Date:			2007.04.30
Description:	Sets a hidden element to the client's UTC offset
-------------------------------------------------------------------------------
*/
function masterPage_onLoad()
{
    document.getElementById('hdClientUTCOffset').value = GetUTCDateOffset();
}

/*
-------------------------------------------------------------------------------
Author:			jbuist
Date:			2007.04.30
Description:	Returns the clients UTC offset
-------------------------------------------------------------------------------
*/
function GetUTCDateOffset()
{
    var now = new Date()
    var offset = now.getTimezoneOffset();
    
    return offset;
}
    
/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.03.01
Description:	Highlights the element when receiving focus
Parameters:
	e				Element to apply change to
-------------------------------------------------------------------------------
*/
function elementFocus(e)
{
	if(e.className.indexOf('elementFocus') < 0)
	{
		e.className += ' elementFocus';
	}
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.03.01
Description:	Un-highlights the element (e) when losing focus
Parameters:
	e				Element to apply change to
-------------------------------------------------------------------------------
*/
function elementBlur(e)
{
	e.className = e.className.replace('elementFocus','');
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.05.09
Description:	Shows or hides help instructions
Parameters:
	strId			ID of the instruction container element
-------------------------------------------------------------------------------
*/
function toggleInstructions(strId)
{
	oEL = document.getElementById(strId); 
	var strCurrentDisplay = new String();
	
	//Get the current display style
	if (oEL.currentStyle)
	{
		//IE uses currentStyle
		strCurrentDisplay = oEL.currentStyle.display;
	}
	else
	{
		//Others use getComputedStyle
		strCurrentDisplay = document.defaultView.getComputedStyle(oEL, null).display;
	}
	
	//Now for the toggle
	strCurrentDisplay.toLowerCase() =='none' ? oEL.style.display='block': oEL.style.display='none';
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.05.11
Description:	Changes the position of two OPTIONs in a SELECT element
Parameters:
	obj				SELECT element containing the OPTIONs
	i				integer, Ordinal position of the OPTION to move
	j				integer, New position of the OPTION
2007.06.27 - aleonard:	Simplified
-------------------------------------------------------------------------------
*/
function swapOptions(obj,i,j)
{
	var o = obj.options;
	var optTemp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var optTemp2 = new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = optTemp2;
	o[j] = optTemp;
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.05.11
Description:	Moves the selected OPTION in a SELECT element up one position
Parameters:
	strSelectId		ID of the SELECT element
2007.06.27 - aleonard:	Simplified - removed loop
-------------------------------------------------------------------------------
*/
function moveSelectedOptionDown(strSelectId)
{
	var obj = document.getElementById(strSelectId);
	var iSelectedIndex = obj.selectedIndex;
	
	if(iSelectedIndex < obj.options.length - 1)
	{
		swapOptions(obj,iSelectedIndex,iSelectedIndex + 1);
		obj.selectedIndex = iSelectedIndex + 1;
	}
}
	
/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.05.11
Description:	Moves the selected OPTION in a SELECT element down one position
Parameters:
	strSelectId		ID of the SELECT element
2007.06.27 - aleonard:	Simplified - removed loop
-------------------------------------------------------------------------------
*/
function moveSelectedOptionUp(strSelectId)
{
	var obj = document.getElementById(strSelectId);
	var iSelectedIndex = obj.selectedIndex;
	
	if(iSelectedIndex > 0)
	{
		swapOptions(obj,iSelectedIndex,iSelectedIndex - 1);
		obj.selectedIndex = iSelectedIndex - 1;
	}
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.05.11
Description:	Adds an OPTION to a SELECT element
Parameters:
	obj				Reference to an object that has the text for the new option
					in it's value property (such as an INPUT text element)
	strSelectId		ID of the SELECT element
-------------------------------------------------------------------------------
*/
function addOptionToList(obj, strListId)
{
	var objSelect = document.getElementById(strListId);
	
	for(var i=0;i < objSelect.options.length;i++)
	{
		if (objSelect.options[i].innerText.toLowerCase() == obj.value.toLowerCase())
		{
			return;
		}
	}
	
	var objOption = new Option(obj.value, -1, true, true);
	
	objSelect.options[objSelect.options.length] = objOption;		
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.05.11
Description:	populates an element (INPUT hidden) with the order of the
				OPTIONs in a SELECT element
				Produces a pipe delimited string of the value of each OPTION
				and a pipe delimited string of the text of each OPTION concatenated
				using a broken bar (¦) [Alt+0166, Unicode 00A6]
				ex. "1|2|3¦Option1|Option2|Option3"
Parameters:
	strSelectId		ID of the SELECT element
	strReturnId		ID of the output element (requires a Value property; such as INPUT hidden)
-------------------------------------------------------------------------------
*/
function optionOrder(strSelectId, strResultId)
{
	var objSelect = document.getElementById(strSelectId);
	var objReturn = document.getElementById(strResultId);
	
	var strText = new String();
	var strValue = new String();
	
	for (var i=0; i < objSelect.options.length; i++)
	{
		
		if (!strValue.length == 0)
		{
			strValue += "|";
			strText += '|';
		}
		
		strValue += objSelect.options[i].value;
		strText += objSelect.options[i].innerText;
	}
	
	objReturn.value = strValue + '¦' + strText;	
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.04.11
Description:	Selects a node in a rendered TreeView control.
Parameters:
		data		Reference to the javascript object created by ASP.NET:
						{TreeView.ClientID}_Data
		node		Reference to the node that was clicked
Revisions:
	2007.06.01 - aleonard:	Changed to use the local versions of AppendClassName
							and RemoveClassName
							Also adds/removes hilighting for the node's parent TD
-------------------------------------------------------------------------------
*/
function SelectedNode(data, node, value)
{
	var treeContainer = node;
	
	//Get a reference to the TreeView container DIV
	do 
	{
		treeContainer = WebForm_GetParentByTagName(treeContainer, "DIV");
	}
	while (treeContainer.className != data.treeViewClass)

	var Elements = treeContainer.getElementsByTagName("*");

	//Loop thru the elements in the TreeView to remove the selectedClass
	for(var i in Elements)
	{
		var oEL = Elements[i];
		//Make sure the element has a tagName and that it's a anchor or table cell
		if (oEL.tagName != 'undefined' && oEL.tagName != null && (oEL.tagName.toUpperCase() == 'A' | oEL.tagName.toUpperCase() == 'TD'))
		{
			RemoveClassName(oEL, data.selectedClass);
		}
	}

	//Set the selectedNodeID
    data.selectedNodeID.value = node.id;
	data.selectedValueField.value = value;

	//Highlight the selected node & its parent TD
    AppendToClassName(node, data.selectedClass);
    node = WebForm_GetParentByTagName(node, "TD");
    AppendToClassName(node, data.selectedClass);
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.06.01
Description:	Appends the className to the elements class attribute
				This is a modified version of what MicroSoft provides in the
				embedded WebResourse.axd, which did not produce the desired
				result for the treeview
Parameters:
		oEL			Reference to the element
		className	CSS class name to append
-------------------------------------------------------------------------------
*/
function AppendToClassName(oEL, className)
{
	if (oEL != 'undefined' && oEL != null)
	{
		var current = oEL.className;
		//The order is important, so put the className up front
		if (current)
		{
			current = className + " " + current;
		}
		else
		{
			current = className;
		}
		oEL.className = current;
	}
}

/*
-------------------------------------------------------------------------------
Author:			aleonard
Date:			2007.06.01
Description:	Removes all instances of a className from the elements class attribute
				This is a modified version of what MicroSoft provides in the
				embedded WebResourse.axd, which did not produce the desired
				result for the treeview
Parameters:
		oEL			Reference to the element
		className	CSS class name to remove
-------------------------------------------------------------------------------
*/
function RemoveClassName(oEL, className)
{
	if(oEL != 'undefined' && oEL != null)
	{
		var current = oEL.className;

		if (current)
		{
			/*
			To replace all instances (case insensitive), the regular expression
			version of replace is needed. Since our replacment string is in a
			variable, creating a new RegExp is required. The "gi" argument
			specifies: "g" = global/all instances and "i" = case insensitive
			*/
			oEL.className = current.replace(new RegExp(className,"gi"),'').trim();
		}
	}
}

/*
-------------------------------------------------------------------------------
Author:			wfitzpatrick
Date:			2008.03.13
Description:	Validates the date value is in a valid date
				result for the treeview
Parameters:
		Date    Date value
-------------------------------------------------------------------------------
*/
function IsDate(date) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    if ((date.match(RegExPattern)) && (date!='')) {
        return true;
    } 
    else {
        return false;
    } 
}
