
/*************************************************************************
				General functions for javascript code
*************************************************************************/

// VALID FLOATING-POINT NUMBER
// return true if val contains only numbers (one decimal point allowed - full-stop, not comma; no thousands separators)
function isNumeric(val) {
	/* commented out - giving trouble with negative numbers:
	var result = val.match(/^(-(?=[1-9]))?(0|[1-9]\d*)(\.\d+)?$/g);
	return (result==val);
	*/
	var bNumeric = true;
	for (i = 0; i < val.length && bNumeric == true; i++) 
		if ("-0123456789.".indexOf(val.charAt(i)) == -1) 
			bNumeric = false;
	return bNumeric;
}

// VALID FLOATING-POINT NUMBER, GREATER THAN ZERO
// return true if val contains only numbers (one decimal point allowed - full-stop, not comma; no thousands separators)
// cannot be negative or zero
function isNumericGZ(val) {
	var result = val.match(/^(-(?=[1-9]))?(0|[1-9]\d*)(\.\d+)?$/g); 
	if (result==val)
		return (result > 0);
	return false;
}

// VALID FLOATING-POINT NUMBER, GREATER THAN OR EQUAL TO ZERO
function isNumericGEZ(val) {
	var result = val.match(/^(-(?=[1-9]))?(0|[1-9]\d*)(\.\d+)?$/g); 
	if (result==val)
		return (result >= 0);
	return false;
}

// VALID INTEGER
// return true if val contains only numbers (one decimal point allowed - full-stop, not comma; no thousands separators)
function isInt(val) {
	var result = val.match(/^(-(?=[1-9]))?(0|[1-9]\d*)$/g);
	return (result==val);
}

// VALID INTEGER, GREATER THAN ZERO
function isIntGZ(val) {
	var result = val.match(/^(-(?=[1-9]))?(0|[1-9]\d*)$/g);
	if (result==val)
		return (val > 0);
	return false;
}

// VALID INTEGER, GREATER THAN OR EQUAL TO ZERO
function isIntGEZ(val) {
	var result = val.match(/^(-(?=[1-9]))?(0|[1-9]\d*)$/g);
	if (result==val)
		return (val >= 0);
	return false;
}

// VALID ALPHANUMERIC STRING, MAY CONTAIN SPACES
// alphanum string that may contain underscore(s), space(s) and tab(s), but cannot be ONLY a space or tab, or start with one, or be empty
// e.g. VALID:		"abc" or "_abc" or "ab c" or "a b c" etc.
// e.g. INVALID:	" " or " abc" or "" etc.
function isAlphaNumericSpace(val)
{
	var result = val.match(/^([ \w])+(([ \t])+([ \w])+)*$/g);
	return (result==val);
}

// VALID NAME STRING, MAY CONTAIN SPACES, APOSTROPHES AND HYPHENS
// alphanum string that may contain spaces, apostrophes and hyphens, but cannot be ONLY spaces, or start with one, or be empty
// e.g. VALID:		"Mary Walsh" or "Ann-Marie O'Neill" etc.
// e.g. INVALID:	" " or " Mary" or "" etc.
function isName(val)
{
	var result = val.match(/^([ \w'\-])+(([ \t])+([ \w'\-])+)*$/g);
	return (result==val);
}

/*	VALID VENUE NAME STRING, MAY CONTAIN SPACES AND HYPHENS BUT NOT APOSTROPHES OR AMPERS ANDS
		Alphanum string that
		CAN contain
			- spaces
			- hyphens
		CANNOT contain
			- apostrophes
			- ampers and
			- cannot be ONLY spaces
			- start with a space
			- empty string
		e.g. VALID:		"My Venue Name" or "Ann-Maries Hotel" etc.
		e.g. INVALID:	" " or "" or "Ann-Marie's Hotel" or "R&R Motel" etc.
*/
function isVenueName(val)
{
	var result = val.match(/^([ \w\-])+(([ \t])+([ \w\-])+)*$/g);
	return (result==val);
}


// VALID EMAIL ADDRESS
// returns true if val is in the form:
// a[b]@c.d[e]		- a = string of letters, numbers, "_" and "-"			(required)
//					- b = string of letters, numbers, "_", "-" and "."		(optional)
//					- c = string of letters, numbers, "_" and "-"			(required)
//					- d = string of letters, numbers, "_" and "-"			(required)
//					- e = string of letters, numbers, "_", "-" and "."		(optional)
function isEmailAddress(val)
{
	var result = val.match(/^([a-zA-Z0-9_\-]+[\.a-zA-Z0-9_\-]*)(?=@)@([a-zA-Z0-9_\-]+(\.[a-zA-Z0-9_\-]+)+)(?!@)$/g);
	return (result == val);
}

// VALID TELEPHONE NUMBER
function isPhoneNumber(val)
{
	
	// +49 (30) 410 21 59 03  also accepts '-' as a separator.  Region code, i.e. '(30)' optional. Separators optional.
	// not allowed Region code without numbers following
	// 1 800 1234567 or 1 800 FLOWERS are good.  Letters can either be capital or lower and accepts '-' as a separator too
	
	var result = val.match(/^\+[0-9]+(([ -](?=\([0-9]+\)))?(\([0-9]+\))(?=[ -]?[0-9]+))?([ -]?[0-9]+)*$/g);
	if (result != val)
	{
		result = val.match(/^1[ -]?8[0-9]+([ -]?[a-zA-Z0-9]+)*$/g);
	}
	return (result==val);
}

// VALID WEB URL
function isURL(val)
{
	
	// Optional "http://" accepting any combination of alphanumeric words separated by dots and forward slash
	
	var result = val.match(/^(http:\/\/)?[^@\.\s\/]+([\.\/](?=[^@\.\s\/]+)[^@\.\s\/]+)+\/?$/gi);
	return (result==val);
}

// VALID PERCENTAGE
function isPercent(val)
{
	var result = val.match(/^(-(?=[1-9]))?(0|[1-9]\d*)(\.\d+)?$/g); 
	if (result==val)
	{
		if (result < 0)
		 	result = result * -1;
		return ( (result >= 0) && (result <= 100) );
	}
	return false;
}

// VALID PERCENTAGE, GREATER THAN OR EQUAL TO ZERO
function isPercentPositive(val)
{
	var result = val.match(/^(0|[1-9]\d*)(\.\d+)?$/g); 
	if (result==val)
		return ( (result >= 0) && (result <= 100) );
	return false;
}

// VALID FILENAME, NOT INCLUDING FOLDER(S)
function isFilename(val)
{
	var result = val.match(/^(\w)+(([ \t])+(\w)+)*\.(\w)+$/g);
	return (result==val);
}

