// JavaScript Document

var errorArray = new Array();
var errorIndex;

String.prototype.trim = function()
{
	return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

function checkBlank(theElement)
{
	if (theElement.value == "")
	{
		errorArray[errorIndex] = theElement;
		errorIndex++;
		return "  - " + theElement.id + " is missing.\n";
	}
	else
		return "";
}

function checkSpaceABC(theElement)
{
	var isBlank = checkBlank(theElement);
	if (!isBlank)
	{
		var illegal = /[^a-zA-Z\. ]/;
		if (theElement.value.match(illegal))
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;			
			return "  - " + theElement.id + " contains numbers or other illegal characters.\n"
		}
		else
			return "";
	}
	else
		return isBlank;
}

function checkSpaceABC123(theElement)
{
	var isBlank = checkBlank(theElement);
	if (!isBlank)
	{
		var illegal = /[^a-zA-Z0-9\. ]/;
		if (theElement.value.match(illegal))
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - " + theElement.id + " contains illegal characters\n"
		}
		else
			return "";
	}
	else
		return isBlank;
}
	
function checkABC123(theElement)
{
	var isBlank = checkBlank(theElement);
	if (!isBlank)
	{
		var illegal = /[^a-zA-Z0-9]/;
		if (theElement.value.match(illegal))
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - " + theElement.id + " contains spaces or other illegal characters\n"
		}
		else
			return "";
	}
	else
		return isBlank;
}

function check123(theElement)
{
	var isBlank = checkBlank(theElement);
	if (!isBlank)
	{
		var illegal = /[^0-9]/;
		if (theElement.value.match(illegal))
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - " + theElement.id + " contains letters, spaces or other illegal characters\n"
		}
		else
			return "";
	}
	else
		return isBlank;
}

function checkEmail(theElement)
{
	var isBlank = checkBlank(theElement);
	if (!isBlank)
	{
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(theElement.value)))
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - Please enter a valid e-mail address\n";
		}
		else
		{
			var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
			if (theElement.value.match(illegalChars))
			{
				errorArray[errorIndex] = theElement;
				errorIndex++;			
				return "  - E-mail address contains illegal characters\n";
			}
			else
				return "";
		}
	}
	else
		return isBlank;
}

function checkPhone(theElement)
{
	var isBlank = checkBlank(theElement);
	if (!isBlank)
	{	
		var illegal = /[^0-9\[\]\(\)\- ]/;
		if (theElement.value.match(illegal))
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - " + theElement.id + " contains illegal characters\n";
		}
		else
		{
			if (theElement.value.length < 7)
			{
				errorArray[errorIndex] = theElement;
				errorIndex++;	
				return "  - " + theElement.id + " is an invalid number.\n";
			}
			else
				return "";
		}
	}
	else
		return isBlank;
}

function checkDropdown(theElement)
{
	if (theElement.selectedIndex == 0)
	{
		errorArray[errorIndex] = theElement;
		errorIndex++;
		return "  - Please select the best way to contact you.\n";
	}
	else
		return "";
}

function checkMax(theElement)
{
	var isNot123 = check123(theElement);
	if (!isNot123)
	{
		if (parseInt(theElement.value) > parseInt(theElement.maxValue))
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - " + theElement.id + " is higher than its allowed maximum\n"
		}
		else
			return "";
	}
	else
		return isNot123;
}

function checkMoney(theElement)
{
	var isBlank = checkBlank(theElement);
	if (!isBlank)
	{
		var moneyFilter=/^\d*\.{1}\d{2}$/;
		if (!(moneyFilter.test(theElement.value)))
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - Please enter a valid dollar amount\n";
		}
		else
			return "";
	}
	else
		return isBlank;
}

function checkDay(theElement)
{
	var isNot123 = check123(theElement);
	if (!isNot123)
	{
		if (parseInt(theElement.value) < 1 || parseInt(theElement.value) > 31)
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - Please enter a valid day\n"
		}
		else
			return "";
	}
	else
		return isNot123;
}

function checkMonth(theElement)
{
	var isNot123 = check123(theElement);
	if (!isNot123)
	{
		if (parseInt(theElement.value) < 1 || parseInt(theElement.value) > 12)
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - Please enter a valid month\n"
		}
		else
			return "";
	}
	else
		return isNot123;
}

function checkYear(theElement)
{
	var isNot123 = check123(theElement);
	if (!isNot123)
	{
		if (parseInt(theElement.value) < 1900 || parseInt(theElement.value) > 2100)
		{
			errorArray[errorIndex] = theElement;
			errorIndex++;
			return "  - Please enter a valid year\n"
		}
		else
			return "";
	}
	else
		return isNot123;
}
