function abrirPasta(d) {
	setEstadoPasta(d,1);
		document.getElementById('div_pasta_'+d).style.display = 'block';
		document.getElementById('abrir_'+d).src = 'img/fechar.gif';
		document.getElementById('pasta_'+d).src = 'img/pasta_aberta.gif';
}
function fecharPasta(d) {
	setEstadoPasta(d,0);
		document.getElementById('div_pasta_'+d).style.display = 'none';
		document.getElementById('abrir_'+d).src = 'img/abrir.gif';
		document.getElementById('pasta_'+d).src = 'img/pasta_fechada.gif';
}
function pasta(d,t) {
	if (t==1) {
		abrirPasta(d);
	}
	else {
			if (document.getElementById('div_pasta_'+d).style.display == 'block') {
				fecharPasta(d);
			}
			else {
				abrirPasta(d);
			}
	}
}

function LTrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
	}
	return s;
}

function RTrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;      
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}
	return s;
}

function trim(str){
   return RTrim(LTrim(str));
}


function soh_numero(numero){
	var validos = "0123456789";
	var numero_ok = '';
	for(i=0;i<numero.length;i++){
		if(validos.indexOf(numero.substr(i,1)) != -1){
			numero_ok += numero.substr(i,1); 
		}
	}
	return numero_ok;
}

function descricao(d) {
	if (document.getElementById('desc'+d).style.display == 'block') {
		document.getElementById('desc'+d).style.display = 'none';
		document.getElementById('img'+d).src = 'img/abrir.gif';
	}
	else {
		document.getElementById('desc'+d).style.display = 'block';
		document.getElementById('img'+d).src = 'img/fechar.gif';
	}
}

function removeFolder(url) {
	if (confirm("Are really want to remove current folder and bookmarks?")) {
		document.location = url;
	}
}

function removeBookmark(url) {
	if (confirm("Are really want to remove current bookmark?")) {
		document.location = url;
	}
}

function readCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return "";
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length;
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function setCookie(cookieName,cookieValue) {
	var today = new Date();
	var expire = new Date();
	expire.setTime(today.getTime() + 315360000000);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function getAllDivs() {
	var x = document.getElementsByTagName('div');
	return x;
}

function getAllOpenFolders(string) {
	var folders = string.split(",");
	return folders;
}

function loadOpenFolders(string) {
	var x = getAllOpenFolders(string);
	for (var i=0; i < x.length; i++) {
		if (x[i] > 0) abrirPasta(x[i]);
	}
}
function removeBookmark(id) {
	if (confirm("Remove selected bookmark?")) {
		document.location = "?secao=bookmark&acao=remove&idBookmark="+id;
	}
}
function removeFolder(id) {
	if (confirm("Remove selected folder?")) {
		document.location = "?secao=pasta&acao=remove&idPasta="+id;
	}
}

function removeTopic(id) {
	if (confirm("Remove selected topic?")) {
		document.location = "?secao=topic&acao=remove&idTopic="+id;
	}
}
function removeNote(id) {
	if (confirm("Remove selected note?")) {
		document.location = "?secao=note&acao=remove&idComment="+id;
	}
}


function abrirTopic(d) {
	a = document.getElementById('div_topic_'+d);
	if (a) {
		document.getElementById('div_topic_'+d).style.display = 'block';
		document.getElementById('abrir_topic_'+d).src = 'img/fechar.gif';
	}
	else {
		document.getElementById('abrir_topic_'+d).style.color = 'blue';
		document.getElementById('abrir_topic_'+d).src = 'img/fechar.gif';
	}

}
function fecharTopic(d) {
	a = document.getElementById('div_topic_'+d);
	if (a) {
		document.getElementById('div_topic_'+d).style.display = 'none';
		document.getElementById('abrir_topic_'+d).src = 'img/abrir.gif';
	}
	else {
		document.getElementById('abrir_topic_'+d).style.color = 'red';
		document.getElementById('abrir_topic_'+d).src = 'img/abrir.gif';
	}
}
function topic(d,t) {
	if (t==1) {
		abrirTopic(d);
	}
	else {
		a = document.getElementById('div_topic_'+d);
		if (a) {
			if (document.getElementById('div_topic_'+d).style.display == 'block') {
				fecharTopic(d);
			}
			else {
				abrirTopic(d);
			}
		}
		else {
			if (document.getElementById('abrir_topic_'+d).style.color == 'red') {
				abrirTopic(d);
			}
			else {
				fecharTopic(d);
			}
		}
	}
}

function newAjax() {
    if (typeof window.ActiveXObject != 'undefined' ) {
       doc = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
       doc = new XMLHttpRequest();
    }
    return doc;
}

function setEstadoPasta(id,estado) {
	var xmlhttp = newAjax();
	xmlhttp.open("GET", "setEstadoPasta.php?idPasta="+id+"&estado="+estado, true);
	xmlhttp.send(null);
}


