
// ---------------------------------------------------------
// Phone number formatter, created for Foodry
// For more info, visit:
// http://www.foodry.com/blog
// ---------------------------------------------------------
function formatPhone(elm, e) {
    var keychar;

    // If used in onkeypress, pass in the event and this will
    // grab the character and do the right thing. This allows
    // for a smoother user experience than if the chars are
    // being visibly deleted.
    if (e) {
        var keynum;
        if (window.event) {
            keynum = e.keyCode
        }
        else if (e.which) {
            keynum = e.which
        }

        keychar = String.fromCharCode(keynum)
    }

    // Allow a backspace to go through, so the user
    // can correct any typos.
    if (/[\b]/.exec(keychar)) {
        return true;
    } else {
        var p = elm.value + keychar;

        // Don't allow a leading 1 or 0. We also strip out all
        // non-numeric characters here to make the formatting
        // easier later on. This could be modified to allow
        // letters if you consider them valid.
        p = p.replace(/^[01]/,"");
        p = p.replace(/\D+/g, "");

        // You can easily change the formatting of the phone
        // number by editing the conditionals below.
        if (p.length > 0 && p.length < 3) {
            p = "("+p;
        }
        else if (p.length >= 3 && p.length < 7) {
            p = "("+p.substring(0,3)+") "+p.substring(3);
        }
        else if (p.length >= 7 && p.length < 10) {
            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6);
        }
        else if (p.length) {
            p = "("+p.substring(0,3)+") "+p.substring(3,6)+"-"+p.substring(6,10);
        }
        elm.value = p;

        return false;
    }
}


/* example
Phone Number: <input type="text"
               size="15" name="phone" id="phone"
               onkeypress="return formatPhone(this, event)"
               onkeyup="formatPhone(this)"
               onchange="formatPhone(this)">
*/

//--------------------- Format number ----------------------------->
function formatInteger(elm, e) {
    var keychar;

    // If used in onkeypress, pass in the event and this will
    // grab the character and do the right thing. This allows
    // for a smoother user experience than if the chars are
    // being visibly deleted.
    if (e) {
        var keynum;
        if (window.event) {
            keynum = e.keyCode
        }
        else if (e.which) {
            keynum = e.which
        }

        keychar = String.fromCharCode(keynum)
    }

    // Allow a backspace to go through, so the user
    // can correct any typos.
    if (/[\b]/.exec(keychar)) {
        return true;
    } else {
        var p = elm.value + keychar;

        // We  strip out all
        // non-numeric characters here to make the formatting
        // easier later on. This could be modified to allow
        // letters if you consider them valid.

        p = p.replace(/\D+/g, "");

        // You can easily change the formatting of the phone
        // number by editing the conditionals below.

        elm.value = p;

        return false;
    }
}


/* example
Integer: <input type="text"
               size="15" name="integer" id="integer"
               onkeypress="return formatInteger(this, event)"
               onkeyup="formatInteger(this)"
               onchange="formatInteger(this)">
*/

//--------------------- Format Zip Code ----------------------------->


function formatZip(elm, e) {
    var keychar;

    // If used in onkeypress, pass in the event and this will
    // grab the character and do the right thing. This allows
    // for a smoother user experience than if the chars are
    // being visibly deleted.
    if (e) {
        var keynum;
        if (window.event) {
            keynum = e.keyCode
        }
        else if (e.which) {
            keynum = e.which
        }

        keychar = String.fromCharCode(keynum)
    }

    // Allow a backspace to go through, so the user
    // can correct any typos.
    if (/[\b]/.exec(keychar)) {
        return true;
    } else {
        var p = elm.value + keychar;

        // We  strip out all
        // non-numeric characters here to make the formatting
        // easier later on. This could be modified to allow
        // letters if you consider them valid.

        p = p.replace(/\D+/g, "");

        // You can easily change the formatting of the zip
        // by editing the conditionals below.
        if (p.length > 5) {
            p = p.substring(0,5)+"-"+p.substring(5,9);
        }

        elm.value = p;

        return false;
    }
}


/* example
Zip + 4: <input type="text"
               size="10" name="zip" id="zip"
               onkeypress="return formatZip(this, event)"
               onkeyup="formatZip(this)"
               onchange="formatZip(this)">
*/
//--------------------- Obfuscate an email address  ----------------------------->
function obfuscateem(user) {
	var a = new Array('net','NorthernV','aHomes.');
	document.write("<a href='mailto:"+user+'@'+a[1]+a[2]+a[0]+"'>"+user+'@'+a[1]+a[2]+a[0]+"</a>");
	}

// example obfuscateem('info');
