// 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');

function addDownload( artigo ){
	Vhtml.open('/artigos/?show=addDownload&artigo=' + artigo );
}

// 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 );
}

function verifyResume() {
	
	if ( ( document.f.resumo_br.value == '' ) && ( document.f.resumo_br.value == '' ) ) {

		if ( confirm( 'Você não preencheu o resumo do artigo. Deseja realmente continuar?' ) )
			return true;
		else
			return false;
	
	}
	
}

function alertSend() {
	
	alert( ' Atenção! O envio do artigo poderá demorar. \nPressione OK e aguarde uma mensagem de envio finalizado. \nA equipe do Artigo Científico agradece sua participação.' );
	return true;
	
}

function verifyNome() {
	
	if ( document.f.nome.value == '' ) {
		
		alert( 'Informe o nome completo do autor a ser pesquisado.' );

		return false;
	}	else {
		
		document.f.type.value = 1;
		return true;
		
	}
	
}

function limparAutor(v) {
	document.f.limpar.value = v;
	return true;
}

function limparInserir(v) {
	document.f.inserir.value = v;
	return true;
}

function verifyNomeIns() {
	
	if ( document.f.nome_ins.value == '' ) {
		
		alert( 'Informe o nome completo do autor a ser inserido.' );
		return false;

	}	else {

		document.f.type.value = 2;
		return true;
		
	}
	
}

function cleanNomeIns() {

	if ( document.f.nome_ins )
		document.f.nome_ins.value = '';
	
}

function cleanNome() {

	if ( document.f.nome )
		document.f.nome.value = '';
	
}

// 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 );
}

function fillSelect( obj_combo, str_result ) {
	// Limpa o combo
	while ( obj_combo.length > 0 )
		obj_combo.options[obj_combo.length - 1] = null;
	
	eval( str_result );
	
	// Insere os novos elementos no array
	for ( x = 0; x < arr_array.length; x++ )
		obj_combo.options[x] = new Option( arr_array[x][1], arr_array[x][0] );
}

function MM_preloadImages() { //v3.0
  var d=document; 
  if(d.images) {
  	
  	if(!d.MM_p)
  		d.MM_p=new Array();
  		
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
    
    for(i=0; i<a.length; i++)
    	if (a[i].indexOf("#")!=0) {
    		d.MM_p[j]=new Image;
    		d.MM_p[j++].src=a[i];
    	}
  }
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
		if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v3.0
	
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	
	for (i=0; i<(args.length-2); i+=3)
		if ((obj=MM_findObj(args[i]))!=null) {
			v=args[i+2];
	
			if (obj.style) {
				obj=obj.style;
				v=(v=='show')?'visible':(v='hide')?'hidden':v;
			}
		
			obj.visibility=v;
		}
}

function Restore1() { //v3.0
  var i,x,a=document.MM_sr;
  MM_findObj('Image1').src=MM_findObj('Image1').oSrc;
}

function Restore2() { //v3.0
  var i,x,a=document.MM_sr;
  MM_findObj('Image1').src=MM_findObj('Image1').oSrc;
  MM_findObj('Image2').src=MM_findObj('Image2').oSrc;
}

function Restore3() { //v3.0
  var i,x,a=document.MM_sr;
  MM_findObj('Image1').src=MM_findObj('Image1').oSrc;
  MM_findObj('Image2').src=MM_findObj('Image2').oSrc;
  MM_findObj('Image3').src=MM_findObj('Image3').oSrc;
}

function Restore4() { //v3.0
  var i,x,a=document.MM_sr;
  MM_findObj('Image1').src=MM_findObj('Image1').oSrc;
  MM_findObj('Image2').src=MM_findObj('Image2').oSrc;
  MM_findObj('Image3').src=MM_findObj('Image3').oSrc;
  MM_findObj('Image4').src=MM_findObj('Image4').oSrc;
}

function Restore5() { //v3.0
  var i,x,a=document.MM_sr;
  MM_findObj('Image1').src=MM_findObj('Image1').oSrc;
  MM_findObj('Image2').src=MM_findObj('Image2').oSrc;
  MM_findObj('Image3').src=MM_findObj('Image3').oSrc;
  MM_findObj('Image4').src=MM_findObj('Image4').oSrc;
  MM_findObj('Image5').src=MM_findObj('Image5').oSrc;
}


function MM_findObj(n, d) { //v4.0
  var p,i,x; 
  
  if(!d)
  	d=document;
  
  if((p=n.indexOf("?"))>0&&parent.frames.length) {

  	d=parent.frames[n.substring(p+1)].document;
  	n=n.substring(0,p);
  
  }
  
  if(!(x=d[n])&&d.all)
  	x=d.all[n];
  	
  for (i=0;!x&&i<d.forms.length;i++)
  	x=d.forms[i][n];
  
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
  	x=MM_findObj(n,d.layers[i].document);
  
  if(!x && document.getElementById)
  	x=document.getElementById(n);
  	
  return x;
  
}
