/*
 * (c) 2000 Tim Slusher
 *
 * This code validates form fields using regular expressions.
 *
 * Notes:
 *	To my knowledge version 3.0+ of Internet Explorer and Netscape Navigator
 *	support all code found herein.  I have found that aols' browser and Opera
 *	do not like my code.  My opinion on aol is to fucking bad you piece of
 *	shit.  Opera should look into there javascript implementation to fix whatever
 *	problems this scrip is having, I might take the time to research the problem
 *	to find out why it don't work.
 *
 */

/*
 * ToDo:
 *	1) Phone numbers.
 *	2) Dates.
 *	3) Validate the credit card type.
 */

//
// These are hex constants defined for the validateIt function.
//
var valCreditCard	= 0x01
var valDate		= 0x02
var valEmailAddress	= 0x03
var valInteger		= 0x04
var valNumeric		= 0x05
var valPhone		= 0x06
var valString		= 0x07
var valSSN		= 0x08
var valZipCode		= 0x09

//
// these are the hex constants for the credit card types.
//
var ccVisa		= "0x01"
var ccMasterCard	= "0x02"
var ccAmericanExpress	= "0x03"

//----------------------------------------------------------------------------
//
function customMsg(str)
{
	alert(str);
	return(false);
}

function errorMsg(str)
{
	return(customMsg('ERROR: You did not enter a valid' + ' "' + str + '".'));
}

//----------------------------------------------------------------------------
//
// ** Get how many checkboxes are checked in the specified form group.
//
// function: getCheckGroupCount(group)
// arguments:
//	group - the checkbox group from a form.
//
// return:
//	The number of checkboxes that are checkecked
//
function getCheckGroupCount(group)
{
	var ii = 0;

	for (var jj = 0; jj < group.length; ii++)
	{
		if (group[jj].checked)
		{
			ii++;
		}
	}
	return(ii);
}

//----------------------------------------------------------------------------
//
// ** Gets the value of the radio button in the specified form group.
//
// function: getRadioGroupValue(group)
// arguments:
//	group - the radio button group form.
//
// return:
//	The value of the checked radio button.
//
function getRadioGroupValue(group)
{
	for (var ii = 0; ii < group.length; ii++)
	{
		if (group[ii].checked)
		{
			return(group[ii].value);
		}
	}
	return('');
}

function getListValue(list, index)
{
	list = (typeof(list) != 'object') ? eval(list) : list;
	index = (typeof(index) == 'undefined') ? list.selectedIndex : index;
	return(list.options[index].value);
}

//----------------------------------------------------------------------------
//
// ** Checks a string to see if it is a credit card.
//
// function: isCreditCard(str, <type>)
// arguments:
//	str  - The string to check to see if it is a credit card.
//	type - The type of credit card.
//
// valid CC:
//	1234 5678 9012 3456, 1234567890123456
//
// return:
//	true  - if it is a valid credit card string.
//	false - if it is not a valid credit card string.
//
function isCreditCard(str, type)
{
	if (validateIt(valCreditCard, str))
	{
		switch (type)
		{
			default:
				return(true);
		}
	}
	else
	{
		return(false)
	}
}

//----------------------------------------------------------------------------
//
// ** Checks a string to see if it is a valid email address.
//
// function: isEmail(str)
// arguments:
//	str - The string to check for a valid email addr.
//
// valid EMail:
//	foo@bar.org
//	foo@bar.org.tw
//	foo@255.255.255.0
//
// return:
//	true  - if it is a valid email address.
//	false - if it is not a valid email address.
//
function isEmail(str)
{
	return(validateIt(valEmailAddress, str));
}

//----------------------------------------------------------------------------
//
// ** Checks to see if the string is empty.
//
// function: isEmpty(str)
// arguments:
//	str - The string to check to see if it empty.
//
// return:
//	true  - if the string is empty (tabs and spaces are removed for the test).
//	false - if the string contains something.
//
function isEmpty(str)
{
	return(String(trim(str)).length == 0);
}

//----------------------------------------------------------------------------
//
// ** Checks to see if the string is a integer.
//
// function: isInteger(str)
// arguments:
//	str - The string to check for the integer.
//
// return:
//	true  - if it is a valid integer.
//	false - if it is not a valid integer.
//
function isInteger(str)
{
	return(validateIt(valInteger, str));
}

//----------------------------------------------------------------------------
//
// ** Checks to see if the string is numeric.
//
// function: isInteger(str)
// arguments:
//	str - The string to check for the numbers.
//
// return:
//	true  - if it is numeric.
//	false - if it is not numeric.
//
function isNumeric(str)
{
	return(validateIt(valNumeric, str));
}

//----------------------------------------------------------------------------
//
// ** Checks a string to see if is a social security number.
//
// function: isSSN(str)
// arguments:
//	str - The string to check for the ssn.
//
// valid SSN:
//	123456789
//	123-45-6789
//
// return:
//	true  - if it is a valid ssn.
//	false - if it is not a valid ssn.
//
function isSSN(str)
{
	return(validateIt(valSSN, str));
}

function isZip(str)
{
	return(validateIt(valZipCode, str));
}

//----------------------------------------------------------------------------
//
// ** Trims the white spaces from a string.
//
// function: trim(str)
// arguments:
//	str - The string to trim the white spaces from.
//
// return:
//	returns the trimmed string.
//
function trim(str)
{
	var regObj;

	regObj = /^(\s*)$/;
	if (regObj.test(str))
	{
		str = str.replace(regObj, '');

		if (str.length == 0)
		{
			return(str);
		}
	}

	regOjb = /^(\s*)([\W\w]*)(\b\s*)$/;
	if (regObj.test(str))
	{
		str = str.replace(regObj, '$2');
	}
	return(str);
}

function validateIt(type, str)
{
	var regObj;

	switch (type)
	{
		case 0x01 :
			regObj = /(^\d{4}\s\d{4}\s\d{4}\s\d{4}$)|(^\d{16}$)/;
			break;
		case 0x03 :
			regObj = /(^[a-zA-Z]([a-zA-Z0-9_.]*)\@([a-zA-Z]([a-zA-Z0-9_.]*))([.][a-zA-Z]{2,3})$)|(^[a-zA-Z]([a-zA-Z0-9_.]*)\@([a-zA-Z0-9]([a-zA-Z0-9_.]*))([.][a-zA-Z]{3})([.][a-zA-Z]{1,3})$)/;
			break;
		case 0x04 :
			regObj = /^(-?\d{1}\d*)$/;
			break;
		case 0x05 :
			regObj = /(^-?\d{1}\d*\.?\d*$)|(^-?\.?\d{1}\d*$)/;
			break;
		case 0x07 :
			regObj = /^(\s*)$/;
			break;
		case 0x08 :
			regObj = /(^\d{3}\-\d{2}\-\d{4}$)|(^\d{9}$)/;
			break;
		case 0x09 :
			regObj = /(^\d{5}$)|(^\d{5}\-\d{4}$)/;
			break;
		default :
			regObj = /^(\s*)$/;
	}
	return(regObj.test(str));
}

