function VSSync(formname){
	var newText = document.getElementById(formname).value;
	
	if (autoformat){
	
		newText = jsve_replace_string(newText,'>\n','*>*\\*n*');
		newText = jsve_replace_string(newText,'\n<','*\\*n*<*');
		
		newText = jsve_replace_string(newText,'\n','<br/>');
		newText = jsve_replace_string(newText,'  ',' &nbsp;');
		
		newText = jsve_replace_string(newText,'*\\*n*<*','\n<');
		newText = jsve_replace_string(newText,'*>*\\*n*','>\n');
	
	}
	
	document.getElementById(formname+'screen').innerHTML = newText;
}

function jsve_html4ajax(input){
	input = jsve_replace_string(input,'&','#ampersand#');
	input = jsve_replace_string(input,'+','#plus#');
	input = jsve_replace_string(input,'%','#percent#');
	return input;
}

function jsve_replace_string(sourceString,searchString,pasteString){
	var f;
	var resultString;
	resultString = sourceString;
	currentIndex = resultString.indexOf(searchString);
	while (currentIndex >= 0){
		if (currentIndex >= 0){
			resultString = resultString.substring(0,currentIndex) + pasteString + resultString.substring(currentIndex + searchString.length);
		}
		currentIndex = resultString.indexOf(searchString);
	}
	return resultString;
}

var jsveUl = "\n<ul>\n<li>List Item</li>\n<li>List Item</li>\n</ul>\n";
var jsveOl = "\n<ol>\n<li>List Item 1</li>\n<li>List Item 2</li>\n</ol>\n";
var jsveTable = "\n<table>\n<caption>Table name</caption>\n<tbody>\n<tr>\n<th>Col 1 title</th>\n<th>Col 2 title</th>\n</tr>\n<tr>\n<td>Row 1, Col 1</td>\n<td>Row 1, Col 2</td>\n</tr>\n</tbody>\n</table>\n";
var jsveH1 = "\n<h1>Headline 1</h1>\n";
var jsveH2 = "\n<h2>Headline 2</h2>\n";
var jsveH3 = "\n<h3>Headline 3</h3>\n";
var jsveH4 = "\n<h4>Headline 4</h4>\n";
var jsveP = "\n<p>Paragraph</p>\n";
var jsveEm = " <em>text</em> ";
var jsveStrong = " <strong>text</strong> ";
var jsveStrike = " <strike>text</strike> ";
var jsveImage = " <img src=\"img-link\" alt=\"\" /> ";
var jsveA = " <a href=\"http://\">link</a> ";

function add2form (formname,xhtml){
	document.getElementById(formname).value += xhtml;
	VSSync(formname);
}

function jsveHTML(JSVEID){
	VSSync(JSVEID);
	document.getElementById(JSVEID).style.display = 'block';
	document.getElementById(JSVEID+'screen').style.display = 'none';
	document.getElementById(JSVEID+'htmlview').className = 'active';
	document.getElementById(JSVEID+'preview').className = 'normal';
}

function jsvePreview(JSVEID){
	VSSync(JSVEID);
	document.getElementById(JSVEID).style.display = 'none';
	document.getElementById(JSVEID+'screen').style.display = 'block';
	document.getElementById(JSVEID+'htmlview').className = 'normal';
	document.getElementById(JSVEID+'preview').className = 'active';
}