/* 
-------------------------------------------------------------------------------
	Functions for validating inputs
	
	Author: Ivo Kotev
----------------------------------------------------------------------------- */


/*
	Validates that the entry is formatted as a number
*/
function isAnyNumber(elem) 
{
	var str = elem;
	var re = /^[-]?\d*\.?\d*$/;
	str = str.toString( );
	if (!str.match(re))
		return false;

	return true;
}

	
/*
	Validates that the entry is formatted as an email address
*/
function isEMailAddr(elem) 
{
	var str = elem;
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if (!str.match(re)) 
		return false;
	else 
		return true;
}


/*
	Example:Allow only numbers to be entered in some fields 
	return goodchars(event, '01234567890 ')
*/
function goodchars(e, chars)
{
	var key, keychar;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	chars = chars.toLowerCase();

	if (chars.indexOf(keychar) != -1)
		return true;

	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;

	return false;
}

/*
	Example:Forbits numbers to be entered in some fields 
	return badchars(event, '0123456789')
*/
function badchars(e, chars)
{
	var key, keychar;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	chars = chars.toLowerCase();

	if (chars.indexOf(keychar) != -1)
		return false;

	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;

	return true;
}

/*
	Validates date
*/
function checkdate(day, month, year)
{
	var err = 0

	if (month < 1 || month > 12)
		err = 1;
	if (day < 1 || day > 31)
		err = 1;
	if (year < 1999 || year > 2015)
		err = 1;

	if (month == 4 || month == 6 || month == 9 || month == 11)
	{
		if (day == 31)
			err = 1;
	}

	if (month == 2)
	{
		var g = parseInt(year / 4)
		if (isNaN(g))
			err = 1;
		if (day > 29)
			err = 1;
		if (day == 29 && ((year / 4)!= parseInt(year / 4)))
			err = 1;
	}

	if (err == 1)
		return false;
	else
		return true;
}
