/**********************************************************************************
*  	File:		library1.js
*	Description:	JavaScript library file for Medi-Cal JavaScript functions that
*					are used across multiple Web Pages.
*	Machine:	Webserver1, Webserver2 - medi-cal site
*	Path:		www.medi-cal.ca.gov/SSI
*   	Where Used:	All public and transaction services Web Pages.
*
*                     M O D I F I C A T I O N S
*No       SE Name              Date       Description
*---  --------------------  ----------  --------------------------------------------
*01   Jim Pratt			 	11/01/2007  SDN07010 - New for the Governor's New look 
*											and Feel
**********************************************************************************/

/**********************************************************************************
*  	Function:	GetElementNumber
*	Description:	Determines the form element number of the field passed in
*	Input parms:	1)	object: Form object
*			2)	string:	name of the field
*   	Success Return:	integer:	element number of the form object that
*			matches the name passed in
*	Failure Return:	integer:	-1
*
*                     M O D I F I C A T I O N S
*No       SE Name              Date       Description
*---  --------------------   ----------  -----------------------------------------
*01   Seth Munroe            09/03/1998  SDN6042B - Created.
**********************************************************************************/
function GetElementNumber(objForm, FieldName)	{

	var ElementReturn = 0

	if (objForm.elements.length > 0)
	{
		while ((ElementReturn <= objForm.elements.length) && (objForm.elements[ElementReturn].name != FieldName))
		{
			ElementReturn += 1
		}
		if (ElementReturn > objForm.elements.length)
		{
			return -1
		}
		else
		{
			return ElementReturn
		}
	}
	else
	{
		return -1
	}
}
/**********************************************************************************
*  	Function:	SetHelp
*	Description:	Sets the value of the HelpField to an integer corresponding
*			to the form element number of the field passed in
*	Requirements:	The form passed in must have hidden input named HelpField
*	Input parms:	1)	object: Form
*			2)	object:	field
*
*                     M O D I F I C A T I O N S
*No       SE Name              Date       Description
*---  --------------------   ----------  -----------------------------------------
*01   Seth Munroe            09/03/1998  SDN6042B - Created.
**********************************************************************************/
function SetHelp(objForm, objField)	{

	objForm.HelpField.value = GetElementNumber(objForm, objField.name)
}



/**********************************************************************************
*  Function:		URLEncode
*	Description:	Converts string to a URLEncoded string:
*					alpha characters and numbers 0 -9 are un altered,
*					spaces are replaced with a "+"
*					all others are converted to their hex values and preceded
*					with a "%"
*	Restrinctions:	Hex values of 80 - 9F (dec. 128 - 159) do not convert in
*					Netscape. (once we are no longer supporting Netscape 3.x,
*					this can be altered to use String.fromCharCode() and
*					stringobject.charCodeAt())
*	Input parms:	String:	string to be converted
*	Return:			String:	URLEncoded string
*
*                     M O D I F I C A T I O N S
*No       SE Name              Date       Description
*---  --------------------   ----------  -----------------------------------------
*01   Jim Yim                03/13/2000  MCNet - copied from Eligibility.asp.
**********************************************************************************/
function URLEncode(strToBeConverted)	{

	var intNextHit

	if (strToBeConverted.length > 0)
	{
		strToBeConverted = escape(strToBeConverted)

		intNextHit = strToBeConverted.indexOf("*")

		while (intNextHit >= 0)
		{
			strToBeConverted = strToBeConverted.substring(0,intNextHit) + "%2A" + strToBeConverted.substring(intNextHit + 1)
			intNextHit = strToBeConverted.indexOf("*", intNextHit)
		}

		intNextHit = strToBeConverted.indexOf("+")

		while (intNextHit >= 0)
		{
			strToBeConverted = strToBeConverted.substring(0,intNextHit) + "%2B" + strToBeConverted.substring(intNextHit + 1)
			intNextHit = strToBeConverted.indexOf("+", intNextHit)
		}

		intNextHit = strToBeConverted.indexOf("-")

		while (intNextHit >= 0)
		{
			strToBeConverted = strToBeConverted.substring(0,intNextHit) + "%2D" + strToBeConverted.substring(intNextHit + 1)
			intNextHit = strToBeConverted.indexOf("-", intNextHit)
		}

		intNextHit = strToBeConverted.indexOf(".")

		while (intNextHit >= 0)
		{
			strToBeConverted = strToBeConverted.substring(0,intNextHit) + "%2E" + strToBeConverted.substring(intNextHit + 1)
			intNextHit = strToBeConverted.indexOf(".", intNextHit)
		}

		intNextHit = strToBeConverted.indexOf("/")

		while (intNextHit >= 0)
		{
			strToBeConverted = strToBeConverted.substring(0,intNextHit) + "%2F" + strToBeConverted.substring(intNextHit + 1)
			intNextHit = strToBeConverted.indexOf("/", intNextHit)
		}

		intNextHit = strToBeConverted.indexOf("@")

		while (intNextHit >= 0)
		{
			strToBeConverted = strToBeConverted.substring(0,intNextHit) + "%40" + strToBeConverted.substring(intNextHit + 1)
			intNextHit = strToBeConverted.indexOf("@", intNextHit)
		}

		intNextHit = strToBeConverted.indexOf("_")

		while (intNextHit >= 0)
		{
			strToBeConverted = strToBeConverted.substring(0,intNextHit) + "%5F" + strToBeConverted.substring(intNextHit + 1)
			intNextHit = strToBeConverted.indexOf("_", intNextHit)
		}

		intNextHit = strToBeConverted.indexOf("%20")

		while (intNextHit >= 0)
		{
			strToBeConverted = strToBeConverted.substring(0,intNextHit) + "+" + strToBeConverted.substring(intNextHit + 3)
			intNextHit = strToBeConverted.indexOf("%20", intNextHit)
		}
	}
	return strToBeConverted
}
/**********************************************************************************
* 	Function:		URLDecode
*	Description:	Converts string from URLEncoded format to appropriate
*					characters
*	Restrinctions:	Hex values of 80 - 9F (dec. 128 - 159) do not convert in
*					Netscape. (once we are no longer supporting Netscape 3.x,
*					this can be altered to use String.fromCharCode() and
*					stringobject.charCodeAt())
*	Input parms:	String:	URLEncoded string to be converted
*	Return:			String:	Converted string
*
*                     M O D I F I C A T I O N S
*No       SE Name              Date       Description
*---  --------------------   ----------  -----------------------------------------
*01   Jim Yim                03/13/2000  MCNet - copied from Eligibility.asp.
**********************************************************************************/
function URLDecode(strToBeConverted)	{

	var strToBeReturned = "", intNextHit

	if (strToBeConverted.length > 0)
	{
		intNextHit = strToBeConverted.indexOf("+")

		while (intNextHit >= 0)
		{
			strToBeConverted = strToBeConverted.substring(0,intNextHit) + "%20" + strToBeConverted.substring(intNextHit + 1)
			intNextHit = strToBeConverted.indexOf("+", intNextHit)
		}
		strToBeReturned = unescape(strToBeConverted)
	}
	return strToBeReturned
}

/**********************************************************************************
*  Function:		GetCookie
*	Description:	Pulls a specific cookie out of the cookie string
*	Input parms:	1)	string: Name of cookie to return
*	Requirements:	URLDecode function
*  Success Return:	string:	value of the cookie
*  Fail Return:	string:	empty string
*
*                     M O D I F I C A T I O N S
*No       SE Name              Date       Description
*---  --------------------   ----------  -----------------------------------------
*01   Jim Yim                03/13/2000  MCNet - copied from Eligibility.asp.
**********************************************************************************/
function GetCookie(strName)	{

	var intStartPosition = 0, intEndPosition = 0, strCookie
	
	if (strName.length == 0)
	{
		return ""
	}
	else
	{
		strCookie = new String(document.cookie)
	
		if (strCookie.length == 0)
		{
			return ""
		}
		else
		{
			intStartPosition = strCookie.indexOf(strName + "=" )
	
			if (intStartPosition < 0)
			{
				return ""
			}
			else
			{
				intEndPosition = strCookie.indexOf(";", intStartPosition)
	
				if (intEndPosition < 0 || intEndPosition >= strCookie.length)
				{
					intEndPosition = (strCookie.length)
				}
				return URLDecode(strCookie.substring(intStartPosition + strName.length + 1, intEndPosition))
			}
		}
	}
}

/**********************************************************************************
*  Function:		SetCookie
*	Description:	Sets a cookie - optional fields may be set to false
*	Requirements:	URLEncode function
*	Input parms:	1)	string:	Name of cookie
*	Input parms:	2)	string:	value of cookie
*	Input parms:	3)	opt	string:	expiration date
*	Input parms:	4)	opt	string:	path cookie should be served to
*	Input parms:	5)	opt	string:	domain or server cookies should be served
*	Input parms:	6)	bool: secure (if true, server will only see	cookie if SSL is on.
*
*                     M O D I F I C A T I O N S
*No       SE Name              Date       Description
*---  --------------------   ----------  -----------------------------------------
*01   Jim Yim                03/13/2000  MCNet - copied from Eligibility.asp.
**********************************************************************************/
function SetCookie (name,value,expires,path,domain,secure)
{
    document.cookie = name + '=' + URLEncode(value) +
        ((expires) ? '; expires=' + expires.toGMTString() : '') +
	((path) ? '; path=' + path : '') +
	((domain) ? '; domain=' + domain : '') +
	((secure) ? '; secure' : '')
}

/**********************************************************************************
*  	Function:	delCookie
*	Description:	Sets a cookie - optional fields may be set to false
*			pathing is set differently depending on whether the
*			cookie was set on the from javascript on the client
*			or ASP from the server. For this reason, there are 2
*			deletes when the path is not set.
*	Input parms:	1)	string:	Name of cookie
*	Input parms:	2)	string:	path cookie was set to
*	Input parms:	3) 	string:	domain or server cookie was set for
*
*                     M O D I F I C A T I O N S
*No       SE Name              Date       Description
*---  --------------------   ----------  -----------------------------------------
*01   Seth Munroe            09/03/1998  SDN6042B - Created.
**********************************************************************************/
function delCookie(name, path, server) {

	document.cookie = name + "=" + "; expires=Thu, 01-Jan-80 00:00:01 GMT" + ((path) ? "; path=" + path : "") + ((server) ? "; domain=" + server : "")

	if (path)
	{
		// Do Nothing
	}
	else
	{
		document.cookie = name + "=" + "; expires=Thu, 01-Jan-80 00:00:01 GMT" + "; path=" + "; domain=" + ((server) ? server : "")
	}
}

