/**
	written by Dustin Hansen (http://www.halofree.com)
	Licensed under the Academic Free License version 2.1 or above
	copyright (c) 2007 HaloFree Studios
*/
function email(name, domain,tld, link)
{

    var link = "<a href='mailto:"+name+"@"+domain+"."+tld+"'>"+link+"</a>";

    document.write(link);
}

var StringManipulation = function(){
	this.version = this.v = this.VERSION = '0.8.1'; // just for fun.

	// trim all beginning and trailing whitespace or _char
	this.trim = function(_string, _char) {
		var tChar = this.slashAll(_char || "s");
		return _string.replace(new RegExp("^" + tChar + "+|" + tChar + "+$", "g"), "");
	};

	// trim all beginning whitespace or _char
	this.ltrim = function(_string, _char) {
		var tChar = this.slashAll(_char || "s");
		return _string.replace(new RegExp("^" + tChar + "+"), "");
	};

	// trim all trailing whitespace or _char
	this.rtrim = function(_string, _char) {
		var tChar = this.slashAll(_char||"s");
		return _string.replace(new RegExp(tChar + "+$"), "");
	};

	//replace all excess whitespace
	this.whiteSpace = function(_string, _char) {
		return _string.replace(/\s{2,}/g, (_char || " "));
	};

	// camelcase _attribute (ie; background-color = backgroundColor)
	this.camelCase = function(_attribute) {
		return _attribute.replace(/[-| ](\w)/g, function(a,b){ return b.toUpperCase(); });
	};
	
	// opposite of camelCase (ie; backgroundColor = background-color)
	this.cssCase = function(_attribute) {
		var self = this;
		return _attribute.replace(/(\w)?([A-Z| ])/g, function(a,b,c){
			return self.trim((b ? b + "-" : '') + c).toLowerCase(); 
		});
	};

	// formats number as currency (ie; 3000 = USD $3,000.00)
	this.asCurrency = function(_string, _currency, _delim) {
		var parsedStr = this.numFormat(parseFloat(_string).toFixed(2), _delim);
		return (_currency === false ? '' : (_currency || "USD $")) + parsedStr;
	};

	// adding commas to integer values	
	this.numFormat = function(_number, _delim) {
		var numDelim = _delim || ",";
		var numStr = this.reverse(_number.toString()).replace(/(\d{3})/g, "$1" + numDelim);
		numStr = this.reverse(numStr).replace(new RegExp("^(-)?" + numDelim), "$1");

		return numStr;
	};
	
	// reverse a string.
	this.reverse = function(_string) {
		var revStr = '';
		for (var i=_string.length; i>=0; i--) { revStr += _string.charAt(i); }

		return revStr;
	};

	// uppercase _string if _first, only the first word is uppercased.	
	this.ucWords = function(_string, _first) {
		var wordRegex = (_first ? /(^| )(\w)/ : /(^| )(\w)/g);
		return _string.replace(wordRegex, function(a,b,c){ return b + c.toUpperCase(); });
	};

	// replace all underscores with spaces
	this.underSpaces = function(_string) {
		return _string.replace(/_/g, " ");
	};
	
	// replace all spaces with underscores
	this.underScores = function(_string) {
		return _string.replace(/ /g, "_");
	};
	
	// pad _string to length of _length, with _char append _char unless _padRight
	this.pad = function(_string, _length, _char, _padRight) {
		if (_string.length < _length) {
			_char = _char || " ";
			for (var i=0; i<(_length - _string.length); i++) {
				if (!_padRight) {
					_string += _char; 
				} else {
					_string = _char + _string;
				}
			}
		}
		
		return _string;
	};

	// limit length of _string to _length, append _append to end (ie; this limit...)
	// if _appIn, the length of _append is included in total length of _string
	this.limit = function(_string, _length, _append, _appIn) {
		var str = _string.toString(), len;
		if (str.length > _length) {
			len = (_appIn ? (_length - _append.length) : _length);
			str = (this.trim(str)).match(new RegExp("^.{" + len + "}")) + (_append || '');
		}

		return str;
	};

	// wrap _string in lengths of _length with _break as the wrapping character
	// if _force break on _length of characters, else break on _length of words.
	this.wordWrap = function(_string, _length, _break, _force) {
		var parsedStr = '';
		if (_force) {
			while (_string.length > 0) {
				var strSeg = this.limit(_string, _length);
				parsedStr += strSeg + _break;
				_string = _string.substring(_length, _string.length);
			}
		} else {
			// needs to be a bit better. this will need refactored
			var words = _string.split(" ");
			var j = 0;
			for (var i=0; i<words.length; i++) {
				if ((j + words[i].length) >= _length) {
					parsedStr += _break;
					j = 0;
				}
				j += words[i].length;
				parsedStr += words[i] + ' ';
			}
			
			// this is not good, if the string originally had a trailing space
			// it will be stripped. :-(
			parsedStr = this.rtrim(parsedStr);
		}

		return parsedStr;
	};

	// adds slashes in front of meta characters. double slashes if _dbl
	this.addSlashes = function(_string, _dbl) {
		// needs to be ', ", \, and the NULL character
		return _string.replace(/(\"|\'|\\)/g, (_dbl ? "\\\\" : "\\") + RegExp.$1);
	};
	// add slashes before ALL characters. double slashes if _dbl
	this.slashAll = function(_string, _dbl) {
		return _string.replace(/(.)/g, (_dbl ? "\\\\" : "\\") + "$1");
	};
	// strip all slashes from _string	
	this.stripSlashes = function(_string) {
		return _string.replace(/\\/g, "");
	};
	
	// returns number of words
	this.wordCount = function(_string) {
		return (_string.match(/([^\s]+)/g)).length;
	};
	// just because
	this.charCount = function(_string) {
		return _string.length;
	};
};
