var msg= "";
var missing = "";
var invNum = "";
var invEmail = "";
var invPhone = "";

function validate(frm)
{

	// loop through form fields
	for (i = 0; i < frm.elements.length; i++)
	{
		var el = frm.elements[i];
		
		// required
		if (el.required)
		{
			if (isEmpty(el))
			{
				missing += "\n - " + el.name + " is a required field";
			}
		}
		
		// numeric
		if (el.numeric)
		{
			if (notNumeric(el))
			{
				invNum += "\n    = " + el.name + " must be a number";
			}
		}
		
		// email
		if (el.email)
		{
			if (notEmail(el))
			{
				invEmail += "\n\n" + el.name + " is not a valid email address";
			}
		}
		
		// phone number
		if (el.phone && el.value.length !=0)
		{
			if (invalidPhone(el) || el.value.length > 14)
			{
				invPhone += "\n\n" + el.value + " is not a valid phone/fax number";
			}
		}
	}
	
	// build the output message
	if (missing.length !=0 || invNum.length != 0 || invEmail.length !=0|| invPhone.length != 0)
	{
		if (missing.length != 0)
		{
			msg += "\n\nThe following required fields are missing:";
			msg += missing;
		}
		
		if (invNum.length != 0)
		{
			msg += "\n\nYou entered incorrect numeric data in these fields:";
			msg += invNum;
		}
		
		if (invEmail.length != 0)
		{
			msg += invEmail;
		}
		
		if (invPhone.length != 0)
		{
			msg += invPhone;
		}
		
		errMsg(msg);
		msg = "";
		missing = "";
		invNum = "";
		invEmail = "";
		invPhone = "";
		return false;
	}
	else
	{
		return true;
	}	
}

function notEmail(field) 
{ 
  var str = field.value; // email string
  var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
 
  if (!reg1.test(str) && reg2.test(str)) 
  { // if syntax is valid
    return false;
  }
  return true;
}

function invalidPhone(field)
{
	newStr = stripNonDigits(field.value);
	
	if (newStr.length == 10)
	{
		return false;
	}
	
	return true;
}

function isEmpty(field)
{
	str = field.value;
	
	if (str == "")
	{
		return true;
	}
	else
	{
		for (j = 0; j < str.length; j++)
		{
			if (str.charAt(j) != " ")
			{
				return false;
			}
		}
	}
	return true
}

function errMsg(msg)
 {
	var theMsg = "You entered some incorrect values into the form. ";
	
	theMsg += "Please correct your entries then re-submit the form.\n";
	theMsg += "____________________________________________________________________";
	theMsg += msg;
	theMsg += "\n____________________________________________________________________\n";
	
	alert(theMsg);
}

function notNumeric(field)
{
	var errCount = 0;
	var numdecs = 0;
	
	for (j = 0; j < field.value.length; j++)
	{
		c = field.value.charAt(j);
		
		if ((c >= 0 && c <= 9) || c == "." || (j == 0 && c == "-"))
		{
			if (c == ".")
			{
				numdecs++;
			}
		}
		else
		{
			errCount++;
			break;
		}
	}
	
	if (errCount > 0 || numdecs > 1)
	{
		return true;
	}
	
	return false;
}	

function stripNonDigits(str)
{
	
	newStr = "";
	
	for (j = 0; j < str.length; j++)
	{
		c = str.charAt(j);
		
		if (c >= 0 && c <= 9)
		{
			newStr += c;
		}
	}
	
	return newStr;
}