//*********************************************
// Utilities.js
// Functions called in this page:
//*********************************************
// function CheckIE
// function GetItemsFromList
// function IsAlphaNumeric
// function IsNotEmpty
// function ResetBackgroundColor
// function OpenFactsheet
// function OpenWindow
// function Trim
// function TrimText
//*******************************
// function CheckIE
// Are we running this in IE (and in the proper version of IE?)
//*******************************
function CheckIE(pstrRootPath)
{
	var userAgent = navigator.userAgent;
	var MSIEIndex = userAgent.indexOf("MSIE");
	if (userAgent.indexOf("Win")  == -1 ||
		userAgent.indexOf("MSIE") == -1 ||
		userAgent.substring((MSIEIndex + 5),(MSIEIndex + 6)) < 5)
		window.location.replace(pstrRootPath + "/default.htm");
}
//*******************************
// function isAlphaNumeric
// Does this string contain
// just alphanumeric chars?
//*******************************
function isAlphaNumeric(strTextValue)
{
  var sCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789/";		
  var checkStr = strTextValue
  var allValid = true;
  var i, j;
  var ch
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < sCharset.length;  j++)
      if (ch == sCharset.charAt(j))
        break;
    if (j == sCharset.length)
    {
      allValid = false;
      break;
    }
  }
  return (allValid);
}
//*******************************
// function GetItemsFromList
// Based on the value of sValueType
// Return a comma-delimited set of IDs from selected items from a list box
// OR a list of text items from selected items of a list box
//*******************************
function GetItemsFromList(lstBox,sValueType)
{
var iCollectionCount = lstBox.length;
var sIDList = ""
	
    if (iCollectionCount != null)
    {
		for(var iCollectionCounter=0; iCollectionCounter < iCollectionCount; iCollectionCounter++)
		{
			if(lstBox[iCollectionCounter].selected)
			{							
				if (sIDList > "")
				{
					if (sValueType == "value")
						sIDList = sIDList + ","
					else
						sIDList = sIDList + " OR "	
				}	
				if (sValueType == "value")				
					sIDList = sIDList + lstBox[iCollectionCounter].value
				else					
					sIDList = sIDList + "<b>" + lstBox[iCollectionCounter].text + "</b>"
			}	
		}
	}	
	
	return sIDList;
}
//*******************************
// function GetItemsFromMultiList
// Based on the value of sValueType
// Return a comma-delimited set of IDs from a multi list box
// OR a list of text items from selected items of a list box
//*******************************
function GetItemsFromMultiList(lstBox,sValueType)
{
var iCollectionCount = lstBox.length;
var sIDList = ""
	
    if (iCollectionCount != null)
    {
		for(var iCollectionCounter=0; iCollectionCounter < iCollectionCount; iCollectionCounter++)
		{
			if (sIDList > "")
			{
				if (sValueType == "value")
					sIDList = sIDList + ","
				else
					sIDList = sIDList + " OR "	
			}	
			if (sValueType == "value")				
				sIDList = sIDList + lstBox[iCollectionCounter].value
			else					
				sIDList = sIDList + "<b>" + lstBox[iCollectionCounter].text + "</b>"
		}	
	}	
	return sIDList;
}

//*******************************
// function IsNotEmpty
// Is the value in this passed object
// not empty?
//*******************************
function IsNotEmpty (thisForm,thisObject,sObjectDesc, sType)
{	
	var RequiredFields = new Array;
	var sCategory = ""	
	var bReturnValue	
	// set all text object background color to white
	ResetBackgroundColor (thisForm)
	
	thisObject.value = Trim(thisObject.value)	
	
	RequiredFields[RequiredFields.length] = new Array(thisObject, sType, sObjectDesc,true);
	bReturnValue = GenerateErrorList (RequiredFields,sCategory);
	return bReturnValue
}
//*******************************
// function OpenFactsheet
// Open Factsheet
//*******************************
function OpenFactsheet(URL)
{
	window.open(URL,"KMPH","height=480,width=670,left=10,top=10,toolbar,scrollbars,resizable,status");
}
//*******************************
// function OpenWindow
// Open an additional page from calling page
//*******************************
function OpenWindow(URL,pblnFull)
{
	if (pblnFull)
		window.open(URL,"KMPH","height=450,width=650, left =100,top = 100, scrollbars, status,resizable");
	else
		window.open(URL,"KMPH","height=250,width=200, left =200,top = 200, scrollbars, status,resizable");
}

//*******************************
// function ResetBackgroundColor
// Reset Background color on all text objects
// to white (IE only)
//*******************************
function ResetBackgroundColor(thisForm)
{
	var intCounter
	var sElementType
	var oFormObject
	//*******************************
	// Loop through objects on form
	//*******************************
	for (intCounter=0; intCounter < thisForm.elements.length; intCounter++)
	{
		oFormObject = thisForm.elements[intCounter]
		sElementType = oFormObject.type
        if ((sElementType == "text") || (sElementType == "textarea"))
		{
			if (navigator.appName != "Netscape")
				oFormObject.style.backgroundColor = "white"
		}
	}	
}
//*******************************
// function Trim
// Take out leading & trailing spaces,
// carriage returns & line feeds from
// incoming string
//*******************************
function Trim(sString)
{
	var iCounter=0;
	var OrigLen=sString.length;	
	while ( (sString.charCodeAt(iCounter)==32 || sString.charCodeAt(iCounter)==10 || sString.charCodeAt(iCounter)==13) && (iCounter < sString.length) )
		iCounter++;
	if (iCounter != 0)
		 sString = sString.substring(iCounter,OrigLen);
	while ( (sString.charCodeAt(sString.length-1)==32 || sString.charCodeAt(sString.length-1)==10 || sString.charCodeAt(sString.length-1)==13) )
		sString=sString.substring(0,sString.length-1)
	
	return sString;
}

//*******************************
// function TrimText
// Take out leading & trailing spaces,
// carriage returns & line feeds from
// all text & textarea form objects
//*******************************
function TrimText(thisForm)
{
	var intCounter
	var sElementType
	var oFormObject
	//*******************************
	// Loop through objects on form
	//*******************************
	for (intCounter=0; intCounter < thisForm.elements.length; intCounter++)
	{
		oFormObject = thisForm.elements[intCounter]
		sElementType = oFormObject.type
        if ((sElementType == "text") || (sElementType == "textarea"))
			oFormObject.value = Trim(oFormObject.value)
		
	}	
}
