/*
 * Javascript code that validates the fields of the
 * agreement form, which is featured on the agreement.php file
 * Date: 02-07-2007
 * Input: form contact
 * Developer: Lucien Soivilus, lsoivilus@mdmc.us
 */

function checkFields(form)
{

  var name = form.name.value;
  var title = form.title.value;
  var organization = form.organization.value;
  var email = form.email.value;
  var telephone = form.telephone.value;
  var message = form.message.value;

  if (name == "")
  {
    alert("Please enter your full name in the \"Full Name\" field.");
    form.name.focus();
    return (false);
  }

  if (name.length < 2)
  {
    alert("Name cannot be less than 2 characters." );
    form.name.focus();
    return (false);
  }

  if (name.length > 50)
  {
    alert("Please enter at most 50 characters in the \"Full Name\" field.");
    form.name.focus();
    return (false);
  }

  // Name field must contain at least two words
  var words = name.split(" ");
  if(!(words.length >= 2) )
  {
	alert("Please give your first and last names in the \"Full Name\" field.");
    form.name.focus();
    return (false);
  }

  if((words[0] == words[1]) || (words[0] == words[2]) || (words[1] == words[2]) ){
	alert("Please provide a valid lastname");
    form.name.focus();
    return (false);
  }

  if (title ==""){
	  alert("What is your tile? Put N/A if you do not have a title");
	  form.title.focus();
	  return (false);
  }
  if (title != ""){
 	if(title.length < 2){
		alert("Invalid title! Please provide a meaningful title");
    	form.title.focus();
    	return (false);
  	}

 	if(title.length > 25){
		alert("Title too wordy! Please reduce title to 2 or 3 words");
    	form.title.focus();
    	return (false);
  	}
  }

  if(organization == ""){
	 alert("Please provide your organization's name");
	 form.organization.focus();
	 return (false);
  }

  if(organization.length < 2 || organization.length > 50){
	  alert("Please provide a correct organization name");
	  form.organization.value="";
	  form.organization.focus();
	  return(false);
  }

  if (email == ""){
    alert("Please enter a valid email address in the \"Email\" field.");
    form.email.focus();
    return (false);
  }

  if (email.length < 8)
  {
    alert("Please enter at least 7 characters in the \"Email\" field.");
    form.email.focus();
    return (false);
  }

  if (email.length > 75)
  {
    alert("Too many characters! Please enter a valid email address in the \"Email\" field.");
	form.email.value="";
    form.email.focus();
    return (false);
  }

  // if the first character of the email string is @ then the string is invalid
  if (email.charAt(0) == '@'){
	  alert("Invalid email address! Please enter a valid email address in the \"Email\" field.");
	  form.email.focus();
	  return (false);
  }

  // email is invalid if any of these character is found in the email string
  if( (email.indexOf(";") != -1) || (email.indexOf(",") != -1) || (email.indexOf("/") !=-1) || (email.indexOf(":")!=-1)){
	  alert("Invalid email address! Please enter a valid email address in the \"Email\" field.");
	  form.email.value="";
	  form.email.focus();
	  return (false);
  }

  // Record the position of the @ character
  // This position will be used to verify the
  // characters that come after the @ character
  var atPosition = email.indexOf("@");

  // The substring before the @ character
  var emailSubstring1 = email.substring(atPosition - 1);

  // The substring that comes right after the @ character
  var emailSubstring = email.substring(atPosition + 1);

    // verify that email address is not in the format 1234567@yahoo.com
	// or johndoe@123456.com
  if(isNumber(emailSubstring1))
  {
	  alert("Invalid email address! Cannot begin with numbers");
	  form.email.value="";
	  form.email.focus();
	  return (false);
  }

  // Email invalid if another @ character is found
  if(emailSubstring.indexOf("@")!= -1)
  {
	  alert("Invalid email address!");
	  form.email.value="";
	  form.email.focus();
	  return (false);
  }

  // Position of the period.
  // There should be one period in the substring
  // that comes after the @ character.
  // Verify that the domain is a correct domain.
  var periodPosition = emailSubstring.indexOf(".");

  // If no period is found in the email string
  // Then the email address is not valid
  if(periodPosition==-1){
	  alert("Invalid email address! Period omitted");
	  form.email.value="";
	  form.email.focus();
	  return (false);
  }

  var domainType = emailSubstring.substring(periodPosition + 1);

  // No real Internet domain is less than 2 characters
  // or greater than 3 characters, except for .info
  if( domainType.length < 2  || domainType.length > 3){
	  alert("Invalid email domain");
	  form.email.focus();
	  return false;
  }


  if(telephone=="" || telephone.length < 12 || telephone.length > 12){
	  alert("Please provide a correctly formatted telephone number");
	  form.telephone.focus();
	  return false;
  }
  if(telephone.charAt(3) != "-"){
	  alert("Please separate area code with a hyphen");
	  form.telephone.focus();
	  return false;
  }
  if(telephone.charAt(7) != "-"){
	  alert("Please separate line number with a hyphen");
	  form.telephone.focus();
	  return false;
  }

  if(message == ""){
	 alert("Please enter a brief message. Thank you!");
	 form.message.focus();
	 return (false);
  }
  return (true);
}

// NOT YET COMPLETE. NEED REWRITE
// REWRITE USING REGULAR EXPRESSION
function isNumber(param)
{

	var digits = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
	var count = 0;

	// verify that the first char is not a digit
	for(j=0; j < digits.length; j++)
		if(param.charAt(0) == digits[j])return true;

	for( i=0; i < param.length; i++ )
		for(j=0; j < digits.length; j++)
			if(param.charAt(i) == digits[j])count++;

	if(count==param.length)return true;
	return false;
}
