// String.js
// Extra methods for String object
//
// Copyright 2001 NobleTech Limited. All rights reserved
// If supplied as part of a project, this file is licenced for internal use
// on that project only. It may not be redistributed in human-readable form
// The contents of this file are declared as pre-existing IPR of NobleTech
// Phone 01235 831746 for further licencing options
// Leave this header intact
//
// Functions declared in this file are mainly added as members
// to the built-in JavaScript object String
//
// This file created by Nathan Phillips of NobleTech Limited
// See version control for history
/////////////////////////////////////////////////////////////////////////////


function _isAll(fnGuard)
{
	for(var i = 0; i < this.length; i++)
		if( !fnGuard( this.substr(i, 1) ) )
			return false;
	return true;
}

function _isAny(fnGuard)
{
	for(var i = 0; i < this.length; i++)
		if( fnGuard( this.substr(i, 1) ) )
			return true;
	return false;
}

function _isAllInList(listOfChars)
{
	function isInList(c) { return listOfChars.indexOf( c ) != -1; }
	return this.isAll(isInList);
}

function _isAnyInList(listOfChars)
{
	function isInList(c) { return listOfChars.indexOf( c ) != -1; }
	return this.isAny(isInList);
}

function _isWhiteSpace()
{
	return /^\s*$/.test(this);
}

function _isPunctuation()
{
	return this.isAllInList("\"-?,.!'/()");
}

function _trimWhiteSpace()
{
	return this.replace(/^\s+|\s+$/g, "");
}

function _isNumeric()
{
	// Compare to numbers to see if JScript can make sense of it as a number
	return this.length > 0 && this.isAllInList("0")
		|| this > 0 || this < 0;
}

function _isAlpha()
{
	function isAlphaChar(c) {
		var cl = c.toLowerCase();
		return cl >= 'a' && cl <= 'z';
	}
	return this.isAll(isAlphaChar);
}

function _toCharArray()
{
	var arChars = new Array();
	for(var nChar = 0; nChar < this.length; nChar++)
		arChars[nChar] = this.substr(nChar, 1);
	return arChars;
}

function _replaceAll(arSearch, arReplace)
{
	var strResult = this;
	for(var nItem = 0; nItem < arSearch.length; nItem++)
		strResult = strResult.replace(new RegExp(arSearch[nItem], "g"), arReplace[nItem]);
	return strResult;
}

function encodeHTMLQuotes(strIn)
{
	var strOut;
	while(strOut != strIn) {
		strOut = strIn;
		strIn = strIn.replace('"', '&quot;');
	}
	return strOut;
}

function _htmlEncode()
{
	var strBaaadChars = "&\"<>\n\r";	// Must do & first
	var arReplacements = new Array("&amp;", "&quot;", "&lt;", "&gt;", "<BR>", "");
	return this.replaceAll( strBaaadChars.toCharArray(), arReplacements );
}

function htmlEncode(strInput) {
	if(strInput == null)
		return "";
	else if(typeof(strInput) == "string")
		return strInput.htmlEncode();
	else
		throw new Error( "Trying to HTML encode a " + typeof(strInput) );
}

// Remove all nasty characters from string
function _makeID()
{
	var strID = this;
	var strEvils = " \t\n@!.,;";
	for(var nChar = 0; nChar < strEvils.length; nChar++)
		strID = strID.replace( strEvils.substr(nChar, 1), "" );
	return strID;
}


//////////////////////////////////////////////////////////////////
// Register all the methods - call from early inline code
// Inline code is run before any <SCRIPT> immediate code
function registerStringMethods()
{
	String.prototype.isAll = _isAll;
	String.prototype.isAny = _isAny;
	String.prototype.isAllInList = _isAllInList;
	String.prototype.isAnyInList = _isAnyInList;
	String.prototype.isWhiteSpace = _isWhiteSpace;
	String.prototype.isPunctuation = _isPunctuation;
	String.prototype.trimWhiteSpace = _trimWhiteSpace;
	String.prototype.isNumeric = _isNumeric;
	String.prototype.isAlpha = _isAlpha;
	String.prototype.toCharArray = _toCharArray;
	String.prototype.replaceAll = _replaceAll;
	String.prototype.htmlEncode = _htmlEncode;
	String.prototype.makeID = _makeID;
}

// For benefit of client-side code
registerStringMethods();

