// Convert problem characters to JavaScript Escaped values

function replace(s, one, another) {   // In a string s replace one substring with another
  if (s == "") {
    return "";
  }
  var res = "";
  var i = s.indexOf(one,0);
  var lastpos = 0;
  while (i != -1) {
    res += s.substring(lastpos,i) + another;
    lastpos = i + one.length;
    i = s.indexOf(one,lastpos);
  }
  res += s.substring(lastpos);  // the rest
  return res;  
}

function convJS(s) {
  if (s == null) {
    return "";
  }
  var t = s;
//  t = replace(t,"\\","\\\\"); // replace backslash with \\
  t = replace(t,"\\","&nbsp;"); // replace backslash with space
//  t = replace(t,"'","\\\'");  // replace an single quote with \'
  t = replace(t,"'","&nbsp;");  // replace an single quote with space
//  t = replace(t,"\"","\\\""); // replace a double quote with \"
  t = replace(t,"\"","&nbsp;"); // replace a double quote with space
//  t = replace(t,"\r","\\r");  // replace CR with \r;
  t = replace(t,"\r","&nbsp;");  // replace CR with space
//  t = replace(t,"\n","\\n");  // replace LF with \n;
  t = replace(t,"\n","&nbsp;");  // replace LF with space
  return t;
} 

