var reqMoveAjax;
var reqMoveElementoAjax;

function CreateXmlHttpReqMoveObject( )
{
	// detecção do browser simplificada
	// e sem tratamento de excepções
	reqMoveAjax_temp=null;

	try
	{
		//IE7 e firefox
		reqMoveAjax_temp=new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			// IE 5 e 6
			reqMoveAjax_temp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		 catch (e)
        {
        	alert("Your browser does not support AJAX!");
        }
	}

	return reqMoveAjax_temp;
}

function ajax_call_move(id_cat)
{
	reqMoveAjax = CreateXmlHttpReqMoveObject();

	if (reqMoveAjax == null)
	{
		alert ("Your browser does not support AJAX!");
	}
	else
	{
		// Definição do URL para efectuar pedido HTTP - método GET
		// O ? no final obriga o brower a ler outra vez o xml, não fazendo cache
		reqMoveAjax.open("POST","cms/modulos/ficheiros/ficheiros_proc.php?", true);
		// Registo do EventHandler
		reqMoveAjax.onreadystatechange = ajax_call_move_handler;
        reqMoveAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        reqMoveAjax.send("accao=ficheiros_cat_get_mover&id_cat="+id_cat);

        document.getElementById('global_popup').style.display = 'block';

		div_erro.innerHTML = '';
		div_erro.style.display = 'none';
		div_sucesso.innerHTML = '';
		div_sucesso.style.display = 'none';
	}


}

function ajax_call_move_handler()
{
	//alert('Handler');
	if ( reqMoveAjax.readyState == 4 && reqMoveAjax.status == 200) // resposta do servidor completa
	{
		// propriedade responseXML que devolve a resposta do servidor
		var docxml = reqMoveAjax.responseText;
		document.getElementById('id_global_popup_msg').innerHTML = docxml;

		global_popup_form.action = 'cms/modulos/ficheiros/ficheiros_proc.php';

	}
}

function ajax_call_move_elem(id_cat,id)
{
	reqMoveAjax = CreateXmlHttpReqMoveObject();

	if (reqMoveAjax == null)
	{
		alert ("Your browser does not support AJAX!");
	}
	else
	{
		// Definição do URL para efectuar pedido HTTP - método GET
		// O ? no final obriga o brower a ler outra vez o xml, não fazendo cache
		reqMoveAjax.open("POST","cms/modulos/ficheiros/ficheiros_proc.php?", true);
		// Registo do EventHandler
		reqMoveAjax.onreadystatechange = ajax_call_move_elem_handler;
        reqMoveAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        reqMoveAjax.send("accao=ficheiros_cat_elemento_get_mover&id_cat="+id_cat+"&id="+id);

        document.getElementById('global_popup').style.display = 'block';

		div_erro.innerHTML = '';
		div_erro.style.display = 'none';
		div_sucesso.innerHTML = '';
		div_sucesso.style.display = 'none';
	}


}

function ajax_call_move_elem_handler()
{
	//alert('Handler');
	if ( reqMoveAjax.readyState == 4 && reqMoveAjax.status == 200) // resposta do servidor completa
	{
		// propriedade responseXML que devolve a resposta do servidor
		var docxml = reqMoveAjax.responseText;
		document.getElementById('id_global_popup_msg').innerHTML = docxml;

		global_popup_form.action = 'cms/modulos/ficheiros/ficheiros_proc.php';

	}
}



function fsubmit(id_form, idiomas)
{
	var formulario = document.getElementById(id_form);
	if(verifica_formulario(idiomas))
	{
		formulario.submit();
	}
}

function verifica_formulario(idiomas)
{
	var lista_idiomas = idiomas.split('|');
	var existe_erros = false;
	var msg = '';

	div_erro.style.display = 'none';
	div_sucesso.style.display = 'none';
	div_mensagem.style.display = 'none';

	for(var i=0; i<lista_idiomas.length ;i++)
	{
		var nome = document.getElementById('nome_'+lista_idiomas[i]);
		nome.style.border = input_border;

		if(ForceEntry(nome) == -1)
		{
			existe_erros = true;

			nome.style.border = input_border_erro;
			msg += '<div>* ('+lista_idiomas[i]+') '+trad[bo_lang]['nome']+'<\/div>';
		}
	}

	if(existe_erros)
	{
		div_erro.innerHTML = msg;
		div_erro.style.display = 'block';

		return false;
	}

	return true; //mudar para true
}

function felementosubmit(id_form, idiomas)
{
	var formulario = document.getElementById(id_form);
	if(verifica_formulario_elemento(idiomas))
	{
		formulario.submit();
	}
}

function verifica_formulario_elemento(idiomas)
{
	var lista_idiomas = idiomas.split('|');
	var existe_erros = false;
	var msg = '';

	div_erro.style.display = 'none';
	div_sucesso.style.display = 'none';
	div_mensagem.style.display = 'none';


	for(var i=0; i<lista_idiomas.length ;i++)
	{
		var titulo = document.getElementById('titulo_'+lista_idiomas[i]);
		titulo.style.border = input_border;

		if(ForceEntry(titulo) == -1)
		{
			existe_erros = true;

			titulo.style.border = input_border_erro;
			msg += '<div>* ('+lista_idiomas[i]+') '+trad[bo_lang]['elem_titulo']+'<\/div>';
		}
	}

	if(existe_erros)
	{
		div_erro.innerHTML = msg;
		div_erro.style.display = 'block';

		return false;
	}

	return true; //mudar para true
}


function delete_cat(id_cat)
{

	global_popup_form.action = "cms/modulos/ficheiros/ficheiros_proc.php";

	global_popup_form_option.innerHTML = '<input type="hidden" name="accao" value="ficheiros_cat_del" \/>';
	global_popup_form_option.innerHTML += '<input type="hidden" name="form_id[id_cat]" value="'+id_cat+'" \/>';

	id_global_popup_msg.innerHTML = trad[bo_lang]['confimar_apagar'];
	global_popup_button_ok.innerHTML = trad[bo_lang]['confimar_apagar_ok'];
	global_popup_button_cancel.innerHTML = trad[bo_lang]['confimar_apagar_cancel'];

	global_popup.style.height = document.getElementById('my_body').scrollHeight + "px";

	global_popup.style.display = 'block';
}

function delete_elem(id_cat,id_elem)
{

	global_popup_form.action = "cms/modulos/ficheiros/ficheiros_proc.php";

	global_popup_form_option.innerHTML = '<input type="hidden" name="accao" value="ficheiros_cat_elemento_del" \/>';

	global_popup_form_option.innerHTML += '<input type="hidden" name="form_id[id_cat]" value="'+id_cat+'" \/>';
	global_popup_form_option.innerHTML += '<input type="hidden" name="form_id[id]" value="'+id_elem+'" \/>';

	id_global_popup_msg.innerHTML = trad[bo_lang]['elemento_confimar_apagar'];
	global_popup_button_ok.innerHTML = trad[bo_lang]['elemento_confimar_apagar_ok'];
	global_popup_button_cancel.innerHTML = trad[bo_lang]['elemento_confimar_apagar_cancel'];

	global_popup.style.height = document.getElementById('my_body').scrollHeight + "px";

	global_popup.style.display = 'block';
}

//verifica as checks
function verifica_formulario2()
{
	div_erro.style.display = 'none';
	div_sucesso.style.display = 'none';
	div_mensagem.style.display = 'none';

	if(fselect_one('select_all_values'))
	{
		return true;
	}
	else
	{
		div_erro.innerHTML = trad[bo_lang]['seleccionar_um'];
		div_erro.style.display = 'block';
		return false;
	}
}

//chama a accao do select
function fsubmitlista()
{
	var lista_accao = document.getElementById('list_action');
	var form_accao = document.getElementById('accao');


	if(lista_accao.selectedIndex > 0 && verifica_formulario2())
	{
		var op_value = lista_accao.options[lista_accao.selectedIndex].value;
		if(op_value == 'multidelete') //eliminar
		{
			form_accao.value='ficheiros_cat_multidel';

			id_global_popup_msg.innerHTML =  trad[bo_lang]['confimar_multi_apagar'];
			global_popup_button_ok.innerHTML = trad[bo_lang]['confimar_multi_apagar_ok'];
			global_popup_button_ok_link.href="javascript:form_submit('formulario_lista')";

			global_popup_button_cancel.innerHTML = trad[bo_lang]['confimar_multi_apagar_cancel'];

			global_popup.style.height = document.getElementById('my_body').scrollHeight + "px";

			global_popup.style.display = 'block';
		}
	}
}

//chama a accao do select
function fsubmitlistaelemento(id_cat)
{
	var lista_accao = document.getElementById('list_action');
	var form_accao = document.getElementById('accao');


	if(lista_accao.selectedIndex > 0 && verifica_formulario2())
	{
		var op_value = lista_accao.options[lista_accao.selectedIndex].value;
		if(op_value == 'multidelete') //eliminar
		{
			form_accao.value='ficheiros_cat_elemento_multidel';

			id_global_popup_msg.innerHTML =  trad[bo_lang]['elemento_confimar_multi_apagar'];
			global_popup_button_ok.innerHTML = trad[bo_lang]['elemento_confimar_multi_apagar_ok'];
			global_popup_button_ok_link.href="javascript:form_submit('formulario_lista')";

			global_popup_button_cancel.innerHTML = trad[bo_lang]['elemento_confimar_multi_apagar_cancel'];

			global_popup.style.height = document.getElementById('my_body').scrollHeight + "px";

			global_popup.style.display = 'block';
		}
	}
}


function tipo_cat_opcoes(indice)
{
	var div_url = document.getElementById('div_url');
	div_url.style.display = 'none';

	if(indice == 1)
	{
		div_url.style.display = 'block';
	}

}


function muda_tipo_elem(id_tipo, lista_tipos)
{
	var lista_tipo_elem = lista_tipos.split("|");

	if(lista_tipos != '')
	{
		for(var i=0; i<lista_tipo_elem.length ; i++)
		{
			var div_tipo_elem = document.getElementById('tipo_elem'+i);
			if(i == id_tipo)
			{
				div_tipo_elem.style.display = 'block';
			}
			else
			{
				div_tipo_elem.style.display = 'none';
			}
		}
	}
}


function show_hide_files(id)
{
	var files_container = document.getElementById("files_container_"+id);

	if(files_container != null)
	{
		if(files_container.style.display == "block")
		{
			files_container.style.display = "none";

			var indicator = document.getElementById("visibility_container_"+id);
			indicator.innerHTML = "+";
		}
		else
		{
			files_container.style.display = "block";

			var indicator = document.getElementById("visibility_container_"+id);
			indicator.innerHTML = "-";
		}
	}
}
