// --------------------------------------------------------------------------------------------
// Additions to JavaScript String class - Copyright (c) 2002 Gene M. Angelo
// Note: This file requires Utilities.js
// --------------------------------------------------------------------------------------------

// --------------------------------------------------------------------------------------------
// Prototypes for String class
// --------------------------------------------------------------------------------------------	

// --------------------------------------------------------------------------------------------	
// Is this string all alpha - does the string contain ONLY a-z or A-Z (spaces not allowed)
// --------------------------------------------------------------------------------------------	
String.prototype.isAlpha = function()
	{
	if(this.length == 0)
		return false;
		
	var Char = new CChar(null);

	for(var n = 0; n < this.length; n++)
		{
		Char.setChar(this.charAt(n));

		if(! Char.isAlpha())
			return false;
		}
		
	return true;
	}

// --------------------------------------------------------------------------------------------	
// Is this string all alphanumeric - does the string contain ONLY a-z, A-Z or 0-9 (spaces not allowed)
// --------------------------------------------------------------------------------------------	
String.prototype.isAlphaNumeric = function()
	{
	if(this.length == 0)
		return false;
		
	var Char = new CChar(null);

	for(var n = 0; n < this.length; n++)
		{
		Char.setChar(this.charAt(n));

		if(! Char.isAlphaNumeric())
			return false;
		}
		
	return true;
	}

// --------------------------------------------------------------------------------------------	
// Is this string all ascii - does the string contain ONLY hex 0x00 through 0x7f
// --------------------------------------------------------------------------------------------	
String.prototype.isAscii = function()
	{
	if(this.length == 0)
		return false;
		
	var Char = new CChar(null);

	for(var n = 0; n < this.length; n++)
		{
		Char.setChar(this.charAt(n));

		if(! Char.isAsciiRange('\x00', '\x7f'))
			return false;
		}
		
	return true;
	}
	
// --------------------------------------------------------------------------------------------	
// Is this string a number - does the string contain ONLY 0-9 (spaces not allowed)
// --------------------------------------------------------------------------------------------	
String.prototype.isNumeric = function()
	{
	if(this.length == 0 || isNaN(this.toString()))
		return false;

	var Char = new CChar(null);

	for(var n = 0; n < this.length; n++)
		{
		Char.setChar(this.charAt(n));

		if(! Char.isNumeric())
			return false;
		}
		
	return true;
	}	

// --------------------------------------------------------------------------------------------	
// Create a method that counts how many times a given character is in a String object
// --------------------------------------------------------------------------------------------	
String.prototype.getCharCount = function(character)
	{
	var count = 0;

	for(var n = 0; n < this.length; n++)
		{
		if(this.charAt(n) == character)
			count++;
		}

	return count;
	}

// --------------------------------------------------------------------------------------------	
// Reverses the value of a string and returns a new string in reverse order
// --------------------------------------------------------------------------------------------	
String.prototype.reverse = function()
	{
	var string = "";

	for(var n = this.length - 1; n >= 0 ; n--)		
		string += this.charAt(n);		

	return string;
	}


// --------------------------------------------------------------------------------------------	
// Create a trim function - trims ALL leading and trailing spaces
// --------------------------------------------------------------------------------------------	
String.trimAll = function(string)
	{
	var s = new String(string);
	var c = s.charAt(0);
		
	if(s.length < 2)	// 0 or 1
		{
		if(c == '\x20' || c == '\n')
			s = "";

		return s;
		}	
			
	// Trim all leading spaces and \n..
	while((c == ' ' || c == '\n') && s.length > 0)
		{
		s = s.substring(1);		
		c = s.charAt(0);
		}
		
	// Trim all trailing spaces and \n..
	if(s.length > 0)
		{
		c = s.charAt(s.length - 1);	

		while((c == '\x20' || c == '\n') && s.length > 0)
			{
			s = s.substring(0, s.length - 1);			
			if(s.length > 0)
				c = s.charAt(s.length - 1);
			}
		}
				 
	return s;
	}



