// FormatText.js

wCLASS['FormatText'] = {tag:'INPUT'};
function _validateKeySSN()
{      
    var k = String.fromCharCode(window.event.keyCode);
    var v = window.event.srcElement.value;
    v = v.replace(/[^\d]/g, "");
    if(isNaN(parseInt(k)) || v.length > 8)
           return false;
    else return true;
}
function _validateKeyPHN()
{      
    var k = String.fromCharCode(window.event.keyCode);
    var v = window.event.srcElement.value;
    v = v.replace(/[^\d]/g, "");
    if(isNaN(parseInt(k)) || v.length > 9)
           return false;
    else return true;
}
function _onChangeSSN()
{
    var v = window.event.srcElement.value;   
    v = v.replace(/[^\d]/g, "");
    v = v.replace(/(\d{3})(\d+)/, "$1-$2");
    v = v.replace(/(\d{3}\-\d{2})(\d+)/, "$1-$2");   
    window.event.srcElement.value = v;
}
function _onChangePHN()
{
    var e = window.event.srcElement
    var v = e.value;   
    v = v.replace(/[^\d]/g, "");
    if(v.length > 7)
        v = v.replace(/(\d{3})(\d{3})(\d{2,})/, "($1) $2-$3");
    else
        v = v.replace(/(\d{3})(\d{1,4})/, "$1-$2");
    window.event.srcElement.value = v;
}
function FormatText(obj, format)
{
    this.base             = obj;
    this.format             = format;
    if(this.format == 'SSN')
    {
        this.base.onkeypress = _validateKeySSN; 
        this.base.onkeyup    = _onChangeSSN;
    }
    else
    {
        this.base.onkeypress = _validateKeyPHN;
        this.base.onkeyup    = _onChangePHN;
    }
}