/*******************************

 Extensions.js, Version 3.2.1, 12-Dec-2007
 Copyright Northgate Information Solutions UK Limited, 2007

 *******************************/

/***********************************
 Additional methods for String class

 endsWith(s{, ignoreCase})
 escapeRegExMetachars()
 isEqualIgnoreCase(s)
 isIn(arg1{, arg2 ...})
 pathConcat(p{, separator})
 removeSubstring(from, to)
 startsWith(s{, ignoreCase})
 unescapeCharacterEntities()
 
 ***********************************/
 
	// Compare this string with another, ignoring case.
String.prototype.isEqualIgnoreCase = function extIsEqualIgnoreCase(s) {
	return(this.toLowerCase() == s.toLowerCase());
}

    /* Returns the string with a substring removed.
       from is the starting position of the string to be removed.
       end is the position following the end of the string to be removed. */
String.prototype.removeSubstring = function extRemoveSubstring(from, to) {
    if (from == null)
        return(this);
    to = (to == null ? this.length : to);
    return(this.substring(0, from) + this.substring(to));
}

	/* Returns true if this string starts with another;
	   otherwise, false. */
String.prototype.startsWith = function extStartsWith(s, ignoreCase) {
    if (s == null)
        return(false);
    var i = (ignoreCase ? "i" : "");
		
    var re = new RegExp("^" + s, i);
	return(re.test(this));
}

	/* Returns true if this string ends with another;
	   otherwise, false. */
String.prototype.endsWith = function extEndsWith(s, ignoreCase) {
    if (s == null)
        return(false);
    var i = (ignoreCase ? "i" : "");
		
    var re = new RegExp(s + "$", i);
	return(re.test(this));
}

	/* If this string occurs in a list, returns the index of the 
	   first matching item; otherwise, returns -1. */
String.prototype.isIn = function extIsIn() {
	for (var i = 0; i < arguments.length; i++)
		if (this == arguments[i]) return i;
	return -1;
}

	/* Appends a path or filename to this string. Use when you are
	   combining two paths or a path and a filename. The method
	   automatically checks to see if you have added the proper
	   number of slashes. */
String.prototype.pathConcat = function extPathConcat(p, separator) {
	if (separator == null)
		separator = "/";
	if (this.slice(-1) != separator)
		p = separator + p;
	return (this + p);
}

	/* Escape any regular expression metacharacters by preceding each
	   of them with a backslash. Returns the modified string. */
String.prototype.escapeRegExMetachars = function extEscapeRegExMetachars() {
	return this.replace(/(?!\\)([\.\^\$\*\+\?\=\!\:\|\\\/\(\)\[\]\{\}])/g, "\\$1");
}

	/* Convert any HTML character entities to the characters they 
	   represent. Returns the modified string. */
String.prototype.unescapeCharacterEntities = function extUnescapeCharacterEntities() {
	var re;
	var result = this;
	var cc, c;
	
	if (result.search(/&(?:#\d{3}|[a-z]+?);/) != -1) {
		var ci = ["&quot;","&amp;","&lt;","&gt;","&nbsp;","&pound;"];
		var ch = "\"&<> £";
		for (var i = 0; i < ci.length; i++) {
			re = new RegExp(ci[i], "g");
			result = result.replace(re, ch.charAt(i));
		}
		var mc = result.match(/&#\d{3};/g);
		if (mc) {
			for (var i = 0; i < mc.length; i++) {
				cc = parseInt(mc[i].substring(2, 5));
				c = String.fromCharCode(cc);
				result = result.replace(mc[i], c);
			}
		}
	}
	return result;
}

