/*** Start of Modify ***/
function ltrim(str){
    /* Trims leading spaces - returns string
     * Returns trimmed string
     */
    return str.replace(/^\s+/, '');
}//eof - ltrim

function rtrim(str) {
    /* Trims trailing spaces - returns string
     * Returns trimmed string
     */
    return str.replace(/\s+$/, '');
}//eof - rtrim

function alltrim(str) {
    /* Trims leading and trailing spaces
     * Returns trimmed string
     */
    return str.replace(/^\s+|\s+$/g, '');
}//eof - alltrim

function padleft(str, ch, num) {
    /* Pads string on left with number characters
     * Args:
     *    str = string to pad
     *    ch = pad character
     *    num = number of total characters in final string
     * returns string
     */

    //Create pad string num chars
    var pad = "";
	if (!ch) ch = " "; //otherwise pad.length remains 0.
    do  {
        pad += ch;
    }while(pad.length < num)

    //Regular express take num chars from right
    var re = new RegExp(".{" + num + "}$");

    return re.exec(pad + str);
}//eof - padleft

function padright(str, ch, num){
    /* Pads string on right with number characters
     * Args:
     *    str = string to pad
     *    ch = pad character
     *    num = number of total characters in final string
     * returns string
     */

    //Create pad string num chars
    var pad = "";
	if (!ch) ch = " "; //otherwise pad.length remains 0.
	do {
        pad += ch;
    } while (pad.length < num)

    //Regular express take num chars from left
    var re = new RegExp("^.{" + num + "}");
    return re.exec(str + pad);
}//eof - padright

function back2forward(dataStr){ 
	return dataStr.replace(/\\/g, "/");
}

function forward2back(dataStr) { 
	return dataStr.replace(/\//g, "\\");
}

function return2br(dataStr) {
    /* Convert returns in string to html breaks
     * return string
     */
    return dataStr.replace(/(\r\n|[\r\n])/g, "<br />");
}//eof - return2br
function return2br2(dataStr) {
    /* Convert returns in string to html breaks
     * return string
     */
    return dataStr.replace(/(\r\n|\r|\n)/g, "<br />");
}//eof - return2br2

function cleanString(str) {
    /* Remove specified characters from string
     * Arg: str = string to clean
     * List is left parenthesis, right parenthesis, period, dash, space
     * Change list inside square brackets [list]
     * Return string
     */
    return str.replace(/[\(\)\.\-\s,]/g, "");
}//eof - cleanString

function cleanString2(str) {
	return str.replace(/[^\d]/g, "");
}

function alpha2numericPhone(phoneStr) {
	var newStr = phoneStr.replace(/[a-zA-Z]/g, alpha2number);
	return checkReplaceParm(newStr, "alpha2number");

	function alpha2number(char) {
		var rtrnVal = null;

		switch (char.toLowerCase()) {
		case "a": case "b": case "c":
			rtrnVal = "2";
			break;
		case "d": case "e": case "f":
			rtrnVal = "3";
			break;
		case "g": case "h": case "i":
			rtrnVal = "4";
			break;
		case "j": case "k": case "l":
			rtrnVal = "5";
			break;
		case "m": case "n": case "o":
			rtrnVal = "6";
			break;
		case "p": case "q": case "r": case "s":
			rtrnVal = "7";
			break;
		case "t": case "u": case "v":
			rtrnVal = "8";
			break;
		case "w": case "x": case "y": case "z":
			rtrnVal = "9";
			break;
		}
		return rtrnVal;
	}
}

function first_first(name) {
	return name.replace(/^([^,]*)[,\s]+(\w+)/, "$2 $1");
}

function last_first (name) {
	return name.replace(/(.+)\s+([\w']+)$/, "$2, $1"); //'
}

function switchWords(name) {
    /* Change order of two words removing optional comma separator
     * Arg: name = string of two words
     * Return string no separator
     */
    return name.replace(/(\w+),?\s*(\w+)/, "$2 $1");
}//eof - switchWords

function reversOrder (name) {
    /* Change order of two words inserting comma separator
     * Args: name = string of two words
     * Return string with separator
	 */

    return name.replace(/(\w+),?\s+(\w+)/, "$2, $1");
}//eof - reversOrder

function cnvrt2upper(str) {
    /* Convert string to title case capital first letters
     * Return converted string or original string if not supported
     */
    str = alltrim(str);
	var newStr = str.toLowerCase().replace(/\b[a-z]/g, cnvrt)

    return checkReplaceParm(newStr, "cnvrt");

    function cnvrt() {
        return arguments[0].toUpperCase();
    }
}//eof - cnvrt2upper

function titleCase(str) {
    str = alltrim(str);
	var newStr = str.toLowerCase().replace(/\b\w+\b/g, cnvrt);
    return checkReplaceParm(newStr, "cnvrt");

    function cnvrt() {
        /*Branch which should be capitalized */
        if (arguments[arguments.length -2] == 0)
            /* First word capitalize if first char is letter*/
            return arguments[0].replace(/^[a-z]/, cnvrt2);
        else if (/^(a|about|after|an|and|at|by|for|from|in|into|nor|of|on|onto|over|the|to|up|with|within)$/.test(arguments[0]) )
            /* List of words to skip */
            return arguments[0];
        else
            /* All other capitalize if first character is letter */
            return arguments[0].replace(/^[a-z]/, cnvrt2);
    }

    function cnvrt2() {
        /* Capitalize letter */
        return arguments[0].toUpperCase();
    }
}//eof
function titleCase2(str) {
	var re = new RegExp(/^(a|about|after|an|and|at|by|for|from|in|into|nor|of|on|onto|over|the|to|up|with|within)$/);		

	return str.toLowerCase().replace(/\b([a-z])(\w*)\b/g, cnvrt);

    function cnvrt() {
    	if (re.test(arguments[0]) && arguments[arguments.length-2])
        	return arguments[0];
        else
			return arguments[1].toUpperCase() + arguments[2];
	}
}
function html2entity(str) {
    /* Convert html <,> to &tl; and &gt;
     * Agr: str = string that may have html
     * Return converted string or original string if not supported
     */
	var newStr = cnvrt(str);
    return checkReplaceParm(newStr, "(s)");

    function cnvrt(str){
        //replace with function
        return str.replace(/[<>]/g, function (s){ return (s == "<")? "&lt;" :"&gt;"});
    }
}//eof - html2entity

function checkReplaceParm(str, fx) {
	/* Check browser supports functions in replace
	 * No support returns function itself
	 */
	var re = new RegExp("^\sfunction\s+" + fx);
	if (re.test(str)) {
		/* This return can be change to null or
		 * a value of your choice to indicate failure
		 */
		alert("This browser doesn't support using a function as an argument to the replace method!");
		return ""
	}
	else {
		return str;
	}
}

