/*#############################################################################

FILE:	filterFieldValue.js

	Copyright (c) 2005 Navitaire Inc. All rights reserved.

	This source code is (i) proprietary to and a trade secret of Navitaire Inc.
	and (ii) protected by copyright law and international treaties.
	Unauthorized disclosure, reproduction, distribution or alteration of this
	source code, or any portion of it, may result in severe civil and criminal
	penalties and will be prosecuted to the maximum extent possible under
	the law.

	www.navitaire.com

DESCRIPTION:

	Contains javascript for the filterFieldValue function.  Note that this 
	file relies on javascript from the jFilterFieldValue.wld template.

#############################################################################*/


/*-----------------------------------------------------------------------

	SUBROUTINE:   	filterFieldValue()

	DESCRIPTION:	Inserts the JavaScript function filterFieldValue.

 	MODIFICATION NOTES:
	  hblack 23-JAN-2001: Added use of "isBlank".  If the user entered
		spaces only in any field, the field will be assigned the value
		of ''.  Why?  Because too many validation routines check if
		the field is '' to see if it is blank.  If the field is '    '
		the field will pass validation eventhough there is nothing in it.
		SR#5172

	  jwilde 30-JAN-2001: Added fixed-length field conditional.  If the
		developer specifies the min length and the max length as the
		same value, then this will force the user to enter exactly that
		length. To enter less or more will cause a message to pop up
		stating "This field must be <n> characters in length".

	  hblack 18-APR-2001: Adding on to Justin's logic, if min=max and
		field length is greater, then clear field.  Otherwise leave
		contents of field untouched.

	  barnett 31-AUG-2001: Added check for ascii value of characters
		only values >= 32 and <= 255 are valid.  Otherwise we do not
		support non printable characters and unicode characters.

	  barnett 4-SEP-2001: Modified check for non-printable ASCII and Unicode
		characters to be compatible with Netscape 3, also changed
		For Loop of invalid string to an indexOf function

	  jwilde 14-SEP-2001: Added support for new 'num' value that can be
		passed as the content type.  'num' accepts decimal points, and
		formats numbers to two places beyond the decimal. (Would have
		been nice to accept commas and/or insert them, but unnecessary.)
		
	  angellp 5-OCT-2005: Split into javascript file for just the javascript
	  	portion and a perl file for gathering prefs and text.

--------------------------------------------------------------------------*/

	var filterText = new Object();
	captureFilterText(filterText);

function filterFieldValue(fieldToFilter, min, max, content)
{
	var filtered	= '';
	var chr			= '';
	var end			= false;
	// Second char of whiteSpace is "\t" char.
	var whiteSpace	= ' 	';
	var startPos	= 0;
	var tempString	= '';

	while ( (whiteSpace.indexOf(fieldToFilter.value.charAt(0)) >= 0)
				&& (fieldToFilter.value.length > 0) )
	{
		fieldToFilter.value = fieldToFilter.value.substring(1, fieldToFilter.value.length);
	}
	if ( fieldToFilter.value.length == 0 )
	{
		fieldToFilter.value = "";
		return false;
	}

	if ( (content) && (content == "name") )
	{
		if ( ( fieldToFilter.value.charAt(0) >= "0")
			&& ( fieldToFilter.value.charAt(0) <= "9") )
		{
				alert( filterText.invalidCharPopup );
				end = true;
		}
	}

	for (var i=0; i < fieldToFilter.value.length; i++)
	{
		chr	= fieldToFilter.value.charAt(i);

		if ( (content) && (content == "int") )
		{
			if ( chr < '0' || chr > '9' )
			{
				alert( filterText.mustUseNumbers );
				end = true;
			}
		}
		else if ( (content) && (content == "num") )
		{
			if ( (chr != ".") && ( chr < '0' || chr > '9' ) )
			{
				alert( filterText.mustUseNumbers );
				end = true;
			}
		}
		else
		{
		var invalid	= '';
		if ( (content) && (content == "name") )
			{
				invalid	= '/&*()#$@%!{}[]^|`<>\"';
			}
			else
			{
				invalid	= '!{}[]^|`<>\"';
			}
	/*
		# IF ( USER.PREFS.LANGUAGE_DATA.LANGUAGES.$LANGUAGE.special_char == 'true' );
		# Netscape on the Mac uses signed bytes for characters so characters
		# in the range 128 to 255 are actually negative.
		#	( chr < ' ' ) || (chr > '\x7f')
		# had to be put together because otherwise all characters over 127 on
		# the Mac in Netscape would test as less than ' '.
		# filterText.special_char == "true"
		
		# If special characters are turned off then we do not want anything
		# greater than 127 or less than 32 so the problem with Netscape on
		# the Mac is not an issue here.
		# filterText.special_char == "false"
	*/
			if (( invalid.indexOf(chr) >= 0 ) || ( chr < '\x20' ) || 
				(filterText.special_char == "true" && (chr > '\xff')) ||
				(filterText.special_char == "false" && ( chr > '\x7f')) )
			{
				alert( filterText.invalidCharPopup );
				end = true;
			}
		}

		if ( end )
		{
			fieldToFilter.value = "";
			if (document.images)
			{
				fieldToFilter.focus();
			}
			return false;
		}
	}

	if ( (content) && (content == "num") )
	{
		if ( fieldToFilter.value.indexOf(".") == -1 )
		{
			fieldToFilter.value = fieldToFilter.value + ".00";
		}
		else
		{
			dec = "00";
			fieldToFilter.value =
				fieldToFilter.value.substring( 0, fieldToFilter.value.indexOf(".") + 3  )
			  + dec.substring( fieldToFilter.value.length - 1 - fieldToFilter.value.indexOf(".") );
		}
	}

	if ((min) && (max) && (fieldToFilter.value != ''))
	{
		if ((min == max) && ( fieldToFilter.value.length != min ) )
		{
			alert ( filterText.popupFieldFixedLength1 + min + filterText.popupFieldFixedLength2 );
			if ( fieldToFilter.value.length > min )
			{
				fieldToFilter.value	= "";
				filtered			= "";
			}

			if (document.images)
			{
				fieldToFilter.focus();
			}
			return false;
		}
		else if (( fieldToFilter.value.length < min ) || ( fieldToFilter.value.length > max ))
		{
			alert ( filterText.popupFieldLength1 + min + filterText.popupFieldLength2 + max + filterText.popupFieldLength3 );
			fieldToFilter.value	= "";
			filtered			= "";

			if (document.images)
			{
				fieldToFilter.focus();
			}
			return false;
		}
	}

	return true;

}  /* End of filterFieldValue */