// biblioteca javascript de funçoes do
// myauth, DOM, XML e criticas
var AjaxText = null;
var AjaxRequest = null;
var lastmsg = null;
var msgid = 0;
var debugin = 0;
var client_index_char = '';
var client_index_page = 1;
var autoupdate = false;
var autoupdateconsole = '';
var autointerval = 0;
var autoprocess = 0;
var autoupdate_count = 0;
var ajahload_name = '';
var ajahget_name = '';
var PRE_URL = 'ajah.php?console=';

function Ajax(){
	var XMLR=false;
	try{XMLR=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){
	try{XMLR=new ActiveXObject("Microsoft.XMLHTTP")}catch(sc){XMLR=null}}
	if(!XMLR&&typeof XMLHttpRequest!="undefined"){
		XMLR=new XMLHttpRequest()
	}
	return XMLR;
}
function XML() {
	DOMD = null;
	if (window.ActiveXObject) { // IE
		try {
			DOMD = new ActiveXObject("Msxml2.DOMDocument");
		} catch (e) {
			try {
			DOMD = new ActiveXObject("Msxml.DOMDocument");
			} catch (e) {
				DOMD = null;
			}
		}
	} else if(document.implementation&&document.implementation.createDocuments) {
		try {
			DOMD = document.implementation.createDocument("","",null); // Mozilla
		} catch (e) {
			DOMD = null;
		}
	}
	return DOMD;
}

// manipulação de mensagens
function hidemsg(id){
	//alert(id);
	if(msgid == id){
		document.getElementById('msg').innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;";	
	}
}

function showmsg(msg, type, time){
	var color = '#336699';
	if(type == 'alert'){
		color = '#FF9900';
	}
	if(type == 'error'){
		color = '#FF0000';
	}
	if(type == 'info'){
		color = '#66CC99';
	}
	var showmsg = "<b><font color='" + color + "'>" + msg + "</font></b>";
	if(msg != lastmsg){
		document.getElementById('msg').innerHTML = showmsg;	
	}
	lastmsg = msg;
	// remover mensagem
	msgid++;
	if(time){
		window.setTimeout('hidemsg(' + msgid + ')',time)
	}
}

// funcao de atualizacao de console ajah
function autoupdate_function(){
	autoupdate_count++;
	// debug("Autoupdate: " + autoupdate + " count: " + autoupdate_count + " interval: " + autointerval);
	if(autoupdate && autoupdate_count>=autointerval){
		if(!autoprocess){
			auto_ajah_request();
		}
		autoupdate_count = 0;
	}
}

// iniciar recurso ajah automatico timeporizado
function autoupdate_start(){
	autoupdate = true;
	if(autointerval==0) autointerval = 5;
	// iniciar objeto temporizado
}

// iniciar recurso ajah automatico timeporizado
function autoupdate_stop(){
	autoupdate = false;
}


// descarrega conteudo (limpa) e destroi objeto
function ajah_unload(htname){
/*
	debug("Flash destroy");
	flash_destroy();
*/
	debug("Empty htname");
	if(htname!='') document.getElementById(htname).innerHTML = "";
}

function ajax_destroy(){
	AjaxRequest = null;
}

function auto_ajah_return(){
	if (AjaxRequest.readyState == 4) {
		if(AjaxRequest.status == 200){
			// showmsg("<img src='/img/check16.gif' align='absmiddle'> Auto-carregado!" + "<!-- " + autoupdate_count + " --> ", 'info', 2000);
			var text = AjaxRequest.responseText;
			if(text != AjaxText){
				// destruir objetos flash
				//flash_destroy();

				document.getElementById('console').innerHTML = text;
				AjaxText = text;
			}
			// limpar memoria;
			ajax_destroy();
		}
		// desativar processamento
		autoprocess = 0;
	}
}

function auto_ajah_request(){
	var timestamp = new Date();

	var url = PRE_URL + autoupdateconsole + '&via=ajah&nocache=' + timestamp.getTime();

	//showmsg("<img src='/img/check16.gif' align='absmiddle'> Auto-carregando...", 'info', 2000);

	// ativar processamento
	autoprocess = 1;

	// requisitar
	AjaxRequest = null;
	AjaxRequest = new Ajax();
	AjaxRequest.onreadystatechange = auto_ajah_return;
	AjaxRequest.open('GET', url, true);
	debug("Auto Ajah request: " + url);
	AjaxRequest.send(null);
}

function ajah_return(){
	// retorno da busca HTML
		if (AjaxRequest.readyState == 4) {
			if (AjaxRequest.status == 200) {
				// requisição concluida
				var text = AjaxRequest.responseText;
				if(text != AjaxText){
					// destruir objetos flash
					//flash_destroy();
					
					document.getElementById('console').innerHTML = text;
					AjaxText = text;
				}
				showmsg("<img src='/smallicons/minicheck.gif' align='absmiddle'> Carregado!", 'info', 2000);
				// limpar memoria
				ajax_destroy();
			} else {
				showmsg("<img src='/smallicons/comments_red.gif' align='absmiddle'> Erro ao carregar conteudo: " + AjaxRequest.statusText, 'error', 5000);
			}
			// desativar processamento
			autoprocess = 0;
		}
}


function ajah_request(console, autoupdateinterval){
	showmsg("<img src='/smallicons/al.gif' align='absmiddle'> Carregando", 'info', 0);

	var timestamp = new Date();
	var url = PRE_URL + console + '&via=ajah&nocache=' + timestamp.getTime();


	// burlar bugs do IE
	if(ajahload_name != ''){
		ajah_unload(ajahload_name);
		ajahload_name = '';
	}

	// ativar processamento
	autoprocess = 1;
	
	// auto-atualizar
	if(autoupdateinterval!='' || autoupdateinterval != 0){
		autoupdate = true;
		autointerval = autoupdateinterval;
		autoupdateconsole = console;
		autoupdate_start();
	}else{
		autoupdate = false;
		autoupdate_stop();
	}
	
	// requisitar
	AjaxRequest = null;
	AjaxRequest = new Ajax();
	AjaxRequest.onreadystatechange = ajah_return;
	AjaxRequest.open('GET', url, true);
	debug("Ajah request: " + url);
	AjaxRequest.send(null);
}

function ajah_requestcache(console){
	var timestamp = new Date();
	var url = PRE_URL + console + '&via=ajah&nocache=' + timestamp.getTime();

	// burlar bugs do IE
	if(ajahload_name != ''){
		ajah_unload(ajahload_name);
		ajahload_name = '';
	}

	// ativar processamento
	autoprocess = 1;
	
	// desativar auto-atualizar
	autoupdate = false;
	autoupdate_stop();


	// requisitar
	AjaxRequest = null;
	AjaxRequest = new Ajax();
	AjaxRequest.onreadystatechange = ajah_return;
	AjaxRequest.open('GET', url, true);
	AjaxRequest.send(null);
}

var autoupdate_obj = setInterval('autoupdate_function()',1000);



