// Reconhece o browser
var ns4 = ( document.layers ) ? true : false
var ie4 = ( document.all ) ? true : false

var iens6 = document.all || document.getElementById;
var ieBox = iens6 && (document.compatMode == null || document.compatMode != 'CSS1Compat');

// Abre uma janela popup
function popup ( url, nome, largura, altura, posicao, resize, barras ) {
	if ( posicao == 0 ) {
		posleft = 20;
		postop  = ( screen.height ) ? ( screen.height - altura ) / 2 - 50 : 100;
	} else if ( posicao == 1 ) {
		posleft = ( screen.width ) ? ( screen.width - largura ) / 2 : 100;
		postop  = ( screen.height ) ? ( screen.height - altura ) / 2 - 50 : 100;
	} else if (posicao == 2 ) {
		posleft = ( screen.width ) ? ( screen.width - largura ) - 28 : 100;
		postop  = ( screen.height ) ? ( screen.height - altura ) / 2 - 50 : 100;
	}
	opBarras = ( barras ) ? "yes" : "no";
	opResize = ( resize ) ? "yes" : "no";
	opResize = 'yes';

	settings = 'z-lock=yes, width=' + largura + ', height=' + altura + ', top=' + postop + ', left=' + posleft + ', scrollbars=' + opBarras + ', location=no, directories=no, status=no, menubar=no, toolbar=no, resizable=' + opResize;
	window.open( url, nome, settings );
}

// Abre a tela das screenshots
function screenshot( image, width, height ) {
	popup( '/?show=screenshot&str_image=' + image, 'screenshot', width, height, 1, 0, 0 );
}

// Faz uma pergunta
function question( str_question ) {
	if ( confirm( str_question ) )
		return true;
	else
		return false;
}

// Escreve dinamicamente dentro de um layer
function writer( id, txt ) {
	if ( ns4 ) {
		var lyr = document.layers[id].document;
		lyr.write( txt );
		lyr.close();
	} else
		document.getElementById( id ).innerHTML = txt;
}

// Recupera um elemento
function getElement( id ) {
	if ( ns4 )
		return document.layers[id].document;
	else
		return document.getElementById(id);
}

// Exibe ou oculta um elemente
function viewElement( str_element, boo_status ) {
	// Recupera o elemento
	obj = getElement( str_element );
	if ( boo_status )
		obj.style.display = '';
	else
		obj.style.display = 'none';
}

// Filtra as teclas pressionadas
function filtra(obj, padrao) {
	tk    = (ie4) ? event.keyCode : event.which;
	letra = String.fromCharCode(tk);

	for (x = 0; x < padrao.length; x++)
		if (padrao.slice(x, x + 1) == letra) return true;

	return false;
}

// Bloqueia caracteres excedentes em textareas
function maximo(obj, valor) {
	if (obj.value.length >= valor) return false;
}

// Retorna a largura da tela
function getWindowWidth() {
	return iens6 ? ( ieBox ? ( document.body.clientWidth  + document.body.scrollLeft ) : ( document.documentElement.clientWidth + document.documentElement.scrollLeft ) ) : window.innerWidth;
}

// Retorna a altura da tela
function getWindowHeight() {
	if ( iens6 ) {
		if ( ieBox )
			teste = document.body.clientHeight + document.body.scrollTop;
		else
			teste = document.documentElement.clientHeight + document.documentElement.scrollTop;
	} else
		teste = window.innerHeight;

	return teste;
}

// Esta função coloca o foco no primeiro campo text que existir na tela
function loadFocus() {
	var obj = document.forms;

	for ( x = 0; x < obj.length; x++ ) {
		var objetos = obj[x].length;
		
		for ( i = 0; i < objetos; i++ ) {
			if ( ( ( obj[x].elements[i].type == 'text' ) || ( obj[x].elements[i].type == 'textarea' ) || ( obj[x].elements[i].type == 'password' ) ) && ( obj[x].elements[i].disabled == false ) ) {
				obj[x].elements[i].focus();
				return true;
			}
		}
	}
	
	return false
}

// Esta função desativa todos os controles do formulário
function disabledControls() {
	var obj = document.forms;

	for ( x = 0; x < obj.length; x++ ) {
		var objetos = obj[x].length;
		
		for ( i = 0; i < objetos; i++ )
			if ( obj[x].elements[i].type != 'button' )
				obj[x].elements[i].disabled = true;
	}
}

// Formata um valor em formato de moeda ( idêntica a do PHP )
function number_format( int_value, int_decimal, str_pointer, str_comma ) {
	var str_result = '';
	
	int_value      = int_value + 1;
	
	str_value      = new String( int_value * 100 );
	
	str_number     = str_value.substr( 0, str_value.length - int_decimal );
	str_decimal    = str_value.substr( str_value.length - int_decimal, int_decimal );
	
	i = 0;
	for ( x = str_number.length; x >= 0; x-- ) {
		if ( i == 4 ) {
			i          = 1;
			str_result = str_pointer + str_result;
		}
		
		str_result   = str_number.substr( x, 1 ) + str_result;
		
		i++;
	}
	
	return ( str_result - 1 ) + str_comma + str_decimal;
}

// Busca o valor gravado em um cookie
function GetCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len   = start + name.length + 1;
	
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
		return null;
	
	if ( start == -1 )
		return null;
	
	var end   = document.cookie.indexOf( ";", len );
	
	if ( end == -1 )
		end     = document.cookie.length;
	
	return unescape( document.cookie.substring( len, end ) );
}

// Grava um cookie
function SetCookie( name, value, expires, domain, secure ) {
	var cookieString = name + "=" + escape( value ) +
										( ( expires ) ? ";expires=" + expires.toGMTString() : "" ) +
										";path=/" +
										( ( domain ) ? ";domain=" + domain : "" ) +
										( ( secure ) ? ";secure" : "" );
	
	document.cookie  = cookieString;
}

// Funcção que alterna a exibição de dois layers
function loadToolbar( int_option, str_cookie, str_objOn, str_objOff ) {
	var int_date = new Date();
	
	if ( int_option == 0 ) {
		// Carrega o status da barra lateral
		int_option = GetCookie( str_cookie );
	}
	
	if ( int_option == 1 ) {
		viewElement( str_objOn, true );
		viewElement( str_objOff, false );
	
	} else if ( int_option == 2 ) {
		viewElement( str_objOn, false );
		viewElement( str_objOff, true );
	
	} else {
		int_option = 1;
		
		viewElement( str_objOn, true );
		viewElement( str_objOff, false );
	}
	
	int_date.setTime( int_date.getTime() + ( 365 * 24 * 60 * 60 * 1000 ) );
	
	SetCookie( str_cookie, int_option, int_date );
}