﻿// JScript File - relating to string manipulation

function trimString(sInString) {
// Trim trailing and leading spaces
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function putInAnd(sText) {
// Given a string, replace the last , with the word 'and'

    var nPos, sResult

    try {
        nPos = sText.lastIndexOf(',')
        if (nPos>=0) 
            sResult = sText.substring(0,nPos) + ' and ' + trimString(sText.substring(nPos+1))
        else
            sResult = sText
        }
    catch(err) {
        var txt
        txt='There was an error on this page.\n\n'
        txt+='putInAnd() error: ' + err.description + '\n\n'
        txt+='Click OK to continue.\n\n'
        alert(txt)
        sResult = sText
    }    
    return sResult
}

