I have created a textbox which keeps dates. The textbox uses javascript so that when a user presses up/down the date will increase/decrease itself depending on where the cursor is. For instance, the cursor is on top of the year, and user presses down, now the new date displayed will be one year less. And so on. Unfortunately, my javascript has some problems which I'd like to debug but I'm not too good at javascript to even figure out what causes this behavior.
My main gripes are these:

1) The script will break when setting a maxlength on a textbox (ie maxlength=10).
2) whenever I type really fast something like 123 inside the textbox, the textbox will display something like 213.
3) If I select everything inside the textbox and press backspace nothing will happen and selection will be gone.

This is what I have for the onclick, onselect, onkeyup events of the textbox:

function setCaretPos(elem)
{
if (elem.isTextEdit)
{
elem.caretPos = document.selection.createRange();
}
}

this is for the onkeydown:
function changeDate(elem)
{
var val = elem.value;
var textboxDate = true;
var bigMonth = false;
var bigDay = false;
var pos = getCaretPos(elem);
var separator;
Today = new Date();

if (val.length == 10)
{
if (!isNaN(val.substring(0, 4)))
{
Today.setYear(val.substring(0, 4));
}
else
{
textboxDate = false;
}

if ((val.substring(4,5) != "-") && (val.substring(4,5) != "/"))
{
textboxDate = false;
}
else
{
separator = val.substring(4,5);
}

if (!isNaN(val.substring(5, 7)))
{
if ((val.substring(5, 7) > 13) || (val.substring(5, 7) < 0))
{
bigMonth = true;
}
else
{
Today.setMonth(val.substring(5, 7)-1);
}
}
else
{
textboxDate = false;
}

if ((val.substring(7,8) != "-") && (val.substring(7,8) != "/"))
{
textboxDate = false;
}
else
{
if (val.substring(7,8) == "/")
{
separator = "/";
}
}

Today.setMonth(Today.getMonth() + 1);
Today.setDate(-1);

if (!isNaN(val.substring(8, 10)))
{
if ((val.substring(8, 10) > (Today.getDate()+2)) || (val.substring(8, 10) < 0))
{
bigDay = true;
}
else
{
Today.setDate(val.substring(8, 10));
}
}
else
{
textboxDate = false;
}
}
else
{
textboxDate = false;
}

if (!bigDay && textboxDate && !bigMonth)
{
if ((pos >= 0) && (pos <= 4))
{
if (event.keyCode == 38)
{
IncreaseYear(Today);
SetCursor(elem, separator, pos);
}
else if (event.keyCode == 40)
{
DecreaseYear(Today);
SetCursor(elem, separator, pos);
}
}
else if ((pos >= 5) && (pos <= 7))
{
if (event.keyCode == 38)
{
IncreaseMonth(Today);
SetCursor(elem, separator, pos);
}
else if (event.keyCode == 40)
{
DecreaseMonth(Today);
SetCursor(elem, separator, pos);
}
}
else
{
if (event.keyCode == 38)
{
IncreaseDay(Today);
SetCursor(elem, separator, pos);
}
else if (event.keyCode == 40)
{
DecreaseDay(Today);
SetCursor(elem, separator, pos);
}
}
}
}

//these functions are called by the changeDate() function
function SetCursor(elem, separator, pos)
{
elem.value = FormatYear(Today.getFullYear())+separator+FormatNumber((Today.getMonth()+1))+separator+FormatNumber( Today.getDate());
elem.caretPos.move("character", pos);
elem.caretPos.select();
}

function getCaretPos(elem)
{
if (elem.isTextEdit && elem.caretPos)
{
var bookmark = "~";
var orig = elem.value;
var caretPos = elem.caretPos;
caretPos.text = bookmark;
var i = elem.value.search( bookmark );
elem.value = orig;
elem.caretPos.move("character", i);
elem.caretPos.select();
return i;
}
}

function FormatYear(number)
{
if (number < 1000)
return "0" + number;
else if (number >= 10000)
return "1999";
else
return number;
}

function FormatNumber(number)
{
if (number < 10)
return "0" + number;
else
return number;
}

function IncreaseMonth(currentDate)
{
currentDate.setMonth(currentDate.getMonth() + 1 );
}

function DecreaseMonth(currentDate)
{
currentDate.setMonth(currentDate.getMonth() - 1 );
}

function IncreaseDay(currentDate)
{
currentDate.setDate(currentDate.getDate() + 1 );
}

function DecreaseDay(currentDate)
{
currentDate.setDate(currentDate.getDate() - 1 );
}

function IncreaseYear(currentDate)
{
currentDate.setYear(currentDate.getFullYear() + 1 );
}

function DecreaseYear(currentDate)
{
currentDate.setYear(currentDate.getFullYear() - 1 );
}

That's it... I know it's a lot, but whatever advice you could give me would be very welcomed.
Thanks again,
V