﻿if (document.captureEvents)
	document.captureEvents(Event.ONKEYPRESS);
document.onkeypress = CatchHardReturn;

function validChar(type, obj, e)
{
	var key = getKey(e);

	if ("email" == type)
	{
	    /* enter || at-symbol || period || dash || underscore */
	    if (key == 13 || key == 64 || key == 46 || key == 45 || key == 95)
	        return true;
	    else if (key >= 97 && key <= 122 || key >= 65 && key <= 90) //a-Z
	        return true;
	}
	if ("date" == type)
	{
	    /* enter || forwardslash || dash */
	    if (key == 13 || key == 47 || key == 45)
		    return true;
    }
    else if ("phone" == type)
    {
	    if (key == 13 || key == 45)
		    return true;
    }
    else if ("numeric" == type)
    {
        if (key == 13)
		    return true;
    }
    else if ("decimal" == type)
    {
        if (key == 13 || key == 46)
            return true;
    }
    else if ("alpha-numeric" == type || "postal-code" == type)
    {
        var v = (key == 8  /* backspace */ /*|| key == 9   tab */  || key == 13 /* enter */ ||
			     key == 35 /* end */       || key == 36 /* home */ || key == 37 /* left */  ||
			     key == 39 /* right */     || key == 46 /* del */ );
        if ("postal-code" == type)
            v = (v || key == 32 || key == 45); // dash & space
        if (v || ( (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122) ))
            return true;
    }    
 
	var allow = false;
    
	// if a number was not pressed
	if(key < 48 || key > 57)
	{
		// check for other keys that have special purposes
		if( key != 8  /* backspace */ && key != 9  /* tab */  && key != 13 /* enter */ &&
			key != 35 /* end */       && key != 36 /* home */ && key != 37 /* left */  &&
			key != 39 /* right */     && key != 46 /* del */
		   )
			allow = false;
		else
		{
			// for detecting special keys (listed above)
			// IE does not support 'charCode' and ignores them in keypress anyway
			if(typeof e.charCode != "undefined")
			{
				// special keys have 'keyCode' and 'which' the same (e.g. backspace)
				if(e.keyCode == e.which && e.which != 0)
					allow = true;
					
				// or keyCode != 0 and 'charCode'/'which' = 0
				else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
					allow = true;
			}
		}
	}
	else
		allow = true;

	return allow;
}
function parsePhone(obj)
{
 	var phoneREGEX_Replace = /[a-zA-Z_\- \.\(\)\;\:]/g
 	obj.value = obj.value.replace(phoneREGEX_Replace, '');
 	var newPhone = obj.value;
 	
 	if (newPhone.length == 10 || newPhone.length == 11) //go ahead and attempt to parse
 	{
		if (newPhone.length == 11) // in case they do a little 1 + xxx-xxx-xxxx
 			newPhone = newPhone.substr(1,10);
 			
 		var newPhoneMask = newPhone.substr(0,3) + '-' + newPhone.substr(3,3) + '-' + newPhone.substr(6,4);
 		obj.value = newPhoneMask;
 	}
}

function parseCreditCard(obj)
{
    if (obj.value.length > 12)
    {
        var cc = '', c = 1;
        obj.value = obj.value.replace(/[a-zA-Z_\- \.\(\)\;\:]/g,'');
        for (var i = 0; i < obj.value.length; i++)
        {
            cc += obj.value.substr(i,1);
            if (c == 4 && (i+1) < obj.value.length) { cc += '-'; c = 0; }        
            c += 1;
        }
        obj.value = cc;
    }
}

function formatNumber(num, precision, comma, leading, parens)
{
    var tmp = num;
    tmp *= Math.pow(10,precision);
    tmp  = Math.floor(tmp);
    tmp /= Math.pow(10,precision);
    var str = new String(tmp);

    if (!leading && num < 1 && num > -1 && num !=0)
        if (num > 0)
            str = str.substring(1,str.length);
        else
            str = "-" + str.substring(2,str.length);                        
    if (parens && num < 0)
        str = "(" + str.substring(1,str.length) + ")";
    if (precision && str.indexOf('.') == -1)
        str = str+'.00';
    else if (precision && str.indexOf('.') == str.length - 2)
        str = str + '0';
    if (comma && num > 999)
    {
        var arp = str.split('.');
        str = arp[0].substr(0,arp[0].length-3) + ',' + arp[0].substr(arp[0].length-3,3);
        str = str + (arp[1]?'.'+arp[1]:'');
    }
   return str;
}

function checkMaxLength(obj,length,e)
{
    var key = getKey(e);
    if (! validChar("alpha-numeric", obj, e))
        return true;
    return (obj.value.length < length);
}
function doTruncate(obj,length)
{
    if (obj.value.length > length)
        obj.value = obj.value.substring(0,length-1);
}

function doZeroDefault(obj)
{
    obj.value = obj.value.replace(' ','');
    if (obj.value.length == 0 || isNaN(obj.value))
        obj.value = '0';
}

var G_AllowHardReturn = true;
function setEnterKey(b) { G_AllowHardReturn = b; }
function setReadOnly(obj,b) { obj.readOnly = b; }
function setHiddenValue(obj,targ) { docObj(targ).value = (obj.nodeName.toUpperCase()=='SPAN' ? 'true' : obj.value ); }
function CatchHardReturn(e)
{
    var key = getKey(e);
    return !(!G_AllowHardReturn && key == 13);
}
function getKey(e)
{
	var key = 0;
	if (!e) var e = window.event;
	if (e.keyCode) key = e.keyCode;
	else if (e.which) key = e.which
	return key;
}