/*
	File: various.js
	This file contains various prototype functions that by some reason aren't part of the
	standard JavaScript language. Most come from the public domain.
*/

/*
	Function: trim
	Strip whitespace (or other characters) from the beginning and end of a string.
	
	Returns:
		The trimmed string
	
	Example:
    	var test = '  hello world '
    	test.trim() // returns 'hello world'
*/
String.prototype.trim = function () {
	var s = this.replace(/^\s+/, "");
	return s.replace(/\s+$/, "");
};


/*
	Function: in_array
	Checks if a value exists in an array.
	
	Parameters:
		needle - the value to search for
		
	Returns:
		True if the value is found in the array, false otherwise
	
	Example:
    	var test = ['apples', 'bananas', 'oranges']
    	test.in_array('apples') // returns true
*/
Array.prototype.in_array = function ( obj ) {
	var len = this.length;
	for ( var x = 0 ; x <= len ; x++ ) {
		if ( this[x] == obj ) return true;
	}
	return false;
}


/*
	Function: htmlEntities
	Convert all applicable characters to HTML entities
	
	Example:
		var test = 'äöü'
		test.htmlEntities() //returns '&auml;&ouml;&uuml;'
*/
String.prototype.htmlEntities = function() {
    
    var chars = new Array ('&','à','á','â','ã','ä','å','æ','ç','è','é',
                           'ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô',
                           'õ','ö','ø','ù','ú','û','ü','ý','þ','ÿ','À',
                           'Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë',
                           'Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö',
                           'Ø','Ù','Ú','Û','Ü','Ý','Þ','€','\"','ß','<',
                           '>','¢','£','¤','¥','¦','§','¨','©','ª','«',
                           '¬','­','®','¯','°','±','²','³','´','µ','¶',
                           '·','¸','¹','º','»','¼','½','¾');

    var entities = new Array ('amp','agrave','aacute','acirc','atilde','auml','aring',
                              'aelig','ccedil','egrave','eacute','ecirc','euml','igrave',
                              'iacute','icirc','iuml','eth','ntilde','ograve','oacute',
                              'ocirc','otilde','ouml','oslash','ugrave','uacute','ucirc',
                              'uuml','yacute','thorn','yuml','Agrave','Aacute','Acirc',
                              'Atilde','Auml','Aring','AElig','Ccedil','Egrave','Eacute',
                              'Ecirc','Euml','Igrave','Iacute','Icirc','Iuml','ETH','Ntilde',
                              'Ograve','Oacute','Ocirc','Otilde','Ouml','Oslash','Ugrave',
                              'Uacute','Ucirc','Uuml','Yacute','THORN','euro','quot','szlig',
                              'lt','gt','cent','pound','curren','yen','brvbar','sect','uml',
                              'copy','ordf','laquo','not','shy','reg','macr','deg','plusmn',
                              'sup2','sup3','acute','micro','para','middot','cedil','sup1',
                              'ordm','raquo','frac14','frac12','frac34');

    newString = this;
    for (var i = 0; i < chars.length; i++) {
	    newString = newString.replace (new RegExp(chars[i], "g"), '&' + entities[i] + ';');
    }
    return newString;
}

