 function escapeVal(textarea,replaceWith){ //textarea is reference to that object, replaceWith is string that will replace the encoded return


s_textarea = escape(textarea) //encode textarea string's carriage returns

	for(i=0; i<s_textarea.length; i++){ //loop through string, replacing carriage return encoding with HTML break tag
	
	 	if(s_textarea.indexOf("%0D%0A") > -1){ //Windows encodes returns as \r\n hex
		s_textarea=s_textarea.replace("%0D%0A",replaceWith)
		}
		else if(s_textarea.indexOf("%0A") > -1){  //Unix encodes returns as \n hex
		s_textarea=s_textarea.replace("%0A",replaceWith)
		}
		else if(s_textarea.indexOf("%0D") > -1){  //Macintosh encodes returns as \r hex
		s_textarea =s_textarea.replace("%0D",replaceWith)
		}
		
		if(s_textarea.indexOf("%u2019") > -1){ //Windows encodes returns `
		s_textarea=s_textarea.replace("%u2018","%27")
		}
		
		if(s_textarea.indexOf("%u2019") > -1){ //Windows encodes returns `
		s_textarea=s_textarea.replace("%u2019","%27")
		}	
		
		if(s_textarea.indexOf("%09") > -1){ //Windows encodes returns `
		s_textarea=s_textarea.replace("%09","&nbsp;&nbsp;")
		}
		
		
		
		
	}
	
 
return  unescape(s_textarea); // unescape all other encoded characters
}

  function htmtest(input){
  if (input =='' ) return 0
  
	input = input.toLowerCase(); 
	if (input.indexOf('<html>') >0 )
	{return 1;}

	if (input.indexOf('<head>') >0 )
	{return 1;} 

	if (input.indexOf('<title>') >0 )
	{return 1;} 

	if (input.indexOf('<body>') >0 )
	{return 1;} 

	if (input.indexOf('<table>') >0 )
	{return 1;} 

	if (input.indexOf('<tr>') >0 )
	{return 1;} 
	
	if (input.indexOf('<td>') >0 )
	{return 1;} 
	
  return 0
  }
  
  function rtf2htm(input){
	//html tags
	   
    if (input =='' ) return ''
    
  bHTML= 0;  
    if  (htmtest(input) > 0)
	{ bHTML= -1;
	} 

str_input = input;
	 
if (bHTML ==0){str_input = escapeVal(input,'<br>');}


str_output='';
	for (var i=0;i<str_input.length;i++) {
		if (str_input.charCodeAt(i) <32 && bHTML ==0 ){
			if (str_input.charCodeAt(i) ==13)
			 str_output  +=  '<br>';
			if (str_input.charCodeAt(i) ==9) 
			 str_output += '&nbsp;&nbsp;';
		}	 
		if (str_input.charCodeAt(i) >=32  && str_input.charCodeAt(i) <=126 ){
		str_output += str_input.charAt(i);
		} 
		
		if (str_input.charCodeAt(i) >=127  && str_input.charCodeAt(i) <=159 ){
		str_output += str_input.charAt(i);
		} 
		if ( str_input.charCodeAt(i) >159 && str_input.charCodeAt(i) <=180 && bHTML ==0 ){
		str_output += '<p>';
		} 
		
		if ( str_input.charCodeAt(i) >180 && bHTML ==0 ){
		str_output += '';
		} 

	}

return str_output
 
} 