//Check characters against standard Latin 1 

function chrcheck(fieldtofilter) 
{
	var str_incoming = fieldtofilter.value
	var len = str_incoming.length;
	var str_corrected = ""
	var character = "";
	var illegal_count = 0;
	var a = 0;
	var i = 0;
	var count_illegal = 0;
	var illegal = 0;
	
	if (str_incoming != "") {
	
		valid_characters = new Array('\r',' ','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','!','\"','#','$','%','&','\'','(',')','*','+',',','-','.','/',':',';','<','=','>','?','@','[','\\',']','^','_','`','{','|','}','~','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','\b','\t','\n','\v','\f');

		count_illegal = valid_characters.length-1;

	        for (var a=0; a<=len-1; a++) {
	        		
	        	character = str_incoming.substring(a, a+1);
        	
		        for (i=0; i<=count_illegal; i++) {
	        		if (character==valid_characters[i]) {     		
          				illegal = 0;
          				break;
			    	} else {
					illegal = 1;
				}
			    }
			    
			    if (illegal==0) {
			    	  str_corrected = str_corrected + character;		    	   
			    } else {
			    	  illegal_count = illegal_count + 1;
			    	  str_corrected = str_corrected + "#";
			    }
			}
			
  	}
	     if (illegal_count > 0) {
	        alert(illegal_count + ' unrecognised characters have been found and marked with a #.\n\n'
		+ 'Characters should be compliant with iso-8859-1 Latin-1 character set.');
	        fieldtofilter.value = str_corrected;
  	     }
}


//Checks login and name fields
function filtervalue(fieldToFilter, min, max, content)
{
	var filtered	= '';
	var chr			= '';
	var end			= false;
	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 == "username") )
	{
		if ( ( fieldToFilter.value.charAt(0) >= "0")
			&& ( fieldToFilter.value.charAt(0) <= "9") )
		{
				alert(
					'\nThis login name must start with a valid letter not a number\n\n'
					+   'Please re-enter the details\n'
									 );
				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("\nPlease enter a valid number in this box\n");
				end = true;
			}
		}
		else if ( (content) && (content == "num") )
		{
			if ( (chr != ".") && ( chr < '0' || chr > '9' ) )
			{
				alert("\nPlease enter a valid number in this box\n");
				end = true;
			}
		}
		else
		{
		var invalid	= '';
		if ( (content) )
			{
				invalid	= '/,&*()#$@%!{}[]^|`<>\"';
			}
			else
			{
				invalid	= '!{}[]^|`<>\"';
			}			if ( ( invalid.indexOf(chr) >= 0 )
				|| ( chr < '\x20' ) || ( chr > '\x7f') )
			{
				alert(
					'\nThis box contains invalid characters such as \' { } [ ] < > ! ^ | `\n\n'
					+   'Please do not use non standard characters such as accents or punctuation\n'
									 );
				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 ('\nPlease enter a value that is ' + min + ' characters in length.\n');
			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 ('\nPlease enter a value that is between ' + min + ' and ' + max + ' characters in length.\n');
			fieldToFilter.value	= "";
			filtered			= "";

			if (document.images)
			{
				fieldToFilter.focus();
			}
			return false;
		}
	}

	return true;

}


