// Copyright 2004-2007 Castle Project - http://www.castleproject.org/
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

function monorail_formhelper_numberonly(e, exceptions, forbidalso)
{
	// 44 = . 46 = ,
	exceptions = exceptions.concat([8,9,13,38,39,40,44]);
	var code = e.charCode;
	if (!code) code = e.keyCode;
	if ((e.ctrlKey && code == 118) || (e.ctrlKey && code == 122))
	{
		return false;
	}
	if (e.ctrlKey || e.altKey) return true;
	for(var i=0; i < forbidalso.length; i++) if (forbidalso[i] == code) return false;
	for(var i=0; i < exceptions.length; i++) if (exceptions[i] == code) return true;
	if (code <= 47 || code > 57) return false;
	return true; 
}

/* 
 The scripts on this page was produced by mordechai Sandhaus - 52action.com,
 and is copyrighted . If you like this script, we encourage you to use it,
 provided that  include this note, and link to 52action.com. 
 */

function monorail_formhelper_getkeycode(e)
{
	if( typeof( e.keyCode ) == 'number'  ) 
	{
		return e.keyCode;
	} 
	else if( typeof( e.which ) == 'number' ) 
	{
		return e.which;
	} 
	else if( typeof( e.charCode ) == 'number'  ) 
	{
		return e.charCode;
	} 
	else 
	{
		return null;
	}
}

function monorail_formhelper_getevent(e)
{
	if (!e)
	{
		if (window.event)
		{
			return window.event;
		} 
		else 
		{
			return null;
		}
	}
	else
	{
		return e;
	}
}
 
function monorail_formhelper_mask(e, elem,loc,delim)
{
	e = monorail_formhelper_getevent(e);
	var keycode = monorail_formhelper_getkeycode(e);

	var locs = loc.split(',');
	var str = elem.value;
	
	for (var i = 0; i <= locs.length; i++)
	{
		for (var k = 0; k <= str.length; k++)
		{
			if (k == locs[i])
			{
				if (str.substring(k, k+1) != delim)
				{
					if (keycode != 8)
					{
						str = str.substring(0,k) + delim + str.substring(k,str.length);
					}
				}
			}
		}
	}
	elem.value = str
} 
