///////////////////////////////////////////////////////////////////////////////
//                                                                           //
//                       Copyright (c) 2003-2006                             //
//                            Marc Peterson                                  //
//                     marc.s.peterson at gmail.com                          //
//                                                                           //
///////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////
// MASTER FILE																 //
// This contains all the available validation functions.  Only the functions //
// needed should be copied to the validate.js file							 //
///////////////////////////////////////////////////////////////////////////////

// Last updated:
//   2007-04-09 - created by moving functions from forms.js

// checkAlphaNum(str)		validates alpha numeric (a-z, 0-9)
// checkDecimal(string)		validates decimal numbers (12.345)
// checkDomain(str)			validates domain (www.domain.com)
// checkEmail(str)			validates email address
// checkHex(str)			validates hex number
// checkHexRange(h,min,max)	ensures hex h is between hex min and max
// hex2dec(str)				converts hex number to decimal number
// checkIp(str)				validates IP address
// checkIpAndDomain(str)	validates IP first, domain if not an IP
// checkIpWithMask(str)		validates IP address with a mask (1.2.3.4/32)
// checkLink(str)			validates link (http://www.domain.com/path)
// checkMac(str)			validates MAC address
// checkNumber(str)			validates string is an pos/neg integer number (0-9 and "-")
// checkNumRange(x,min,max)	ensures x is between min and max
// checkPortList(str)		validates comma-deliminated list of ports (1, 3, 23, 4096)
// checkTheseChars(str,str)	validates for user-supplied characters
// checkTime(str)			validates time (3:45)

function checkAlphaNum(string)
{
	var good="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for (var i = 0; i < string.length; i++) {
		if (good.indexOf(string.charAt(i)) == -1) return false;
	}
	return true;
} 


// Check for a valid email address
function checkEmail(string)
{
	string				= trim(string);
	var pos_at			= string.indexOf("@");
	var pos_last_at		= string.lastIndexOf("@");
	var pos_last_dot	= string.lastIndexOf(".");
	var pos_space		= string.indexOf(" ");
	var str_len			= string.length;

	// Make sure all characters are valid
	var good=".@-+_0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for (var i = 0; i < string.length; i++)
	{
		if (good.indexOf(string.charAt(i)) == -1) return false;
	}

	// At least one @ must be present and not at start of string
	if ( pos_at < 1 ) return false;

	// Make sure there is only one @
	if ( pos_at != pos_last_at ) return false;

	// At least one "." afer the @ is required
	if ( pos_last_dot < pos_at ) return false;

	// At least two characters must occur after the last dot
	if ( str_len - pos_last_dot <= 2 ) return false;

	return true;
}



// Check for valid characters in a number
function checkNumber(string)
{
	var good = "-0123456789";
	for (var i = 0; i < string.length; i++) {
		if (good.indexOf(string.charAt(i)) == -1) return false;
	}
	return true;
}


// Ensures the string is between the min and max values
function checkNumRange(string, min_val, max_val)
{
	var good = "-0123456789";

	if (string.length == 0) return false;	// If blank, return false

	// Check all characters
	for (var i = 0; i < string.length; i++)
	{
		if (good.indexOf(string.charAt(i)) == -1) return false;
	}

	// If min/max values are incorrect, swap 'em
	if ( min_val > max_val )
	{
		var temp = min_val;
		min_val = max_val;
		max_val = temp;
	}

	string = Number(string);
	if (string < min_val) return false;
	else if (string > max_val) return false;
	else return true;
}


// Ensures all the characters in string are in good.
function checkTheseChars(string, good)
{
	for (var i = 0; i < string.length; i++) {
		if (good.indexOf(string.charAt(i)) == -1) return false;
	}
	return true;
}

