//+----------------------------------------------------------------------------
//
//  Function:       MaskPhone
//
//  Description:    Takes the innerText or value of the tag (depending on the
//                  type of tag), and formats it as a 7 or 10 digit phone number.
//
//  Arguments:      sValue - innerText or value of the tag
//
//  Returns:        "" (empty string) if sValue is an empty string
//                  sNewValue - parsed and formatted phone number
//
//-----------------------------------------------------------------------------

function MaskPhone(sValue)
{
    var sNewValue = sValue;
    var iLength = 7;
    
    //  Parse out applicable characters by calling ParseChar()
    var zChar = new Array(' ', '(', ')', '-', '.');
    sNewValue = ParseChar(sNewValue, zChar);
 
    //  Determine if this is a 7 or 10 digit phone number
    if (sNewValue.length == 7);
    else if (sNewValue.length == 10) iLength = 10;
    else if (sNewValue.length == 0) return "";
    else if (sNewValue.length < 7)
    {
        while (sNewValue.length < 7) sNewValue += "0";
        alert("Not a valid phone number");
    }
    else if (sNewValue.length < 10)
    {
        sNewValue = sNewValue.substring(0,7);
        alert("Not a valid phone number");
    }
    else if (sNewValue.length > 10)
    {
        iLength = 10;
        
        if (sNewValue.charAt(0) == "1" && sNewValue.length == 11)
        {
            sNewValue = sNewValue.substring(1,11);
        }
        
        else
        {
        	sNewValue = sNewValue.substring(0,10);
        	alert("Not a valid phone number");
        }
    }
    
    //  Call FormatPhone() to apply formatting
    sNewValue = FormatPhone(sNewValue,iLength);
    
    return sNewValue;
}


//+----------------------------------------------------------------------------
//
//  Function:       FormatPhone
//
//  Description:    Adds the proper formatting for a phone number (either 7 or
//                  10 digits) to a string of numbers passed in.
//
//  Arguments:      sPhone - a 7 or 10 digit string of numbers
//                  iLength - the desired phone number length
//
//  Returns:        sNewPhone - the formatted phone number
//
//-----------------------------------------------------------------------------

function FormatPhone(sPhone, iLength)
{
    var sNewPhone = "";

    if (iLength == 7)
    {
        sNewPhone = sPhone.substring(0,3) + " " + sPhone.substring(3,7);
    }
    if (iLength == 10)
    {
        sNewPhone = "" + sPhone.substring(0,3) + " " + sPhone.substring(3,6)
            + " " + sPhone.substring(6,10);
    }

    return sNewPhone;
}


//+----------------------------------------------------------------------------
//
//  Function:       ParseChar
//
//  Description:    This function takes a string and parses out certain
//                  characters.  The characters to be parsed out are passed in
//                  as a string or as a array.
//
//  Arguments:      sStr - The string intended to be parsed
//                  sChar - A string or array of characters to be parsed out
//                      of sStr
//
//  Returns:        sNewStr - The rebuilt (parsed) string
//
//-----------------------------------------------------------------------------

function ParseChar(sStr, sChar)
{
    //  If sChar is a string, create an array to hold it
    if (sChar.length == null) 
    {
        zChar = new Array(sChar);
    }
    else zChar = sChar;
    
    //  Iterate through the array, removing each character from the string
    for (i=0; i<zChar.length; i++)
    {
        sNewStr = "";
    
        var iStart = 0;
        var iEnd = sStr.indexOf(sChar[i]);
    
        while (iEnd != -1)
        {
            sNewStr += sStr.substring(iStart, iEnd);
            iStart = iEnd + 1;
            iEnd = sStr.indexOf(sChar[i], iStart);
        }
        sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);
        
        sStr = sNewStr;
    }
    
    return sNewStr;
}


