function validateEmail(thisElement)
{
	allowSubmit = 1;
	str = thisElement.value;
	
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	{
		allowSubmit = 0;
		return;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		allowSubmit = 0;
		return;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		allowSubmit = 0;
		return;
	}

	 if (str.indexOf(at,(lat+1))!=-1)
	 {
		allowSubmit = 0;
		return;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	 {
		allowSubmit = 0;
		return;
	 }

	 if (str.indexOf(dot,(lat+2))==-1)
	 {
		allowSubmit = 0;
		return;
	 }
	
	 if (str.indexOf(" ")!=-1)
	 {
		allowSubmit = 0;
		return;
	 }
}



//-------------------------------------------------------------------------------------------------------------------------------
//VALIDATION FOR PHONE NUMBERS

PHONEFORMAT = 1;

function validatePhone(thisElement)
{
	allowSubmit = 1;
	currentPhone = stripInteger(thisElement.value);
	if (currentPhone.substring(0,1) == 1)
	{
		currentPhone = currentPhone.substring(1, currentPhone.length);
	}
	if (!stringCheck(currentPhone, "0123456789"))
	{
		allowSubmit = 0;
		return;
	}
	else if (currentPhone.length != 10)
	{
		allowSubmit = 0;
		return;
	}
	else if (PHONEFORMAT > 0)
	{
		thisElement.value = formatPhone(currentPhone);
	}
	else
	{
		thisElement.value = currentPhone;
	}
	return;
}

//-------------------------------------------------------------------------------------------------------------------------------

function formatPhone(phoneNumber)
{
	phoneAreaCode = phoneNumber.substring(0,3);
	phonePrefix = phoneNumber.substring(3,6);
	phoneSuffix = phoneNumber.substring(6,10);
	if (PHONEFORMAT == 1)
	{
		phoneNumber = "(" + phoneAreaCode + ") " + phonePrefix + "-" + phoneSuffix;
	}
	else if (PHONEFORMAT == 2)
	{
		phoneNumber = phoneAreaCode + "." + phonePrefix + "." + phoneSuffix;
	}
	return(phoneNumber);
}

//-------------------------------------------------------------------------------------------------------------------------------

ZIPFORMAT = 0;

function validateZip(thisElement)
{
	allowSubmit = 1;
	if (thisElement.value != "")
	{
		currentZip = stripInteger(thisElement.value);
		if (!stringCheck(currentZip, "0123456789"))
		{
			allowSubmit = 0;
			return;
		}
		else if ((currentZip.length != 5)&&(currentZip.length != 9))
		{
			allowSubmit = 0;
			return;
		}
		else if ((ZIPFORMAT == 1)&&(currentZip.length == 9))
		{
			thisElement.value = formatZip(currentZip);
		}
	}
	else
	{
		allowSubmit = 0;
	}
	return;
}

//-------------------------------------------------------------------------------------------------------------------------------

function formatZip(zipCode)
{
	prefix = zipCode.substring(0,5);
	suffix = zipCode.substring(5,9);
	return(prefix + "-" + suffix);
}

//-------------------------------------------------------------------------------------------------------------------------------

function validateInteger(thisElement)
{
	if (thisElement.value != "")
	{
		currentInteger = stripInteger(thisElement.value);
		thisElement.value = currentInteger;
		if (!stringCheck(currentInteger, "0123456789"))
		{
			allowSubmit = 0;
			return;
		}
	}
	else
	{
		thisElement.value = 0;
	}
	return;
}

//-------------------------------------------------------------------------------------------------------------------------------

function stripInteger(passedString)
{
	returnedString = "";
	comparisonString = " .-_()\/$,";
	for (h=0;h<passedString.length;h++)
	{
		testingString = passedString.substring(h, h+1);
		if (comparisonString.indexOf(testingString) < 0)
		{
			returnedString = returnedString + testingString;
		}
	}
	return(returnedString);
}

//-------------------------------------------------------------------------------------------------------------------------------

function validateDate(thisElement)
{
	allowSubmit = 1;
	if (thisElement.value != "")
	{
		currentDate = thisElement.value;
		if (!stringCheck(currentDate, "0123456789/"))
		{
			allowSubmit = 0;
			return;
		}
		else if (currentDate.length != 10)
		{
			allowSubmit = 0;
			return;
		}
		else
		{
			monthPart = parseInt(currentDate.substring(0,2));
			dayPart = parseInt(currentDate.substring(3,5));
			yearPart = parseInt(currentDate.substring(6,10));
			if ((monthPart < 13)&&(monthPart > 0))
			{
				if ((dayPart > 0)&&(dayPart < 32))
				{
					if ((yearPart > 2000)&&(yearPart < 3000))
					{
						allowSubmit = 1;
						return;
					}
				}
			}
		}
	}
	else
	{
		allowSubmit = 0;
	}
	return;
}

//-------------------------------------------------------------------------------------------------------------------------------

function validateDecimal(thisElement)
{
	if (thisElement.value != "")
	{
		currentDecimal = stripDecimal(thisElement.value);
		thisElement.value = currentDecimal;
		if (!stringCheck(currentDecimal, "0123456789."))
		{
			allowSubmit = 0;
			return;
		}
		else
		{
			decimalPoint = currentDecimal.indexOf(".");
			decimalString = currentDecimal.substring((decimalPoint+1), currentDecimal.length);
			if (decimalString.indexOf(".") != -1)
			{
				allowSubmit = 0;
			}
		}
	}
	else
	{
		allowSubmit = 0;
	}
	return;
}

//-------------------------------------------------------------------------------------------------------------------------------

function stripDecimal(passedString)
{
	returnedString = "";
	comparisonString = " -_()\/$,";
	for (h=0;h<passedString.length;h++)
	{
		testingString = passedString.substring(h, h+1);
		if (comparisonString.indexOf(testingString) < 0)
		{
			returnedString = returnedString + testingString;
		}
	}
	return(returnedString);
}

//-------------------------------------------------------------------------------------------------------------------------------

function stringCheck(passedString, comparisonString)
{
	for (h=0;h<passedString.length;h++)
	{
		testingString = passedString.substring(h, h+1);
		if (comparisonString.indexOf(testingString) < 0)
		{
			return(0);
		}
	}
	return(1);
}
