var templatesBase = '../AjaxRequests/';
//var templatesBase = '../../AjaxRequests/';

// Leticias Stuff
function overTexto(id){
	backgroundAntes = document.getElementById(id).style.backgroundColor;
	document.getElementById(id).style.background= "#72A4FF";
}
function outTexto(id){
	document.getElementById(id).style.background= backgroundAntes;
}
// Old Stuff
var selectedId;
function showContent(id) {
	var body = document.getElementById("Body_" + id).innerHTML;
	document.getElementById("texto_arquivo").innerHTML = body;
	if (id!=selectedId) {
		document.getElementById("title").value = "";
		document.getElementById("body").innerHTML = "";
		selectedId = id;
	}
}
function loadContent() {
	if (!isNaN(selectedId)) {
		var title = document.getElementById("Title_" + selectedId).innerHTML;
		var body = document.getElementById("Body_" + selectedId).innerHTML;
		document.getElementById("title").value = title;
		document.getElementById("body").innerHTML = body;
	}
}
// Template Stuff
function showTemplatesEditor(id,section,name) {
	var idTemp = id;
	new Ajax(templatesBase + "Templates.php", {
		postBody: "action=show" + 
				  "&section=" + section +
				  "&name=" + name,
		update: $("template" + idTemp)
	}).request();
}
function hideTemplatesEditor(id) {
	var idTemp = id;
	with (document.getElementById("edit" + id)) {
		new Ajax(templatesBase + "Templates.php", {
			postBody: "action=hide" + 
					  "&section=" + section.value +
					  "&name=" + name.value,
			update: $("template" + idTemp)
		}).request();
	}
}
function saveTemplate(id, publish) {
	var idTemp = id;
	var publishFlag = 0;
	if (publish)
		publishFlag = 1;
	with (document.getElementById("edit" + id)) {
		var _body = body.value;
		_body = _body.replace(/\+/g, "&#43");
		_body = _body.replace(/\\/g, "&#92");
		_body = _body.replace(/\'/g,"`");
		_body = escape(_body);
		
		var _title = title.value
		_title = _title.replace(/\+/g, "&#43");
		_title = _title.replace(/\\/g, "&#92");
		_title = _title.replace(/\'/g,"`");
		_title = escape(_title);
	
    	new Ajax(templatesBase + "Templates.php", {
			postBody: "action=save" +
					  "&publish=" + publishFlag +
					  "&title=" + _title +
					  "&body=" + _body + 
					  "&section=" + section.value +
					  "&name=" + name.value,
			update: $("template" + idTemp)
		}).request();
	}
	alert("Template has been saved");
}
function saveAndReload(id,publish) {
var idTemp = id;
	var publishFlag = 0;
	if (publish)
		publishFlag = 1;
	with (document.getElementById("edit" + id)) {
    	new Ajax(templatesBase + "Templates.php", {
			postBody: "action=save" +
					  "&publish=" + publishFlag +
					  "&title=" + title.value +
					  "&body=" + body.value + 
					  "&section=" + section.value +
					  "&name=" + name.value,
			onComplete: function() {
				window.location.reload();
			}
		}).request();
	}
}
