/*
 * @description validate all fields within a form, that have validation set to it.
 */
function validateForm()
{
	var strError = '';
	// Grab all elements within this form.
	var objForm = document.forms[0];
	for (i = 0; i < objForm.length; i++)
	{
		var strValue = trim(objForm[i].value);
		if (objForm[i].getAttribute('validate'))
		{
			var arrValidation = objForm[i].getAttribute('validate').split(',');
			var strTempError = '';
			for (ii = 0; ii < arrValidation.length; ii++)
			{
				var arrAttributes = arrValidation[ii].split('=');
				if (arrAttributes[0] == 'type' && !validateStringValue(strValue, arrValidation[0]))
				{
					strTempError += '\n\t-contains invalid data';
				}
				if (arrAttributes[0] == 'minlength' && strValue.length < arrAttributes[1])
				{
					strTempError += '\n\t-must be a minimum of ' + arrAttributes[1] + ' character';
					if (arrAttributes[1] != 1)
					{
						strTempError += 's';
					}
				}
				if (arrAttributes[0] == 'maxlength' && strValue.length > arrAttributes[1])
				{
					strTempError += '\n\t-must be a maximum of ' + arrAttributes[1] + ' character';
					if (arrAttributes[1] != 1)
					{
						strTempError += 's';
					}
				}
			}
			if (strTempError != '')
			{
				strError += '\n' + objForm[i].title + ':' + strTempError + '\n';
				document.getElementById(objForm[i].name + '_label').className = 'label_error';
			}
			else
			{
				document.getElementById(objForm[i].name + '_label').className = '';
			}
		}
	}
	if (strError != '')
	{
		alert ('Please verify the following:\n' + strError);
	}
	else
	{
		// Submit form.
		objForm.submit();
	}
}
/*
 * @description Validate a string using common patterns.
 * @param strValue this is the string value that is compared.
 * @param strType this is the string value for the type of comparison.
 * @return bool true or false.
 */
function validateStringValue(strValue, strType)
{
	var strPattern = '';
	switch (strType)
	{
		case 'text':
			strPattern = '';
			break;
		case 'date':
			strPattern = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
			break;
		case 'zip_code':
			strPattern = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
			break;
		case 'postal_code':
			strPattern = /^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/;
			break;
		case 'time':
			strPattern = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/;
			break;
		case 'dollar_value':
			strPattern = /^((\$\d*)|(\$\d*\.\d{2})|(\d*)|(\d*\.\d{2}))$/;
			break;
		case 'us_social_security':
			strPattern = /^\d{3}\-?\d{2}\-?\d{4}$/;
			break;
		case 'canadian_social_security':
			strPattern = /^\d{9}$/;
			break;
		case 'ip_address':
			strPattern = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
			break;
		case 'email_address':
			strPattern = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
			break;
		case 'phone_number':
			strPattern = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
			break;
		case 'number':
			strPattern = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
			break;
		case 'integer':
			strPattern = /(^-?\d\d*$)/;
			break;
		case 'string':
			strPattern = /\W/;
			break;
		default:
			strPattern = /\//;
			break;
	}
	var objRegExp = new RegExp(strPattern);
	return objRegExp.test(strValue);
}