var whitespace = " \t\n\r";

function isEmpty(s)  {   
	return ((s == null) || (s.length == 0))
	}

function isWhitespace(s)  {
    var i;
    if (isEmpty(s)) return true;
 
    for (i = 0; i < s.length; i++)  {
	var c = s.charAt(i);
	if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function validEmail(strEmail) {
 invalidChars = " /:,;";
 if (strEmail == "")  {
  return false;
 }
 
 for (i=0; i<invalidChars.length; i++)  {
  badChar = invalidChars.charAt(i)
  if (strEmail.indexOf(badChar,0) != -1)  {
     return false;
     }
  }
  atPos = strEmail.indexOf("@", 1)
  if (atPos == -1)  {
    return false;
  }
  if (strEmail.indexOf("@", atPos+1) != -1)  {
    return false;
  }
  periodPos = strEmail.indexOf(".", atPos)
  if (periodPos == -1)  {
    return false;
  }
  if (periodPos+3 > strEmail.length)  {
    return false;
  }
 return true;
}

function submitForm() {
	var form = document.getElementById("EmailForm");
	if(vaidateForm(form)) {
		form.submit();
	}
}

function vaidateForm(form) {
	if (form.FirstName && isWhitespace(form.FirstName.value))  {
	 	alert("Please enter missing first name and then re-submit.");
 		form.FirstName.focus();
		return false;
	}
	if (form.LastName && isWhitespace(form.LastName.value))  {
	 	alert("Please enter missing last name and then re-submit.");
 		form.LastName.focus();
		return false;
	}
	if (!validEmail(form.Email.value))  {
	 	alert("Invalid e-mail address.\n Please try again.");
		form.Email.focus();
	 	form.Email.select();
		return false;
	}
	if (form.Message && isWhitespace(form.Message.value))  {
	 	alert("Please enter message and then re-submit.");
 		form.Message.focus();
		return false;
	}
	return true;
}

function submitSaveaLife() {
	var form = document.getElementById("EmailForm");
	if(vaidateSaveaLife(form)) {
		document.getElementById("EmailForm").submit();
	}
}

function vaidateSaveaLife(form) {
	if (form.FromFirstName && isWhitespace(form.FromFirstName.value))  {
	 	alert("Please enter missing first name and then re-submit.");
 		form.FromFirstName.focus();
		return false;
	}
	if (form.FromLastName && isWhitespace(form.FromLastName.value))  {
	 	alert("Please enter missing last name and then re-submit.");
 		form.FromLastName.focus();
		return false;
	}
	if (form.ToFirstName && isWhitespace(form.ToFirstName.value))  {
	 	alert("Please enter missing first name and then re-submit.");
 		form.ToFirstName.focus();
		return false;
	}
	if (form.ToLastName && isWhitespace(form.ToLastName.value))  {
	 	alert("Please enter missing last name and then re-submit.");
 		form.ToLastName.focus();
		return false;
	}
	if (!validEmail(form.FromEmail.value))  {
	 	alert("Invalid e-mail address.\n Please try again.");
		form.FromEmail.focus();
	 	form.FromEmail.select();
		return false;
	}
	if (!validEmail(form.ToEmail.value))  {
	 	alert("Invalid e-mail address.\n Please try again.");
		form.ToEmail.focus();
	 	form.ToEmail.select();
		return false;
	}
	return true;
}
