/**
Funções padrao do admin
*/

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function MM_setTextOfLayer(objId,x,newText) { //v9.0
  with (document) if (getElementById && ((obj=getElementById(objId))!=null))
    with (obj) innerHTML = unescape(newText);
}

function inativo(objId) { //v9.0
  var obj = null; with (document){ if (getElementById)
  obj = getElementById(objId); }
  if (obj){
    if (obj.style.display == 'none')
      eval("obj.style.display='block'");
    else eval("obj.style.display='none'");
  }
}

function ativar(objId) { //v9.0
	var obj = null; with (document){ if (getElementById)
	obj = getElementById(objId); }
	if (obj){
		if (obj.style.display == 'none')
			eval("obj.style.display='block'");
	}
}

function desativar(objId) { //v9.0
	var obj = null; with (document){ if (getElementById)
	obj = getElementById(objId); }
	if (obj){
		if (obj.style.display == 'block')
			eval("obj.style.display='none'");
	}
}

function abrir_croper(imagem,pasta,id,tabela,coluna,retorno,alvo,tamanho,fundo,miniatura,mascara) {
	var newWindow = window.open('ocroper.php', 'croper','resizable=yes,scrollbars=no,location=no,directories=no,status=yes,toolbar=no,menubar=no,width=650,height=450,left='+(screen.availWidth/2-325)+',top='+(screen.availHeight/2-225)+'');
	if (!newWindow) return false;
	var html = "";
	html += "<html><head></head><body><form id='formid' method='post' action='ocroper.php'>";
	html += "<input type='hidden' name='editor' value='sim'/>";
	html += "<input type='hidden' name='imagem' value='" + imagem + "'/>";
	html += "<input type='hidden' name='pasta' value='" + pasta + "'/>";
	html += "<input type='hidden' name='id' value='" + id + "'/>";
	html += "<input type='hidden' name='tabela' value='" + tabela + "'/>";
	html += "<input type='hidden' name='coluna' value='" + coluna + "'/>";
	html += "<input type='hidden' name='retorno' value='" + retorno + "'/>";
	html += "<input type='hidden' name='alvo' value='" + alvo + "'/>";
	html += "<input type='hidden' name='tamanho' value='" + tamanho + "'/>";
	html += "<input type='hidden' name='fundo' value='" + fundo + "'/>";
	html += "<input type='hidden' name='miniatura' value='" + miniatura + "'/>";
	html += "<input type='hidden' name='mascara' value='" + mascara + "'/>";
	html += "</form><script type='text/javascript'>document.getElementById(";
	html += '"formid"';
	html += ").submit()</s";
	html += "cript></bo";
	html += "dy></ht";
	html += "ml>";
	newWindow.document.write(html);
	return newWindow;
}

//Função AJAX para carregar conteúdo externo em uma DIV
var loadedobjects=""
var rootdomain="http://"+window.location.hostname

function ajaxpage(url, containerid){
	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject){ // if IE
		try {
			page_request = new ActiveXObject("Msxml2.XMLHTTP")
		} 
		catch (e){
			try{
				page_request = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	else
	return false
	page_request.onreadystatechange=function(){
		loadpage(page_request, containerid)
	}
	page_request.open('GET', url, true)
	page_request.send(null)
}

//Usa-se esta função em especial
//Ex: ajaxpage('página', 'id da div');
function loadpage(page_request, containerid){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
	document.getElementById(containerid).innerHTML=page_request.responseText
	//Código extra para rodar javascripts do arquivo
	// contentElem is the HTML element you've loaded via ajax
	scriptx = document.getElementById(containerid).getElementsByTagName("script");
	
	// Load scripts into new array because 
	// scriptx seems to change as scripts execute, at least in some browsers
	scripts = new Array();
	for (var idx=0; idx<scriptx.length; idx++) {
		scripts[idx] = scriptx[idx].text;
	}
	
	// execute each script in turn
	for(idx=0; idx<scripts.length; ++idx) {
		 if (scripts[idx].length!=0) {
				try {
					// create a function for the script & execute it
					f = new Function(scripts[idx]);
					f();
				} catch(se) {
						 alert(se.tagId+": Script error (" + se.message + "):\n"+scripts[idx]);
				} // end try-catch
		 } // end if
	} // end for
}

function loadobjs(){
	if (!document.getElementById)
	return
	for (i=0; i<arguments.length; i++){
		var file=arguments[i]
		var fileref=""
		if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
			if (file.indexOf(".js")!=-1){ //If object is a js file
				fileref=document.createElement('script')
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			}
			else if (file.indexOf(".css")!=-1){ //If object is a css file
				fileref=document.createElement("link")
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
		}	
		if (fileref!=""){
			document.getElementsByTagName("head").item(0).appendChild(fileref)
			loadedobjects+=file+" " //Remember this object as being already added to page
		}
	}
}


//Função de ativar guias de edição 
//2 primeiras linhas de exemplo
var guias_1 = new Array ("nenhum","o_texto","o_destaque");
var guias_itens_1 = new Array ("nenhum","o_conteudo_texto","o_conteudo_destaque");

function MM_changeClass(objId,theValue) { //v9.0
  var obj = null; with (document){ if (getElementById)
  obj = getElementById(objId); }
  if (obj){
    if (theValue == true || theValue == false)
      eval("obj.className="+theValue);
    else eval("obj.className='"+theValue+"'");
  }
}

function ativar_guia (grupo,guia) {

	ArrayGuia = eval('guias_'+grupo);
	ArrayItens = eval('guias_itens_'+grupo);

	for (i=1; i <= ArrayItens.length-1; i++) {
		if (i!=guia) {
  			desativar(ArrayItens[i]);
				MM_changeClass(ArrayGuia[i],'guia_inativa');
			}
	}
	ativar(ArrayItens[guia]);
	MM_changeClass(ArrayGuia[guia],'guia_ativa');
	retorno = false;
	if (eval ("guia_"+grupo+"_"+guia)) {
		retorno = true;
		eval ("guia_"+grupo+"_"+guia+"=false;");
	}
	if ((eval ("guia_"+grupo+"_ativa"))==guia) {
		retorno = true;
	}
	eval ("guia_"+grupo+"_ativa="+guia+";");
	return retorno;
}

//Funções de Janelas arrastáveis
function abrir_janela() {
	//Exemplo de uso
	//abrir_janela('id_base','destino','titulo',lagura,altura,outras variaveis do destino)
	
	id_base = abrir_janela.arguments[0];
	destino = abrir_janela.arguments[1];
	titulo = abrir_janela.arguments[2];
	largura = abrir_janela.arguments[3];
	altura = abrir_janela.arguments[4];
	
	if ( typeof abrir_janela.arguments[5] != 'undefined' ) {
		destino += '?'+abrir_janela.arguments[5];
	}
	
	var obj = null; with (document){ if (getElementById)
  obj = getElementById(("anc_"+id_base)); }
	
		oconteudo ='<div id="'+id_base+'" class="div_admin" style="width:'+largura+'px;">\n';
		oconteudo +='<div class="cabecalho_admin">\n';
		oconteudo +='<img src="imagens/imagens_admin/fechar.gif" alt="fechar" width="8" height="20" onclick="fechar_janela(\''+id_base+'\');" />\n';
		oconteudo +='<img src="imagens/imagens_admin/minimizar.gif" alt="minimizar" width="8" height="20" onclick="minimizar_janela(\''+id_base+'\');" />\n';
		oconteudo +='<div onmousedown="dragStart(event, \''+id_base+'\');">'+titulo+'</div>\n';
		oconteudo +='</div>\n';
		oconteudo +='<iframe id="form_'+id_base+'" name="form_'+id_base+'" src="'+destino+'" width="'+largura+'" height="'+altura+'"  scrolling="no" frameborder="0"></iframe>\n';
		oconteudo +='</div>';
	if (obj){
		obj.innerHTML = oconteudo;
	}
}

function abrir_editor() {
	//Exemplo de uso
	//abrir_editor('id_base','destino','titulo',lagura,altura,outras variaveis do destino)
	
	id_base = abrir_editor.arguments[0];
	destino = abrir_editor.arguments[1];
	titulo = abrir_editor.arguments[2];
	largura = abrir_editor.arguments[3];
	altura = abrir_editor.arguments[4];
	
	if ( typeof abrir_editor.arguments[5] != 'undefined' ) {
		destino += '?'+abrir_editor.arguments[5];
	}
	
	var obj = null; with (document){ if (getElementById)
  obj = getElementById(("anc_"+id_base)); }
	
		oconteudo ='<div id="'+id_base+'" class="div_admin" style="width:'+largura+'px;">\n';
		oconteudo +='<div class="cabecalho_admin">\n';
		oconteudo +='<img src="imagens/imagens_admin/fechar.gif" alt="fechar" width="8" height="20" onclick="fechar_janela(\''+id_base+'\');" />\n';
		oconteudo +='<img src="imagens/imagens_admin/minimizar.gif" alt="minimizar" width="8" height="20" onclick="minimizar_janela(\''+id_base+'\');" />\n';
		oconteudo +='<div onmousedown="dragStart(event, \''+id_base+'\');">'+titulo+'</div>\n';
		oconteudo +='</div>\n';
		oconteudo +='<div id="form_'+id_base+'"></div>\n';
		oconteudo +='</div>';
	if (obj){
		obj.innerHTML = oconteudo;
	}
	ajaxpage(destino, "form_"+id_base);
}

function fechar_janela(id_base) {
	var obj = null; with (document){ if (getElementById)
  obj = getElementById(("anc_"+id_base)); }
	if (obj){
		obj.innerHTML = '';
	}
}

function minimizar_janela(id_base) {
	var obj = null; with (document){ if (getElementById)
  obj = getElementById(("form_"+id_base)); }
  if (obj){
    if (obj.style.display == 'none')
      eval("obj.style.display='block'");
    else eval("obj.style.display='none'");
  }
}
//Precisa de ajustes para o novo abrir_editor
function ajustar_janela(id_base,titulo,largura,altura) {
	var obj = null; with (document){ if (getElementById)
  obj = getElementById((id_base)); }
  if (obj){
		eval("obj.getElementsByTagName('div')[0].getElementsByTagName('div')[0].innerHTML = '"+titulo+"'");
    eval("obj.getElementsByTagName('iframe')[0].width='"+largura+"'");
		eval("obj.getElementsByTagName('iframe')[0].height='"+altura+"'");
		eval("obj.style.width='"+largura+"px'");
  }
}

function apagar(){
	apaga = confirm("Deseja realmente excluir o item ?");
	return apaga;
}

function MM_changeProp(objId,x,theProp,theValue) { //v9.0
  var obj = null; with (document){ if (getElementById)
  obj = getElementById(objId); }
  if (obj){
    if (theValue == true || theValue == false)
      eval("obj.style."+theProp+"="+theValue);
    else eval("obj.style."+theProp+"='"+theValue+"'");
  }
}


function abrir_form(tipo,id,info) {
	switch(tipo) {
		case 'senha':
			MM_changeProp('anc_site_admin','','marginLeft','640px');
			MM_changeProp('anc_site_admin','','marginTop','0px');
			abrir_janela('site_admin','admin/senhaupd.php','alteração de senha',300,130);
			break;
		case 'downloads':
			MM_changeProp('anc_downloads_admin','','marginLeft','500px');
			abrir_janela('downloads_admin','admin_textos.php','downloads',458,400,'tipo=downloads');
			break;
		case 'envio_senha':
			cabecalho_tx='e-mail de login e senha';
			MM_changeProp('anc_envio_admin','','marginLeft','130px');
			MM_changeProp('anc_envio_admin','','marginTop','-30px');
			abrir_janela('envio_admin','',cabecalho_tx,300,250);
			break;
		case 'envio_fax':
			cabecalho_tx='e-mail de fax';
			if (id=='12') {
				cabecalho_tx='e-mail de pedido emitido';
			}
			if (id=='13') {
				cabecalho_tx='e-mail de número de apólice';
			}
			MM_changeProp('anc_envio_admin','','marginLeft','130px');
			MM_changeProp('anc_envio_admin','','marginTop','-30px');
			abrir_janela('envio_admin','',cabecalho_tx,300,250);
			break;
		case 'opcoes':
			cabecalho_tx = 'opções do pedido nº'+id;
			MM_changeProp('anc_mini_opcoes_admin_'+id,'','marginTop','-30px');
			abrir_editor('mini_opcoes_admin_'+id,'admin_opcoes.php',cabecalho_tx,370,80,'id='+id+'&vazio=sim');
			break;
		case 'envio_opcoes':
			cabecalho_tx='e-mail de fax';
			if (info=='12') {
				cabecalho_tx='e-mail de pedido emitido';
			}
			if (info=='13') {
				cabecalho_tx='e-mail de número de apólice';
			}
			abrir_janela('mini_opcoes_admin_'+id,'',cabecalho_tx,370,80);
			break;
		case 'texto':
			MM_changeProp('anc_texto_admin_'+id,'','marginLeft','-18px');
			MM_changeProp('anc_texto_admin_'+id,'','marginTop','-153px');
			cabecalho_tx = 'edição de texto';
			abrir_editor('texto_admin_'+id,'admin_textos.php',cabecalho_tx,350,310,'id='+id+'&vazio=sim');
			break;
		case 'texto2':
			MM_changeProp('anc_texto2_admin','','marginLeft','-18px');
			MM_changeProp('anc_texto2_admin','','marginTop','-202px');
			cabecalho_tx = 'edição de texto';
			abrir_editor('texto2_admin','admin_textos.php',cabecalho_tx,350,310,'id='+id+'&vazio=sim');
			break;
		case 'itens':
			MM_changeProp('anc_texto_admin_'+id,'','marginLeft','-18px');
			MM_changeProp('anc_texto_admin_'+id,'','marginTop','-153px');
			cabecalho_tx = 'edição de itens';
			abrir_editor('texto_admin_itens','admin_textos.php',cabecalho_tx,350,310,'id='+id+'&vazio=sim&tipo=itens');
			break;
		case 'rodape':
			MM_changeProp('anc_texto_admin_'+id,'','marginLeft','-18px');
			MM_changeProp('anc_texto_admin_'+id,'','marginTop','-101px');
			cabecalho_tx = 'edição do rodapé';
			abrir_editor('texto_admin_'+id,'admin_textos.php',cabecalho_tx,570,50,'id='+id+'&vazio=sim'+'&tipo=rodape');
			break;
	}
}

//Funções de setup do editor de textos
// foi editado o arquivo themes/advanced/langs/pt.js linhas 6,12
// foi editado o arquivo themes/advanced/skins/default/ui.css linhas 115, 120
function setup_texto(alvo) {
   tinyMCE.init({
		theme: "advanced",
		mode : "specific_textareas",
		editor_selector : "texto_setup_"+alvo,
		plugins : "advlink,safari,searchreplace,advimage",
		auto_reset_designmode : true,
		language : "pt",
		content_css : "estilos_texto.css",
		relative_urls : true,
		inline_styles : false,
		theme_advanced_toolbar_location : "top",
		external_link_list_url : "links_lista.php",
		external_image_list_url : "lista_imagens.php?id="+alvo,
		theme_advanced_resizing : true,
		theme_advanced_statusbar_location: "bottom",
		theme_advanced_path : false,
		theme_advanced_resizing_use_cookie : false,
		theme_advanced_buttons1 : "undo,redo,|,cut,copy,paste,selectall,|,search,replace,|,cleanup,code",
		theme_advanced_buttons2 : "image,ali_cen,ali_esq,ali_dir,|,bullist,|,sub,sup,|,outdent,indent,|,anchor,link,unlink",
		theme_advanced_buttons3 : "bold,italic,|,charmap,|,justifyleft,justifycenter,justifyright, justifyfull,|,formatselect",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resize_horizontal : false,
		theme_advanced_blockformats : "p,h2,h3,h4",
		valid_elements : 'p[align],a[name|href|target|title|onclick],code[*],strong/b,h2[align],h3[align],h4[align],em/i,u,br,ul,li,sub,sup,img[width|height|class|alt|title|src]',
		setup : function(ed) {
			// Add a custom button
			ed.addButton('ali_esq', {
			title : 'Alinhar imagem a esquerda do texto',
			image : 'imagens/imagens_admin/foto_esq.gif',
				onclick : function(n) {
					// Add you own code to execute something on click
					ed.execCommand('mceSetCSSClass',false,'foto_aesq');
				}
			});
			ed.onNodeChange.add(function(ed, cm, n) {
				cm.setActive('ali_esq', n.nodeName == 'IMG' && n.className == 'foto_aesq');
				cm.setDisabled ('ali_esq', n.nodeName != 'IMG');
			});
			
			ed.addButton('ali_dir', {
			title : 'Alinhar imagem a direita do texto',
			image : 'imagens/imagens_admin/foto_dir.gif',
				onclick : function() {
					// Add you own code to execute something on click
					ed.execCommand('mceSetCSSClass',false,'foto_adir');
				}
			});
			ed.onNodeChange.add(function(ed, cm, n) {
				cm.setActive('ali_dir', n.nodeName == 'IMG' && n.className == 'foto_adir');
				cm.setDisabled ('ali_dir', n.nodeName != 'IMG');
			});
			
			ed.addButton('ali_cen', {
			title : 'Alinhamento padrão da imagem',
			image : 'imagens/imagens_admin/foto_cen.gif',
				onclick : function() {
					// Add you own code to execute something on click
					ed.execCommand('mceSetCSSClass',false,'');
				}
			});
			ed.onNodeChange.add(function(ed, cm, n) {
				cm.setActive('ali_cen', n.nodeName == 'IMG' && n.className == '');
				cm.setDisabled ('ali_cen', n.nodeName != 'IMG');
			});
		}
	});
}

function setup_rodape(alvo) {
   tinyMCE.init({
		theme: "advanced",
		mode : "specific_textareas",
		editor_selector : "texto_setup_"+alvo,
		plugins : "advlink,safari,searchreplace",
		auto_reset_designmode : true,
		language : "pt",
		content_css : "estilos_rodape.css",
		relative_urls : true,
		theme_advanced_toolbar_location : "top",
		external_link_list_url : "links_lista.php",
		theme_advanced_statusbar_location: "bottom",
		theme_advanced_path : false,
		theme_advanced_resizing_use_cookie : false,
		theme_advanced_buttons1 : "undo,redo,|,bold,italic,|,charmap,|,sub,sup,|,link,unlink,|,cut,copy,paste,selectall,|,search,replace,|,cleanup,code",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		valid_elements : 'a[name|href|target|title|onclick],p[style],code[*],strong/b,h3[align],em/i,u,br,ul,li,sub,sup'
	});
}

//Funcoes deste projeto em especial
function ativar2(objId) { //v9.0
	var obj = null; with (document){ if (getElementById)
	obj = getElementById(objId); }
	if (obj){
		if (obj.style.display == 'none')
			eval("obj.style.display='inline'");
	}
}

function desativar2(objId) { //v9.0
	var obj = null; with (document){ if (getElementById)
	obj = getElementById(objId); }
	if (obj){
		if (obj.style.display == 'inline')
			eval("obj.style.display='none'");
	}
}

function inativo_atendimento(objId) { //v9.0
  var obj = null; with (document){ if (getElementById)
  obj = getElementById(objId); }
  if (obj){
    if (obj.style.display == 'none') {
      eval("obj.style.display='block'");
			window.open ('admin_atendimento_form.php', 'frame_atendimento');
		} else {
			frame_atendimento.document.liberar.submit();
		}
  }
}
function inativo_novo(objId) { //v9.0
  var obj = null; with (document){ if (getElementById)
  obj = getElementById(objId); }
  if (obj){
    if (obj.style.display == 'none') {
      eval("obj.style.display='block'");
			window.open ('admin_atendimento_form.php?id=novo', 'frame_novo');
		} else {
			eval("obj.style.display='none'");
			window.open ('', 'frame_novo');
		}
  }
}

