﻿//The JavaScript 'prototype' library must be loaded before this file
function isPhone(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode == 45)
        return true;
        
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}
//Any input field with a class of decimal is limited to numeric, '-', and '.' values
function bindDecimals()
{
    $$('input.decimal').each(function(item) {
        Event.observe(item, 'keypress', function(event) {
            var charCode = (event.which) ? event.which : event.keyCode;
            
            //This ensures that only one decimal point and negative is typed into a textbox. (We are talking about money, not IP Addresses)
            if((item.value.indexOf('.') != -1 && charCode == 46) || (item.value.indexOf('-') != -1 && charCode == 45))
                Event.stop(event);
            
            if (charCode <= 31 || (charCode >= 48 && charCode <= 57) || (event.which==0 && charCode > 34 && charCode < 41) ||
                charCode == 45 || charCode == 46
            )
                return true;

            Event.stop(event);
        });
    });
}
//Any input field with a class of numeric is limited to numerical values only (no '-' or '.')
//IE does not fire 'keypress' events with non-printing characters and always uses event.keyCode
//Firefox sets event.which to '0' with non-printing characters
function bindNumerics()
{
    $$('input.numeric').each(function(item) {
        Event.observe(item, 'keypress', function(event) {
            var charCode = (event.which) ? event.which : event.keyCode;

            // Keys less than 31 are specialty keys, 48-57 are the numeric keys, and 35-40 are navigation keys if event.keyCode is used,
            // 46 is delete on the keyCode
            if (charCode <= 31 || (charCode >= 48 && charCode <= 57) || (event.which==0 && charCode > 34 && charCode < 41) ||
                (event.which==0 && charCode==46)
            )
                return true;
            
            Event.stop(event);
        });
    });
}
//Any input field with a class of date will get the current date with a double click
function bindDates()
{
    $$('input.date').each(function(item) {
        Event.observe(item, 'dblclick', function(event) {
            var now = new Date();
            item.value = now.getMonth()+1+'/'+now.getDate()+'/'+now.getFullYear();
            item.blur();
        });
        Event.observe(item, 'blur', function(event) {
            if(item.value != '')
                if(isNaN(Date.parse(item.value)))
                    alert('Invalid Date!\nPlease use the MM/dd/YYYY format.');
                else {
                    var entered = new Date();
                    entered.setTime(Date.parse(item.value));
                    item.value = (entered.getFullYear()>1930) ? entered.getMonth()+1+'/'+entered.getDate()+'/'+(entered.getFullYear()):
                        entered.getMonth()+1+'/'+entered.getDate()+'/'+(entered.getFullYear()+100);
                }
        });
    });
} 
