function fnValidationItem(sForm, sTitle, sObjName, sType, bReq)
{
	this.form = sForm;
	this.title = sTitle;
	this.name = sObjName;
	this.type = sType;
	this.required = bReq;
}

function fnValidateMatchingPair(sForm, sType, sTitle, sTitleAlt, sObjName, sObjNameMatch, bReq)
{
	this.form = sForm;
	this.type = sType;
	this.title = sTitle;
	this.titleAlt = sTitleAlt;
	this.name = sObjName;
	this.nameMatch = sObjNameMatch;
	this.required = bReq;
}

function fnValidation(a)
{
	var sM = '';
	
	for(var i = 0; i < a.length; i ++)
	{
		
		var o = document[a[i].form][a[i].name];
		var sValue = '';
		
		if(a[i].type != 'radio')
		{
			sValue = o.value;
			if(a[i].required)
			{
				if(sValue == '')
				{
					alert('Please provide: ' + a[i].title);
					o.focus();
					return false;
				}
			}
		}
		
		if(a[i].type == 'email' && sValue != '')
		{
			emailVal = sValue;
			if(!sValue.isEmail())
			{
				alert(a[i].title + ' doesn\'t appear to be a valid email address');
				o.select();
				o.focus();
				return false;
			}
		}
		
		if(a[i].type == 'reemail' && sValue != '')
		{
			if(!sValue.isEmail())
			{
				alert(a[i].title + ' doesn\'t appear to be a valid email address');
				o.select();
				o.focus();
				return false;
			}
			if(sValue != emailVal)
			{
				alert(a[i].title + ' Doesn\'t Match!');
				o.select();
				o.focus();
				return false;
			}
		}
		
		if(a[i].type == 'telephone' && sValue != '')
		{
			if(!sValue.isPhone())
			{
				sM = fnValidatorMessage('telephone');
				alert(a[i].title + ' doesn\'t appear to be a valid telephone/fax number' + (sM == '' ? '' : '\n\n' + sM));
				o.select();
				o.focus();
				return false;
			}
		}
		
		if(a[i].type == 'pair')
		{
			var oMatch = document[a[i].form][a[i].nameMatch];
			var sValueMatch = oMatch.value;
			if(sValue != sValueMatch)
			{
				alert(a[i].title + ' and ' + a[i].titleAlt + ' must have the same value');
				o.select();
				o.focus();
				return false;
			}
		}
		
		if(a[i].type == 'password' && sValue != '')
		{
			pswVal = sValue;
			if(!sValue.isPassword())
			{
				alert(a[i].title + ' doesn\'t appear to be a valid password. Must contain at least 5 characters');
				o.select();
				o.focus();
				return false;
			}
		}
		
		if(a[i].type == 'repassword' && sValue != '')
		{
			if(!sValue.isPassword())
			{
				alert(a[i].title + ' doesn\'t appear to be a valid password. Must contain at least 5 characters');
				o.select();
				o.focus();
				return false;
			}
			if(sValue != pswVal)
			{
				alert(a[i].title + ' Doesn\'t Match!');
				o.select();
				o.focus();
				return false;
			}
		}
		
		if(a[i].type == 'number' && sValue != '')
		{
			if(!sValue.isNumber())
			{
				alert(a[i].title + ' doesn\'t appear to be a number');
				o.select();
				o.focus();
				return false;
			}
		}
		
		if(a[i].type == 'numberLow' && sValue != '')
		{
			if(!sValue.isNumber())
			{
				alert(a[i].title + ' doesn\'t appear to be a number');
				o.select();
				o.focus();
				return false;
			}
		}
		
		if(a[i].type == 'numberLow' && sValue < 10)
		{
			alert(a[i].title + ' number needs to be a minimum of 10');
			o.select();
			o.focus();
			return false;
		}
		
		if(a[i].type == 'numberLow' && sValue > 1000)
		{
			alert(a[i].title + ' number needs to be a minimum of 10');
			o.select();
			o.focus();
			return false;
		}
		
		if(a[i].type == 'currency' && sValue != '')
		{
			if(!sValue.isCurrency())
			{
				sM = fnValidatorMessage('currency');
				alert(a[i].title + ' doesn\'t appear to be a currency' + (sM == '' ? '' : '\n\n' + sM));
				o.select();
				o.focus();
				return false;
			}
		}
		
		if(a[i].type == 'radio')
		{
			if(o.length > 0)
			{
				var bCheck = false;
				for(var j = 0; j < o.length; j ++)
				{
					if(o[j].checked)
					{
						bCheck = true;
						break;
					}
				}
			}
			if(!bCheck)
			{
				sM = fnValidatorMessage('radio');
				alert('Please select a value for \'' + a[i].title + '\'');
				return false;
			}
		}
		
		if(a[i].type == 'selectBox' && sValue != '')
		{
			if(o.length > 0)
			{
				var bSelect = false;
				for(var aa = 0; aa < o.length; aa ++)
				{
					if(o[aa].selected)
					{
						bSelect = true;
						break;
					}
				}
			}
			if(!bSelect)
			{
				sM = fnValidatorMessage('selectBox');
				alert('Please select a value for \'' + a[i].title + '\'');
				return false;
			}
		}
		
	}
	return true;
}

// return a message about a given validation type.
// i.e. if currency, then return a message showing the required format
function fnValidatorMessage(sType)
{
	if(sType == 'currency')
	{
	return 'Must be of the format 12,345 or 12345';
	}
	if(sType == 'telephone')
	{
	return 'Can only contain numbers or +()';
	}
	return '';
}

function fnResetForm(frm) {
	document[frm].reset();
}

function fnSelectMe(o) {
	
}

String.prototype.isCurrency = fnValidCurrency;
String.prototype.isEmail = fnValidEmail;
String.prototype.isPhone = fnValidPhone;
String.prototype.isNumber = fnValidNumber;
String.prototype.isPassword = fnValidPassword;

function fnValidCurrency()
{
	if(this.indexOf(',') != -1)
	{
		return /^\-{0,1}(\d{1,3}\,)*(\d{3}\,)*(\d{3})(\.\d{2}){0,1}$/.test(this);
	}
	else
	{
		return /^\-{0,1}\d+(\.\d{2}){0,1}$/.test(this);
	}
}

function fnValidPassword() {
	return /^.{5,8}$/.test(this);
}

function fnValidNumber() {
	return /^\-{0,1}\d+$/.test(this);
}

function fnValidEmail() {
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(this);
}

function fnValidPhone() {
	// remove spaces
	var p = this.replace(/[\(|\)| ]+/ig, '')
	// test: can have a plus at start.
	// all other characters numeric
	return /^\+?\d+$/.test(p);
}
