String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
};
//------------------------------------------------------------------
function gE(id){
	return document.getElementById(id);
}
//------------------------------------------------------------------
function validarNoticia(){

	var titulo = gE('titulo').value;
	if(titulo.trim() == ""){
		alert('Título em branco.');
		return false;
	}
	var texto = tinyMCE.get('texto').getContent();
	if(texto.trim() == ""){
		alert('Notícia em branco.');
		return false;
	}
	return true;
}
//------------------------------------------------------------------
function validarSenha(){

	var senha1 = gE('senha1').value;
	var senha2 = gE('senha2').value;
	if(senha1.trim() == ""){
		alert('Entre com a nova senha.');
		return false;
	}
	if(senha2.trim() == ""){
		alert('Redigite a senha.');
		return false;
	}
	if(senha1.trim() != senha2.trim()){
		alert('A senha não confere.');
		return false;
	}	
	return true;
}
//------------------------------------------------------------------

function validarPesquisa(){
	var expre = gE('expressao_pesquisa').value;
	if(expre.trim() == ""){
		alert('Entre com uma expressão para pesquisa.');
		return false;
	}
	return true;
}
function excluirNoticia(id){
	if(confirm("Confirma exclusão da notícia?")){
		location.href="index.php?pagina=noticia&excluir=true&cod="+id;	
	}
}
function validarPagina(){
	var texto = tinyMCE.get('texto').getContent();
	if(texto.trim() == ""){
		alert('Nenhum texto digitado.');
		return false;
	}
	return true;
}

function validarEmail(){

	var nome = gE('nomeremetente').value;
	if(nome.trim() == ""){
		alert('Digite seu nome.');
		return false;
	}
	var email = gE('emailremetente').value;
	if(email.trim() == ""){
		alert('Digite seu e-mail.');
		return false;
	}
	var telefone = gE('telefone').value;
	if(telefone.trim() == ""){
		alert('Digite um telefone para contato.');
		return false;
	}	
	var texto = gE('mensagem').value();
	if(texto.trim() == ""){
		alert('Redija uma mensagem.');
		return false;
	}
	return true;
}
function validarProduto(){

	var nome = gE('nome').value;
	if(nome.trim() == ""){
		alert('Entre com o nome');
		return false;
	}
	var categoria = gE('categoria').value;
	if(categoria == "nenhum"){
		alert('Escolha uma categoria');
		return false;
	}
	var tipo = gE('tipo').value;
	if(tipo == "nenhum"){
		alert('Escolha um tipo');
		return false;
	}
	var subtipo = gE('subtipo').value;
	if(subtipo == "nenhum"){
		alert('Escolha um subtipo');
		return false;
	}	
	return true;
}
function validarNome(){
	var nome = gE('nome').value;
	if(nome.trim() == ""){
		alert('Entre com o nome');
		return false;
	}	
}
function excluirSubtipo(id){
	if(confirm("ATENÇAO! A exclusao de um subtipo excluirá todos os produtos relacionados. Deseja continuar?")){
		location.href="index.php?pagina=editar_menu_prod&excluir_subtipo="+id;	
	}
	else { return false; }
}
function excluirTipo(id){
	if(confirm("ATENÇAO! A exclusao de um tipo excluirá todos os subtipo e produtos relacionados. Deseja continuar?")){
		location.href="index.php?pagina=editar_menu_prod&excluir_tipo="+id;	
	}
	else { return false; }
}
function excluirCategoria(id){
	if(confirm("ATENÇAO! A exclusao de uma categoria excluirá todos os tipos, subtipo e produtos relacionados. Deseja continuar?")){
		location.href="index.php?pagina=editar_menu_prod&excluir_categoria="+id;	
	}
	else { return false; }
}

function createObject() {
	var ajax;
	// navegadores não IE
	try {
		ajax = new XMLHttpRequest();
	// navegador IE (antigo e novo)
	} catch(ee) {
		try {
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(E) {
				ajax = false;
			}
		}
	}
	return ajax;
}

function getSelect(tipo, origem, valor, opcao) {
	
	var http = createObject();
	var dados = "tipo="+tipo+"&origem="+origem+"&id="+valor+"&opcao="+opcao;
	
	document.getElementById(tipo+"box").style.display = "none";
	
	http.onreadystatechange = function() {
		
		if(http.readyState == 4){
			var response = http.responseText;
			var target = document.getElementById(tipo+"box");
			target.innerHTML = response;
			target.style.display = "block";
		}
	};
	http.open('POST', 'actions/select.php', true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(dados);
}
function setupTiny(){

	tinyMCE_GZ.init({
		plugins : "media,tabfocus,safari,pagebreak,table,advimage,advlink,emotions,preview,searchreplace,print,contextmenu,paste,fullscreen,noneditable,layer",
		themes: "advanced",
		languages : 'pt',
		disk_cache : true,
		debug : false
	});

	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		language : 'pt',
		plugins : "media,tabfocus,safari,pagebreak,table,advimage,advlink,emotions,preview,searchreplace,print,contextmenu,paste,fullscreen,noneditable,layer",
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontselect,fontsizeselect,|,forecolor,backcolor,|,link,unlink,anchor,image,hr,insertlayer,pagebreak,media,|,cleanup,removeformat,code,|,help",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,sub,sup,|,charmap,emotions,|,print,preview,|,tablecontrols",
		theme_advanced_buttons3 : "",
		theme_advanced_buttons4 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "none",
		editor_selector : "mceEditor",
		//content_css : "empty.css",
		//theme_advanced_resizing : true,
		//extended_valid_elements : "a[name|href|target|title|onclick],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
		skin : "o2k7",
		skin_variant : "silver",
		tab_focus : ':prev,:next',
		button_tile_map : true
		//readonly : modo,
		//entity_encoding : "raw"
	});

}
function setupEditorMinimo() {
	
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		language : 'pt',
		plugins : "tabfocus",
		theme_advanced_buttons1 : "fontselect,fontsizeselect,|,bold,italic,underline,strikethrough,|,forecolor,|,undo,redo,|,cleanup,|,link,unlink",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "center",
		editor_selector : "mceEditorMinimo",
		skin : "o2k7",
		skin_variant : "silver",
		tab_focus : ':prev,:next',
		button_tile_map : true
		//readonly : modo,
		//entity_encoding : "raw"
   });
}
function renomearItemMenu(nome_antigo, id, categoria, target) {

	var nome_novo = '';
	while(nome_novo.trim() == '') {
		nome_novo = prompt('Digite o novo nome', nome_antigo);
		if (nome_novo === null){
			return false;
		}
	}
	var descr = document.createElement('input');
	descr.type = 'hidden';
	descr.id = 'nome';
	descr.name = 'nome';
	descr.value = nome_novo;
	
	var form = document.createElement('form');
	form.appendChild(descr);
	form.method = 'POST';
	form.name = 'frmNovaDescr';
	form.id = 'frmNovaDescr';
	form.action = 'index.php?pagina=produtos&lista=destaque&nome=Destaques&categoria='+categoria+'&target='+target+'&id='+id;
	
	document.body.appendChild(form);
	form.submit();
}

function pisca(id){
	if(!parar) {
		var status = document.getElementById(id).style.visibility;
		
		if(status == "hidden") {
			document.getElementById(id).style.visibility = "visible";
		}
		else{
			document.getElementById(id).style.visibility = "hidden";
		}
	}
	else{
		document.getElementById(id).style.visibility = "visible";
	}
}

function prepararPagina(){
	setInterval ("pisca('dnb');", 1000 );
}
function pararPiscar(){
	parar = true;
}
function comecarPiscar(){
	parar = false;
}
var parar = false;
setupTiny();
setupEditorMinimo();

window.onload = prepararPagina;
