﻿// JScript File

 function CommaFormatted(amount)
{
    //alert('my amount: ' + amount);
    if(amount.length > 0)
    {
        //kill commas already existing
        amount = amount.replace(/,/g,'');
        //alert("amount after replace: " + amount);
    
        var delimiter = ","; // replace comma if desired
        var a = amount.split('.',2)
        //alert("a array size: " + a.length);
        var d = a[1];
        //alert("d: " + d);
        var i = parseInt(a[0]);
        if(isNaN(i)) { return ''; }
        var minus = '';
        if(i < 0) { minus = '-'; }
        i = Math.abs(i);
        var n = new String(i);
        //alert ('i: ' + i);
        //alert ('n: ' + n);
        
        var a = [];
        while(n.length > 3)
        {
            //alert('loop: n.length ' + n.length);
            
	        var nn = n.substr(n.length-3);
	        a.unshift(nn);
	        n = n.substr(0,n.length-3);
	        //alert('looop end n:' + n);  
	        
        }
        if(n.length > 0) { a.unshift(n); }
        n = a.join(delimiter);
        if(d != null)
        {
            if(d.length < 1) { amount = n; }
            else { amount = n + '.' + d; }
        }
        else
        {
            amount = n;
        }
        amount = minus + amount;
    }
    return amount;
}

function numbersonly(e)
{
    var unicode=e.charCode? e.charCode : e.keyCode
    if (unicode!=8)
    { 
        //if the key isn't the backspace key (which we should allow)
        if (unicode<48||unicode>57) //if not a number
            return false; //disable key press
        else
        {
            // alert("e.value = " + e.value);
            phoneformat(e.value);
        }
    }
}

function phoneformat(number)
{
    var phone = number;
    //alert("number: " + phone);
    if (phone.length > 0)
    {
        
        phone = phone.replace(/-/g,'');
        phone = phone.replace('(','');
        phone = phone.replace(')','');
        if (phone.length > 6)
        {
            var area = phone.substr(0,3);
            // alert("area: " + area);
            var first = phone.substr(3,3);
            // alert("first: " + first);
            var last = phone.substr(6,4);
            // alert("last: " + last);
            
            phone = area + "-" + first + "-" + last; 
            
            return phone;
        }
        else if (phone.length > 3 && phone.length < 7)
        {
            var area = phone.substr(0,3);
            // alert("area: " + area);
            var first = phone.substr(3,phone.length + 1);
            // alert("first: " + first);
            
            phone = area + "-" + first;
            
            return phone;
        }
    }
    return phone;
}

// Javascript Numeric EditMask
// Written By John McGlothlin - Patoooey@optonline.net
// April 7th, 2004
//
// simple RegEx patterns to make life easy.

var reOneOrMoreDigits = /[\d+]/;
var reNoDigits = /[^\d]/gi;

function doMask(textBox) {
	var keyCode = event.which ? event.which : event.keyCode;

	// enter, backspace, delete and tab keys are allowed thru
    if(keyCode == 13 || keyCode == 8 || keyCode == 9 || keyCode == 46)
		return true;

	// get character from keyCode....dealing with the "Numeric KeyPad" 
	// keyCodes so that it can be used
	var keyCharacter = cleanKeyCode(keyCode);

	// grab the textBox value and the mask
	var val = textBox.value;
	var mask = textBox.mask;

	// simple Regex to check if key is a digit
	if(reOneOrMoreDigits.test(keyCharacter) == false)
		return false;

	// get value minus any masking by removing all non-numerics
	val = val.replace(reNoDigits,'');			

	// add current keystroke
	val += keyCharacter;

	// mask it...val holds the existing TextBox.value + the current keystroke
	textBox.value = val.maskValue(mask);

	setCaretAtEnd(textBox);
	return false;
}


// puts starting chars in field
function onFocusMask(textBox) {
	var val = textBox.value;
	var mask = textBox.mask;

	if(val.length == 0 || val == null) {
		var i = mask.indexOf('#');
		textBox.value = mask.substring(0,i);
	}
	setCaretAtEnd(textBox);

	// set just in case.
	textBox.maxlength = mask.length;
}


// blank field if no digits entered
function onBlurMask(textBox) {
	var val = textBox.value;

	// if no digits....nada entered.....blank it.
	if(reOneOrMoreDigits.test(val) == false) {
		textBox.value = '';
	}
}

String.prototype.maskValue = function(mask) {
	var retVal = mask;
	var val = this;

	//loop thru mask and replace #'s with current value one at a time
	// better way of doing this ???
	for(var i=0;i<val.length;i++) {
		retVal = retVal.replace(/#/i, val.charAt(i));
	}

	// get rid of rest of #'s
	retVal = retVal.replace(/#/gi, "");
	return retVal;
}

// The Numeric KeyPad returns keyCodes that ain't all that workable.
// ie: KeyPad '1' returns keyCode 97 which String.fromCharCode converts to an 'a'.
// This cheesy way allows the Numeric KeyPad to be used
function cleanKeyCode(key)
{
	switch(key)
	{
		case 96: return "0"; break;
		case 97: return "1"; break;
		case 98: return "2"; break;
		case 99: return "3"; break;
		case 100: return "4"; break;
		case 101: return "5"; break;
		case 102: return "6"; break;
		case 103: return "7"; break;
		case 104: return "8"; break;
		case 105: return "9"; break;
		default: return String.fromCharCode(key); break;
	}
}