/**
 * This file must be included to add functionality to strings.
 * 
 * @version 09-11-2009
 * @author <a href="mailto:r.tennapel@griponservice.nl?SUBJECT=string.js">R. ten Napel, ing.</a>
 **/

/**
 * Returns if a string contains a particular string.
 * 
 * @param String		The string to search for.
 * 
 * @return				True or false.
 **/
String.prototype.contains = function(string) {
	return (this.indexOf(string) != -1);
};

/**
 * Returns if a string starts with a particular string.
 * 
 * @param string		The string to check.
 * 
 * @return				True or false.
 **/
String.prototype.startsWith = function(string) {
	return this.indexOf(string) == 0;
};

/**
 * Returns if a string ends with a particular string.
 * 
 * @param string		The string to check.
 * 
 * @return				True or false.
 **/
String.prototype.endsWith = function(string) {
	return (this.length - this.indexOf(string) - string.length) == 0;
};

/**
 * Replace all occurences of 'find' and replace it with 'replace'.
 * 
 * @param find			The string to find.
 * @param replace		The replacement.
 * 
 * @return				The changed string.
 **/
String.prototype.replaceAll = function(find, replace) {
	return this.replace(new RegExp(find, "gi"), replace);
};

/**
 * Removes whitespace at the start of a string.
 * 
 * @return				The trimmed string.
 **/
String.prototype.ltrim = function() {
	return this.replace(/^\s+/, "");
};

/**
 * Removes whitespace at the end of a string.
 * 
 * @return				The trimmed string.
 **/
String.prototype.rtrim = function() {
	return this.replace(/\s+$/, "");
};

/**
 * Removes whitespace at the start & end of a string.
 * 
 * @return				The trimmed string.
 **/
String.prototype.trim = function() {
	return this.ltrim().rtrim();
};

/**
 * Pad the string to a certain length with a certain character.
 * 
 * @param length		The length of the padded string (-x for padding before or x for padding after).
 * @param character		The character to pad the string with.
 * 
 * @return				The padded string.
 **/
String.prototype.pad = function(length, character) {
	var loop = (length < 0) ? -length : length;
	
	// Check if padding is necessery:
	if (loop - this.length <= 0) {
		return this;
	}
	
	// Do the padding:
	var pad = "";
	for (var i = 0; i < loop - this.length; i++) {
		pad += character;
	}
	
	// Return string with padding:
	if (length > 0) {
		return this + pad;
	}
	
	return pad + this;
};

/**
 * Alternate notation for the $-function. Use it like "elemId".$(). If the $-function doesn't exist, an alert will be generated.
 * 
 * @return				The element that has the given string as ID.
 **/
String.prototype.$ = function() {
	try {
		return $(String(this));
	} catch(e) {
		alert(e.message);
	}
	
	return null;
};