Wednesday, August 01, 2007

Creating a Modular 'Waiting' dialog in javascript

Have you ever needed to create a modular pop up that displayed a message to the user while you did some processing in the background? I have and I got fed up of constantly creating a new modular pop up each time.

The following javascript code is easy to use and is based upon the grayOut javascript function.

This snippet of code creates a modal dialog box. This is useful for times when you want to make the user wait while you do something in the background or if you would like to make the user aware of something on the page. While the dialog box is active, most interactive elements on the page (links, etc) are inactive.

The dialog box is dynamically created and can be customized to a certain height and width. The dialog box has a high z-index and should be placed above most other elements. Items which have a higher z-index than the dialog box will appear on top of the dialog box.

By default the dialog box has a z-index of 100.

Other default parameters are:

  • height - 25% of the page height
  • width - 35% of the page width
  • opacity - 90% (i.e. semi-transparent)
  • border style - solid
  • border color - black
  • border width - 3px
  • text alignment - center
  • many others ...
All of the above can be configured to your liking. Look at the comments in the script for more customization features.

Example. This snippet has been tested in Firefox 2, Safari 3, and Internet Explorer 7.


function modular_waiting(vis, msg, options) {
// Pass true to show the dialogue screen, false to hide
// options are optional. This is a JSON object with the following (optional) properties
// opacity:0-100 // Lower number implies a more transparent dialog box
// zindex: # // HTML elements with a higher zindex appear on top of others with a lower number
// height: 0-100 // The height in percentage terms relative to the browser screen
// width: 0-100 // The width in percentage terms relative to the browser screen
// name: text // A name for your dialog box - this is what you will refer to when showing and hiding the dialog
// align: text // Should the text be placed in the: center, justify, left or right
// fontcolor: (#xxxxxx) // Standard RGB Hex color code
// fontweight: 100-900 // The boldness of the text in increments of 100. 900 implies maximum boldness
// bgcolor: (#xxxxxx) // Standard RGB Hex color code
// borderwidth: # // The thickness of the border
// bordercolor: (#xxxxxx)// The color of the border in HEX
// borderstyle: text // The style of the border. One of dashed, dotted, double, groove, inset, none, outset, ridge, solid
// modular_waiting(false); // hide the dialog
// modular_waiting(true, 'my message to show here', {'name':'myName', 'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'}); // show the dialog
// Because options is JSON opacity/zindex/bgcolor are all optional and can appear
// in any order. Pass only the properties you need to set.
var options = options || {};
var zindex = options.zindex || 100;
var height = options.height || 25;
var width = options.width || 35;
var opacity = options.opacity || 90;
var opaque = (opacity / 100);
var bgcolor = options.bgcolor || '#FFDC75';
var fColor = options.fontcolor || '#0000FF';
var fWeight = options.fontweight || 600;
var bcolor = options.bordercolor || 'black';
var bstyle = options.borderstyle || 'solid'
var bwidth = options.borderweight || '3px'
var align = options.align || 'center';
var __div_id__ = options.name || '__modular__popup__';

var dark=document.getElementById(__div_id__ );

if (!dark) {
var tbody = document.getElementsByTagName("body")[0];
var tnode = document.createElement('div'); // Create the layer.
tnode.style.position='fixed'; // Position absolutely
tnode.style.overflow='auto';
tnode.style.display='none'; // Start out Hidden
tnode.id=__div_id__ ; // Name it so we can find it later
tbody.appendChild(tnode); // Add it to the web page
dark=document.getElementById(__div_id__ ); // Get the object.
}

if (vis) {
var txt = document.createElement('font')
txt.setAttribute('color',fColor);
txt.style.fontWeight = fWeight;
txt.innerHTML = msg.replace(/\"/g,""").replace(/\'/g,"'").replace(/\/g,">").replace(/\n/g,"<br/>")

dark.style.border = bwidth + " " + bstyle + " " + bcolor;
dark.setAttribute('align',align);
dark.style.opacity=opaque;
dark.style.MozOpacity=opaque;
dark.style.filter='alpha(opacity='+opacity+')';
dark.style.zIndex=zindex;
dark.style.backgroundColor=bgcolor;
dark.style.width= width+'%';
dark.style.height= height+ '%';
dark.style.display='block';
dark.style.cursor = 'wait';
dark.appendChild(txt);
dark.style.left = '40%'
dark.style.top = '50%'
} else {
// TODO - should i remove the element?
dark.style.display='none';
dark.innerHTML = "";
}
}

Activate/Deactivate with a function call to modular_waiting(vis, msg, options).

vis is a true/false variable. If you pass true, the dialog will appear. If you pass false, the dialog will be removed.

"msg" is the message that you would like to display.

options is an optional JSON object this allows you to send only the properties you want modified. You don't have to pass options if you don't want. modular_waiting(true,"Hello world") and modular_waiting(false) will work quite nicely.

To set all of the options, do the following {'opacity':'70', 'bgcolor':'#0000FF', 'zindex':'25', 'height':'50','height':'50', 'name':'myPopUpDialog', 'align':'left', 'fontcolor':'red', 'fontweight':'900', 'bgcolor':'yellow', 'borderwidth':'5', 'bordercolor':'blue', 'borderstyle':'dotted' }. This will create a somewhat transparent dialog box that covers approximately 50% of the window, with text aligned left, colored red and bold on a yellow background and a dotted, blue border!

Feel free to change the values yourself!

Order doesn't matter, mix and match however you wish.


Tuesday, June 05, 2007

The Dogs of the Dow - Half Year Performance 2007

Previously, I talked about the dogs of the dow and how you could use that as a strategy. Below is an update for those that are interested in this investment strategy. The table includes the Dow component and it's year to date increase.

Please note that dividends are not re-invested, but are included in the rate of return.

As of June 4, 2007:

Pfizer5.8%
Verizon Communications15.9%
Altria-16.1%
AT&T19.2%
Citigroup0.0%
Merck18.5%
General Motors4.0%
General Electric0.3%
Dupont9.3%
Coca-Cola9.1%

The overall rate of return for the first 6 months was about 6.6%.
The overall gain for the Dow in the same period was roughly 9.3%.

There were 2 components in negative territory (Citigroup was slightly in the red and Altria was in the red by double digits).

Cheers!

Disclaimer:
  • I am not be liable for any investment decision made or action taken based upon the information on this page.
  • I suggest you check with a broker or financial adviser before making any stock investing decisions.
  • I am not offering financial advise, but rather commenting on widely available investment strategies.
  • Finally, Caveat emptor!

Thursday, May 03, 2007

Gold Stocks - Trade Idea Part 2

Last time, I talked about naked put selling on gold stocks that you wouldn't mind owning. Let's assume, following the earlier example, that at the end of May you are 'put' into Barrick Gold Corp because the stock traded lower than the strike price of the put options you wrote (i.e. $30 USD).

This means that you are now the proud owner of 500 shares of ABX. What can you do? You could hold the stock and hope it goes up or you could sell the stock for a slight loss (remember, it must be less than $30 because you were 'put' into the stock). Alternatively, you could write covered calls on the stock.

So, what is a 'call'? A call option is an agreement that gives an investor the right (but not the obligation) to buy a stock (or some other other financial instrument) at a specified price within a specific time period. If you sell someone the right to buy stock that you own, then this is a covered call.

Imagine now that you sell 5 call contracts that expire in one months' time at the $30 strike price. Imagine that each contract is trading at $0.75. This would mean that you would receive:
  • $0.75 x 500 or $375 for selling someone the right to buy your stock in one months' time!
If at the end of next month, the stock is trading below the strike price of $30, then you keep your stock and the $375 that you made on the call options. For argument's sake, if the stock is trading at $50, you are obligated to sell your stock for $30. In other words, if on the day of expiration for your call option, ABX is trading at any price over $30, you must deliver your stock for $30.

Let's assume now that for one year, you write naked put options for ABX, and when you are put into the stock you write covered calls. Moreover, when you called to deliver your stock, you write naked puts for the following month. In other words, every month that goes by, you either own ABX and sell covered calls, or don't own ABX and sell naked puts.

Let's also assume that your monthly premium on the option writing is approximately $0.85. Then each month, you generate about 500 x $0.85 or $425. For the year, you would generate about $5,100. Remembering that you had to initially have $15,000 in your account to cover the naked put options, you are looking at yearly rate of return of about 33%. Not too shabby!

Disclaimer:
  • I am not be liable for any investment decision made or action taken based upon the information on this page.
  • I suggest you check with a broker or financial adviser before making any stock investing decisions.
  • I am not offering financial advise, but rather commenting on widely available investment strategies.
  • Options trading can be extremely risky.
  • Finally, Caveat emptor!