
// In a string replace one substring with another
String replace(String s, String one, String another) {
	if (s.equals("")) return "";
	String res = "";
	int i = s.indexOf(one,0);
	int 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; 
}

// Convert problem characters to JavaScript Escaped values
String convJS(Object s) {
	if (s == null) {
		return "";
	}
	
	String t = (String)s;
	t = replace(t,"\\","\\\\"); // replace backslash with \\
	t = replace(t,"'","\\\'");  // replace an single quote with \'
	t = replace(t,"\"","\\\""); // replace a double quote with \"
	t = replace(t,"\r","\\r"); // replace CR with \r;
	t = replace(t,"\n","\\n"); // replace LF with \n;   return t;
}