// Standard Netmedia JavaScript Functions

function NM_isBlank(s)
// returns true if any non-whitespace characters are present in the passed string
	{
	var re = /\S+/gi;
	var nonWS = s.match(re);

	if(nonWS == null)
		{
		return true;
		}
	else
		{
		nonWS = nonWS.toString().toLowerCase();
		}
	if(nonWS != '' && nonWS != '<p>&nbsp;</p>' && nonWS != '&nbsp;' && nonWS != '<p></p>')
		{
		return false;
		}
	return true;
	}


function NM_checkTest(f)
// checks that at least one checkbox or radio button has been checked
// returns true if at least one has been checked
{
	if (!f.length && f.checked) return true;

	for (var i = 0; i < f.length; i++) {
		if (f[i].checked) return true;
	}
	return false;
}


function NM_checkAllTest(f)
// checks that at all of a set of checkboxes or radio buttons has been checked
// returns true if all have been checked
{
	if (!f.length && !f.checked) return false;

	for (var i = 0; i < f.length; i++) {
		if (!f[i].checked) return false;
	}
	return true;
}


function NM_getCheckBoxCount(f)
// counts how many checkboxes have been checked
// returns count
{
	var count = 0;
	if (!f.length && f.checked) return 1;

	for (var i = 0; i < f.length; i++) {
		if (f[i].checked) count = count +1;
	}
	return count
}


function NM_getCheckedIndex(f)
// checks that at least one checkbox or radio button has been checked
// returns the index value
{
	if(f)
	{
		if (!f.length && f.checked) return 0;
	
		for (var i = 0; i < f.length; i++) {
			if (f[i].checked) return i;
		}
	}
	return -1
}


function NM_checkHTML(string)
// this checks a string for HTML tags (i.e. any instance of "<" and ">")
// returns true if no instances of "<" and ">" are present in the string
	{
	if(string.indexOf('<') != -1 || string.indexOf('>') != -1)
		{
		return false;
		}
	return true;
	}

function NM_containsDokuFormatting(string) {
/** this checks a string for the following Doku wiki-style formatting:
  * **bold**
  * //italic//
  * __underline__
  * line\\
  * break
  */
	return string.search(/(\/\/|\\\\|\*\*|__)/)!=-1;
}

function NM_checkInvalidChars(string)
// this checks a string for invlaid chars , . / ! " £ $ % ^ & * ( ) + = ? <> | \ ` ¬ # 
// returns true if no instances of invalid chars are present in the string
	{
	if(string.indexOf('`') != -1 || string.indexOf('¬') != -1 || string.indexOf('!') != -1 || string.indexOf('"') != -1 || string.indexOf('£') != -1  || string.indexOf('$') != -1 || string.indexOf('%') != -1  || string.indexOf('^') != -1 || string.indexOf('&') != -1 || string.indexOf('&') != -1 || string.indexOf('*') != -1 || string.indexOf('(') != -1 || string.indexOf(')') != -1 || string.indexOf('+') != -1 || string.indexOf('=') != -1 || string.indexOf('#') != -1 || string.indexOf('|') != -1 || string.indexOf('|') != -1 || string.indexOf('<') != -1 || string.indexOf(',') != -1 || string.indexOf('>') != -1 || string.indexOf('.') != -1 || string.indexOf('?') != -1 || string.indexOf('/') != -1 || string.indexOf(':') != -1 || string.indexOf(';') != -1 || string.indexOf('@') != -1 || string.indexOf('\'') != -1 || string.indexOf('-') != -1 )
		{
		return false;
		}
	return true;
	}


function NM_checkQuotes(string)
//	this checks a string for double quotes
//	returns true if no quotes are present in the string
	{
	if(string.indexOf('\"') != -1)
		{
		return false;
		}
	return true;
	}


function NM_checkSingleQuotes(string)
//	this checks a string for single quotes
//	returns true if no single quotes are present in the string
	{
	if(string.indexOf('\'') != -1)
		{
		return false;
		}
	return true;
	}


function NM_checkSpaces(string)
//	this checks a string for spaces within a string
//	returns true if no quotes are present in the string
	{
	if(string.indexOf(' ') != -1 )
		{
		return false;
		}
	return true;
	}


function NM_checkEmail(email)
	{
//	this checks if the passed e-mail address is valid
//	returning true for a valid e-mail and false for an invalid one...
	var test = email.match(/(\w+[\.\'\-])*\w+@([a-z0-9\-]+\.)+[a-z]{2,}/gi);

	//	the try block is here to catch an attempt to convert test to a string if it is NULL
	try
		{
		//	without the "length" bit, this function would pass a string that contains a valid e-mail,
		//	it ensures the ENTIRE string IS a valid e-mail
		//	i.e. it prevents accepting strings such as "my e-mail is myportal@netmediaeducation.com"
		if(test == null || test.toString().length != email.length)
			{
			return false;
			}
		}
	//	this should only catch if the match is NULL
	catch(e)
		{
		return false;
		}
	return true;
	}


function NM_checkURL(url, allowMailto)	{
	/*	checks a URL begins with a valid protocol and contains no spaces
		url is the url to be checked, mailto (if 'allowmailto') means mailto: is allowed */

	//	default 'allowMailto' to blank (i.e. false)
	if (!allowMailto)	{
		allowMailto = '';
	}

	//	default url to blank
	if (!url)	{
		url = '';
	}

	//	get the allowed protocols
	var protocols = NM_getDefaultProtocols();

	//	check for white space
	if (url.match(/\s+/g) != null) {
		return false;
	}

	//	check for mailto (if allowed)
	if (allowMailto.toLowerCase() == 'allowmailto')	{
		var mailparts = url.split(':');
		if (mailparts.length == 2 && mailparts[0].toLowerCase() == 'mailto' && NM_checkEmail(mailparts[1]))	{
			return true;	//	valid e-mail
		}
	}
	//	check for valid protocol
	var regex = new RegExp;
	var i = 0;
	while (i < protocols.length)	{
		regex  = new RegExp('^' + protocols[i] + ':\\/\\/.+', 'i');
		if (url.match(regex) != null)	{
			return true;
		}
		i++;
	}
	//	got this far? no protocol match
	return false;
}

//	returns the site's default link protocols
function NM_getDefaultProtocols ()	{
	return new Array ('http', 'https', 'mms', 'mmsu', 'mmst', 'rtsp', 'rtspu', 'rtspt', 'pnm');
	
}

//	this checks for a valid UK postcode
function NM_checkPostCode(postcode)
	{
	var isValid = postcode.match(/[a-z]{1,2}\d(\d|[a-z])?\s\d{1,2}[a-z]{2}/i);

	if(isValid == null)
		{
		return false;
		}
	else if(isValid[0].toUpperCase() != postcode.toUpperCase())
		{
		return false;
		}
	return true;
	}


//	check that the value of a numeric field is within a specified range
//	if value is in range then return true else return false
//	'allow_null' is optional - if it exists, a null value will return true
function NM_checkrange(object_value, min_value, max_value, allow_null)
    {
    if (!allow_null && object_value.length == 0)
        return false;

    if (!NM_checknumber(object_value))
	{
	return false;
	}
    else
	{
	return (NM_numberrange((eval(object_value)), min_value, max_value));
	}

	//All tests passed, so...
	return true;
	}


function NM_numberrange(object_value, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
		return false;
	}

	// check maximum
	if (max_value != null)
	{
	if (object_value > max_value)
		return false;
	}
	
	//All tests passed, so...
	return true;
	}

function NM_checknumber(object_value)
	{
	//Returns true if value is a number or is NULL
	//otherwise returns false	

	if (object_value.length == 0)
		return true;

	//Returns true if value is a number defined as
	//   having an optional leading + or -.
	//   having at most 1 decimal point.
	//   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }

function NM_checkPositiveInteger(object_value)
//	checks the passed number is a positive integer
	{
	try
		{
		//	less than 1
		if(object_value < 1)
			{
			return false;
			}
		//	not an integer
		if(parseInt(object_value) != parseFloat(object_value))
			{
			return false;
			}
		//	checks for non-numeric (stops hex numbers like "3f" slipping through)
		if(object_value.match(/[^0-9]/) != null)
			{
			return false;
			}
		}
	catch(e)
		{
		//	not a number
		return false;
		}
	//	all tests passed, so...
	return true;
	}

//	check that the passed value is a positive integer
function NM_isPositiveInt(num)
	{
	if(isNaN(num))
		{
		return false;
		}
	if(num != parseInt(num) || num < 0)
		{
		return false;
		}
	return true;
	}
