/*'***************************************************************	
'* Program: 					General_Functions.js
'*
'* Created By/Date:		James Laughlin – 7/17/2002
'*										
'* Purpose						I created this include file to hold general
'*										client-side functions that are called from
'*										many places.
'*										
'*										It is my intent that, over time, functions
'*										that are very general, but included in 
'*										more specifically designed .js files will 
'*										be moved to this file.
'*										
'******************************************************************/

function disableRadios(vstrName, vboolDisable){
	//disables or enables a group of radio buttons
	var i
	var objRdo = document.getElementsByName(vstrName)
	
	switch (vboolDisable){
		case 'true':
			for (i=0;i<objRdo.length;i++){
				objRdo(i).disabled = vboolDisable;
				//alert(objRdo(i).value);
			}
		case 'false':	
			for (i=0;i<objRdo.length;i++){
				objRdo(i).disabled = vboolDisable;
				//alert(objRdo(i).value);
			}
		default:
			//just toggle it 
			if(objRdo(0).disabled == true){
				for (i=0;i<objRdo.length;i++){
					objRdo(i).disabled = false;
					//alert(objRdo(i).value);
				}
			}else{
				for (i=0;i<objRdo.length;i++){
					objRdo(i).disabled = true;
					//alert(objRdo(i).value);
				}
			}
	}
}

function getRadioValue(vstrName){
	//returns the value of the radio button that is selected in a group
	var i
	var returnValue
	var objRdo = document.getElementsByName(vstrName)
	
	for (i=0;i<objRdo.length;i++){
		if (objRdo(i).checked == true){
			returnValue = objRdo(i).value
		}
	}
	
	return returnValue;
}

function showHideObject(vTargetObjectID, vBoolForceOnOff){
/*'***************************************************************	
'* Program: 					showHideObject()
'*
'* Created By/Date:		James Laughlin - 7/19/2002
'*
'* Purpose: 					Shows or Hides the passed in object
'*
'* Return Value:			NONE
'*
'* Input Parameters:	vTargetObjectID - string - the ID of the object
'*										to be shown or hidden
'*										
'*										vBoolForceOnOff - boolean - optional argument
'*										but when set forces the display attribute
'*										of the span.
'*											false = "style.display = 'none';"
'*											true = "style.display = 'inline';"
'*										if you don't pass a value, the function 
'*										simply toggles the state of the object.
'*										
'*										
'*										
'* Modified:					Person / Date / Reason:
'*	
'******************************************************************/
	var Object, Button
	
	Object = document.getElementById(vTargetObjectID)
//	Button = event.srcelement;
	
	//alert (Button)
	
	switch (vBoolForceOnOff) { 
		case false :
					Object.style.display = 'none';
					break;
		case true : 
					Object.style.display = 'inline';
					break;
		default :  
		    if(Object.style.display == 'none'){
					Object.style.display = 'inline';
				}else{
					Object.style.display = 'none';
				}
				break;
	} //switch
}
	
	function selectDefaults(strSelectName, arrSelections, intIndex){
	/*'***************************************************************	
		'* Program: 					selectDefaults()
		'*
		'* Created By/Date:		James Laughlin – 4/26/2002
		'*										
		'* Purpose: 					this function accepts the name of a select 
		'*										box (multi) to loop through and an array 
		'*										of values to compare the option values 
		'*										with...selects those that it finds in 
		'*										the array.
		'*									
		'* Return Value:			[NONE]
		'*
		'* Input Parameters:	strSelectName - Name of the select control
		'*																		to loop through
		'*										
		'*										arrSelections - Array of values that will
		'*																		selected in the control
		'*										
		'* Modified:	Person / Date / Reason:
		'*	
		'******************************************************************/

		if (arguments.length == 2)
		{
			intIndex = 0;
		}
		
		var i, j;
		var objSelect = document.getElementById(strSelectName);

		for (j=0;j<arrSelections.length;j++)
		{
			for (i=0;i<objSelect.options.length;i++)
			{
				if (objSelect.options[i].value == arrSelections[j][intIndex])
				{
					objSelect.options[i].selected = true
				}
			}
		}
	}

	function AdjustCounter(Control)
	{
		var strCommand; 
			
		if (arguments.length == 0)
		{
			Control = event.srcElement;
		}
			
		strCommand = 'try { taMaxLength(document.getElementById(\'' + Control.id + '\'), document.getElementById(\'' + Control.Counter + '\'), ' + Control.CounterMax + '); } catch(e) { alert(\'The stringUtils.js file does not seem to have been included.\\r\\nCounting can no be done.  This text field wil be disabled.\'); document.getElementById(\'' + Control.id + '\').disabled=true; }';
		setTimeout(strCommand, 10);
	}

function CheckBackStop1()
{
	var CurrentBackStopKey = GetCookie('LOGIN_INFO', 'BACK_STOPPER');

	//alert(CurrentBackStopKey);
	if (document.getElementById('BackStopperKey') != null)
	{
		if (document.getElementById('BackStopperKey').value != CurrentBackStopKey)
		{
			alert('The Back button may not be used to return to this page.  Please use system navigation instead.');
			//alert(history.length);
			history.forward();
		}
	}
}

function CheckBackStop()
{
	var CurrentBackStopKey = GetCookie('LOGIN_INFO', 'BACK_STOPPER');

	//alert(CurrentBackStopKey);
	if (document.getElementById('BackStopperKey') != null)
	{
		if (document.getElementById('BackStopperKey').value != CurrentBackStopKey)
		{
			//alert('The Back button may not be used to return to this page.  Please use system navigation instead.');
			//alert(history.length);
			DocumentLoad('/SystemMsg/NoBack.asp');;
		}
	}
}

function DocumentLoad(strURL)
{
	location.href=strURL;
}


function SubmitDisable(strSubmitID)
{
	if (arguments.length == 0)
	{
		strSubmitID = 'Submit';
		if (!isElement(strSubmitID))
		{
			strSubmitID = 'btnSubmit';
			if (!isElement(strSubmitID))
			{
				strSubmitID = 'btnSave';
				if (!isElement(strSubmitID))
				{
					return;
				}
			}
		}
	}
	
	if (isElement(strSubmitID))
	{
		document.getElementById(strSubmitID).disabled = true;
	}
}

function SubmitEnable(strSubmitID)
{
	if (arguments.length == 0)
	{
		strSubmitID = 'Submit';
		if (!isElement(strSubmitID))
		{
			strSubmitID = 'btnSubmit';
			if (!isElement(strSubmitID))
			{
				strSubmitID = 'btnSave';
				if (!isElement(strSubmitID))
				{
					return;
				}
			}
		}
	}

	if (isElement(strSubmitID))
	{	
		if (!event.returnValue)
		{
			document.getElementById(strSubmitID).disabled = false;
		}
	}
}

function SetToFirst()
{
	try
	{
		document.forms[0].elements[0].focus();
	}
	catch (e)	
	{}
}
	
function DisableAll(){
try		{
	var intCounter, arrForm, iFormCount;
						
	for(iFormCount=0;iFormCount<document.forms.length;iFormCount++)	{
		arrForm = document.forms[iFormCount].elements;
		for (intCounter = 0; intCounter < arrForm.length; intCounter++)	{
			try		{
				if (arrForm[intCounter].type != 'submit')	{
					if (arrForm[intCounter].getAttribute('AllowDisable') != 'no')	{
						arrForm[intCounter].disabled = true;
					}	}	}
			catch (e2){}
			try	{
				if (arrForm[intCounter].getAttribute('AllowDisable') != 'no')
					arrForm[intCounter].readonly = true;
			}
			catch (e3)	{}
		}}
			
	arrForm = document.getElementsByTagName('IMG');
	for (intCounter = 0; intCounter < arrForm.length; intCounter++){
		try	{
			if (arrForm[intCounter].onclick != null){
				if (arrForm[intCounter].getAttribute('AllowDisable') != 'no')	{
					arrForm[intCounter].onclick = null;
					arrForm[intCounter].style.cursor = '';
					if (arrForm[intCounter].alt != '')
						arrForm[intCounter].alt += ' - Disabled';
				}	}	}
		catch(e){}
	}	}
catch (e)	{}}

function Left(strString, intLength)
/**************************************************************************
*	Emulates the VB function LEFT
*
*	Input :
*		strString - a string value a part of which will be returned
*		intLength - an integer value specifiying how many chars to return
*
*	Return Value :
*		a string value with the left part of the original string
**************************************************************************/
{
	return strString.substring(0, intLength);
}

function Right(strString, intLength)
/**************************************************************************
*	Emulates the VB function RIGHT
*
*	Input :
*		strString - a string value a part of which will be returned
*		intLength - an integer value specifiying how many chars to return
*
*	Return Value :
*		a string value with the right part of the original string
**************************************************************************/
{
	var intStart;
	var intEnd;
	
	intStart = strString.length - intLength;
	intEnd = intStart + intLength;
	
	return strString.substring(intStart, intEnd);
}

function Mid(strString, intStart, intLength)
/**************************************************************************
*	Emulates the VB function MID
*
*	Input :
*		strString - a string value a part of which will be returned
*		intStart  - an integer value specifiying the index at which to start
*		intLength - an OPTIONAL integer value specifiying how many chars to return
*
*	Return Value :
*		a string value with the part of the original string
**************************************************************************/
{
	if (arguments.length == 2)
	{
		return strString.substr(intStart);
	}
	else
	{
		return strString.substr(intStart, intLength);
	}
}

function Replace(strString, strFindString, strReplaceString)
/**************************************************************************
*	Emulates the VB function REPLACE
*
*	Input :
*		strString - a string value to search
*		strFindString - a string value that will be searched
*		strReplaceString - a string value that will replace the found value
*
*	Return Value :
*		a string value with the found string replaced
**************************************************************************/
{
	var strTemp = strString;
	var intLocation = strTemp.indexOf(strFindString, 0);
	var intLength = strFindString.length;
	var intReplaceLength = strReplaceString.length;

	while (intLocation != -1)
	{
		strTemp = strTemp.substring(0, intLocation) + strReplaceString + strTemp.substr(intLocation + intLength);
		intLocation = strTemp.indexOf(strFindString, (intLocation + intReplaceLength));
	}
	return strTemp;
}

function split(strString, strDelimiter)
{
	var array = new Array();
	var strTemp = strString;
	var strValue;
	var intLocation = strTemp.indexOf(strDelimiter, 0)
	
	while (intLocation != -1)
	{
		strValue = strTemp.substring(0, intLocation);
		strTemp = strTemp.substring(intLocation + 1);
		array[array.length] = strValue;
		intLocation = strTemp.indexOf(strDelimiter, 0);
	}
	array[array.length] = strTemp;
	
	return array;
}

function Trim(strString)
/**************************************************************************
*	Emulates the VB function TRIM
*
*	Input :
*		strString - a string value a part of which will be returned
*
*	Return Value :
*		a string value with the leading and trailing spaces removed
**************************************************************************/
{
	var strTemp = strString;
	
	while(strTemp.charAt(0) == " ")
	{
		strTemp = strTemp.substring(1,strTemp.length); 
	}
	
	while(strTemp.charAt(strTemp.length-1) == " ")
	{
		strTemp = strTemp.substring(0,strTemp.length-1); 
	}
	
	return strTemp;
}
