/*
 * Javascript code that validates the fields of the
 * agreement form, which is featured on the agreement.php file
 * Date: 		02-07-2007
 * Updated: 	03-06-2007
 * Input: 		form agreement
 * Developer: 	Lucien Soivilus, lsoivilus@mdmc.us
 */
// generate a random string for the digital signature field
function securityCode(form)
{
	var sigItems = new Array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
							"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
							"W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g",
							"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
							"s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3",
							"4", "5", "6", "7", "8", "9", "0");

	// generate a random number from 0 to the size of the array below minus 1
	var randomIndex = Math.floor(Math.random() * 62);

	// add first character to random signature string
	var securityCode = sigItems[randomIndex];

	for(counter=0; counter < 4; counter++)
	{
		// generate additional random numbers
		var randomIndex = Math.floor(Math.random() * 62);
		securityCode = securityCode.concat(sigItems[randomIndex]);
	}

	form.securityCode.value = securityCode;
	return true;
}


function checkForm(form)
{

  // store the form fields' values into variables
  // for easy reference
  var name = form.name.value;
  var title = form.title.value;
  var organization = form.organization.value;
  var address1 = form.address1.value;
  var city = form.city.value;
  var zip = form.zip.value;
  var country = form.country.value;
  var state = form.state.value;
  var email = form.email.value;
  var telephone = form.telephone.value;
  var fax = form.fax.value;
  var url = form.url.value;
  var codeConfirm = form.codeConfirm.value;
  var securityCode = form.securityCode.value;
  var signature = form.signature.value;


  // If the user does not provide his/her name,
  // kindly ask for the viewer's name
  if (name == "")
  {
    alert("Please enter your full name in the \"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 \"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 provide your full name in the \"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 != ""){
 	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(address1 == ""){
	  alert("Please provide us with a valid address");
	  form.address1.focus();
	  return (false);
  }

  if(address1.length < 2){
	  alert("Incomplete address! Please enter your street address in the \"Address\" field");
	  form.address1.focus();
	  return (false);
  }

  if(address1.length > 50){
	  alert("Incorrect street address! Please enter your street address in the \"Address\" field");
	  form.address1.value="";
	  form.address1.focus();
	  return (false);
  }

  if(city=="" || city.length < 2 || city.length > 50){
	  alert("Please provide a correct city name. Thank you!");
	  form.city.focus();
	  return false;
  }

  if(state==""){
	  alert("Please select your state. Thank you!");
	  form.state.focus();
	  return false;
  }

  if(zip =="" || zip.length < 5 || zip.length > 5){
	  alert("Please provide a five-digit Zip Code. Thank you!");
	  form.zip.focus();
	  return false;
  }
  else
  {
	var re5digit=/^\d{5}$/; //regular expression defining a 5 digit number
	                        // Ensure the Zip code has only digits
	if (zip.search(re5digit)==-1)//if match failed
	{
		alert("Digits only please in the Zip code textbox!");
		form.zip.value = "";
		form.zip.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);
  }
  // If the last character of the email string is @, then the email is invalid
  else if(email.charAt(email.length -1) == '@')
  {
  	  alert("Invalid email address! Please enter a valid email address in the \"Email\" field.");
	  // Delete whatever the user typed in the Email textbox
	  form.email.value = "";
	  form.email.focus();
	  return (false);
  }
  var invalidCharacters = new Array(":", ";", ",", "/", "\"", "?", ">", "<", "+",
									"#", "$", "~", "!", "&", "%", "^", "(", ")", "{", "}", "[", "]", "|");

  for(i=0; i < invalidCharacters.length; i++)
  {
  	// email is invalid if any of these character is found in the email string
  	if( (email.indexOf(invalidCharacters[i]) != -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(fax !=""){
	  if(fax.length < 12 || fax.length > 12){
	  	alert("Please provide a correctly formatted fax number");
	  	form.telephone.focus();
	  	return false;
	  }
  }

  // Ensures that the user provides his/her signature
  if (signature == "")
  {
    alert("Please enter your full name in the \"Signature \" field.");
    form.signature.focus();
    return (false);
  }

  // Ensure the user's name and his/her signature is the same
  if (signature.toLowerCase() != name.toLowerCase() )
  {
    alert("Name mismatch! Please enter your name exactly as you did in the \"Name \" field.");
    form.signature.focus();
    return (false);
  }

  // Ensures that the user does leave empty the 'Confirm Security Code' text field
  if (codeConfirm == "")
  {
    alert("Please confirm the security code in the \"Confirm Security Code\" field.");
    form.codeConfirm.focus();
    return (false);
  }

  // Ensure the user types all the characters of the security code
  if (codeConfirm.length < 5 || codeConfirm.length > 5)
  {
    alert("Please confirm the security code exactly as shown in the \"Security Code\" field.");
	form.codeConfirm.value="";
    form.codeConfirm.focus();
    return (false);
  }

  // Verify that security code and its confirmation are the same
  if(codeConfirm != securityCode)
  {
    alert("The Security Code and the Confirmation do not match!");
	form.codeConfirm.value="";
    form.codeConfirm.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;
}
