

/* Copyright (c) 2006 Vurv Technology Inc. All rights reserved. */
/**
 * @fileoverview Extended functionality from core String object
 */

/**
 * Strips all consecutive whitespace from string
 * @version OrangeScript 2.0
 * @type String
 * @return Returns the string removed of whitespace
 */
String.prototype.cleanWhitespace = function() {
	return this.replace(/\s+/g, " ").trim();
};

/**
 * Escapes regular expression symbols out in the string
 * @version OrangeScript 2.0
 * @type String
 * @return returns a string
 */
String.prototype.escapeRE = function() {
	return this.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1");
};

/*
 * Returns true/false whether the string is in a valid date format
 * @version OrangeScript 2.x
 * @type Boolean
 * @return Returns true/false
 */
/*String.prototype.isDate = function() {
	// TODO: need to add locale functionality
	var re = new RegExp(/^\d{1,2}\/\d{1,2}\/\d{2,4}$/);
	if (!re.test(this)) { return false; }
	var _date = this.split("\-. /");
	var month = _date[0];
	var day = _date[1];
	var year = _date[2];
	if (month < 1 || month > 12) { return false; }
	if (day < 1 || day > 31) { return false; }
	if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) { return false; }
	if (month == 2) {
		var leap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day == 29 && !leap)) { return false; }
	} return true;
};*/

/**
 * Returns true/false whether the string is in a valid email address format
 * @version OrangeScript 2.0
 * @type Boolean
 * @return Returns true/false
 */
String.prototype.isEmail = function() {
	var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return re.test(this);
};

/**
 * Returns true/false whether the string is a valid GUID
 * @version OrangeScript 2.0
 * @type Boolean
 * @return Returns true/false
 */
String.prototype.isGUID = function() {
	var re = /^\{?[a-fA-F\d]{8}-(?:[a-fA-F\d]{4}-){3}[a-fA-F\d]{12}\}?$/;
	return re.test(this);
};

/**
 * Drops the whitespace from the left of text
 * @version OrangeScript 2.0
 * @type String
 * @return Returns string without preceeding white space
 */
String.prototype.lTrim = function(){
	return this.replace(/^\s+/, "");
};

/**
 * Repeats a string specified number of times<br>
 * Example: "a".repeat(5) - returns "aaaaa"
 * @version OrangeScript 2.0
 * @type String
 * @return Returns repeated string
 */
String.prototype.repeat = function(n) {
	var s = "";
	var t = this.toString();
	while (--n >= 0) { s += t; }
	return s;
};

/**
 * Drops the whitespace from the right of text
 * @version OrangeScript 2.0
 * @type String
 * @return Returns string without trailing white space
 */
String.prototype.rTrim = function() {
	return this.replace(/\s+$/, "");
};

/**
 * Removes any repeated words, only keeping first occurance, from the string
 * @version OrangeScript 2.0
 * @type String
 * @return Returns string with dupicate words removed
 */
String.prototype.stripDuplicateWords = function() {
	return this.replace(/\b(\w+)\b(?=.*\b\1\b)/g, "");
};

/**
 * Returns the string with any specified tag blocks and contents removed
 * @version OrangeScript 2.0
 * @type String
 * @return Returns the string without specified tags blocks
 */
String.prototype.stripTagBlockByNames = function(tags) {
	var _ret = this;
	$A(tags.split(",")).forEach(function(tag) {
		_ret = _ret.replace(new RegExp("(?:<"+tag+".*?>)((\n|\r|.)*?)(?:<\/"+tag+">)", "img"), "");
	});
	return _ret;
};

/**
 * Returns the string with any HTML or XML tags removed
 * @version OrangeScript 2.0
 * @type String
 * @return Returns the string without any markup tags
 */
String.prototype.stripTags = function() {
	return this.replace(/<\/?[^>]+>/gi, " ");
};

/**
 * Drops the whitespace both sides of the text
 * @version OrangeScript 2.0
 * @type String
 * @return Returns string without preceeding/trailing white space
 */
String.prototype.trim = function () {
    return this.lTrim().rTrim();
};

