﻿function TextBoxNumericValidation(textBox)
{
    var value = parseFloat(textBox.value);
    
    if(isNaN(value))
        value = 0;

    textBox.value = value.toFixed(2);
}

function TextBoxMaxLengthCheck(textBox, maxLength)
{
    var value = textBox.value;
    
    if(value.length > maxLength) {
        value = value.substring(0, maxLength);
        textBox.value = value;
    }
}

var g_TextBoxMaxLengthIndicator = null;

function TextBoxShowMaxLengthIndicator(textBox)
{
    var indicator = document.createElement("div");
    indicator.className = "TextBoxMaxLengthIndicator";
    indicator.style.zIndex = 999;
    
    // get textbox position
    var x = 0, y = 0;
    var tmp = textBox;
    while(tmp != null) {
        x += tmp.offsetLeft;
        y += tmp.offsetTop;
        tmp = tmp.offsetParent;
    }
    
    indicator.style.left = (x + textBox.offsetWidth) + "px";
    indicator.style.top = y + "px";
    
    document.getElementById("divPage").appendChild(indicator);
    g_TextBoxMaxLengthIndicator = indicator;
}

function TextBoxHideMaxLengthIndicator()
{
    document.getElementById("divPage").removeChild(g_TextBoxMaxLengthIndicator);
    g_TextBoxMaxLengthIndicator = null;
}

function TextBoxUpdateMaxLengthIndicator(textBox, maxLength)
{
    var indicator = g_TextBoxMaxLengthIndicator;
    if(indicator == null) {
        TextBoxShowMaxLengthIndicator(textBox);
        indicator = g_TextBoxMaxLengthIndicator;
    }
    
    var text = "Maximum characters: <strong>" + maxLength + "</strong>";
    text += "<br />Used / Remaining: <strong>" + textBox.value.length;
    text += " / " + (maxLength - textBox.value.length) + "</strong>";
    
    indicator.innerHTML = text;
}