/*
     Copyright 2005 Speartek, Inc.

     All rights reserved.

     All materials contained in this file are protected by United States copyright law
     and may not be reproduced, distributed, transmitted, displayed, published or
     broadcast without the prior written permission of Speartek, Inc.
     You may not alter or remove this notice.
*/
function tAllHiddenInput(obj, objName)
{
	var aryTmp = new Array();
	var strSelect = '';
		for(var i = 0; i < obj.length; i++)
		{
			aryTmp = obj[i];
			strSelect += '<input type="hidden" name="' + objName + '_' + obj[i][0] + '" id="' + objName + '_' + obj[i][0] + '" value="' + obj[i][1] + '"/>';
		}
	//alert(strSelect);
	document.write(strSelect);
}//end of tAllHiddenInput()
/*	obj is the Type Array
	name is the name of the value
*/
function tGetTypeID(obj, name)
{
	var aryTmp = new Array();
		for(var i = 0; i < obj.length; i++)
		{
			aryTmp = obj[i];
			if(aryTmp[0].toUpperCase() == name.toUpperCase()){return aryTmp[1]}
		}
		return null;
}//end of tGetTypeID()
function tHiddenInput(obj, name, tagName)
{	
	document.write('<input type="hidden" name="' + tagName + '" id="' + tagName + '" value="' + tGetTypeID(obj, name) + '"/>');
}
/*	obj is the Type Array
	selected is the name of the value
	name is the name of the tag
*/
function tSelect(obj, selected, tagName, aryDisplay, blnSort, addlOptions)
{
	var aryTmp = new Array();
	var strSelect = '<select id="' + tagName + '"  name="' + tagName + '" ' + addlOptions + '>';
		if(arguments[3])
		{
			for(var i = 0; i < aryDisplay.length; i++)
			{
				strSelect += '<option value="' + aryDisplay[i][1] + '">' + aryDisplay[i][0] + '</option>';
			}
		}
		if (arguments[4])
		{
			obj.sort(); //sort by first element -psb
		}
		for(var i = 0; i < obj.length; i++)
		{
			if(obj[i][1] == selected)
			{
				strSelect += '<option value="' + obj[i][1] + '" selected>' + obj[i][0] + '</option>';
			}else{
				strSelect += '<option value="' + obj[i][1] + '">' + obj[i][0] + '</option>';
			}
		}
		strSelect += '</select>';
	document.write(strSelect);
}
/*	obj is the Type Array
	selected is the name of the value
	name is the name of the tag
*/
function tMultiSelect(obj, selected, tagName, aryDisplay, blnSort)
{
	var aryTmp = new Array();
	var strSelect = '<select name="' + tagName + '" id="' + tagName + '" multiple size="4">';
		if(arguments[3])
		{
			for(var i = 0; i < aryDisplay.length; i++)
			{
				strSelect += '<option value=\"' + aryDisplay[i][1] + '\">' + aryDisplay[i][0] + '</option>';
			}
		}		
		if (arguments[4])
		{
			obj.sort(); //sort by first element -psb
		}
		for(var i = 0; i < obj.length; i++)		
		{
			if(obj[i][1] == selected)
			{
				strSelect += '<option value=\"' + obj[i][1] + '\" selected>' + obj[i][0] + '</option>';
			}else{
				strSelect += '<option value=\"' + obj[i][1] + '\">' + obj[i][0] + '</option>';
			}
		}
		strSelect += '</select>';
	document.write(strSelect);
}
/*	obj is the Type Array
	selected is the name of the value
	name is the name of the tag
*/
function tCheckBox(obj, selected, tagName)
{
	var aryTmp = new Array();
	var strSelect = '';
	try
	{
		for(var i = 0; i < obj.length; i++)
		{
			aryTmp = obj[i];
			if(aryTmp[1] == selected)
			{
				strSelect += aryTmp[0] + '<input type="checkbox" value="' + aryTmp[1] + '" name="' + tagName + '" ="' + tagName + '" />';
			}else{
				strSelect += aryTmp[0] + '<input type="checkbox" value="' + aryTmp[1] + '" name="' + tagName + '" id="' + tagName + '" checked />';
			}
		}
		strSelect += '</select>';
	}
	catch(er)
	{
	}
	document.write(strSelect);
}
//#############################################################################################
//############################## Security Functions ###########################################
//#############################################################################################
function tSelectAllSecurity(formName)
{
	try
	{
		if(document[formName]['AllSecurityLevels'].checked)
		{
			for(var i = 0; i < SECURITY_TYPES.length; i++)
			{
				if(document[formName][SECURITY_TYPES[i][0]])
				{
					document[formName][SECURITY_TYPES[i][0]].checked = true;
				}
			}
		}
		tSecuritySum(formName);
	}
	catch(er){}
}
function tSecuritySum(formName)
{
	try
	{
		var SecSum = 0;
		if(document[formName]['AllSecurityLevels'])
		{
			if(document[formName]['AllSecurityLevels'].checked)
			{
				SecSum = 2147483647;
			}else{
				for(var i = 0; i < SECURITY_TYPES.length; i++)
				{
					if(document[formName][SECURITY_TYPES[i][0]])
					{
						if(document[formName][SECURITY_TYPES[i][0]].checked)
						{
							SecSum += Math.pow(2, (SECURITY_TYPES[i][1] - 1));
						}
					}else if((SECURITY_TYPES[i][0].toUpperCase() == 'MASTERADMIN') || SECURITY_TYPES[i][0].toUpperCase() == 'SITEADMIN'){
						SecSum += Math.pow(2, (SECURITY_TYPES[i][1] - 1));
					}
				}
			}
			document[formName]['SecurityLevel'].value = SecSum;
		}
	}
	catch(er){}
}
function tIsChecked(tag, formName)
{
	try
	{
		tag = GetTag(tag);
		if(document[formName]['AllSecurityLevels'].checked)
		{
			if(!tag.checked)
			{
				tag.checked = true;
				throw 'You must uncheck All Security Levels before you can remove another Security Level';
			}
		}
		tSecuritySum(formName);
	}
	catch(er)
	{
		alert(er);
	}
}
function tSecurity(tag, formName, selectedValue)
{
	try
	{
		var strOut = '<table>';
		tag = GetTag(tag);

		strOut += '<tr><td><input type="checkbox" name="AllSecurityLevels" id="AllSecurityLevels" value="2147483647" onclick="tSelectAllSecurity(\'' + formName + '\');"';
		if(selectedValue == 2147483647){strOut += ' checked'}
		strOut += '/></td>';
		strOut += '<td class="Primary">&nbsp;All Security Levels</td></tr>';

		for(var i = 0; i < SECURITY_TYPES.length; i++)
		{
			if((SECURITY_TYPES[i][0].toUpperCase() != 'MASTERADMIN') && (SECURITY_TYPES[i][0].toUpperCase() != 'SITEADMIN'))
			{
				strOut += '<tr><td><input type="checkbox" name="' + SECURITY_TYPES[i][0] + '" id="' + SECURITY_TYPES[i][0] + '" value="' + Math.pow(2, (SECURITY_TYPES[i][1] - 1)) + '" onclick="tIsChecked(this, \'' + formName + '\');"';
				if(selectedValue & Math.pow(2, (SECURITY_TYPES[i][1] - 1))){strOut += ' checked'}
				strOut += '/></td>';
				strOut += '<td class="Primary">&nbsp;' + SECURITY_TYPES[i][0] + '</td></tr>'
			}
		}

		strOut += '<tr><td colspan="2"><input type="hidden" name="SecurityLevel" id="SecurityLevel" value=""/>';
		strOut += '<input type="hidden" name="MasterAdminSecurityTypeID" id="MasterAdminSecurityTypeID" value="' + tGetTypeID(SECURITY_TYPES, 'MasterAdmin').toString() + '"/>';
		strOut += '<input type="hidden" name="SiteAdminSecurityTypeID" id="SiteAdminSecurityTypeID" value="' + tGetTypeID(SECURITY_TYPES, 'SiteAdmin').toString() + '"/>';
		strOut += '</td></tr>';
		strOut += '</table>';
		tag.innerHTML = strOut;
		tSecuritySum(formName);
	}
	catch(er)
	{
	}
}
//#########################################################################################################
//################################ Second Gen Types Functions #############################################
//#########################################################################################################
/*	obj is the Type Array
	name is the name of the value
*/
function ttGetTypeID(aryTypes, typeName)
{
	try
	{
		for(var i = 0; i < aryTypes.length; i++)
		{
			if(aryTypes[i][0].toUpperCase() == typeName.toUpperCase()){return aryTypes[i][1]}
		}
	}
	catch(er)
	{
	}
}
function ttGetTypeName(aryTypes, typeId)
{
	try
	{
		for(var i = 0; i < aryTypes.length; i++)
		{
			if(aryTypes[i][1] == typeId){return aryTypes[i][0]}
		}
	}
	catch(er)
	{
	}
}
function ttSelect(tag, aryTypes, selectedID, formName, tagName, aryDisplay)
{
	try
	{
		var strSelect = '<select name="' + tagName + '" id="' + tagName + '">';

		if(arguments[5])
		{
			for(var i = 0; i < aryDisplay.length; i++)
			{
				strSelect += '<option value="' + aryDisplay[i][1] + '">' + aryDisplay[i][0] + '</option>';
			}
		}
		for(var i = 0; i < aryTypes.length; i++)
		{
			if(aryTypes[i][1] == selectedID)
			{
				strSelect += '<option value="' + aryTypes[i][1] + '" selected>' + aryTypes[i][0] + '</option>';
			}
			else
			{
				strSelect += '<option value="' + aryTypes[i][1] + '">' + aryTypes[i][0] + '</option>';
			}
		}
		strSelect += '</select>'
	}
	catch(er){}
	GetTag(tag).innerHTML = strSelect
}
function ttMultiSelect(tag, aryTypes, selectedID, formName, tagName, aryDisplay)
{
	//!!
	//!tag CANNOT EQUAL tagName!
	//!!
	try
	{
		var strSelect = '<select name="' + tagName + '" id="' + tagName + '" multiple>';
		tag = GetTag(tag);

		if(arguments[5])
		{
			for(var i = 0; i < aryDisplay.length; i++)
			{
				strSelect += '<option value="' + aryDisplay[i][1] + '">' + aryDisplay[i][0] + '</option>';
			}
		}
		for(var i = 0; i < aryTypes.length; i++)
		{
			if(aryTypes[i][1] == selectedID)
			{
				strSelect += '<option value="' + aryTypes[i][1] + '" selected>' + aryTypes[i][0] + '</option>';
			}else{
				strSelect += '<option value="' + aryTypes[i][1] + '">' + aryTypes[i][0] + '</option>';
			}
		}
		strSelect += '</select>';
	}
	catch(er)
	{
	}
	tag.innerHTML = strSelect;
	return document[formName][tagName];
}
function ttCheckBox(tag, aryTypes, selectedID, formName, tagName)
{
	try
	{
		var strSelect = '';
		tag  = GetTag(tag);

		for(var i = 0; i < aryTypes.length; i++)
		{
			if(aryTypes[i][1] == selectedID)
			{
				strSelect += aryTypes[i][0] + '<input type="checkbox" value="' + aryTypes[i][1] + '" name="' + tagName + '" id="' + tagName + '" checked />';
			}else{
				strSelect += aryTypes[i][0] + '<input type="checkbox" value="' + aryTypes[i][1] + '" name="' + tagName + '" id="' + tagName + '" />';
			}
		}
		strSelect += '</select>';
	}
	catch(er)
	{
	}
	tag.innerHTML = strSelect;
	return document[formName][tagName];
}
function ttHiddenInput(tag, aryTypes, typeName, formName, tagName)
{
	try
	{
		tag = GetTag(tag);
		tag.innerHTML = '<input type="hidden" name="' + tagName + '" id="' + tagName + '" value="' + tGetTypeID(aryTypes, typeName) + '"/>';
	}
	catch(er)
	{
	}
	return document[formName][tagName];
}
function ttAllHiddenInput(tag, aryTypes, tagName)
{
	try
	{
		var strSelect = '';
		tag = GetTag(tag);
		for(var i = 0; i < aryTypes.length; i++)
		{
			strSelect += '<input type="hidden" name="' + tagName + '_' + aryTypes[i][0] + '" id="' + tagName + '_' + aryTypes[i][0] + '" value="' + aryTypes[i][1] + '"/>';
		}
	}
	catch(er)
	{
	}
	tag.innerHTML = strSelect;
}

//FUNCTIONS TO INTERACT WITH ARRAYS
function GetArrayValue(ArrayName, ID, Column){
	// assumes the first column is the ID column - johnt 8/14/2001
	try	{
		var val = '' ;
		var i = 0 ;
		var found = false ;
		while((i < ArrayName.length) && (!found)){
			if(ArrayName[i][0] == ID){
				val = ArrayName[i][Column] ;
				found = true;
			}
			i++ ;
		}
		return val ;
	}
	catch(er){}
}

function GetArrayID(ArrayName, SearchString, SearchColumn, FilterString, FilterColumn)
{
	// assumes the first column is the ID column - johnt 9/18/2001
	// returns zero if id not found
	try
	{
		var val = 0 ;
		var i = 0 ;
		var found = false ;
		
		while((i < ArrayName.length) && (!found))
		{
			var filtermatch = true ;
			if(FilterString!=='')
			{
				filtermatch = (ArrayName[i][FilterColumn] == FilterString);
			}
			if(filtermatch)
			{
				var upperValue = ArrayName[i][SearchColumn].toString().toUpperCase();
				if(upperValue == SearchString.toString().toUpperCase())
				{
					val = ArrayName[i][0] ;
					found = true;
				}
			}
			i++ ;
		}
		return val ;
	}
	catch(er){}
}
function arySelect(aryName, valueColumn, displayColumn, selectedValue, tagName, aryDisplay, blnSort)
{
	var aryTmp = new Array();
	var strSelect = '<select name="' + tagName + '" id="' + tagName + '">';
		if(arguments[5])
		{
			for(var i = 0; i < aryDisplay.length; i++)
			{
				strSelect += '<option value=\"' + aryDisplay[i][1] + '\">' + aryDisplay[i][0] + '</option>';
			}
		}
		if (arguments[6])
		{
			aryName.sort(); //sort by first element -psb
		}
		for(var i = 0; i < aryName.length; i++)
		{
			if(aryName[i][valueColumn] == selectedValue)
			{
				strSelect += '<option value=\"' + aryName[i][valueColumn] + '\" selected>' + aryName[i][displayColumn] + '</option>';
			}else{
				strSelect += '<option value=\"' + aryName[i][valueColumn] + '\">' + aryName[i][displayColumn] + '</option>';
			}
		}
		strSelect += '</select>';
	document.write(strSelect);
}

function ttTargetSecurityType(tag, aryTypes, selectedID, formName, tagName)
{
	// This function is used to created a dropdown list of SecurityTypes based on the UIZones 
	// value passed in.  The main purpose of this function is to create the dropdown list
	// of securitytypes that a person can connect a login to.  It allows you to control 
	// the  security types that the person can see and select from

	try
	{
		var strSelect = '<select name=\"' + tagName + '\" id=\"id' + tagName + '\">';
		tag = GetTag(tag);

		for(var i = 0; i < aryTypes.length; i++)
		{
			if(aryTypes[i][1] == selectedID)
			{
				strSelect += '<option value=\"' + aryTypes[i][1] + '\" selected>' + aryTypes[i][0] + '</option>';
			}else{
				strSelect += '<option value=\"' + aryTypes[i][1] + '\">' + aryTypes[i][0] + '</option>';
			}
		}
		strSelect += '</select>';
	}
	catch(er)
	{
	}
	tag.innerHTML = strSelect;
	return document[formName][tagName];
}
