/* string.js
 * Adds several useful functions to the Jasvascript String class.
 * Several of these were taken from http://www.crockford.com/javascript/remedial.html 
 * All code here is in the public domain.*/

// String.entityify() produces a string in which '<', '>', and '&' are replaced with their HTML entity equivalents.
String.prototype['entityify'] = function()
{	return this.replace(/&/g, "&amp;").replace(/</g,
		"&lt;").replace(/>/g, "&gt;");
}

// String.quote() produces a quoted string. This method returns a string 
// which is like the original string except that it is wrapped in quotes 
// and all quote and backslash characters are preceded with backslash.
String.prototype['quote'] =  function ()
{	var c, i, l = this.length, o = '"';
	for (i = 0; i < l; i += 1) {
		c = this.charAt(i);
		if (c >= ' ') {
			if (c == '\\' || c == '"')
				o += '\\';				
			o += c;
		} else
		{	switch (c)
			{	case '\b':  o += '\\b';  break;
				case '\f':  o += '\\f';  break;
				case '\n':  o += '\\n';  break;
				case '\r':  o += '\\r';  break;
				case '\t':  o += '\\t';  break;
				default:
					c = c.charCodeAt();
					o += '\\u00' + Math.floor(c / 16).toString(16) +
						(c % 16).toString(16);
			}
		}
	}
	return o + '"';
}

/* String.supplant() does variable substitution on the string. It scans through the string 
 * looking for expressions enclosed in { } braces. If an expression is found, use it as a 
 * key on the object, and if the key has a string value or number value, it is substituted 
 * for the bracket expression and it repeats. This is useful for automatically fixing URLs. So
 * 		param = {domain: 'valvion.com', media: 'http://media.{domain}/'};
 * 		url = "{media}logo.gif".supplant(param);
 * produces a url containing "http://media.valvion.com/logo.gif". */
String.prototype['supplant'] = function(o)
{	var i, j, s = this, v;
	for (;;) {
		i = s.lastIndexOf('{');
		if (i < 0) {
			break;
		}
		j = s.indexOf('}', i);
		if (i + 1 >= j) {
			break;
		}
		v = o[s.substring(i + 1, j)];
		if (!isString(v) && !isNumber(v)) {
			break;
		}
		s = s.substring(0, i) + v + s.substring(j + 1);
	}
	return s;
}

// String.trim() removes whitespace characters from the beginning and end of the string.
String.prototype['trim'] = function()
{	return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

/* String.base64Encode() produces a base-64 encoded version of the string.
 * This code was written by Tyler Akins and has been placed in the
 * public domain.  It would be nice if you left this header intact.
 * Base64 code from Tyler Akins -- http://rumkin.com
 * Modified by Eric Poggel to add to String class. */
String.prototype['base64Encode'] = function()
{	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	do
	{	chr1 = this.charCodeAt(i++);
		chr2 = this.charCodeAt(i++);
		chr3 = this.charCodeAt(i++);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2))
			enc3 = enc4 = 64;
		else if (isNaN(chr3))
			enc4 = 64;

		output += (keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
			keyStr.charAt(enc3) + keyStr.charAt(enc4));
	} while (i < this.length);
	
	return output;
}

/* String.base64Decode() produces a base-64 decoded version of the string.
 * This code was written by Tyler Akins and has been placed in the
 * public domain.  It would be nice if you left this header intact.
 * Base64 code from Tyler Akins -- http://rumkin.com
 * Modified by Eric Poggel to add to String class. */
String.prototype['base64Decode'] = function()
{	
	var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	input = this.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	do
	{	enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		output += String.fromCharCode(chr1);

		if (enc3 != 64)
			output += String.fromCharCode(chr2);
		if (enc4 != 64)
			output += String.fromCharCode(chr3);

	} while (i < input.length);
	
	return output;
}
