﻿function SearchAndReplace(Content, SearchFor, ReplaceWith) {

    var tmpContent = Content;
    var tmpBefore = new String();
    var tmpAfter = new String();
    var tmpOutput = new String();
    var intBefore = 0;
    var intAfter = 0;

    if (SearchFor.length == 0)
        return;


    while (tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase()) > -1) {

        // Get all content before the match
        intBefore = tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase());
        tmpBefore = tmpContent.substring(0, intBefore);
        tmpOutput = tmpOutput + tmpBefore;

        // Get the string to replace
        tmpOutput = tmpOutput + ReplaceWith;


        // Get the rest of the content after the match until
        // the next match or the end of the content
        intAfter = tmpContent.length - SearchFor.length + 1;
        tmpContent = tmpContent.substring(intBefore + SearchFor.length);

    }

    return tmpOutput + tmpContent;

}