Thank God, its the last week of the month! With salary just a few days away, its time to party! Alas, not enough funds!!! Does this situation sound familiar? If yes, then you probably have fallen prey to the necessary (or unnecessary) evil called the credit card.
Well, credit card is the first hurdle to effective wealth management especially for salaried people. The credit card business is really flourishing these days. How many times have you received calls from credit card companies, offering lifetime free credit cards? The calls won't stop coming until you accept one of their offers! Its really hard to reject their offers. 'I am calling from xxxx, and we are offering you a lifetime free credit card. You will get 20% cash back on booking air tickets. You also get one rupee back on every hundred rupees spent on your credit card. In addition, you also get blah, blah, blah...'. You don't want a credit card, so you say, 'Thank you, but I am not interested right now'. 'Why sir? You are getting it free for lifetime. You get this, and this, and this, and that on the card...'. Well, you just cannot refuse, especially if the person at the other end has a sweet female voice ;) Chances are that you will go for one of the cards eventually...
Initially, we are a bit tentative to use the dreaded card. But alas, once we use it, we get into the habit. Now, we end up using the credit card for each and every transaction, however small or big it may be, mostly because of the seemingly attractive offers you get. The result? A long monthly bill statement, consuming more than half of your monthly salary...When you analyze your bill statement, you agree with all the billing details. But you just keep wondering, how it adds to so much. I myself have once paid a bill of 32k(without interest), but have stopped using it. Now, I just use it for booking travel tickets... If you have absolutely no control over your credit card, then chances are that you can't pay your bill on time, and you end up paying a hefty interest the next time...
Some people will use 2-3 credit cards and then use one to pay the bill of another. While they continue to repeat this cycle, they end up paying a transaction charge every time. Little do we realize that we become used to staying in debt.
I won't say you should not use a credit card. Use it only when needed, for example when booking travel tickets. For making general purchase, get into the habit of using a debit card. Atleast, you will stop spending wastefully when your account balance reaches a certain threshold. Still better use hard cash! Atleast you would know how much you have in your wallet and how much you can spend at that moment. This seems like an obvious and useless advice at first. But it certainly ain't as useless as it might seem to be! I hope this post will make you think twice before swiping!!!
Thursday, July 26, 2007
Thursday, July 12, 2007
Getting and setting cursor position in Design mode HTML controls
Well this post is for people who have used or might use Javascript in their life. Imagine you creating a WYSIWYG text editor in javascript and wanting full control over how the text should be displayed. Say for example you display the text after processing the user input. The processing is done as the user types. One approach which immediately comes to the mind is to use a hidden textarea control to capture the key events and display the processed text inside a div element. You will have to create a custom cursor inside the div for the user to know where he is typing. The cursor problem can be best solved using some character like say '|' inside an absolutely positioned div and then toggling the display of the div with the help of a timer. So far good! But now, imagine you want to go to the middle of a previously typed word and edit it. Where would you show the cursor? You will need to calculate the position of the cursor when the user presses any key, or clicks the mouse button. Needless to say, it is not easy to manipulate the cursor position. Say for example you had the word "foobar" and wanted to insert a 't' before 'b'. You will somehow need to calculate the desired cursor position in pixels and then display it accordingly. How to calculate? Well, not my cup of tea...
In such situations, the only feasible approach seems to be to turn design mode on for the control. So you use an iframe and set its design mode property to true. You no longer need a hidden textarea control to capture the keystrokes. You can capture the key events, process the user input and then display it inside the design mode iframe. Now the big question of reading and setting cursor position! Gecko based browsers support the W3C specified API. For example in firefox, you have the properties anchorNode, anchorOffset, focusNode and focusOffset which help you to determine and set the cursor position wherever you want to. You get the DOM reference and also the offset required. So pretty fine! (The details can be found in the Midas specification). But what about IE? IE's API doesn not even come close to the W3C API in this case (as in most cases). I was playing around with the design mode control last month and was stuck here. I did a lot of search on the internet but could not find any satisfactory way to get and set the cursor position in IE. One inefficient way found over the internet, to read the cursor position was to move the cursor to the left by some arbitrarily huge number, using the method moveStart on a selection object. This method will then return the number of characters moved which will give you the cursor position. You will use something like
document.selection.createRange().duplicate().moveStart(1000000, "characters");
This method will work fine for small text. But as you keep typing, the editor will become slower and slower. Setting the cursor position is also another problem. You will need to calculate the offset in characters from the start of the editable iframe and then use moveStart followed by collapse. This is doubly slow. The complexity of this technique would be O(n) where n is the offset number of characters from the start position. This is clearly unacceptable for large text input. I continued to hunt for a better approach. Eventually, my friend Shivram suggested a unique hack. He found a post on the internet where some person was using the method pasteHTML of the range object in IE to insert some image at the cursor position. You can paste HTML at the current cursor position using this method. The hack was to create a dummy DOM node and insert at the current cursor position using pasteHTML. Then using the DOM attributes, you can get the required DOM reference. The dummy node can then be removed from the DOM tree. So the following hack solved the problem with a complexity of O(1)
getNodeAtCursor = function() {
var range = document.selection.createRange().duplicate();
range.pasteHTML('<span id="foo"></span>');
var temp = getElementById("foo");
var n = temp.parentNode;
removeElement(temp);
return n;
};
The offset can be obtained by finding the length of the previousSibling of the dummy span.
Thus you get the DOM reference as well as the offset number of characters relative to the node without having to iterate over all the nodes before the one under consideration. Not a clean way, but certainly quite efficient for large text. Let us hope future versions of IE conform to the W3C standard as in Firefox and eliminate the need to use such hacks.
Well, I hope this post helps somebody who might be stuck with getting and setting cursor position in design mode controls especially in IE 7 and below. Cheers!!!
In such situations, the only feasible approach seems to be to turn design mode on for the control. So you use an iframe and set its design mode property to true. You no longer need a hidden textarea control to capture the keystrokes. You can capture the key events, process the user input and then display it inside the design mode iframe. Now the big question of reading and setting cursor position! Gecko based browsers support the W3C specified API. For example in firefox, you have the properties anchorNode, anchorOffset, focusNode and focusOffset which help you to determine and set the cursor position wherever you want to. You get the DOM reference and also the offset required. So pretty fine! (The details can be found in the Midas specification). But what about IE? IE's API doesn not even come close to the W3C API in this case (as in most cases). I was playing around with the design mode control last month and was stuck here. I did a lot of search on the internet but could not find any satisfactory way to get and set the cursor position in IE. One inefficient way found over the internet, to read the cursor position was to move the cursor to the left by some arbitrarily huge number, using the method moveStart on a selection object. This method will then return the number of characters moved which will give you the cursor position. You will use something like
document.selection.createRange().duplicate().moveStart(1000000, "characters");
This method will work fine for small text. But as you keep typing, the editor will become slower and slower. Setting the cursor position is also another problem. You will need to calculate the offset in characters from the start of the editable iframe and then use moveStart followed by collapse. This is doubly slow. The complexity of this technique would be O(n) where n is the offset number of characters from the start position. This is clearly unacceptable for large text input. I continued to hunt for a better approach. Eventually, my friend Shivram suggested a unique hack. He found a post on the internet where some person was using the method pasteHTML of the range object in IE to insert some image at the cursor position. You can paste HTML at the current cursor position using this method. The hack was to create a dummy DOM node and insert at the current cursor position using pasteHTML. Then using the DOM attributes, you can get the required DOM reference. The dummy node can then be removed from the DOM tree. So the following hack solved the problem with a complexity of O(1)
getNodeAtCursor = function() {
var range = document.selection.createRange().duplicate();
range.pasteHTML('<span id="foo"></span>');
var temp = getElementById("foo");
var n = temp.parentNode;
removeElement(temp);
return n;
};
The offset can be obtained by finding the length of the previousSibling of the dummy span.
Thus you get the DOM reference as well as the offset number of characters relative to the node without having to iterate over all the nodes before the one under consideration. Not a clean way, but certainly quite efficient for large text. Let us hope future versions of IE conform to the W3C standard as in Firefox and eliminate the need to use such hacks.
Well, I hope this post helps somebody who might be stuck with getting and setting cursor position in design mode controls especially in IE 7 and below. Cheers!!!
Sunday, July 1, 2007
Height of absent-mindedness
What am I doing still in Bangalore? I was supposed to be in Goa by yesterday evening. Did I say I missed my flight? Well, friends close to me will say nothing new! This one is a totally irresponsible and absent minded guy is what they'll say to themselves. And why not? I have done that before too.
Yesterday, I had a flight to Goa at 14:30 hrs. What do I do? I leave by 13:20 from my flat, about 25 kms from the Bangalore airport, get into an auto at about 13:25. On time I thought! But alas, the auto simply doesnt seem to cross 30 kmph on a lonely Bangalore road. Lonely, compared to the standards associated with Bangalore!!! Finally, I make it to the airport. As I didn't have any check-in baggage, I thought I would finally make it. I reach the boarding counter at 14:15 and is nicely greeted by the lady there! Then all of a sudden, she says, 'Sorry sir, all seats filled up!' Then what do I do? I meet the airlines supervisor, and take out my entire frustration on her :( 'I have a valid ticket. The receptionist at the boarding counter says all seats filled. Do you mean to say you have given my seat to somebody else? How on earth can you do that?' The lady just listens patiently and even though I was on the verge of shouting, manages to keep her cool and is very polite. 'I understand sir, that there might be some problems due to which you couldn't make it 30 minutes prior to the departure. But I'm extremely sorry, we cannot make any adjustments now and let you board the plane. But as a special case, we can reschedule your flight for tomorrow. However, you will have to pay the difference in the fare as an additional charge'. Frustration had already gotten the better of me by this time. After a lot of argument with another supervisor, I just ask for a refund. 'You will get only the taxes as a refund', is his final say. Well, I wasn't feeling well that day. Was feeling a bit feverish in the morning and also a bit of nausea. So I go back to my flat after that and have a good nap. And its after the nap that I suddenly realized how wrong I was. There was absolutely no need for me to take my frustration on the airlines staff! I couldn't make it on time and it was totally my mistake. Well, I've another flight in about 2.5 hrs from now. I'll see if I get a chance to apologize to the airlines staff today...
As I've said earlier, my misery with flights is not a new thing at all! About two months back, I went to the airport 23 hrs after my flight's scheduled departure. But as we all know, today can't be yesterday, and I decided to take a bus to Bangalore. That time, I had nothing to say, even for the sake of an argument ;) In another separate incident, I had a bus ticket from Chennai to Bangalore. In this one, I just forget my ticket at the place of residence and reach the Chennai Mofussil bus stand. The gentleman at the KSRTC booking counter was not as gentle as I expected and simply refused to help me. I had to buy another ticket to be able to travel. After all, absent mindedness, has a price to pay!!! Nevertheless, I still believe the gentleman could have acccepted a valid photo Id proof and let me travel, without additional charge as the booking counter was computerized and had the details of the passengers stored...
Ok, its time for me to leave. I don't want to miss today's flight. Hope to catch you soon...
Yesterday, I had a flight to Goa at 14:30 hrs. What do I do? I leave by 13:20 from my flat, about 25 kms from the Bangalore airport, get into an auto at about 13:25. On time I thought! But alas, the auto simply doesnt seem to cross 30 kmph on a lonely Bangalore road. Lonely, compared to the standards associated with Bangalore!!! Finally, I make it to the airport. As I didn't have any check-in baggage, I thought I would finally make it. I reach the boarding counter at 14:15 and is nicely greeted by the lady there! Then all of a sudden, she says, 'Sorry sir, all seats filled up!' Then what do I do? I meet the airlines supervisor, and take out my entire frustration on her :( 'I have a valid ticket. The receptionist at the boarding counter says all seats filled. Do you mean to say you have given my seat to somebody else? How on earth can you do that?' The lady just listens patiently and even though I was on the verge of shouting, manages to keep her cool and is very polite. 'I understand sir, that there might be some problems due to which you couldn't make it 30 minutes prior to the departure. But I'm extremely sorry, we cannot make any adjustments now and let you board the plane. But as a special case, we can reschedule your flight for tomorrow. However, you will have to pay the difference in the fare as an additional charge'. Frustration had already gotten the better of me by this time. After a lot of argument with another supervisor, I just ask for a refund. 'You will get only the taxes as a refund', is his final say. Well, I wasn't feeling well that day. Was feeling a bit feverish in the morning and also a bit of nausea. So I go back to my flat after that and have a good nap. And its after the nap that I suddenly realized how wrong I was. There was absolutely no need for me to take my frustration on the airlines staff! I couldn't make it on time and it was totally my mistake. Well, I've another flight in about 2.5 hrs from now. I'll see if I get a chance to apologize to the airlines staff today...
As I've said earlier, my misery with flights is not a new thing at all! About two months back, I went to the airport 23 hrs after my flight's scheduled departure. But as we all know, today can't be yesterday, and I decided to take a bus to Bangalore. That time, I had nothing to say, even for the sake of an argument ;) In another separate incident, I had a bus ticket from Chennai to Bangalore. In this one, I just forget my ticket at the place of residence and reach the Chennai Mofussil bus stand. The gentleman at the KSRTC booking counter was not as gentle as I expected and simply refused to help me. I had to buy another ticket to be able to travel. After all, absent mindedness, has a price to pay!!! Nevertheless, I still believe the gentleman could have acccepted a valid photo Id proof and let me travel, without additional charge as the booking counter was computerized and had the details of the passengers stored...
Ok, its time for me to leave. I don't want to miss today's flight. Hope to catch you soon...
Subscribe to:
Posts (Atom)