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

//
// Executa a funcao especificada caso a tecla tambem especificada seja pressionada
// em um campo "text" ou "textarea" especifico
//
goPress = function( obj_event, int_key, str_function ) {
	if ( window.event )
		int_keyPress = window.event.keyCode;
	
	else if ( obj_event )
		int_keyPress = obj_event.which;
	
	if ( int_keyPress == int_key )
		eval( str_function + '();' );
}

//
// Bloqueia caracteres excedentes em textareas
//
maximum = function( obj_input, obj_event, int_value ) {
	if ( window.event )
		int_keyPress = window.event.keyCode;
	
	else if ( obj_event )
		int_keyPress = obj_event.which;
	
	// Valida se a tecla nao for o "backspace" e as setas
	if ( ( int_keyPress != 8 ) && ( int_keyPress != 0 ) )
		if ( obj_input.value.length >= int_value ) return false;
	
	return true;
}

//
// Funcao que adiciona o site aos favoritos
//
addFavorites = function( str_url, str_title ) {
	if ( window.external.addPanel )
		// Firefox
		window.external.addPanel( str_title, str_url, '' );
	
	else
		if ( window.external )
			// IE
			window.external.AddFavorite( str_url, str_title );
}

//
// Seta o foco no primeiro campo "text" que existir na pagina
//
loadFocus = function() {
	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
}

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

//
// Retira os espacos ao redor da expressao
//
String.prototype.trim = function() {
	return this.replace( /^\s+|\s+$/, '' );
};

//
// Adiciona o site como pagina inicial
//
function addHomePage() {
	if ( ie4 ) {
		document.getElementById( 'base' ).style.behavior = "url( #default#homepage )";
		document.getElementById( 'base' ).setHomePage( "http://www.bemdesaude.com" );
	
	} else {
		alert( "Este recurso automático só está disponível para o Internet Explorer.\n\nConfigure no menu \"ferramentas -> opções\" o endereço \"http://www.bemdesaude.com\"." );
	}
}

//
// 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";

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

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

// Retorna a altura da tela
getWindowHeight = function() {
	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;
}


/*function dumpProps(obj, parent) {
			for (var i in obj) {
				if (parent) { msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
				if (!confirm(msg)) { return; }
				if (typeof obj[i] == "object") { 
					if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
				}
			}
		}*/
		
/*

*/

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

/*

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



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





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

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