var prettynames = {		fname:"First Name",
					lname:"Last Name",
					email:"Email Address",
					phone:"Phone Number"
					};

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
		
// Utility function that returns true if a string contains only
// whitespace characters
// Taken from "Javascript: The Definitive Guide" pg. 264
function isblank(s)
{
	for (var i=0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if ((c!=' ') && (c != '\n') && (c != '')) 
		{
			return false;
		}
	}
	return true;
}

function verify(f)
{
	// Taken from "Javascript: The Definitive Guide" pg. 264.
	// Modified heavily to suit our needs
	// Loop through each element of the form and validate each item
	//
	
	
	var msg;
	var empty_fields = "";
	var errors = "";
	
	for (var i=0; i < f.length; i++)
	{	
		var element = f.elements[i];
		
		if (element.type == 'text')
		{
			if ( (element.value == null || element.value == "") || isblank(element.value) )
			{
				empty_fields += "\n        " + prettynames[element.name];
				continue;
			}
			if (element.numeric)
			{
				var v = parseFloat(element.value);
				if (isNaN(v))
				{
					errors +="- The field " + prettynames[element.name] + " must be a number\n";
				}
			}
			if ( (element.requiredlength > 0) && (element.value.length != element.requiredlength) )
			{
				errors += "- The field " + prettynames[element.name] + " must be " + element.requiredlength + " digits long\n";
			}
		}
		else if (element.type == 'select-one')
		{
			// The first option, option[0] can not be selected
			if ( (element.value == null || element.value == "" || element.selectedIndex == 0) || isblank(element.value) )
			{
				empty_fields += "\n        " + prettynames[element.name];
				continue;
			}
		}

	}
	
	if (! isValidEmail(f.email.value,true))
	{
		errors += "- Enter a valid email address\n";
	}
	if (!empty_fields && !errors) { return true; }
	
	msg		 = "__________________________________________\n\n";
	msg		+= "The form was not submitted because of the following error(s).\n";
	msg		+= "Please correct these error(s) and re-submit.\n";
	msg		+= "__________________________________________\n\n";
	
	if (empty_fields) 
	{
		msg += "- The following required field(s) are empty:" + empty_fields + "\n";
		if (errors) { msg += "\n"; }
	}
	msg += errors;
	alert(msg);
	return false;
}
